author | Claus Gittinger <cg@exept.de> |
Fri, 22 Jan 2016 13:25:02 +0100 | |
changeset 19079 | 57efea75b8a2 |
parent 19030 | 688b462a4941 |
child 19103 | 71257a47eba2 |
child 19223 | 24a06c982e51 |
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 |
||
17363
a394abb3f789
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17257
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
a394abb3f789
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17257
diff
changeset
|
15 |
|
1 | 16 |
Collection subclass:#SequenceableCollection |
6183 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'MissingClassInLiteralArrayErrorSignal' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Abstract' |
|
1 | 21 |
! |
22 |
||
2069 | 23 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
8874 | 28 |
All Rights Reserved |
1 | 29 |
|
88 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
88 | 39 |
documentation |
40 |
" |
|
11272 | 41 |
SequenceableCollections have ordered elements which can be accessed via a numeric index. |
42 |
SequenceableCollection is an abstract class - there are no direct instances of it in the system. |
|
1385 | 43 |
|
44 |
The methods found here assume that implementations of (at least) |
|
11272 | 45 |
#at:/#at:put: are implemented (which are always found in Object for indexable objects). |
46 |
||
47 |
For performance, some subclasses redefine more methods, |
|
48 |
knowing the detail of how elements are stored. |
|
49 |
||
8874 | 50 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
11272 | 51 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this kind of tuneup. |
52 |
||
53 |
The most heavily used SequentialCollections in the system are probably Array, String, ByteArray |
|
54 |
OrderedCollection and SortedCollection. |
|
1289 | 55 |
|
56 |
[author:] |
|
11272 | 57 |
Claus Gittinger |
1385 | 58 |
|
59 |
[see also:] |
|
11272 | 60 |
Array ByteArray OrderedCollection SortedCollection |
61 |
CharacterArray String |
|
88 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
65 |
!SequenceableCollection class methodsFor:'initialization'! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
66 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
67 |
initialize |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
68 |
MissingClassInLiteralArrayErrorSignal isNil ifTrue:[ |
8874 | 69 |
MissingClassInLiteralArrayErrorSignal := Error newSignalMayProceed:true. |
70 |
MissingClassInLiteralArrayErrorSignal nameClass:self message:#missingClassInLiteralArrayErrorSignal. |
|
71 |
MissingClassInLiteralArrayErrorSignal notifierString:'Missing class in literal encoding'. |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
72 |
] |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
73 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
74 |
"Created: / 18.5.1999 / 14:49:51 / cg" |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
75 |
! ! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
76 |
|
2069 | 77 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 78 |
|
4807 | 79 |
decodeFromLiteralArray:anArray |
80 |
"create & return a new instance from information encoded in anArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
81 |
Redefined for faster creation." |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
82 |
|
8874 | 83 |
|collection |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
84 |
sz "{ Class: SmallInteger }"| |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
85 |
|
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
86 |
sz := anArray size. |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
87 |
collection := self newWithSize:sz-1. |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
88 |
2 to:sz do:[:idx| collection at:idx-1 put:(anArray at:idx) decodeAsLiteralArray]. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
89 |
^ collection |
4807 | 90 |
|
8797 | 91 |
" |
92 |
Array |
|
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
93 |
decodeFromLiteralArray:#(Array 1 2 3) |
8797 | 94 |
" |
4807 | 95 |
! |
96 |
||
1 | 97 |
new:size withAll:element |
360 | 98 |
"return a new collection of size, where all elements are |
99 |
initialized to element." |
|
1 | 100 |
|
101 |
|newCollection| |
|
102 |
||
103 |
newCollection := self new:size. |
|
104 |
newCollection atAllPut:element. |
|
105 |
^ newCollection |
|
360 | 106 |
! |
107 |
||
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
108 |
newWithConcatenationOfAll:aCollectionOfCollections |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
109 |
"this creates and returns a new collection consisting |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
110 |
of the concatenation of all elements of the argument. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
111 |
I.e. it has the same effect as concatenating all elements |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
112 |
of the argument using #, ,but is implemented more efficiently, |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
113 |
by avoiding repeated garbage allocations. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
114 |
This is an O runtime algorithm, in contrast to the #, loop, which is O*O" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
115 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
116 |
|totalSize newColl idx| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
117 |
|
8874 | 118 |
totalSize := aCollectionOfCollections |
119 |
inject:0 |
|
120 |
into:[:sumSoFar :el | sumSoFar + el size]. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
121 |
newColl := self new:totalSize. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
122 |
idx := 1. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
123 |
aCollectionOfCollections do:[:el | |
8874 | 124 |
|sz| |
125 |
||
126 |
sz := el size. |
|
127 |
newColl replaceFrom:idx to:(idx+sz-1) with:el startingAt:1. |
|
128 |
idx := idx + sz |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
129 |
]. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
130 |
^ newColl |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
131 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
132 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
133 |
this method optimizes the following (common) operation: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
134 |
|
8874 | 135 |
|s| |
136 |
||
137 |
s := ''. |
|
138 |
#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') do:[:e| |
|
139 |
s := s , e. |
|
140 |
]. |
|
141 |
s |
|
142 |
||
143 |
String |
|
144 |
newWithConcatenationOfAll:#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') |
|
145 |
||
146 |
String |
|
147 |
newWithConcatenationOfAll:#() |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
148 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
149 |
Array |
8874 | 150 |
newWithConcatenationOfAll:#( (1 2 3 4) (5 6 7 8) (9 10 11 12) ) |
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
151 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
152 |
timing: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
153 |
------- |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
154 |
speed-break-even is at around 5 elements for strings; |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
155 |
for more elements, #newWithConcatenationOfAll: is much faster. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
156 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
157 |
|arr1 arr2 arr3 t| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
158 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
159 |
arr1 := #('hello' ' ' 'world' ' ' 'this is ' 'ST/X'). |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
160 |
arr2 := Array new:1000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
161 |
arr3 := Array new:10000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
162 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
163 |
(Array with:arr1 with:arr2 with:arr3) with:#(10000 100 10) do:[:arr :cnt | |
8874 | 164 |
t := Time millisecondsToRun:[ |
165 |
cnt timesRepeat:[ |
|
166 |
String |
|
167 |
newWithConcatenationOfAll:arr |
|
168 |
] |
|
169 |
]. |
|
170 |
Transcript showCR:(arr size printString , ' elements - time for #newWithConcatenationOfAll :' , (t/cnt) asFloat printString , 'mS'). |
|
171 |
Transcript endEntry. |
|
172 |
||
173 |
t := Time millisecondsToRun:[ |
|
174 |
cnt timesRepeat:[ |
|
175 |
|s| |
|
176 |
||
177 |
s := ''. |
|
178 |
arr do:[:e| |
|
179 |
s := s , e. |
|
180 |
]. |
|
181 |
s |
|
182 |
] |
|
183 |
]. |
|
184 |
Transcript showCR:(arr size printString , ' elements - time for loop over #, :' , (t/cnt) asFloat printString , 'mS'). |
|
185 |
Transcript endEntry. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
186 |
] |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
187 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
188 |
! |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
189 |
|
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
190 |
newWithSize:size |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
191 |
"return a new collection of size. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
192 |
For variable size collections, this is different from #new:, |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
193 |
in that #new: creates an empty collection with preallocated size, |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
194 |
while #withSize: creates a non empty one." |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
195 |
|
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
196 |
^ (self new:size) grow:size. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
197 |
|
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
198 |
" |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
199 |
(OrderedCollection new:10) inspect. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
200 |
(OrderedCollection withSize:10) inspect. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
201 |
(Array new:10) inspect. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
202 |
(Array withSize:10) inspect. |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
203 |
" |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
204 |
! |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
205 |
|
360 | 206 |
withSize:size |
207 |
"return a new collection of size. |
|
208 |
For variable size collections, this is different from #new:, |
|
209 |
in that #new: creates an empty collection with preallocated size, |
|
210 |
while #withSize: creates a non empty one." |
|
211 |
||
9162 | 212 |
^ (self new:size) grow:size. |
360 | 213 |
|
214 |
" |
|
215 |
(OrderedCollection new:10) inspect. |
|
216 |
(OrderedCollection withSize:10) inspect. |
|
217 |
(Array new:10) inspect. |
|
218 |
(Array withSize:10) inspect. |
|
219 |
" |
|
1 | 220 |
! ! |
221 |
||
5188 | 222 |
!SequenceableCollection class methodsFor:'Signal constants'! |
223 |
||
224 |
missingClassInLiteralArrayErrorSignal |
|
225 |
^ MissingClassInLiteralArrayErrorSignal |
|
226 |
||
227 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
228 |
! ! |
|
229 |
||
7304 | 230 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 231 |
|
232 |
_at:nIndices |
|
233 |
"this is a synthetic selector, generated by the compiler, |
|
234 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 235 |
I.e. |
236 |
Array[n] |
|
7255 | 237 |
generates |
8874 | 238 |
Array _at: n |
7255 | 239 |
" |
240 |
||
241 |
^ self new:nIndices |
|
242 |
! |
|
243 |
||
244 |
_at:dim1 at:dim2 |
|
245 |
"this is a synthetic selector, generated by the compiler, |
|
246 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 247 |
I.e. |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
248 |
Array[n,m] |
7255 | 249 |
generates |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
250 |
Array _at:n at:m |
7255 | 251 |
" |
252 |
||
253 |
|data| |
|
254 |
||
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
255 |
data := self newWithSize:(dim1 * dim2). |
7255 | 256 |
^ MultiDimensionalArrayAccessor |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
257 |
collection:data |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
258 |
dimensions:(Array with:dim1 with:dim2) |
7255 | 259 |
! |
260 |
||
261 |
_at:dim1 at:dim2 at:dim3 |
|
262 |
"this is a synthetic selector, generated by the compiler, |
|
263 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 264 |
I.e. |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
265 |
Array[n,m,o] |
7255 | 266 |
generates |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
267 |
Array _at:n at:m at:o |
7255 | 268 |
" |
269 |
||
270 |
|data| |
|
271 |
||
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
272 |
data := self newWithSize:(dim1 * dim2 * dim3). |
8874 | 273 |
^ MultiDimensionalArrayAccessor |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
274 |
collection:data |
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
275 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
7255 | 276 |
! ! |
277 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
278 |
!SequenceableCollection class methodsFor:'instance creation-streaming'! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
279 |
|
15685
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
280 |
new:newSize streamContents:blockWithArg |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
281 |
"create a write-stream on an instance of the receiver-class with initial size, |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
282 |
evaluate blockWithArg, passing that stream, |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
283 |
extract and return the streams contents." |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
284 |
|
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
285 |
|stream| |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
286 |
|
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
287 |
stream := WriteStream on:(self new:newSize). |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
288 |
blockWithArg value:stream. |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
289 |
^ stream contents |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
290 |
! |
9bc8ab0a3bcc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15661
diff
changeset
|
291 |
|
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
292 |
streamContents:blockWithArg |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
293 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
294 |
evaluate blockWithArg, passing that stream, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
295 |
extract and return the streams contents." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
296 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
297 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
298 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
299 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
300 |
blockWithArg value:stream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
301 |
^ stream contents |
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 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
304 |
|rslt| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
305 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
306 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
307 |
" |
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 |
"Modified: / 29-03-2007 / 15:20:20 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
310 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
311 |
|
10590 | 312 |
streamContents:blockWithArg limitedTo:limit |
313 |
"create a limited write-stream on an instance of the receiver-class, |
|
314 |
evaluate blockWithArg, passing that stream, |
|
315 |
extract and return the streams contents (possibly truncated)." |
|
316 |
||
317 |
|stream| |
|
318 |
||
319 |
stream := self writeStream. |
|
320 |
stream writeLimit:limit. |
|
321 |
blockWithArg value:stream. |
|
322 |
^ stream contents |
|
323 |
||
324 |
"Created: / 04-06-2007 / 17:22:38 / cg" |
|
325 |
! |
|
326 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
327 |
streamContentsEnumerating:aReceiver using:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
328 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
329 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
330 |
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
|
331 |
enumerator somewhere, and you want this as a collection." |
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 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
334 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
335 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
336 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
337 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
338 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
339 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
340 |
String streamContentsEnumerating:'hello' using:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
341 |
Array streamContentsEnumerating:Method using:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
342 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
343 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
344 |
"Created: / 29-03-2007 / 15:21:12 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
345 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
346 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
347 |
streamContentsOf:aReceiver enumerator:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
348 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
349 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
350 |
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
|
351 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
352 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
353 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
354 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
355 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
356 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
357 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
358 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
359 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
360 |
String streamContentsOf:'hello' enumerator:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
361 |
Array streamContentsOf:Method enumerator:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
362 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
363 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
364 |
"Created: / 29-03-2007 / 15:05:30 / cg" |
13202 | 365 |
! |
366 |
||
367 |
writeStream |
|
368 |
"create a write-stream on an instance of the receiver-class" |
|
369 |
||
370 |
^ self writeStreamClass on:(self new:50). |
|
371 |
||
372 |
" |
|
373 |
OrderedCollection writeStream |
|
374 |
" |
|
375 |
||
376 |
"Modified: / 09-01-2011 / 10:37:35 / cg" |
|
377 |
! |
|
378 |
||
379 |
writeStreamWithInitialSize:l |
|
380 |
"create a write-stream on an instance of the receiver-class" |
|
381 |
||
382 |
^ self writeStreamClass on:(self new:l). |
|
383 |
||
384 |
" |
|
385 |
OrderedCollection writeStream |
|
386 |
" |
|
387 |
||
388 |
"Created: / 09-01-2011 / 10:36:28 / cg" |
|
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
389 |
! ! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
390 |
|
8893 | 391 |
!SequenceableCollection class methodsFor:'queries'! |
392 |
||
393 |
isAbstract |
|
11219 | 394 |
"Return if this class is an abstract class. |
395 |
True is returned for SequenceableCollection here; false for subclasses. |
|
396 |
Abstract subclasses must redefine again." |
|
397 |
||
8893 | 398 |
^ self == SequenceableCollection |
399 |
! ! |
|
400 |
||
7261 | 401 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 402 |
|
7796 | 403 |
allButFirst |
8874 | 404 |
"Return a copy of the receiver containing all but the first element. |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
405 |
Returns an empty collection if there are not at least two elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
406 |
Differs from #copyButFirst in its error behavior." |
7796 | 407 |
|
18714
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
408 |
^ self allButFirst:1 |
8571 | 409 |
|
410 |
" |
|
411 |
'1234567890' allButFirst |
|
18711
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
412 |
'ab' allButFirst |
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
413 |
'a' allButFirst |
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
414 |
'' allButFirst |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
415 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
416 |
'' copyButFirst |
8571 | 417 |
" |
418 |
! |
|
419 |
||
420 |
allButFirst:n |
|
8874 | 421 |
"Return a copy of the receiver containing all but the first n elements. |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
422 |
Returns a short (or empty) collection if there are not enough elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
423 |
Differs from #copyButFirst: in its error behavior." |
18711
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
424 |
|
18714
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
425 |
n > self size ifTrue:[^ self copyEmpty]. |
8571 | 426 |
^ self copyFrom:n+1 |
427 |
||
428 |
" |
|
15058
ad77c7e43ebd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14947
diff
changeset
|
429 |
'1234567890' allButFirst:5 |
ad77c7e43ebd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14947
diff
changeset
|
430 |
'123456' allButFirst:5 |
ad77c7e43ebd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14947
diff
changeset
|
431 |
'12345' allButFirst:5 |
ad77c7e43ebd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14947
diff
changeset
|
432 |
'1234' allButFirst:5 |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
433 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
434 |
'1234' copyButFirst:5 |
8571 | 435 |
" |
7796 | 436 |
! |
437 |
||
6149 | 438 |
allButLast |
8874 | 439 |
"Return a copy of the receiver containing all but the last element. |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
440 |
Returns an empty collection if there are not at least two elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
441 |
Differs from #copyButFirst: in its error behavior." |
6183 | 442 |
|
443 |
^ self allButLast:1 |
|
444 |
||
8571 | 445 |
" |
446 |
'1234567890' allButLast |
|
18711
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
447 |
'ab' allButLast |
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
448 |
'0' allButLast |
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
449 |
'' allButLast |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
450 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
451 |
'' copyButLast |
8571 | 452 |
" |
453 |
||
6183 | 454 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
455 |
! |
|
456 |
||
457 |
allButLast:n |
|
8874 | 458 |
"Return a copy of the receiver containing all but the last n elements. |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
459 |
Returns a short (or empty) collection if there are not enough elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
460 |
Differs from #copyButFirst: in its error behavior." |
6183 | 461 |
|
18714
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
462 |
|mySize| |
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
463 |
|
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
464 |
mySize := self size. |
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
465 |
n >= mySize ifTrue:[^ self copyEmpty]. |
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
466 |
^ self copyFrom: 1 to: (mySize - n) |
6183 | 467 |
|
8571 | 468 |
" |
469 |
'1234567890' allButLast:5 |
|
18711
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
470 |
'12345' allButLast:5 |
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
471 |
'123' allButLast:5 |
18714
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
472 |
{ 1 . 2 . 3 . 4 . 5 . 6 . 7} asOrderedCollection allButLast:5 |
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
473 |
{ 1 . 2 . 3 . 4 . 5} asOrderedCollection allButLast:5 |
08183ed2c404
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18713
diff
changeset
|
474 |
{ 1 . 2 . 3 } asOrderedCollection allButLast:5 |
18711
81ebc1f3389d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18549
diff
changeset
|
475 |
'' allButLast:5 |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
476 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
477 |
'' copyButLast:5 |
8571 | 478 |
" |
479 |
||
6183 | 480 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 481 |
! |
482 |
||
8874 | 483 |
atPin: index |
4414 | 484 |
"Return the index'th element of me if possible. |
485 |
Return the first or last element if index is out of bounds." |
|
486 |
||
487 |
index < 1 ifTrue:[^ self first]. |
|
488 |
index > self size ifTrue:[^ self last]. |
|
489 |
^ self at:index |
|
490 |
||
491 |
" |
|
8874 | 492 |
#(1 2 3) atPin:4 |
493 |
#(1 2 3) atPin:0 |
|
494 |
#(1 2 3) atPin:1 |
|
495 |
#(1 2 3) atPin:3 |
|
496 |
" |
|
497 |
! |
|
498 |
||
499 |
atRandom |
|
4459 | 500 |
"Return any random element from the receiver" |
501 |
||
8573 | 502 |
^ self atRandom:Random |
503 |
||
504 |
" |
|
8874 | 505 |
#(1 2 3) atRandom |
506 |
" |
|
507 |
! |
|
508 |
||
509 |
atRandom:aRandomGenerator |
|
8573 | 510 |
"Return any random element from the receiver" |
511 |
||
4459 | 512 |
|max idx| |
513 |
||
514 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 515 |
self emptyCollectionError |
4459 | 516 |
]. |
8573 | 517 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 518 |
^ self at:idx |
519 |
||
520 |
" |
|
8874 | 521 |
#(1 2 3) atRandom:(Random new) |
522 |
" |
|
523 |
! |
|
524 |
||
525 |
atWrap:index |
|
4975 | 526 |
"Return the index'th element of me if possible. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
527 |
Wrap the index modulu the receiver's size if it is out of bounds." |
4975 | 528 |
|
529 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
530 |
||
531 |
" |
|
8874 | 532 |
#(1 2 3) atWrap:1 |
533 |
#(1 2 3) atWrap:2 |
|
534 |
#(1 2 3) atWrap:3 |
|
535 |
#(1 2 3) atWrap:4 |
|
536 |
#(1 2 3) atWrap:5 |
|
537 |
#(1 2 3) atWrap:6 |
|
538 |
#(1 2 3) atWrap:7 |
|
539 |
#(1 2 3) atWrap:0 |
|
540 |
#(1 2 3) atWrap:-1 |
|
4975 | 541 |
" |
5372 | 542 |
! |
543 |
||
8611 | 544 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 545 |
"squeak uses a different naming here - sigh. |
546 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
547 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
548 |
this method first takes a slice (from:to:) and then collects over that. |
|
549 |
I.e. it is equivalent to: |
|
8874 | 550 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 551 |
|
552 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
553 |
! |
|
554 |
||
6980 | 555 |
copyUpToLast:element |
8874 | 556 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 557 |
of element; uses = for comparison" |
558 |
||
559 |
|idx| |
|
560 |
||
561 |
idx := self lastIndexOf:element. |
|
562 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
563 |
^ self copyTo:idx - 1 |
|
564 |
||
565 |
" |
|
8874 | 566 |
'hello world' copyUpToLast:$l |
567 |
'123456123456' copyUpToLast:$2 |
|
568 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
569 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
570 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 571 |
" |
572 |
! |
|
573 |
||
8276 | 574 |
copyWithoutAll:elementsToSkip |
575 |
"return a new collection consisting of a copy of the receiver, with |
|
576 |
ALL elements equal to any in elementsToSkip are left out. |
|
577 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
578 |
||
579 |
|n "{ Class: SmallInteger }" |
|
580 |
sz "{ Class: SmallInteger }" |
|
581 |
srcIndex "{ Class: SmallInteger }" |
|
582 |
dstIndex "{ Class: SmallInteger }" |
|
583 |
skipIndex "{ Class: SmallInteger }" |
|
584 |
copy l| |
|
585 |
||
8874 | 586 |
"the code below may look like overkill, |
8276 | 587 |
however, for big collections its better to move data |
588 |
around in big chunks" |
|
589 |
||
590 |
n := self occurrencesOfAny:elementsToSkip. |
|
591 |
n == 0 ifTrue:[^ self copy]. |
|
592 |
||
593 |
sz := self size. |
|
594 |
copy := self copyEmptyAndGrow:(sz - n). |
|
595 |
||
596 |
srcIndex := 1. |
|
597 |
dstIndex := 1. |
|
598 |
||
599 |
n timesRepeat:[ |
|
8874 | 600 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
601 |
l := skipIndex - srcIndex. |
|
602 |
l ~~ 0 ifTrue:[ |
|
603 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
604 |
with:self startingAt:srcIndex. |
|
605 |
dstIndex := dstIndex + l |
|
606 |
]. |
|
607 |
srcIndex := skipIndex + 1 |
|
8276 | 608 |
]. |
609 |
l := sz - srcIndex. |
|
610 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 611 |
with:self startingAt:srcIndex. |
8276 | 612 |
^ copy |
613 |
||
614 |
" |
|
615 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 616 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 617 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
618 |
" |
|
619 |
! |
|
620 |
||
7304 | 621 |
copyWithoutFirst |
18713
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
622 |
"An alias for copyButFirst: for squeak compatibility. |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
623 |
Raises an error, if the receiver is empty." |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
624 |
|
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
625 |
self isEmpty ifTrue:[ self notEnoughElementsError ]. |
7304 | 626 |
^ self copyFrom:2 |
18713
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
627 |
|
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
628 |
" |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
629 |
'foo' copyWithoutFirst |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
630 |
'f' copyWithoutFirst |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
631 |
'' copyWithoutFirst |
f777ab47baa5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18712
diff
changeset
|
632 |
" |
7304 | 633 |
! |
634 |
||
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
635 |
joinWith:separatingElement |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
636 |
"return a collection generated by concatenating my elements |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
637 |
and embedding separatingElement in between. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
638 |
Similar to asStringWith:, but not specifically targeted towards collections of strings." |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
639 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
640 |
^ self |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
641 |
joinWithAll:(Array with:separatingElement) |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
642 |
from:1 to:(self size) as:nil |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
643 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
644 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
645 |
#('hello' 'world' 'foo' 'bar' 'baz') joinWith:$; |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
646 |
#('hello' 'world' 'foo' 'bar' 'baz') joinWith:$| |
16701
316577a57c5d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16694
diff
changeset
|
647 |
#('hello' 'world' 'foo' 'bar' 'baz') joinWithAll:'; ' |
316577a57c5d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16694
diff
changeset
|
648 |
" |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
649 |
! |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
650 |
|
12051 | 651 |
shuffled |
652 |
"return a randomly shuffled copy of the receiver" |
|
653 |
||
654 |
^ self shuffledBy:Random |
|
655 |
||
656 |
" |
|
657 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
658 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
659 |
" |
|
660 |
! |
|
661 |
||
662 |
shuffledBy:aRandom |
|
663 |
"return a randomly shuffled copy of the receiver, using the given random generator" |
|
664 |
||
665 |
|copy| |
|
666 |
||
667 |
copy := self shallowCopy. |
|
668 |
copy size to: 1 by: -1 do:[:i | |
|
15576 | 669 |
copy swap: i with: (aRandom nextIntegerBetween:1 and:i) |
12051 | 670 |
]. |
671 |
^ copy |
|
672 |
||
673 |
" |
|
674 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
675 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
676 |
" |
|
677 |
! |
|
678 |
||
11279 | 679 |
sortBy:aBlock |
680 |
"Sort inplace - destructive" |
|
681 |
||
682 |
self sort:aBlock |
|
683 |
||
684 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 685 |
! ! |
686 |
||
14138
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
687 |
!SequenceableCollection methodsFor:'Compatibility-V''Age'! |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
688 |
|
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
689 |
replaceFrom:start to:end withObject:anObject |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
690 |
"Replace the elements from start to end with anObject. |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
691 |
Return the receiver." |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
692 |
|
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
693 |
self from:start to:end put:anObject. |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
694 |
^ self. |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
695 |
|
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
696 |
" |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
697 |
|a| |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
698 |
|
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
699 |
a := Array new: 10. |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
700 |
a replaceFrom:3 to:7 withObject:999. |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
701 |
a |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
702 |
" |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
703 |
|
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
704 |
"Created: / 16-05-2012 / 11:13:55 / cg" |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
705 |
! ! |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
706 |
|
7261 | 707 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 708 |
|
709 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
710 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
711 |
! ! |
|
712 |
||
1 | 713 |
!SequenceableCollection methodsFor:'accessing'! |
714 |
||
1366 | 715 |
after:anObject |
716 |
"return the element, after anObject. |
|
5231 | 717 |
If anObject is not in the receiver, report an error; |
718 |
if anObject is the last in the receiver, return nil. |
|
1366 | 719 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
720 |
||
721 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
722 |
||
723 |
" |
|
8874 | 724 |
#(4 3 2 1) asOrderedCollection after:3. |
725 |
#(4 3 2 1) asOrderedCollection after:5 |
|
726 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 727 |
" |
728 |
||
729 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
730 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
731 |
! |
|
732 |
||
733 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 734 |
"return the element, after anObject. |
5231 | 735 |
If anObject is the last in the receiver, return nil. |
736 |
If there is no such element (anObject is not found), |
|
1366 | 737 |
return the value from exceptionBlock." |
738 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
739 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
740 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
741 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
742 |
idx ~~ 0 ifTrue:[ |
8874 | 743 |
idx == self size ifTrue:[^ nil]. |
744 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
745 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
746 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
747 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
748 |
" |
8874 | 749 |
#(4 3 2 1) after:3 ifAbsent:nil. |
750 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
751 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
752 |
(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
|
753 |
" |
1366 | 754 |
! |
755 |
||
6967 | 756 |
anElement |
8874 | 757 |
"return any element from the collection, |
17553
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
758 |
report an error if there is none. |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
759 |
Use this to fetch the some element from a collection which is non-indexed or which |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
760 |
has a non-numeric index. I.e. if someone gets an arbitrary collection which might be either indexable |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
761 |
or not, anElement is a save way to access some element without a need to check for a proper key." |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
762 |
|
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
763 |
^ self at:1 |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
764 |
|
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
765 |
" |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
766 |
#() anElement -> Error |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
767 |
#(1 2 3) anElement -> 1 |
6d4e48b6091d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17542
diff
changeset
|
768 |
#(1 2 3) asSet anElement -> any one (undefined which one) |
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
769 |
" |
6967 | 770 |
! |
771 |
||
32 | 772 |
at:index ifAbsent:exceptionBlock |
8874 | 773 |
"return the element at index if valid. |
774 |
If the index is invalid, return the result of evaluating |
|
302 | 775 |
the exceptionblock. |
8874 | 776 |
NOTICE: |
777 |
in ST-80, this message is only defined for Dictionaries, |
|
778 |
however, having a common protocol with indexed collections |
|
779 |
often simplifies things." |
|
32 | 780 |
|
8579 | 781 |
(index between:1 and:self size) ifFalse:[ |
8874 | 782 |
^ exceptionBlock value |
32 | 783 |
]. |
784 |
^ self at:index |
|
785 |
||
95 | 786 |
" |
787 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 788 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
789 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 790 |
|
95 | 791 |
(Dictionary with:(#foo -> #bar) |
8874 | 792 |
with:(#frob -> #baz)) |
793 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 794 |
" |
362 | 795 |
! |
796 |
||
13003 | 797 |
atAllIndices:indexCollection |
798 |
"return the elements at each index from indexCollection." |
|
799 |
||
800 |
^ indexCollection collect:[:eachIdx | self at:eachIdx]. |
|
801 |
||
802 |
" |
|
803 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:#( 8 5 12 12 15) |
|
804 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:( 5 to: 10) |
|
805 |
" |
|
806 |
||
807 |
"Created: / 08-08-2010 / 01:15:06 / cg" |
|
808 |
! |
|
809 |
||
810 |
atIndex:index |
|
811 |
"return an element at a given index. This allows for seqentialCollections |
|
812 |
and orderedDictionaries to be both accessed via a numeric index." |
|
813 |
||
814 |
^ self at:index |
|
815 |
||
816 |
"Created: / 08-08-2010 / 00:50:10 / cg" |
|
817 |
! |
|
818 |
||
15535 | 819 |
atIndex:index ifAbsent:absentBlock |
820 |
"return an element at a given index. This allows for seqenveableCollections |
|
821 |
and orderedDictionaries to be both accessed via a numeric index." |
|
822 |
||
823 |
^ self at:index ifAbsent:absentBlock |
|
824 |
! |
|
825 |
||
13003 | 826 |
atIndex:index put:newValue |
827 |
"return an element at a given index. This allows for seqentialCollections |
|
828 |
and orderedDictionaries to be both accessed via a numeric index." |
|
829 |
||
830 |
^ self at:index put:newValue |
|
831 |
||
832 |
"Created: / 08-08-2010 / 00:50:27 / cg" |
|
833 |
! |
|
834 |
||
1366 | 835 |
before:anObject |
836 |
"return the element before the argument, anObject. |
|
5231 | 837 |
If anObject is not in the receiver, report an error; |
838 |
if anObject is the first in the receiver, return nil. |
|
1366 | 839 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
840 |
||
841 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
842 |
||
843 |
" |
|
8874 | 844 |
#(4 3 2 1) asOrderedCollection before:3. |
845 |
#(4 3 2 1) asOrderedCollection before:4 |
|
846 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 847 |
" |
848 |
||
849 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
850 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
851 |
! |
|
852 |
||
853 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 854 |
"return the element, before anObject. |
5231 | 855 |
If anObject is the first in the receiver, return nil. |
856 |
If there is no such element (anObject is not found), |
|
1366 | 857 |
return the value from exceptionBlock." |
858 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
859 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
860 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
861 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
862 |
idx ~~ 0 ifTrue:[ |
8874 | 863 |
idx == 1 ifTrue:[^ nil]. |
864 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
865 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
866 |
^ exceptionBlock value |
1366 | 867 |
|
5231 | 868 |
" |
8874 | 869 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
870 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
871 |
#(4 3 2 1) asOrderedCollection before:5. |
|
872 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
873 |
#(4 3 2 1) asOrderedCollection before:4. |
|
874 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
875 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 876 |
" |
877 |
||
1366 | 878 |
! |
879 |
||
602 | 880 |
first |
8874 | 881 |
"return the first element; |
5231 | 882 |
report an error, if the collection is empty." |
602 | 883 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
884 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 885 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
886 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
887 |
" |
8874 | 888 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
889 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
890 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
891 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
892 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 893 |
! |
894 |
||
5887 | 895 |
first:n |
896 |
"return the n first elements of the collection. |
|
17701 | 897 |
No longer raises an error if there are not enough elements; |
898 |
instead, returns what is there" |
|
5887 | 899 |
|
900 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
17701 | 901 |
|
902 |
"/ OLD: |
|
903 |
"/ "error if collection has not enough elements" |
|
904 |
"/ n > self size ifTrue:[ |
|
905 |
"/ ^ self notEnoughElementsError |
|
906 |
"/ ]. |
|
907 |
||
908 |
"/ NEW: |
|
909 |
"/ return what we have - no error if not enough elements |
|
910 |
^ self copyFirst:(n min:self size) |
|
5887 | 911 |
|
912 |
" |
|
8874 | 913 |
#(1 2 3 4 5) first:3 |
914 |
#(1 2 3 4 5) first:6 |
|
915 |
#(1 2 3 4 5) asSet first:3 |
|
916 |
'hello world' first:5 |
|
5887 | 917 |
" |
918 |
! |
|
919 |
||
11878 | 920 |
indexOfNth:n occurrenceOf:what |
921 |
"return the index of the nTh occurence of a value, or 0 if there are not that many" |
|
922 |
||
923 |
|idx cnt| |
|
924 |
||
925 |
n > self size ifTrue:[^ 0]. |
|
926 |
cnt := 0. |
|
927 |
idx := 0. |
|
928 |
[ |
|
929 |
idx := self indexOf:what startingAt:idx+1. |
|
930 |
idx == 0 ifTrue:[^ 0]. |
|
931 |
cnt := cnt + 1. |
|
932 |
cnt = n ifTrue:[ ^ idx]. |
|
933 |
] loop. |
|
934 |
||
16702
7b1f3ddf3032
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16701
diff
changeset
|
935 |
" 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 |
11878 | 936 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1 |
16702
7b1f3ddf3032
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16701
diff
changeset
|
937 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:2 occurrenceOf:7 |
11878 | 938 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9 |
939 |
" |
|
940 |
! |
|
941 |
||
362 | 942 |
keyAtEqualValue:value |
943 |
"return the index of a value. |
|
944 |
This is normally not used (use indexOf:), but makes the |
|
945 |
protocol more compatible with dictionaries." |
|
946 |
||
947 |
^ self indexOf:value |
|
948 |
! |
|
949 |
||
950 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
951 |
"return the index of a value. |
|
952 |
This is normally not used (use indexOf:), but makes the |
|
953 |
protocol more compatible with dictionaries." |
|
954 |
||
955 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 956 |
! |
957 |
||
958 |
keyAtValue:value |
|
959 |
"return the index of a value. |
|
960 |
This is normally not used (use indexOf:), but makes the |
|
961 |
protocol more compatible with dictionaries." |
|
962 |
||
963 |
^ self identityIndexOf:value |
|
964 |
! |
|
965 |
||
966 |
keyAtValue:value ifAbsent:exceptionBlock |
|
967 |
"return the index of a value. |
|
968 |
This is normally not used (use indexOf:), but makes the |
|
969 |
protocol more compatible with dictionaries." |
|
970 |
||
971 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
972 |
! |
|
973 |
||
974 |
last |
|
5231 | 975 |
"return the last element; |
976 |
report an error, if the collection is empty." |
|
602 | 977 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
978 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
979 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
980 |
(sz := self size) == 0 ifFalse:[ |
8874 | 981 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
982 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
983 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
984 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
985 |
|
5231 | 986 |
" |
987 |
#(1 2 3 4 5) last |
|
988 |
#() last |
|
989 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
990 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
991 |
|
5887 | 992 |
last:n |
993 |
"return the n last elements of the collection. |
|
18175 | 994 |
No longer raises an error if there are not enough elements; |
995 |
instead, returns what is there." |
|
5887 | 996 |
|
997 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
18175 | 998 |
"/ OLD: |
999 |
"/ n > self size ifTrue:[^ self notEnoughElementsError]. |
|
1000 |
||
1001 |
^ self copyLast:(n min:self size) |
|
5887 | 1002 |
|
1003 |
" |
|
8874 | 1004 |
#(1 2 3 4 5) last:3 |
1005 |
#(1 2 3 4 5) last:6 |
|
1006 |
#(1 2 3 4 5) asSet last:3 |
|
1007 |
'hello world' last:5 |
|
18175 | 1008 |
'hello' last:10 |
5887 | 1009 |
" |
1010 |
! |
|
1011 |
||
4986 | 1012 |
nth:n |
5231 | 1013 |
"return the nth element; |
1014 |
report an error, if the collections size is smaller than n." |
|
4986 | 1015 |
|
1016 |
^ self at:n |
|
1017 |
||
1018 |
" |
|
8874 | 1019 |
args: |
4986 | 1020 |
returns: nth element <object> |
1021 |
" |
|
1022 |
||
5231 | 1023 |
" |
8874 | 1024 |
#(10 20 30 40 50) nth:4 |
5231 | 1025 |
#(10 20 30 40 50) nth:6 |
1026 |
#() nth:1 |
|
1027 |
" |
|
1028 |
||
4986 | 1029 |
! |
1030 |
||
11915 | 1031 |
order |
1032 |
"the order is the set of keys " |
|
1033 |
||
1034 |
^ 1 to:self size |
|
1035 |
! |
|
1036 |
||
4855 | 1037 |
second |
5231 | 1038 |
"return the second element; |
1039 |
report an error, if the collections size is smaller than 2." |
|
4855 | 1040 |
|
1041 |
^ self at:2 |
|
1042 |
||
1043 |
" |
|
8874 | 1044 |
args: |
4855 | 1045 |
returns: secondElement <object> |
1046 |
" |
|
1047 |
||
1048 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
1049 |
! |
|
1050 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1051 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1052 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1053 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1054 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1055 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1056 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1057 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1058 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1059 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1060 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 1061 |
! ! |
1062 |
||
602 | 1063 |
!SequenceableCollection methodsFor:'adding & removing'! |
1064 |
||
1065 |
add:anObject |
|
1066 |
"append the argument, anObject to the collection. |
|
1067 |
Return the argument, anObject. |
|
1068 |
||
1069 |
Notice, that this is modifies the receiver NOT a copy. |
|
1070 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1071 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1072 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1073 |
||
1074 |
|newSize| |
|
1075 |
||
1076 |
newSize := self size + 1. |
|
1077 |
self grow:newSize. |
|
1078 |
self at:newSize put:anObject. |
|
1079 |
^ anObject |
|
1080 |
||
1081 |
" |
|
1082 |
|a| |
|
1083 |
a := #(1 2 3 4 5 6 7 8). |
|
1084 |
a add:'hello'. |
|
1085 |
a |
|
1086 |
" |
|
1087 |
" |
|
1088 |
|c| |
|
1089 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1090 |
c add:'hello'. |
|
1091 |
c |
|
1092 |
" |
|
1093 |
! |
|
1094 |
||
1095 |
add:anElement beforeIndex:index |
|
1096 |
"insert the first argument, anObject into the collection before slot index. |
|
1097 |
Return the receiver (sigh - ST-80 compatibility). |
|
1098 |
||
1099 |
Notice, that this is modifies the receiver NOT a copy. |
|
1100 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1101 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1102 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1103 |
||
1104 |
|newSize| |
|
1105 |
||
1106 |
newSize := self size + 1. |
|
1107 |
self grow:newSize. |
|
1108 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
1109 |
self at:index put:anElement |
|
1110 |
||
1111 |
" |
|
1112 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
1113 |
" |
|
202 | 1114 |
! |
1115 |
||
15174
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1116 |
addAll:aCollection beforeIndex:index |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1117 |
"insert all elements of the argument, anObject to become located at index. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1118 |
The collection may be unordered, but then order of the sliced-in elements |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1119 |
is undefined. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1120 |
Return the receiver." |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1121 |
|
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1122 |
|idx| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1123 |
|
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1124 |
idx := index. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1125 |
aCollection do:[:element | |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1126 |
self add:element beforeIndex:idx. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1127 |
idx := idx + 1. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1128 |
]. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1129 |
|
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1130 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1131 |
|c| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1132 |
c := #(1 2 3 4) asOrderedCollection. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1133 |
c addAll:'here' beforeIndex:3 |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1134 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1135 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1136 |
|c| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1137 |
c := #(1 2 3 4) asOrderedCollection. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1138 |
c removeFirst. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1139 |
c addAll:'here' beforeIndex:3 |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1140 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1141 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1142 |
|c| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1143 |
c := #(1 2 3 4) asOrderedCollection. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1144 |
c addAll:'here' beforeIndex:1 |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1145 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1146 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1147 |
|c| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1148 |
c := #(1 2 3 4) asOrderedCollection. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1149 |
c addAll:'here' beforeIndex:5 |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1150 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1151 |
|
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1152 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1153 |
|c| |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1154 |
c := #(1 2 3 4) asOrderedCollection. |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1155 |
c addAll:('hello' asSet) beforeIndex:3 |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1156 |
" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1157 |
|
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1158 |
"Modified: 15.4.1997 / 12:43:59 / cg" |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1159 |
! |
120368418a69
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15156
diff
changeset
|
1160 |
|
602 | 1161 |
addFirst:anObject |
1162 |
"prepend the argument, anObject to the collection. |
|
1163 |
Return the argument, anObject. |
|
1164 |
||
1165 |
Notice, that this is modifies the receiver NOT a copy. |
|
1166 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1167 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1168 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1169 |
||
1170 |
|newSize| |
|
1171 |
||
1172 |
newSize := self size + 1. |
|
1173 |
self grow:newSize. |
|
1174 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1175 |
self at:1 put:anObject. |
|
1176 |
^ anObject |
|
1177 |
||
1178 |
" |
|
8874 | 1179 |
|a| |
1180 |
a:= #(1 2 3 4 5 6 7 8). |
|
1181 |
a addFirst:'hello'. |
|
1182 |
a |
|
602 | 1183 |
" |
1184 |
" |
|
1185 |
|c| |
|
1186 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1187 |
c addFirst:'hello'. |
|
1188 |
c |
|
1189 |
" |
|
69 | 1190 |
! |
1191 |
||
602 | 1192 |
remove:anElement ifAbsent:aBlock |
19079 | 1193 |
"search for the first occurrrence of object |
1194 |
(i.e. which is equal to anElement) |
|
1195 |
If found remove and return it; |
|
1196 |
if not, return the value from evaluating aBlock. |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1197 |
Use equality compare (=) to search for an occurrence. |
602 | 1198 |
|
1199 |
Notice, that this is modifies the receiver NOT a copy. |
|
1200 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1201 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1202 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1203 |
||
19079 | 1204 |
|any foundElement |
602 | 1205 |
dstIndex "{ Class: SmallInteger }" |
1206 |
sz "{ Class: SmallInteger }"| |
|
1207 |
||
1208 |
any := false. |
|
1209 |
sz := self size. |
|
1210 |
1 to:sz do:[:srcIndex | |
|
19079 | 1211 |
any ifFalse:[ |
1212 |
"/ still searching |
|
1213 |
(anElement = (foundElement := self at:srcIndex)) ifTrue:[ |
|
1214 |
any := true. |
|
1215 |
dstIndex := srcIndex. |
|
1216 |
] |
|
1217 |
] ifTrue:[ |
|
1218 |
"/ already copying |
|
1219 |
self at:dstIndex put:(self at:srcIndex). |
|
1220 |
dstIndex := dstIndex + 1 |
|
1221 |
] |
|
602 | 1222 |
]. |
1223 |
any ifTrue:[ |
|
19079 | 1224 |
self grow:dstIndex - 1. |
1225 |
^ foundElement |
|
602 | 1226 |
]. |
1227 |
^ aBlock value |
|
1228 |
||
1229 |
" |
|
1422 | 1230 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1231 |
#(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
|
1232 |
#(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
|
1233 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
19079 | 1234 |
|
1235 |
|a| |
|
1236 |
a := #(1 2 3 1 2 3 1 2 3). |
|
1237 |
a remove:3. |
|
1238 |
a |
|
1239 |
||
1240 |
|a| |
|
1241 |
a := #(1 2 3 1 2 3 1 2 3) asOrderedCollection. |
|
1242 |
a remove:3. |
|
1243 |
a |
|
602 | 1244 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1245 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1246 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1247 |
! |
1248 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1249 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1250 |
"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
|
1251 |
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
|
1252 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1253 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1254 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1255 |
"/ 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
|
1256 |
|
16456
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1257 |
|runIndex removed element stop| |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1258 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1259 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1260 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1261 |
runIndex := 1. |
16456
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1262 |
stop := self size. |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1263 |
[runIndex <= stop] whileTrue:[ |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1264 |
element := self at:runIndex. |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1265 |
(aBlock value:element) ifTrue:[ |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1266 |
removed add:element. |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1267 |
self removeAtIndex:runIndex. |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1268 |
stop := self size. "/ could probably go along with stop := stop - 1 |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1269 |
] ifFalse:[ |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1270 |
runIndex := runIndex + 1 |
dcf3f5f016a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16422
diff
changeset
|
1271 |
] |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1272 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1273 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1274 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1275 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1276 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1277 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1278 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1279 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1280 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1281 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1282 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1283 |
"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
|
1284 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1285 |
|
602 | 1286 |
removeAtIndex:anIndex |
1287 |
"remove the element stored at anIndex. Return the removed object. |
|
1288 |
||
1289 |
Notice, that this is modifies the receiver NOT a copy. |
|
1290 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1291 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1292 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1293 |
||
1294 |
|element| |
|
1295 |
||
1296 |
element := self at:anIndex. |
|
1297 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1298 |
^ element |
|
1299 |
||
1300 |
" |
|
1301 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1302 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1303 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1304 |
" |
|
13 | 1305 |
! |
1306 |
||
602 | 1307 |
removeFirst |
1308 |
"remove the first element of the receiver and return it. |
|
1309 |
||
1310 |
Notice, that this is modifies the receiver NOT a copy. |
|
1311 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1312 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1313 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1314 |
||
1315 |
^ self removeAtIndex:1 |
|
1316 |
||
1317 |
" |
|
1318 |
|a| |
|
1319 |
a := #(1 2 3 4 5 6). |
|
1320 |
a removeFirst. |
|
8874 | 1321 |
a |
602 | 1322 |
" |
13 | 1323 |
! |
1324 |
||
2294 | 1325 |
removeFromIndex:startIndex |
1326 |
"remove the elements stored at indexes from startIndex to the end. |
|
1327 |
Return the receiver. |
|
1328 |
Returning the receiver is a historic leftover - it may at one |
|
1329 |
time return a collection of the removed elements. |
|
1330 |
||
8874 | 1331 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1332 |
therefore any other users of the receiver will also see this change. |
1333 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1334 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1335 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1336 |
||
1337 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1338 |
||
1339 |
" |
|
8874 | 1340 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1341 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1342 |
" |
|
1343 |
||
1344 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1345 |
! |
|
1346 |
||
602 | 1347 |
removeFromIndex:startIndex toIndex:endIndex |
1348 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1349 |
Return the receiver. |
|
14893
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1350 |
|
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1351 |
Notice, that this modifies the receiver - NOT a copy; |
602 | 1352 |
therefore any other users of the receiver will also see this change. |
1353 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1354 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1355 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1356 |
||
1357 |
|size newSize| |
|
1358 |
||
1359 |
(endIndex >= (size := self size)) ifTrue:[ |
|
14893
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1360 |
self grow:(startIndex - 1) |
602 | 1361 |
] ifFalse:[ |
14893
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1362 |
newSize := size - endIndex + startIndex - 1. |
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1363 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
c2a574a182f3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14686
diff
changeset
|
1364 |
self grow:newSize |
602 | 1365 |
] |
1366 |
||
1367 |
"/ |newSize| |
|
1368 |
"/ |
|
1369 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1370 |
"/ newSize <= 0 ifTrue:[ |
|
1371 |
"/ self grow:0 |
|
1372 |
"/ ] ifFalse:[ |
|
1373 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1374 |
"/ self grow:newSize |
|
1375 |
"/ ] |
|
1376 |
||
1377 |
" |
|
8874 | 1378 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1379 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1380 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1381 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1382 |
" |
1383 |
! |
|
1384 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1385 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1386 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1387 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1388 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1389 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1390 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1391 |
Also note, that it may be a slow operation for some collections, |
8874 | 1392 |
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
|
1393 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1394 |
|
19079 | 1395 |
|any |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1396 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1397 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1398 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1399 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1400 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1401 |
1 to:sz do:[:srcIndex | |
19079 | 1402 |
any ifFalse:[ |
1403 |
"/ still searching |
|
1404 |
(anElement == (self at:srcIndex)) ifTrue:[ |
|
1405 |
any := true. |
|
1406 |
dstIndex := srcIndex. |
|
1407 |
] |
|
1408 |
] ifTrue:[ |
|
1409 |
"/ already copying |
|
1410 |
self at:dstIndex put:(self at:srcIndex). |
|
1411 |
dstIndex := dstIndex + 1 |
|
1412 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1413 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1414 |
any ifTrue:[ |
19079 | 1415 |
self grow:dstIndex - 1. |
1416 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1417 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1418 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1419 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1420 |
" |
8874 | 1421 |
#(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
|
1422 |
#(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
|
1423 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1424 |
|
2363 | 1425 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1426 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1427 |
|
602 | 1428 |
removeIndex:index |
1429 |
"remove the argument stored at index. Return the receiver. |
|
1430 |
||
1431 |
Notice, that this is modifies the receiver NOT a copy. |
|
1432 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1433 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1434 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1435 |
||
1436 |
self removeFromIndex:index toIndex:index |
|
1437 |
||
1438 |
" |
|
1439 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1440 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1441 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1442 |
" |
|
77 | 1443 |
! |
1444 |
||
602 | 1445 |
removeLast |
1446 |
"remove the last element of the receiver and return it. |
|
1447 |
||
1448 |
Notice, that this is modifies the receiver NOT a copy. |
|
1449 |
Also note, that it may be a slow operation for some collections, |
|
1450 |
due to the grow:-message, which is inefficient for fixed size |
|
1451 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1452 |
||
8874 | 1453 |
^ self removeAtIndex:(self size) |
602 | 1454 |
|
1455 |
" |
|
1456 |
|a| |
|
1457 |
a := #(1 2 3 4 5 6). |
|
1458 |
a removeLast. |
|
8874 | 1459 |
a |
602 | 1460 |
" |
1461 |
! ! |
|
1462 |
||
11407 | 1463 |
!SequenceableCollection methodsFor:'combinatoric'! |
1464 |
||
1465 |
combinationsDo: aBlock |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
1466 |
"Repeatly evaluate aBlock with all combinations of elements from the receiver's elements. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
1467 |
The receiver's elements must be collections of the individuals to be taken for the combinations" |
11407 | 1468 |
|
1469 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1470 |
||
1471 |
" |
|
1472 |
(Array |
|
11410 | 1473 |
with:($a to:$d)) |
1474 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1475 |
" |
|
1476 |
" |
|
1477 |
(Array |
|
11407 | 1478 |
with:($a to:$d) |
1479 |
with:(1 to: 4)) |
|
1480 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1481 |
" |
|
1482 |
" |
|
1483 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1484 |
" |
|
11408 | 1485 |
"all combinations of two letters are generated with: |
1486 |
(Array |
|
1487 |
with:($a to:$z) |
|
1488 |
with:($a to:$z)) |
|
1489 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1490 |
" |
|
11432 | 1491 |
" |
1492 |
(Array |
|
1493 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1494 |
with:#(A)) |
|
1495 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1496 |
" |
|
11407 | 1497 |
! |
1498 |
||
1499 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1500 |
"a helper for combinationsDo:" |
|
1501 |
||
11431 | 1502 |
|loopedElement| |
1503 |
||
1504 |
loopedElement := self at:anInteger. |
|
1505 |
||
11407 | 1506 |
anInteger == self size ifTrue:[ |
11431 | 1507 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1508 |
^ self. |
11431 | 1509 |
]. |
1510 |
||
1511 |
loopedElement do:[:el | |
|
1512 |
|newPrefix| |
|
1513 |
||
1514 |
newPrefix := (prefix copyWith:el). |
|
1515 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1516 |
]. |
1517 |
||
1518 |
" |
|
1519 |
(Array |
|
1520 |
with:($a to:$d) |
|
1521 |
with:(1 to: 4)) |
|
1522 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1523 |
" |
|
11431 | 1524 |
" |
1525 |
(Array |
|
1526 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1527 |
with:#(A)) |
|
1528 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1529 |
" |
|
11407 | 1530 |
! |
1531 |
||
11462 | 1532 |
lexicographicPermutationsDo:aBlock |
1533 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1534 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1535 |
permutations in lexicographical order (i.e. sorted by size). |
|
11932 | 1536 |
|
11462 | 1537 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1538 |
the identical same collection (but with different contents) every time. |
1539 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1540 |
of permuted copies. The user of this function must be careful in the block, |
|
1541 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1542 |
|
1543 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1544 |
||
1545 |
" |
|
1546 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1547 |
" |
|
1548 |
! |
|
1549 |
||
1550 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1551 |
"a helper for lexicographicPermutationsDo:" |
|
1552 |
||
1553 |
|mySize values indices| |
|
1554 |
||
1555 |
mySize := self size. |
|
1556 |
anInteger > mySize ifTrue: [^ self]. |
|
1557 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1558 |
values := self copyFrom:anInteger to:mySize. |
|
1559 |
indices := (anInteger to:mySize) asArray. |
|
1560 |
values sortWith:indices. |
|
1561 |
indices do:[:i | |
|
1562 |
self swap: anInteger with: i. |
|
1563 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1564 |
self swap: anInteger with: i |
|
1565 |
] |
|
1566 |
||
1567 |
" |
|
1568 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1569 |
" |
|
1570 |
! |
|
1571 |
||
16111
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1572 |
permutationsDetect:aBlock |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1573 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1574 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1575 |
Return the first permutation for which aBlock returns true, nil if there is none. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1576 |
|
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1577 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1578 |
the identical same collection (but with different contents) every time. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1579 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1580 |
of permuted copies. The user of this function must be careful in the block, |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1581 |
to copy the argument, if it has to be stored somewhere." |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1582 |
|
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1583 |
self permutationsDo:[:p | (aBlock value:p) ifTrue:[^ p]]. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1584 |
^ nil. |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1585 |
! |
b87aaedf9878
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16099
diff
changeset
|
1586 |
|
11462 | 1587 |
permutationsDo:aBlock |
1588 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1589 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1590 |
|
11462 | 1591 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1592 |
the identical same collection (but with different contents) every time. |
1593 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1594 |
of permuted copies. The user of this function must be careful in the block, |
|
1595 |
to copy the argument, if it has to be stored somewhere." |
|
11407 | 1596 |
|
1597 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1598 |
||
1599 |
" |
|
11462 | 1600 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1601 |
" |
1602 |
||
1603 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1604 |
! |
|
1605 |
||
1606 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1607 |
"a helper for permutationsDo:" |
11407 | 1608 |
|
1609 |
|mySize| |
|
1610 |
||
1611 |
mySize := self size. |
|
11462 | 1612 |
anInteger > mySize ifTrue: [^ self]. |
1613 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1614 |
anInteger to:mySize do:[:i | |
1615 |
self swap: anInteger with: i. |
|
11462 | 1616 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1617 |
self swap: anInteger with: i |
1618 |
] |
|
1619 |
||
1620 |
" |
|
1621 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1622 |
|
1623 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1624 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1625 |
" |
1626 |
||
1627 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1628 |
! |
1629 |
||
1630 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1631 |
"a helper for permutationsDo:" |
|
1632 |
||
1633 |
|mySize| |
|
1634 |
||
1635 |
mySize := self size. |
|
1636 |
anInteger > mySize ifTrue: [^ self]. |
|
1637 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1638 |
anInteger to:mySize do:[:i | |
|
1639 |
self swap: anInteger with:i. |
|
1640 |
anotherCollection swap: anInteger with:i. |
|
1641 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1642 |
self swap:anInteger with: i. |
|
1643 |
anotherCollection swap:anInteger with:i. |
|
1644 |
] |
|
1645 |
||
1646 |
" |
|
1647 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1648 |
||
1649 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1650 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1651 |
" |
|
1652 |
||
1653 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1654 |
! |
|
1655 |
||
1656 |
permutationsWith:anotherCollection do:aBlock |
|
1657 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1658 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1659 |
|
11462 | 1660 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1661 |
the identical same collection (but with different contents) every time. |
1662 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1663 |
of permuted copies. The user of this function must be careful in the block, |
|
1664 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1665 |
|
1666 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1667 |
||
1668 |
" |
|
1669 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1670 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1671 |
" |
|
1672 |
||
1673 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1674 |
! ! |
1675 |
||
602 | 1676 |
!SequenceableCollection methodsFor:'comparing'! |
1677 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1678 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1679 |
"compare two sequenceable collections" |
8874 | 1680 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1681 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1682 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1683 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1684 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1685 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1686 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1687 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1688 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1689 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1690 |
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
|
1691 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1692 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1693 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1694 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1695 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1696 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1697 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1698 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1699 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1700 |
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
|
1701 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1702 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1703 |
(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
|
1704 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1705 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1706 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1707 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1708 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1709 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1710 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1711 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1712 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1713 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1714 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1715 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1716 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1717 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1718 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1719 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1720 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1721 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1722 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1723 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1724 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1725 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1726 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1727 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1728 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1729 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1730 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1731 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1732 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1733 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1734 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1735 |
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
|
1736 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1737 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1738 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1739 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1740 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1741 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1742 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1743 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1744 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1745 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1746 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1747 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1748 |
"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
|
1749 |
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
|
1750 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1751 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1752 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1753 |
|
602 | 1754 |
= aCollection |
1755 |
"return true if the receiver and aCollection represent collections |
|
12290 | 1756 |
with equal contents, and if they are of the same species." |
602 | 1757 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1758 |
| stop "{ Class: SmallInteger }" | |
602 | 1759 |
|
1760 |
(aCollection == self) ifTrue:[^true]. |
|
1761 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1762 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1763 |
|
1764 |
stop := self size. |
|
1765 |
stop == (aCollection size) ifFalse:[^false]. |
|
1766 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1767 |
1 to:stop do:[:index| |
9366 | 1768 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1769 |
]. |
1770 |
^ true |
|
1771 |
||
1772 |
" |
|
8874 | 1773 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1774 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1775 |
#($1 $2 $3 $4 $5) = '12345' |
|
1776 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1777 |
" |
1778 |
! |
|
1779 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1780 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1781 |
"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
|
1782 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1783 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1784 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1785 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1786 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1787 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1788 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1789 |
"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
|
1790 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1791 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1792 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1793 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1794 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1795 |
|
16176
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1796 |
beginsWith:aCollection |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1797 |
"Squeak & VW compatibility: similar to #startsWith: but returns false for an empty argument - sigh" |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1798 |
|
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1799 |
^ (self startsWith:aCollection) and:[aCollection notEmpty] |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1800 |
|
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1801 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1802 |
! |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
1803 |
|
8874 | 1804 |
commonPrefixWith:aCollection |
6507 | 1805 |
"return the common prefix of myself and the argument, aCollection. |
1806 |
If there is none, an empty collection is returned." |
|
1807 |
||
1808 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1809 |
||
1810 |
" |
|
8874 | 1811 |
'hello' commonPrefixWith:'hello' |
1812 |
'hello' commonPrefixWith:'hElLo' |
|
1813 |
'hello' commonPrefixWith:'hello world' |
|
1814 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1815 |
||
1816 |
'hello world' commonPrefixWith:'hello' |
|
1817 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1818 |
||
1819 |
'abcd' commonPrefixWith:'bcde' |
|
1820 |
||
1821 |
'abcd' commonPrefixWith:'abab' |
|
1822 |
'abcd' commonPrefixWith:'ab' |
|
1823 |
'abcd' commonPrefixWith:'ababab' |
|
1824 |
'abcd' commonPrefixWith:'abcdef' |
|
1825 |
||
1826 |
'abab' commonPrefixWith:'abcd' |
|
1827 |
'ab' commonPrefixWith:'abcd' |
|
1828 |
'ababab' commonPrefixWith:'abcd' |
|
1829 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1830 |
" |
1831 |
! |
|
1832 |
||
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1833 |
commonPrefixWith:aCollection caseSensitive:caseSensitive |
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1834 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1835 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1836 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1837 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1838 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1839 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1840 |
1 to:matchLen do:[:idx | |
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1841 |
|elHere elThere same| |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1842 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1843 |
elHere := self at:idx. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1844 |
elThere := aCollection at:idx. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1845 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1846 |
caseSensitive ifFalse:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1847 |
same := elHere sameAs:elThere |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1848 |
] ifTrue:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1849 |
same := elHere = elThere |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1850 |
]. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1851 |
same ifFalse:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1852 |
^ self copyTo:(idx - 1). |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1853 |
] |
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1854 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1855 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1856 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1857 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1858 |
" |
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1859 |
'hello' commonPrefixWith:'hello' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1860 |
'hello' commonPrefixWith:'hElLo' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1861 |
'hello' commonPrefixWith:'hello world' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1862 |
'hello' commonPrefixWith:'hElLo WoRlD' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1863 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1864 |
'hello world' commonPrefixWith:'hello' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1865 |
'hello WoRlD' commonPrefixWith:'hElLo' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1866 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1867 |
'abcd' commonPrefixWith:'bcde' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1868 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1869 |
'abcd' commonPrefixWith:'abab' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1870 |
'abcd' commonPrefixWith:'aBAb' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1871 |
'abcd' commonPrefixWith:'ab' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1872 |
'abcd' commonPrefixWith:'ababab' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1873 |
'abcd' commonPrefixWith:'abcdef' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1874 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1875 |
'abab' commonPrefixWith:'abcd' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1876 |
'ab' commonPrefixWith:'abcd' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1877 |
'ababab' commonPrefixWith:'abcd' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1878 |
'abcdef' commonPrefixWith:'abcd' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1879 |
" |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1880 |
! |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1881 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1882 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1883 |
<resource: #obsolete> |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1884 |
"return the common prefix of myself and the argument, aCollection. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1885 |
If there is none, an empty collection is returned." |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1886 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1887 |
^ self commonPrefixWith:aCollection caseSensitive:ignoreCase not |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1888 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1889 |
" |
8874 | 1890 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1891 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1892 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1893 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1894 |
||
1895 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1896 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1897 |
||
1898 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1899 |
||
1900 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1901 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1902 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1903 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1904 |
||
1905 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1906 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1907 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1908 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1909 |
" |
|
1910 |
! |
|
1911 |
||
1912 |
commonSuffixWith:aCollection |
|
6507 | 1913 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1914 |
If there is none, an empty collection is returned." |
|
1915 |
||
1916 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1917 |
||
1918 |
" |
|
8874 | 1919 |
'hello' commonSuffixWith:'hello' |
1920 |
'hello' commonSuffixWith:'hElLo' |
|
1921 |
'hello' commonSuffixWith:'hello world' |
|
1922 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1923 |
'hello2 world' commonSuffixWith:'hello world' |
|
1924 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1925 |
||
1926 |
'hello world' commonSuffixWith:'world' |
|
1927 |
'hello WoRlD' commonSuffixWith:'world' |
|
1928 |
||
1929 |
'dcba' commonSuffixWith:'edcb' |
|
1930 |
||
1931 |
'dcba' commonSuffixWith:'baba' |
|
1932 |
'dcba' commonSuffixWith:'ba' |
|
1933 |
'dcba' commonSuffixWith:'bababa' |
|
1934 |
'dcba' commonSuffixWith:'fdcba' |
|
1935 |
||
1936 |
'baba' commonSuffixWith:'dcba' |
|
1937 |
'ba' commonSuffixWith:'dcba' |
|
1938 |
'bababa' commonSuffixWith:'dcba' |
|
1939 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1940 |
" |
1941 |
! |
|
1942 |
||
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1943 |
commonSuffixWith:aCollection caseSensitive:caseSensitive |
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1944 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1945 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1946 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1947 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1948 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1949 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1950 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1951 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1952 |
1 to:matchLen do:[:idx | |
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1953 |
|elHere elThere same| |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1954 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1955 |
elHere := self at:(l1 - idx + 1). |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1956 |
elThere := aCollection at:(l2 - idx + 1). |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1957 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1958 |
caseSensitive ifFalse:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1959 |
same := elHere sameAs:elThere |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1960 |
] ifTrue:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1961 |
same := elHere = elThere |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1962 |
]. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1963 |
same ifFalse:[ |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1964 |
^ self copyFrom:(l1 - idx + 2). |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1965 |
] |
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1966 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1967 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1968 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1969 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1970 |
" |
15735
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1971 |
'hello' commonSuffixWith:'hello' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1972 |
'hello' commonSuffixWith:'hElLo' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1973 |
'hello' commonSuffixWith:'hello world' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1974 |
'hello' commonSuffixWith:'hElLo WoRlD' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1975 |
'hello2 world' commonSuffixWith:'hello world' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1976 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1977 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1978 |
'hello world' commonSuffixWith:'world' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1979 |
'hello WoRlD' commonSuffixWith:'world' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1980 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1981 |
'dcba' commonSuffixWith:'edcb' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1982 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1983 |
'dcba' commonSuffixWith:'baba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1984 |
'dcba' commonSuffixWith:'ba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1985 |
'dcba' commonSuffixWith:'bababa' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1986 |
'dcba' commonSuffixWith:'fdcba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1987 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1988 |
'baba' commonSuffixWith:'dcba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1989 |
'ba' commonSuffixWith:'dcba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1990 |
'bababa' commonSuffixWith:'dcba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1991 |
'fdcba' commonSuffixWith:'dcba' caseSensitive:false |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1992 |
" |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1993 |
! |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1994 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1995 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1996 |
<resource: #obsolete> |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1997 |
"return the common suffix (tail) of myself and the argument, aCollection. |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1998 |
If there is none, an empty collection is returned." |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
1999 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
2000 |
^ self commonSuffixWith:aCollection caseSensitive:ignoreCase not |
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
2001 |
|
395899428c2f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15710
diff
changeset
|
2002 |
" |
8874 | 2003 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
2004 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
2005 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
2006 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
2007 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
2008 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
2009 |
||
2010 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
2011 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
2012 |
||
2013 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
2014 |
||
2015 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
2016 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
2017 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
2018 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
2019 |
||
2020 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
2021 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
2022 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
2023 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
2024 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
2025 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
2026 |
|
15813 | 2027 |
compareWith:aSequenceableCollection |
2028 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2029 |
greater, 0 if equal and -1 if less than the argument." |
|
2030 |
||
2031 |
|mySize "{ Class: SmallInteger }" |
|
2032 |
otherSize "{ Class: SmallInteger }" |
|
2033 |
n "{ Class: SmallInteger }" |
|
2034 |
e1 e2| |
|
2035 |
||
2036 |
mySize := self size. |
|
2037 |
otherSize := aSequenceableCollection size. |
|
2038 |
n := mySize min:otherSize. |
|
2039 |
||
2040 |
1 to:n do:[:index | |
|
2041 |
e1 := self at:index. |
|
2042 |
e2 := aSequenceableCollection at:index. |
|
2043 |
e1 ~~ e2 ifTrue:[ |
|
2044 |
"identity compare is faster" |
|
2045 |
e1 > e2 ifTrue:[^ 1]. |
|
2046 |
e1 < e2 ifTrue:[^ -1]. |
|
2047 |
]. |
|
2048 |
]. |
|
2049 |
mySize > otherSize ifTrue:[^ 1]. |
|
2050 |
mySize < otherSize ifTrue:[^ -1]. |
|
2051 |
^ 0 |
|
2052 |
! |
|
2053 |
||
5813 | 2054 |
deepSameContentsAs:aCollection |
2055 |
"return true, if the receiver and the arg have the same contents |
|
2056 |
in both the named instance vars and any indexed instVars. |
|
2057 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
2058 |
||
2059 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
2060 |
unused space" |
|
2061 |
||
2062 |
|index "{ Class: SmallInteger }" |
|
2063 |
stop "{ Class: SmallInteger }" | |
|
2064 |
||
2065 |
(aCollection == self) ifTrue:[^true]. |
|
2066 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
2067 |
||
2068 |
stop := self size. |
|
2069 |
stop == (aCollection size) ifFalse:[^false]. |
|
2070 |
||
2071 |
index := 1. |
|
2072 |
[index <= stop] whileTrue:[ |
|
8874 | 2073 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
2074 |
index := index + 1 |
|
5813 | 2075 |
]. |
2076 |
^ true |
|
2077 |
||
2078 |
" |
|
8874 | 2079 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
2080 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
2081 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
2082 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 2083 |
" |
2084 |
! |
|
2085 |
||
602 | 2086 |
endsWith:aCollection |
16176
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
2087 |
"return true, if the receiver's last elements match those of aCollection. |
6b4b2fd26d9d
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16173
diff
changeset
|
2088 |
If aCollection is empty, true is returned (incompatible to some other dialect's endsWith.)" |
602 | 2089 |
|
2090 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 2091 |
index2 "{ Class: SmallInteger }" |
2092 |
stop "{ Class: SmallInteger }" |
|
602 | 2093 |
sz "{ Class: SmallInteger }"| |
2094 |
||
2095 |
(aCollection == self) ifTrue:[^ true]. |
|
2096 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
2097 |
||
2098 |
stop := aCollection size. |
|
77 | 2099 |
sz := self size. |
602 | 2100 |
stop > sz ifTrue:[^false]. |
2101 |
||
2102 |
index1 := sz. |
|
2103 |
index2 := stop. |
|
2104 |
[index2 > 0] whileTrue:[ |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2105 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2106 |
index1 := index1 - 1. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2107 |
index2 := index2 - 1 |
77 | 2108 |
]. |
602 | 2109 |
^ true |
2110 |
||
2111 |
" |
|
2112 |
'abcde' endsWith:#($d $e) |
|
8874 | 2113 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 2114 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
2115 |
" |
|
2116 |
! |
|
2117 |
||
7278 | 2118 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 2119 |
"return true, if the receiver endswith any in aCollection" |
7278 | 2120 |
|
2121 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
2122 |
||
2123 |
" |
|
2124 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
2125 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 2126 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
2127 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 2128 |
" |
2129 |
! |
|
2130 |
||
11820 | 2131 |
hasEqualElements: otherCollection |
2132 |
"Answer whether the receiver's size is the same as otherCollection's |
|
12290 | 2133 |
size, and each of the receiver's elements equal the corresponding |
2134 |
element of otherCollection. |
|
2135 |
This should probably replace the current definition of #= ." |
|
2136 |
||
2137 |
^ self isSameSequenceAs:otherCollection |
|
11820 | 2138 |
! |
2139 |
||
602 | 2140 |
hash |
2141 |
"return a hash key for the receiver" |
|
2142 |
||
2143 |
"this hash is stupid - but for larger collections, the hashing |
|
2144 |
time can become much bigger than the time lost in added probing. |
|
2145 |
Time will show ..." |
|
2146 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2147 |
|mySize h| |
3653 | 2148 |
|
2149 |
mySize := self size. |
|
2150 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2151 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2152 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2153 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2154 |
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
|
2155 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2156 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2157 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2158 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
2159 |
"/ 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
|
2160 |
"/ ^ ((((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
|
2161 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
2162 |
" |
8874 | 2163 |
#(1 2 3 4 5 6) hash |
2164 |
(1 to:6) hash |
|
2165 |
||
2166 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
2167 |
#(1 2 3 4 5) hash |
|
602 | 2168 |
" |
3653 | 2169 |
|
2170 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 2171 |
! |
2172 |
||
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2173 |
isSameSequenceAs:otherCollection |
8874 | 2174 |
"Answer whether the receiver's size is the same as otherCollection's size, |
12290 | 2175 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
2176 |
This should probably replace the current definition of #= ." |
|
6118 | 2177 |
|
2178 |
| size | |
|
2179 |
||
15813 | 2180 |
(otherCollection == self) ifTrue:[^ true]. |
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2181 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2182 |
otherCollection isSequenceable ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2183 |
|
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2184 |
1 to:size do:[:index | |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
2185 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false] |
6118 | 2186 |
]. |
2187 |
^ true |
|
2188 |
! |
|
2189 |
||
12938 | 2190 |
sameContentsAs:aCollection |
2191 |
"return true, if the receiver and the arg have the same contents. |
|
2192 |
||
15291
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2193 |
Redefinded, so that SequenceableCollections are equivalent, |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2194 |
especially OrderedCollections with unused space" |
12938 | 2195 |
|
15813 | 2196 |
|stop "{ Class: SmallInteger }" | |
12938 | 2197 |
|
18973 | 2198 |
(aCollection == self) ifTrue:[^ true]. |
12938 | 2199 |
(aCollection isSequenceable) ifFalse:[ |
18973 | 2200 |
aCollection isNil ifTrue:[^ false]. |
15813 | 2201 |
^ aCollection sameContentsAs:self. |
12938 | 2202 |
]. |
2203 |
||
2204 |
stop := self size. |
|
18973 | 2205 |
stop ~~ (aCollection size) ifTrue:[^ false]. |
12938 | 2206 |
|
15813 | 2207 |
1 to:stop do:[:index| |
18973 | 2208 |
((self at:index) ~= (aCollection at:index)) ifTrue:[^ false]. |
12938 | 2209 |
]. |
2210 |
^ true |
|
2211 |
||
2212 |
" |
|
2213 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) copy |
|
18973 | 2214 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4) |
2215 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) asSet |
|
15813 | 2216 |
#(1 2 3 4 5) asSet sameContentsAs: #(1 2 3 4 5) copy |
12938 | 2217 |
#($1 $2 $3 $4 $5) sameContentsAs: #(1 2 3 4 5) |
2218 |
#($1 $2 $3 $4 $5) sameContentsAs: '12345' |
|
2219 |
#($1 $2 $3 $4 $5) sameContentsAs: '54321' asSortedCollection |
|
18973 | 2220 |
#(1 2 3 4 5) sameContentsAs: nil |
12938 | 2221 |
" |
2222 |
||
2223 |
"Created: / 09-07-2010 / 12:44:28 / sr" |
|
2224 |
! |
|
2225 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2226 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2227 |
"return true if the receiver and aCollection represent collections |
8874 | 2228 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2229 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2230 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2231 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2232 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2233 |
|
15813 | 2234 |
|stop "{ Class: SmallInteger }" | |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2235 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2236 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2237 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2238 |
(aCollection isSequenceable) ifFalse:[ |
15813 | 2239 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2240 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2241 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2242 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2243 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2244 |
|
15813 | 2245 |
1 to:stop do:[:index| |
2246 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2247 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2248 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2249 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2250 |
" |
8874 | 2251 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
15813 | 2252 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) asSet whenComparedWith:[:a :b | a = b] |
8874 | 2253 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
2254 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
2255 |
#(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
|
2256 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2257 |
#('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
|
2258 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2259 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2260 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2261 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2262 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2263 |
|
15291
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2264 |
sameContentsFrom:start1 to:stop1 as:aCollection startingAt:start2 |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2265 |
"return true, if the receiver's slice from start to stop |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2266 |
has the same contents as aCollection's slice starting at startIndex. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2267 |
The argument, aCollection must be sequenceable." |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2268 |
|
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2269 |
|index1 "{ Class: SmallInteger }" |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2270 |
index2 "{ Class: SmallInteger }" |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2271 |
nSlice "{ Class: SmallInteger }" | |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2272 |
|
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2273 |
(aCollection == self) ifTrue:[^true]. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2274 |
|
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2275 |
nSlice := stop1-start1+1. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2276 |
start2 + nSlice - 1 > (aCollection size) ifTrue:[^false]. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2277 |
|
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2278 |
index1 := start1. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2279 |
index2 := start2. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2280 |
[index1 <= stop1] whileTrue:[ |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2281 |
((self at:index1) = (aCollection at:index2)) ifFalse:[^false]. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2282 |
index1 := index1 + 1. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2283 |
index2 := index2 + 1. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2284 |
]. |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2285 |
^ true |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2286 |
|
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2287 |
" |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2288 |
#(1 2 3 4 5) sameContentsFrom:2 to:4 as:#(10 20 2 3 4 50) startingAt:3 |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2289 |
#(1 2 3 4 5) sameContentsFrom:2 to:4 as:#(10 20 2 30 4 50) startingAt:3 |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2290 |
#($1 $2 $3 $4 $5) sameContentsFrom:2 to:4 as:'54321234987' startingAt:6 |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2291 |
" |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2292 |
! |
67c4f025a7ca
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15215
diff
changeset
|
2293 |
|
602 | 2294 |
startsWith:aCollection |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2295 |
"return true, if the receiver's first elements match those |
4389
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
2296 |
of aCollection |
17372
243de0c11918
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17363
diff
changeset
|
2297 |
If the argument is empty, true is returned. |
243de0c11918
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17363
diff
changeset
|
2298 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
243de0c11918
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17363
diff
changeset
|
2299 |
which are both inconsistent w.r.t. an empty argument." |
602 | 2300 |
|
2301 |
|index "{ Class: SmallInteger }" |
|
2302 |
stop "{ Class: SmallInteger }" | |
|
2303 |
||
2304 |
(aCollection == self) ifTrue:[^true]. |
|
2305 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
2306 |
||
2307 |
stop := aCollection size. |
|
2308 |
stop > self size ifTrue:[^false]. |
|
2309 |
||
2310 |
index := 1. |
|
2311 |
[index <= stop] whileTrue:[ |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2312 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2313 |
index := index + 1 |
602 | 2314 |
]. |
2315 |
^ true |
|
2316 |
||
2317 |
" |
|
2318 |
'abcde' startsWith:#($a $b $c) |
|
17363
a394abb3f789
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17257
diff
changeset
|
2319 |
'abcde' startsWith:'abc' |
a394abb3f789
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17257
diff
changeset
|
2320 |
'abcd' startsWith:'abcde' |
a394abb3f789
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17257
diff
changeset
|
2321 |
'abcde' startsWith:'abd' |
602 | 2322 |
#[1 2 3 4] startsWith:#(1 2 3) |
2323 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 2324 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 2325 |
" |
7279 | 2326 |
! |
2327 |
||
2328 |
startsWithAnyOf:aCollectionOfCollections |
|
2329 |
"return true, if the receiver starts with any in aCollection" |
|
2330 |
||
2331 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2332 |
||
2333 |
" |
|
2334 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2335 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2336 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2337 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2338 |
" |
13897 | 2339 |
! |
2340 |
||
2341 |
with:aCollection findFirst:aBlock |
|
2342 |
"return the index of the first element for which aTwoArgBlock returns true |
|
2343 |
against corresponding elements of aCollection. |
|
2344 |
If any collection is smaller, the size of the smaller plus one is returned" |
|
2345 |
||
2346 |
|mySize otherSize minSize| |
|
2347 |
||
2348 |
mySize := self size. |
|
2349 |
otherSize := aCollection size. |
|
2350 |
minSize := mySize min:otherSize. |
|
2351 |
||
2352 |
self from:1 to:minSize with:aCollection doWithIndex:[:el1 :el2 :i | |
|
2353 |
(aBlock value:el1 value:el2) ifTrue:[ ^ i]. |
|
2354 |
]. |
|
2355 |
||
2356 |
mySize ~= otherSize ifTrue:[ |
|
2357 |
^ minSize + 1 |
|
2358 |
]. |
|
2359 |
^ 0 |
|
2360 |
||
2361 |
" |
|
2362 |
'hello' with:'helLo' findFirst:[:a :b | a ~= b] |
|
2363 |
'hello' with:'hello' findFirst:[:a :b | a ~= b] |
|
2364 |
'hello' with:'hello1' findFirst:[:a :b | a ~= b] |
|
2365 |
'hello1' with:'hello' findFirst:[:a :b | a ~= b] |
|
2366 |
" |
|
2367 |
||
2368 |
"Created: / 08-01-2012 / 17:12:00 / cg" |
|
13 | 2369 |
! ! |
2370 |
||
260 | 2371 |
!SequenceableCollection methodsFor:'converting'! |
2372 |
||
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2373 |
asCollectionOfSubCollectionsOfSize:pieceSize |
15521
cd0f31cdec97
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15476
diff
changeset
|
2374 |
"slice into pieces; return a collection containing pieces of size pieceSize from the receiver. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
2375 |
The last piece may be smaller, if the receiver's size is not a multiple of pieceSize." |
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2376 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2377 |
|pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2378 |
start "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2379 |
stop "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2380 |
mySize "{ Class:SmallInteger }"| |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2381 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2382 |
pieces := OrderedCollection new. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2383 |
start := 1. stop := start + pieceSize - 1. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2384 |
mySize := self size. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2385 |
[stop <= mySize] whileTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2386 |
pieces add:(self copyFrom:start to:stop). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2387 |
start := start + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2388 |
stop := stop + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2389 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2390 |
(start <= mySize) ifTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2391 |
pieces add:(self copyFrom:start to:mySize). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2392 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2393 |
^ pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2394 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2395 |
" |
15521
cd0f31cdec97
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15476
diff
changeset
|
2396 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2397 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2398 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2399 |
! |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2400 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2401 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2402 |
"return a collection containing the subcollections (separated by anElement) |
2403 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2404 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2405 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2406 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2407 |
|cols myClass |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2408 |
mySize "{ Class:SmallInteger }" startIndex "{ Class:SmallInteger }" |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2409 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2410 |
|
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2411 |
cols := OrderedCollection new. |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2412 |
myClass := self species. |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2413 |
mySize := self size. |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2414 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2415 |
startIndex := 1. |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2416 |
[ startIndex <= mySize ] whileTrue:[ |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2417 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2418 |
stopIndex == 0 ifTrue:[ |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2419 |
stopIndex := mySize |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2420 |
] ifFalse: [ |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2421 |
stopIndex := stopIndex - 1. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2422 |
]. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2423 |
|
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2424 |
(stopIndex < startIndex) ifTrue: [ |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2425 |
cols add:(myClass new:0) |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2426 |
] ifFalse: [ |
17589
eb5555986d10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17581
diff
changeset
|
2427 |
cols add:(self copyFrom:startIndex to:stopIndex) |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2428 |
]. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2429 |
startIndex := stopIndex + 2 |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2430 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2431 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2432 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2433 |
" |
8874 | 2434 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2435 |
'1 one 2 two 3 three 4 four 5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:Character space |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2436 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2437 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2438 |
'foo-bar-baz' asCollectionOfSubCollectionsSeparatedBy: $-. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
2439 |
'foobarbaz' asCollectionOfSubCollectionsSeparatedBy: $-. |
18195 | 2440 |
'' asCollectionOfSubCollectionsSeparatedBy: $-. |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2441 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2442 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2443 |
|
9234 | 2444 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2445 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2446 |
of the receiver by anElement. |
|
2447 |
If anElement occurs multiple times in a row, |
|
2448 |
the block will be invoked with empty collections as argument. |
|
2449 |
This algorithm uses equality-compare to detect the element." |
|
2450 |
||
2451 |
|subCollection |
|
2452 |
endIndex "{ Class:SmallInteger }" |
|
2453 |
startIndex "{ Class:SmallInteger }" |
|
2454 |
stopIndex "{ Class:SmallInteger }" | |
|
2455 |
||
2456 |
startIndex := 0. |
|
2457 |
endIndex := self size. |
|
2458 |
||
2459 |
[startIndex <= endIndex] whileTrue:[ |
|
2460 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2461 |
stopIndex == 0 ifTrue:[ |
|
2462 |
stopIndex := self size |
|
2463 |
] ifFalse: [ |
|
2464 |
stopIndex := stopIndex - 1. |
|
2465 |
]. |
|
2466 |
||
2467 |
(stopIndex < startIndex) ifTrue: [ |
|
2468 |
subCollection := self species new:0 |
|
2469 |
] ifFalse: [ |
|
2470 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2471 |
]. |
|
2472 |
aBlock value:subCollection. |
|
2473 |
startIndex := stopIndex + 1 |
|
2474 |
]. |
|
2475 |
||
2476 |
" |
|
2477 |
'' |
|
2478 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2479 |
||
2480 |
'1 one' |
|
2481 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2482 |
||
2483 |
'1 one:2 two:3 three:4 four:5 five' |
|
2484 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2485 |
||
2486 |
'a::b' |
|
2487 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2488 |
||
2489 |
':' |
|
2490 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2491 |
||
2492 |
':a' |
|
2493 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2494 |
||
2495 |
'a:' |
|
2496 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2497 |
" |
|
2498 |
! |
|
2499 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2500 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2501 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2502 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2503 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2504 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2505 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2506 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2507 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2508 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2509 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2510 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2511 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2512 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2513 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2514 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2515 |
[done] whileFalse:[ |
8874 | 2516 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2517 |
stopIndex == 0 ifTrue:[ |
|
2518 |
stopIndex := self size. |
|
2519 |
done := true. |
|
2520 |
] ifFalse: [ |
|
2521 |
stopIndex := stopIndex - 1. |
|
2522 |
]. |
|
2523 |
||
2524 |
(stopIndex < startIndex) ifTrue: [ |
|
2525 |
items add:(myClass new:0) |
|
2526 |
] ifFalse: [ |
|
2527 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2528 |
]. |
|
2529 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2530 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2531 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2532 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2533 |
" |
8874 | 2534 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2535 |
#(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
|
2536 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2537 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2538 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2539 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2540 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2541 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2542 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2543 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2544 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2545 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2546 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2547 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2548 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2549 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2550 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2551 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2552 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2553 |
#(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
|
2554 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2555 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2556 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2557 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2558 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2559 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2560 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2561 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2562 |
|words |
8874 | 2563 |
start "{ Class:SmallInteger }" |
2564 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2565 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2566 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2567 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2568 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2569 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2570 |
[start <= mySize] whileTrue:[ |
8874 | 2571 |
"skip multiple separators" |
2572 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2573 |
start := start + 1 . |
|
2574 |
start > mySize ifTrue:[ |
|
2575 |
^ words |
|
2576 |
]. |
|
2577 |
]. |
|
2578 |
||
2579 |
stop := self findFirst:aBlock startingAt:start. |
|
2580 |
stop == 0 ifTrue:[ |
|
2581 |
words add:(self copyFrom:start to:mySize). |
|
2582 |
^ words |
|
2583 |
]. |
|
2584 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2585 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2586 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2587 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2588 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2589 |
" |
8874 | 2590 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2591 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2592 |
#(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
|
2593 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2594 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2595 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2596 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2597 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2598 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2599 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2600 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2601 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2602 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2603 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2604 |
|
1413 | 2605 |
asStringCollection |
2606 |
"return a new string collection containing the elements; |
|
2607 |
these ought to be strings. (i.e. String or Text instances)" |
|
2608 |
||
2609 |
|newColl sz| |
|
2610 |
||
2611 |
sz := self size. |
|
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
2612 |
newColl := StringCollection newWithSize:sz. |
1413 | 2613 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2614 |
^ newColl |
|
2615 |
||
2616 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2617 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2618 |
! |
|
2619 |
||
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2620 |
asStringWith:sepCharOrString |
8874 | 2621 |
"return a string generated by concatenating my elements |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2622 |
(which must be strings or nil) and embedding sepCharOrString characters in between. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2623 |
The argument sepCharOrString may be a character, a string or nil. |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2624 |
Nil entries and empty strings are counted as empty lines. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2625 |
Similar to joinWith:, but specifically targeted towards collections of strings." |
1188 | 2626 |
|
8874 | 2627 |
^ self |
12941
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2628 |
from:1 to:(self size) |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2629 |
asStringWith:sepCharOrString |
12941
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2630 |
compressTabs:false |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2631 |
final:nil |
1413 | 2632 |
|
2633 |
" |
|
2634 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
12941
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2635 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:'|' |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2636 |
'hello|world|foo|bar|baz' asCollectionOfSubstringsSeparatedBy:$| |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2637 |
" |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2638 |
|
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2639 |
"Modified: / 10-07-2010 / 22:59:29 / cg" |
1188 | 2640 |
! |
2641 |
||
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2642 |
asStringWith:sepCharacterOrString from:firstLine to:lastLine |
1188 | 2643 |
"return part of myself as a string with embedded sepCharacters. |
2644 |
My elements must be strings or nil; nil entries and empty strings are |
|
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2645 |
taken as empty lines. |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2646 |
The argument sepCharOrString may be a character, a string or nil. |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2647 |
Similar to joinWith:, but specifically targeted towards collections of strings." |
1188 | 2648 |
|
8874 | 2649 |
^ self |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2650 |
from:firstLine to:lastLine |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2651 |
asStringWith:sepCharacterOrString |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2652 |
compressTabs:false |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2653 |
final:nil |
1188 | 2654 |
" |
2655 |
creating entries for searchpath: |
|
2656 |
||
8874 | 2657 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2658 |
||
2659 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2660 |
" |
2661 |
||
2662 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2663 |
! |
|
2664 |
||
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2665 |
asStringWith:sepCharacterOrString from:firstLine to:lastLine compressTabs:compressTabs final:endCharacterOrString |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2666 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2667 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2668 |
taken as empty lines. |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2669 |
The arguments sepCharacterOrString and endCharacterOrString may be nil, a character or a string. |
260 | 2670 |
If the argument compressTabs is true, leading spaces are converted |
2671 |
to tab-characters (8col tabs). The last line is followed by a final |
|
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2672 |
character (if non-nil). |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2673 |
Similar to joinWith:, but specifically targeted towards collections of strings." |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2674 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2675 |
^ self |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2676 |
from:firstLine to:lastLine |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2677 |
asStringWith:sepCharacterOrString |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2678 |
compressTabs:compressTabs |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2679 |
final:endCharacterOrString |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2680 |
withEmphasis:true |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2681 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2682 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2683 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2684 |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2685 |
asStringWith:sepCharacterOrString from:firstLine to:lastLine compressTabs:compressTabs final:endCharacterOrString withEmphasis:withEmphasis |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2686 |
"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
|
2687 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2688 |
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
|
2689 |
taken as empty lines. |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2690 |
The arguments sepCharacterOrString and endCharacterOrString may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2691 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2692 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2693 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2694 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2695 |
any emphasis. If false, a plain string is returned. |
260 | 2696 |
This method is tuned for big collections, in not creating many |
263 | 2697 |
intermediate strings (has linear runtime). For very small collections |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2698 |
and small strings, it may be faster to use the comma , operation. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2699 |
Similar to joinWith:, but specifically targeted towards collections of strings." |
260 | 2700 |
|
5575 | 2701 |
^ self |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2702 |
from:firstLine to:lastLine |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2703 |
asStringWith:sepCharacterOrString |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2704 |
compressTabs:compressTabs |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2705 |
final:endCharacterOrString |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2706 |
withEmphasis:withEmphasis |
5575 | 2707 |
! |
2708 |
||
2709 |
asStringWithCRs |
|
8874 | 2710 |
"return a string generated by concatenating my elements |
5575 | 2711 |
(which must be strings or nil) and embedding cr characters in between. |
2712 |
Nil entries and empty strings are counted as empty lines." |
|
2713 |
||
2714 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2715 |
||
2716 |
" |
|
8874 | 2717 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2718 |
|
2719 |
(OrderedCollection new |
|
8874 | 2720 |
add:'hello'; |
2721 |
add:'world'; |
|
2722 |
add:'foo'; |
|
2723 |
add:('bar' asText allBold); |
|
2724 |
yourself) asStringWithCRs |
|
5575 | 2725 |
|
2726 |
Transcript showCR: |
|
8874 | 2727 |
(OrderedCollection new |
2728 |
add:'hello'; |
|
2729 |
add:'world'; |
|
2730 |
add:'foo'; |
|
2731 |
add:('bar' asText allBold); |
|
2732 |
yourself) asStringWithCRs |
|
5575 | 2733 |
" |
2734 |
||
2735 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2736 |
! |
|
2737 |
||
2738 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2739 |
"return a string generated by concatenating some of my elements |
5575 | 2740 |
(which must be strings or nil) and embedding cr characters in between. |
2741 |
Nil entries and empty strings are counted as empty lines." |
|
2742 |
||
2743 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2744 |
||
2745 |
" |
|
8874 | 2746 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2747 |
|
2748 |
" |
|
2749 |
||
2750 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2751 |
! |
|
2752 |
||
2753 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2754 |
"return part of myself as a string with embedded cr's. |
|
2755 |
My elements must be strings or nil. |
|
2756 |
If the argument compressTabs is true, leading spaces are converted |
|
2757 |
to tab-characters (8col tabs). |
|
2758 |
Nil entries and empty strings are taken as empty lines." |
|
2759 |
||
2760 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2761 |
! |
|
2762 |
||
2763 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2764 |
"return part of myself as a string with embedded cr's. |
|
2765 |
My elements must be strings or nil; nil entries and empty strings are |
|
2766 |
taken as empty lines. |
|
2767 |
If the argument compressTabs is true, leading spaces are converted |
|
2768 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2769 |
should be followed by a cr or not." |
|
2770 |
||
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2771 |
^ self |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2772 |
asStringWith:(Character cr) |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2773 |
from:firstLine to:lastLine |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2774 |
compressTabs:compressTabs |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
2775 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
5575 | 2776 |
! |
2777 |
||
2778 |
decodeAsLiteralArray |
|
2779 |
"given a literalEncoding in the receiver, |
|
2780 |
create & return the corresponding object. |
|
2781 |
The inverse operation to #literalArrayEncoding." |
|
2782 |
||
9414 | 2783 |
|clsName cls| |
5575 | 2784 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2785 |
self size == 0 ifTrue:[ |
9358 | 2786 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2787 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2788 |
|
9414 | 2789 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2790 |
clsName isSymbol ifFalse:[ |
9358 | 2791 |
^ MissingClassInLiteralArrayErrorSignal |
2792 |
raiseRequestWith:clsName |
|
9414 | 2793 |
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
|
2794 |
]. |
9414 | 2795 |
cls := Smalltalk classNamed:clsName. |
5575 | 2796 |
cls isNil ifTrue:[ |
9414 | 2797 |
^ MissingClassInLiteralArrayErrorSignal |
2798 |
raiseRequestWith:clsName |
|
2799 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2800 |
]. |
9414 | 2801 |
|
5575 | 2802 |
^ cls decodeFromLiteralArray:self. |
2803 |
||
2804 |
" |
|
14329
7bafeeed19bc
comment/format in: #decodeAsLiteralArray
Claus Gittinger <cg@exept.de>
parents:
14265
diff
changeset
|
2805 |
#(10 20) literalArrayEncoding |
10476 | 2806 |
#(Array 10 20) decodeAsLiteralArray |
14329
7bafeeed19bc
comment/format in: #decodeAsLiteralArray
Claus Gittinger <cg@exept.de>
parents:
14265
diff
changeset
|
2807 |
#() literalArrayEncoding |
7bafeeed19bc
comment/format in: #decodeAsLiteralArray
Claus Gittinger <cg@exept.de>
parents:
14265
diff
changeset
|
2808 |
#(Array) decodeAsLiteralArray |
10476 | 2809 |
" |
2810 |
||
2811 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2812 |
! |
2813 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2814 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2815 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2816 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2817 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2818 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2819 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2820 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2821 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2822 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2823 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2824 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2825 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2826 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2827 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2828 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2829 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2830 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2831 |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2832 |
from:firstLine to:lastLine asStringWith:sepCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2833 |
"return part of myself as a string with embedded sepCharacterOrStrings. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2834 |
The argument sepCharacterOrString may be a character, a string or nil. |
5575 | 2835 |
My elements must be strings or nil; nil entries and empty strings are |
2836 |
taken as empty lines." |
|
2837 |
||
8874 | 2838 |
^ self |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2839 |
from:firstLine |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2840 |
to:lastLine |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2841 |
asStringWith:sepCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2842 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2843 |
final:nil |
5575 | 2844 |
" |
2845 |
creating entries for searchpath: |
|
2846 |
||
8874 | 2847 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2848 |
||
2849 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2850 |
||
2851 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2852 |
" |
2853 |
||
2854 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2855 |
! |
|
2856 |
||
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2857 |
from:firstLine to:lastLine asStringWith:sepCharacterOrString compressTabs:compressTabs final:endCharacterOrString |
5575 | 2858 |
"return part of myself as a string or text with embedded sepCharacters. |
2859 |
My elements must be strings or nil; nil entries and empty strings are |
|
2860 |
taken as empty lines. |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2861 |
The arguments sepCharacterOrString and endCharacterOrString may be characters, |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2862 |
strings or nil. |
5575 | 2863 |
If the argument compressTabs is true, leading spaces are converted |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2864 |
to tab-characters (8col tabs). |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2865 |
The last line is followed by a final character (if non-nil)." |
5575 | 2866 |
|
2867 |
^ self |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2868 |
from:firstLine |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2869 |
to:lastLine |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2870 |
asStringWith:sepCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2871 |
compressTabs:compressTabs |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2872 |
final:endCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2873 |
withEmphasis:true |
5575 | 2874 |
|
2875 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2876 |
! |
|
2877 |
||
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2878 |
from:firstLine to:lastLine asStringWith:sepCharacterOrString compressTabs:compressTabs final:endCharacterOrString withEmphasis:withEmphasis |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2879 |
"return part of myself as a string or text with embedded sepCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2880 |
and followup endCharacterOrString. |
5575 | 2881 |
My elements must be strings or nil; nil entries and empty strings are |
2882 |
taken as empty lines. |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2883 |
sepCharacterOrString and endCharacterOrString may be nil, a character or a string. |
5575 | 2884 |
If the argument compressTabs is true, leading spaces are converted |
2885 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2886 |
character (if non-nil). |
|
2887 |
The withEmphais argument controls if the returned string should preserve |
|
2888 |
any emphasis. If false, a plain string is returned. |
|
2889 |
This method is tuned for big collections, in not creating many |
|
2890 |
intermediate strings (has linear runtime). For very small collections |
|
2891 |
and small strings, it may be faster to use the comma , operation." |
|
2892 |
||
293 | 2893 |
|idx1 "{ Class:SmallInteger }" |
2894 |
idx2 "{ Class:SmallInteger }" |
|
2895 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2896 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2897 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2898 |
newString lineString spaces idx nTabs |
8099 | 2899 |
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
|
2900 |
thisLen anyTab| |
260 | 2901 |
|
2902 |
" |
|
2903 |
first accumulate the size of the string, to avoid |
|
2904 |
countless reallocations. If tabs are compressed, |
|
2905 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2906 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2907 |
" |
8099 | 2908 |
needEmphasis := false. |
2909 |
maxBitsPerCharacter := 8. |
|
1015 | 2910 |
|
260 | 2911 |
totalLength := 0. |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2912 |
sepCharacterOrString isNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2913 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2914 |
] ifFalse:[ |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2915 |
sepCharacterOrString isCharacter ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2916 |
sepCnt := 1 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2917 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2918 |
sepCnt := sepCharacterOrString size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2919 |
] |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2920 |
]. |
260 | 2921 |
|
293 | 2922 |
idx1 := firstLine. |
2923 |
idx2 := lastLine. |
|
2924 |
idx1 to:idx2 do:[:lineIndex | |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2925 |
lineString := self at:lineIndex. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2926 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2927 |
lineString notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2928 |
withEmphasis ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2929 |
lineString hasChangeOfEmphasis ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2930 |
needEmphasis := true |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2931 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2932 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2933 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2934 |
totalLength := totalLength + lineString size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2935 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2936 |
totalLength := totalLength + sepCnt |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2937 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2938 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2939 |
|
8099 | 2940 |
maxBitsPerCharacter > 8 ifTrue:[ |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2941 |
maxBitsPerCharacter > 16 ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2942 |
stringClass := Unicode32String. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2943 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2944 |
stringClass := Unicode16String. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2945 |
] |
8099 | 2946 |
] ifFalse:[ |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2947 |
stringClass := String. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2948 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2949 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2950 |
endCharacterOrString notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2951 |
endCharacterOrString isCharacter ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2952 |
totalLength := totalLength + 1 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2953 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2954 |
totalLength := totalLength + endCharacterOrString size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2955 |
]. |
260 | 2956 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2957 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2958 |
|
260 | 2959 |
spaces := ' '. |
1016 | 2960 |
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
|
2961 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2962 |
needEmphasis ifTrue:[ |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2963 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2964 |
]. |
260 | 2965 |
|
2966 |
" |
|
2967 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2968 |
Be careful with runArrays: |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2969 |
replacing individual elements is VERY expensive. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2970 |
Therefore, create a new runArray from scratch. |
260 | 2971 |
" |
2972 |
pos := 1. |
|
293 | 2973 |
idx1 to:idx2 do:[:lineIndex | |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2974 |
lineString := self at:lineIndex. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2975 |
thisLen := lineString size. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2976 |
thisLen ~~ 0 ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2977 |
withEmphasis ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2978 |
lineString := lineString string. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2979 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2980 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2981 |
(anyTab := compressTabs) ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2982 |
" |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2983 |
mhmh: could use withTabs from String-class here, |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2984 |
but we should avoid creating too many temporary strings |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2985 |
(especially, since this method is typically used when converting |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2986 |
big texts such as when saving in the filebrowser ...). |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2987 |
Therefore, we convert tabs inline here doing a direct replace |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2988 |
in newString." |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2989 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2990 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2991 |
nTabs := (idx-1) // 8. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2992 |
anyTab := (nTabs > 0) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2993 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2994 |
anyTab ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2995 |
"any tabs" |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2996 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2997 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2998 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
2999 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3000 |
newRuns notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3001 |
newRuns add:nil withOccurrences:nTabs |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3002 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3003 |
pos := pos + nTabs. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3004 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3005 |
newString replaceFrom:pos with:lineString startingAt:idx. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3006 |
newRuns notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3007 |
lineString hasChangeOfEmphasis ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3008 |
idx to:lineString size do:[:pos | |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3009 |
newRuns add:(lineString emphasisAt:pos) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3010 |
] |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3011 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3012 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3013 |
] |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3014 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3015 |
pos := pos + thisLen - (nTabs * 8). |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3016 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3017 |
newString replaceFrom:pos with:lineString. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3018 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3019 |
newRuns notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3020 |
lineString hasChangeOfEmphasis ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3021 |
newRuns addAll:(lineString emphasis) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3022 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3023 |
newRuns add:nil withOccurrences:lineString size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3024 |
] |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3025 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3026 |
pos := pos + thisLen. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3027 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3028 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3029 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3030 |
lineIndex ~~ lastLine ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3031 |
c := sepCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3032 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3033 |
c := endCharacterOrString |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3034 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3035 |
|
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3036 |
c notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3037 |
c isCharacter ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3038 |
newString at:pos put:c. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3039 |
newRuns notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3040 |
newRuns add:nil. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3041 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3042 |
pos := pos + 1 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3043 |
] ifFalse:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3044 |
newString replaceFrom:pos with:c. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3045 |
newRuns notNil ifTrue:[ |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3046 |
newRuns add:nil withOccurrences:c size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3047 |
]. |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3048 |
pos := pos + c size |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3049 |
] |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3050 |
]. |
260 | 3051 |
]. |
3052 |
||
3053 |
" |
|
3054 |
in case of tab compression, the result has to be |
|
3055 |
cut to size ... sorry |
|
3056 |
" |
|
3057 |
pos ~~ totalLength ifTrue:[ |
|
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3058 |
newString := newString copyTo:(pos - 1) |
260 | 3059 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
3060 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
3061 |
newRuns notNil ifTrue:[ |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3062 |
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
|
3063 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
3064 |
|
260 | 3065 |
^ newString |
3066 |
||
3067 |
" |
|
3068 |
creating entries for searchpath: |
|
3069 |
||
8874 | 3070 |
#('foo' 'bar' 'baz' '/foo/bar') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3071 |
asStringWith:$: |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3072 |
from:1 to:4 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3073 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3074 |
final:nil |
260 | 3075 |
|
3076 |
with trailing colon: |
|
3077 |
||
8874 | 3078 |
#('foo' 'bar' 'baz' '/foo/bar') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3079 |
asStringWith:$: |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3080 |
from:1 to:4 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3081 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3082 |
final:$: |
260 | 3083 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3084 |
concatenating elements (nil sepChars and nil endChars): |
260 | 3085 |
|
8874 | 3086 |
#('foo' 'bar' 'baz') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3087 |
asStringWith:nil |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3088 |
from:1 to:3 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3089 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3090 |
final:nil |
260 | 3091 |
|
3092 |
creating a string from a collection of lines: |
|
3093 |
||
8874 | 3094 |
#('foo' 'bar' 'baz') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3095 |
asStringWith:(Character cr) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3096 |
from:1 to:3 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3097 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3098 |
final:(Character cr) |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
3099 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
3100 |
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
|
3101 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
3102 |
(Array |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3103 |
with:'foo' |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3104 |
with:('bar' asText allBold) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3105 |
with:'baz' |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3106 |
with:('baz2' asText emphasizeAllWith:#italic) |
8874 | 3107 |
) |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3108 |
asStringWith:(Character cr) |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3109 |
from:1 to:4 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3110 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3111 |
final:(Character cr) |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3112 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3113 |
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
|
3114 |
|
8874 | 3115 |
#('foo' 'bar' 'baz') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3116 |
asStringWith:'__' |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3117 |
from:1 to:3 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3118 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3119 |
final:nil |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3120 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3121 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3122 |
|
8874 | 3123 |
#('foo' 'bar' 'baz') |
17574
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3124 |
asStringWith:'__' |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3125 |
from:1 to:3 |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3126 |
compressTabs:false |
83bccff028ad
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17570
diff
changeset
|
3127 |
final:'***' |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
3128 |
|
260 | 3129 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
3130 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
3131 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
3132 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
11827 | 3133 |
! |
3134 |
||
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3135 |
joinWithAll:separatingCollection |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3136 |
"return a collection generated by concatenating my elements |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3137 |
and slicing separatingCollection in between. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3138 |
Similar to asStringWith:, but not specifically targeted towards collections of strings." |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3139 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3140 |
^ self |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3141 |
joinWithAll:separatingCollection |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3142 |
from:1 to:(self size) as:nil |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3143 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3144 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3145 |
#('hello' 'world' 'foo' 'bar' 'baz') joinWithAll:' ; ' |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3146 |
#('hello' 'world' 'foo' 'bar' 'baz') joinWithAll:' | ' |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3147 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3148 |
! |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3149 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3150 |
joinWithAll:separatingCollection from:startIndex to:endIndex as:speciesOrNil |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3151 |
"extract parts of myself as a new collection with optional embedded separator. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3152 |
Separator may be nil, or a collection of elements to be sliced in between. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3153 |
SpeciesOrNil specifies the species of the resultig object, allowing for Arrays to be converted |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3154 |
as OrderedCollection or vice versa on the fly. If nil is passed in, the species of the first non-nil |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3155 |
element is used. |
16422
9ee8ac24e422
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16392
diff
changeset
|
3156 |
This counts the overall size first, then allocates the new collection once and replaces elements |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3157 |
via bulk copies. For very small collections, it may be faster to use the comma , operation. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3158 |
Similar to asStringWith:, but not specifically targeted towards string handling." |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3159 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3160 |
|totalLength "{ Class:SmallInteger }" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3161 |
pos "{ Class:SmallInteger }" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3162 |
sepCnt "{ Class:SmallInteger }" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3163 |
subColl newColl |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3164 |
species| |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3165 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3166 |
startIndex = endIndex ifTrue:[ ^ self at:startIndex ]. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3167 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3168 |
species := speciesOrNil. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3169 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3170 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3171 |
first accumulate the size of the result, |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3172 |
to avoid countless reallocations. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3173 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3174 |
totalLength := 0. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3175 |
sepCnt := separatingCollection size. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3176 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3177 |
startIndex to:endIndex do:[:index | |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3178 |
subColl := self at:index. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3179 |
totalLength := totalLength + subColl size. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3180 |
species isNil ifTrue:[ |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3181 |
subColl notNil ifTrue:[ |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3182 |
species := subColl species |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3183 |
] |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3184 |
] |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3185 |
]. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3186 |
totalLength := totalLength + ((endIndex - startIndex) * sepCnt). |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
3187 |
newColl := species newWithSize:totalLength. |
14674
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3188 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3189 |
pos := 1. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3190 |
startIndex to:endIndex do:[:index | |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3191 |
subColl := self at:index. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3192 |
subColl size ~~ 0 ifTrue:[ |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3193 |
newColl replaceFrom:pos with:subColl startingAt:1. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3194 |
pos := pos + subColl size. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3195 |
]. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3196 |
((sepCnt ~~ 0) and:[index ~~ endIndex]) ifTrue:[ |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3197 |
newColl replaceFrom:pos to:(pos+sepCnt-1) with:separatingCollection startingAt:1. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3198 |
pos := pos + sepCnt. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3199 |
]. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3200 |
]. |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3201 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3202 |
^ newColl |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3203 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3204 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3205 |
#( 'aa' 'bb' '' 'cc' ) joinWith:'|' from:1 to:4 as:String |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3206 |
#( 'aa' 'bb' '' 'cc' ) joinWith:nil from:1 to:4 as:String |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3207 |
#( 'aa' 'bb' '' 'cc' ) joinWith:'|' from:1 to:4 as:Array |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3208 |
#( (1 2 3) (4 5 6) (7 6 8) ) joinWith:#(nil) from:1 to:3 as:OrderedCollection |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3209 |
#( (1 2 3) (4 5 6) (7 6 8) ) joinWith:nil from:1 to:3 as:nil |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3210 |
" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3211 |
|
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3212 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3213 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3214 |
! |
672133ad1b1b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14621
diff
changeset
|
3215 |
|
16152
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3216 |
splitBy:anElement |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3217 |
"return a collection containing the subcollections (separated by anElement) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3218 |
of the receiver. If anElement occurs multiple times in a row, |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3219 |
the result will contain empty collections. |
17570
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
3220 |
This algorithm uses equality-compare to detect the element. |
df5624bc2c35
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17553
diff
changeset
|
3221 |
Same as asCollectionOfSubCollectionsSeparatedBy: for Squeak compatibility" |
16152
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3222 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3223 |
^ self asCollectionOfSubCollectionsSeparatedBy:anElement |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3224 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3225 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3226 |
'1 one:2 two:3 three:4 four:5 five' withCRs splitBy:$: |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3227 |
'1 one 2 two 3 three 4 four 5 five' withCRs splitBy:Character space |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3228 |
#(a b c d e f g h) splitBy: #d. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3229 |
#(a b c d e f d d g h) splitBy: #d. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3230 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3231 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3232 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3233 |
splitBy:anElement do:aBlock |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3234 |
"evaluate aBlock for each subcollection generated by separating elements |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3235 |
of the receiver by anElement. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3236 |
If anElement occurs multiple times in a row, |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3237 |
the block will be invoked with empty collections as argument. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3238 |
This algorithm uses equality-compare to detect the element." |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3239 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3240 |
^ self asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3241 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3242 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3243 |
'' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3244 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3245 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3246 |
'1 one' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3247 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3248 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3249 |
'1 one:2 two:3 three:4 four:5 five' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3250 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3251 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3252 |
'a::b' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3253 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3254 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3255 |
':' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3256 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3257 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3258 |
':a' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3259 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3260 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3261 |
'a:' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3262 |
splitBy:$: do:[:each | Transcript showCR:each storeString] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3263 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3264 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3265 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3266 |
splitByAll:aSeparatorCollection |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3267 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3268 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3269 |
the result will contain empty strings. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3270 |
Uses equality-compare when searching for aSeparatorCollection." |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3271 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3272 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3273 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3274 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3275 |
'1::2::3::4::5::' splitByAll:'::' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3276 |
#(1 2 3 1 2 3 4 3 1 1 2 3 1 4 5) splitByAll:#(3 1) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3277 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3278 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3279 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3280 |
splitByAny:aCollectionOfSeparators |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3281 |
"return a collection containing the subCollection |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3282 |
(separated by any from aCollectionOfSeparators) of the receiver. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3283 |
This allows breaking up strings using a number of elements as separator. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3284 |
Uses equality-compare when searching for separators." |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3285 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3286 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3287 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3288 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3289 |
'hello:world:isnt:this nice' splitByAny:#($:) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3290 |
'hello:world:isnt:this nice' splitByAny:':' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3291 |
'hello:world:isnt:this nice' splitByAny:(Array with:$: with:Character space) |
16392
9adc675755a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16254
diff
changeset
|
3292 |
'hello:world:isnt:this nice' splitByAny:#( $: $ ) |
9adc675755a4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16254
diff
changeset
|
3293 |
'hello:world:isnt:this nice' splitByAny:{ $: . $ } |
16152
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3294 |
'hello:world:isnt:this nice' splitByAny:': ' |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3295 |
'h1e2l3l4o' splitByAny:($1 to: $9) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3296 |
#(1 9 2 8 3 7 4 6 5 5) splitByAny:#(1 2 3) |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3297 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3298 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3299 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3300 |
splitByAnyForWhich:aBlock |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3301 |
"return a collection containing the subCollection |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3302 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3303 |
This allows breaking up strings using an arbitrary condition." |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3304 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3305 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3306 |
'hello:world:isnt:this nice' splitByAnyForWhich:[:ch | ch = $:] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3307 |
'h1e2l3l4o' splitByAnyForWhich:[:ch | ch isDigit] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3308 |
#(1 9 2 8 3 7 4 6 5 5) splitByAnyForWhich:[:n | n odd] |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3309 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3310 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3311 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3312 |
splitForSize:pieceSize |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3313 |
"slice into pieces; return a collection containing pieces of size pieceSize from the receiver. |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3314 |
The last piece may be smaller, if the receiver's size is not a multiple of pieceSize." |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3315 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3316 |
^ self asCollectionOfSubCollectionsOfSize:pieceSize |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3317 |
|
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3318 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3319 |
'123123123123123123' splitForSize:3 |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3320 |
'12312312312312312312' splitForSize:3 |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3321 |
" |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3322 |
! |
51a113b4c53f
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16123
diff
changeset
|
3323 |
|
11827 | 3324 |
subCollections:aBlock |
3325 |
"Answser an ordered collection of ordered collections |
|
3326 |
where each subcollection is delimited by an element of the receiver |
|
3327 |
for which the given block evaluates to true." |
|
3328 |
||
3329 |
|str answer current e| |
|
3330 |
||
3331 |
str := self readStream. |
|
3332 |
answer := OrderedCollection new. |
|
3333 |
current := OrderedCollection new. |
|
3334 |
[ str atEnd ] whileFalse:[ |
|
3335 |
e := str next. |
|
3336 |
current add:e. |
|
3337 |
(aBlock value:e) ifTrue:[ |
|
3338 |
answer add:current. |
|
3339 |
current := OrderedCollection new |
|
3340 |
] |
|
3341 |
]. |
|
3342 |
current notEmpty ifTrue:[ |
|
3343 |
answer add:current |
|
3344 |
]. |
|
3345 |
^ answer |
|
3346 |
||
3347 |
" |
|
3348 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
|
3349 |
" |
|
260 | 3350 |
! ! |
3351 |
||
7253 | 3352 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
3353 |
||
8874 | 3354 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3355 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3356 |
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
|
3357 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3358 |
however, physically, no copy is made). |
9291 | 3359 |
Warning: |
3360 |
The slice SHARES the memory for the element-data with the original, |
|
3361 |
this means that any modifications in the original are visible in the slice |
|
3362 |
and vice versa." |
|
7253 | 3363 |
|
3364 |
^ self |
|
9291 | 3365 |
from:startIndex |
3366 |
to:self size |
|
3367 |
by:1 |
|
8874 | 3368 |
|
3369 |
" |
|
3370 |
#(1 2 3 4 5 6 7) from:3 |
|
3371 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
3372 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
3373 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
3374 |
" |
|
3375 |
! |
|
3376 |
||
3377 |
from:startIndex by:step |
|
9291 | 3378 |
"Create a ReindexedCollection from the receiver. |
3379 |
The new collection represents the elements starting at startIndex to the end. |
|
3380 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
3381 |
however, physically, no copy is made). |
|
3382 |
Warning: |
|
3383 |
The slice SHARES the memory for the element-data with the original, |
|
3384 |
this means that any modifications in the original are visible in the slice |
|
3385 |
and vice versa." |
|
7253 | 3386 |
|
3387 |
^ self |
|
9291 | 3388 |
from:startIndex |
3389 |
to:(step > 1 |
|
3390 |
ifTrue: [self size] |
|
3391 |
ifFalse: [1]) |
|
3392 |
by:step |
|
8874 | 3393 |
|
3394 |
" |
|
3395 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
3396 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
3397 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
3398 |
( #(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
|
3399 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3400 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3401 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3402 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3403 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3404 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 3405 |
Warning: |
3406 |
The slice SHARES the memory for the element-data with the original, |
|
3407 |
this means that any modifications in the original are visible in the slice |
|
3408 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3409 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3410 |
^ self |
9291 | 3411 |
from:startIndex |
3412 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 3413 |
|
3414 |
" |
|
3415 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
3416 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3417 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3418 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3419 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3420 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3421 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3422 |
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
|
3423 |
slice := coll from:3 to:7. |
8874 | 3424 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3425 |
coll at:4 put:40. |
8874 | 3426 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3427 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3428 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3429 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3430 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3431 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3432 |
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
|
3433 |
slice := coll from:3 to:7. |
8874 | 3434 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3435 |
slice at:1 put:40. |
8874 | 3436 |
slice. |
3437 |
" |
|
3438 |
! |
|
3439 |
||
3440 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3441 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3442 |
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
|
3443 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3444 |
however, physically, no copy is made). |
9291 | 3445 |
Warning: |
3446 |
The slice SHARES the memory for the element-data with the original, |
|
3447 |
this means that any modifications in the original are visible in the slice |
|
3448 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3449 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3450 |
^ self |
9291 | 3451 |
from:startIndex |
3452 |
to:endIndex |
|
3453 |
by: (startIndex <= endIndex |
|
3454 |
ifTrue: [1] |
|
3455 |
ifFalse: [-1]) |
|
8874 | 3456 |
|
3457 |
" |
|
3458 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
3459 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
3460 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
3461 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
3462 |
" |
|
3463 |
! |
|
3464 |
||
3465 |
from:startIndex to:endIndex by:step |
|
9291 | 3466 |
"Create a ReindexedCollection from the receiver. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3467 |
The new collection represents the receiver's elements up to endIndex. |
9291 | 3468 |
(i.e. logically it represents the receiver copyTo:endIndex, |
3469 |
however, physically, no copy is made). |
|
3470 |
Warning: |
|
3471 |
The slice SHARES the memory for the element-data with the original, |
|
3472 |
this means that any modifications in the original are visible in the slice |
|
3473 |
and vice versa." |
|
7253 | 3474 |
|
3475 |
^ ReindexedCollection |
|
9291 | 3476 |
on:self |
3477 |
from:startIndex |
|
3478 |
to:endIndex |
|
3479 |
by:step |
|
8874 | 3480 |
|
3481 |
" |
|
3482 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
3483 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
3484 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
3485 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
3486 |
" |
|
3487 |
! |
|
3488 |
||
3489 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3490 |
"Create a ReindexedCollection from the receiver. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3491 |
The new collection represents the receiver's elements up to endIndex. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3492 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3493 |
however, physically, no copy is made). |
9291 | 3494 |
Warning: |
3495 |
The slice SHARES the memory for the element-data with the original, |
|
3496 |
this means that any modifications in the original are visible in the slice |
|
3497 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3498 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3499 |
^ self |
9291 | 3500 |
from:1 |
3501 |
to:endIndex |
|
3502 |
by:1 |
|
8874 | 3503 |
|
3504 |
" |
|
3505 |
#(1 2 3 4 5 6 7) to:4 |
|
3506 |
( #(1 2 3 4 5 6 7) to:4) first |
|
3507 |
( #(1 2 3 4 5 6 7) to:4) last |
|
3508 |
( #(1 2 3 4 5 6 7) to:4) size |
|
3509 |
" |
|
3510 |
! |
|
3511 |
||
3512 |
to:endIndex by:step |
|
9291 | 3513 |
"Create a ReindexedCollection from the receiver. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3514 |
The new collection represents the receiver's elements up to endIndex. |
9291 | 3515 |
(i.e. logically it represents the receiver copyTo:endIndex, |
3516 |
however, physically, no copy is made). |
|
3517 |
Warning: |
|
3518 |
The slice SHARES the memory for the element-data with the original, |
|
3519 |
this means that any modifications in the original are visible in the slice |
|
3520 |
and vice versa." |
|
7253 | 3521 |
|
3522 |
^ self |
|
9291 | 3523 |
from:(step > 0 |
3524 |
ifTrue: [1] |
|
3525 |
ifFalse: [self size]) |
|
3526 |
to:endIndex |
|
3527 |
by:step |
|
8874 | 3528 |
|
3529 |
" |
|
3530 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
3531 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
3532 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
3533 |
( #(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
|
3534 |
" |
7253 | 3535 |
! ! |
3536 |
||
1 | 3537 |
!SequenceableCollection methodsFor:'copying'! |
3538 |
||
9106 | 3539 |
++ aCollection |
16065
9b79c6542b04
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16064
diff
changeset
|
3540 |
"This is just syntactic sugar for javascript. |
9b79c6542b04
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16064
diff
changeset
|
3541 |
Return a new collection formed from concatenating the receiver with |
9106 | 3542 |
the argument. The class of the new collection is determined by the |
16065
9b79c6542b04
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16064
diff
changeset
|
3543 |
receiver's class, so mixing classes is possible, if the second collections |
9b79c6542b04
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16064
diff
changeset
|
3544 |
elements can be stored into instances of the receiver's class." |
9106 | 3545 |
|
3546 |
^ self , aCollection |
|
3547 |
||
3548 |
" |
|
3549 |
#($a $b $c) ++ #(1 2 3) |
|
3550 |
#($a $b $c) ++ '123' |
|
3551 |
'abc' ++ '123' |
|
3552 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
3553 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3554 |
'abc' asArray ++ #(1 2 3 4 5) |
|
3555 |
" |
|
14010 | 3556 |
|
3557 |
"Modified (comment): / 14-02-2012 / 14:17:11 / cg" |
|
9106 | 3558 |
! |
3559 |
||
1 | 3560 |
, aCollection |
14073 | 3561 |
"return a new collection formed from concatenating the receiver with the argument. |
3562 |
The class of the new collection is determined by the |
|
3563 |
receiver's class, so mixing classes is possible, if the second collection's |
|
3564 |
elements can be stored into instances of the receiver's class." |
|
1 | 3565 |
|
8874 | 3566 |
|newCollection |
1 | 3567 |
mySize "{ Class: SmallInteger }" |
3568 |
newSize "{ Class: SmallInteger }" |
|
3569 |
otherSize "{ Class: SmallInteger }" |
|
3570 |
dstIndex "{ Class: SmallInteger }"| |
|
3571 |
||
3572 |
mySize := self size. |
|
3573 |
otherSize := aCollection size. |
|
3574 |
newSize := mySize + otherSize. |
|
202 | 3575 |
|
252 | 3576 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3577 |
|
3578 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3579 |
dstIndex := mySize + 1. |
|
202 | 3580 |
aCollection isSequenceable ifTrue:[ |
14073 | 3581 |
" |
3582 |
yes, aCollection has indexed elements |
|
3583 |
" |
|
3584 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3585 |
] ifFalse:[ |
14073 | 3586 |
" |
3587 |
no, enumerate aCollection |
|
3588 |
" |
|
3589 |
aCollection do:[:element | |
|
3590 |
newCollection at:dstIndex put:element. |
|
3591 |
dstIndex := dstIndex + 1 |
|
3592 |
] |
|
1 | 3593 |
]. |
3594 |
^ newCollection |
|
44 | 3595 |
|
95 | 3596 |
" |
3597 |
#($a $b $c) , #(1 2 3) |
|
3598 |
#($a $b $c) , '123' |
|
3599 |
'abc' , '123' |
|
3600 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3601 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3602 |
'abc' asArray , #(1 2 3 4 5) |
|
3603 |
" |
|
14073 | 3604 |
|
3605 |
"Modified (comment): / 01-04-2012 / 13:17:56 / cg" |
|
1 | 3606 |
! |
3607 |
||
16064
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3608 |
,* n |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3609 |
"return a new collection formed from concatenating the receiver n times, |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3610 |
with 0 returning an empty collection, 1 returning the receiver, etc." |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3611 |
|
16072
b1ddbf85b85b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16069
diff
changeset
|
3612 |
^ self repeatedConcatenation:n |
16704
7c006f1bbb16
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16703
diff
changeset
|
3613 |
|
16064
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3614 |
" |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3615 |
#($a $b $c) ,* 3 |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3616 |
'abc' ,* 5 |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3617 |
'abc' ,* 0 |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3618 |
'abc' ,* 1 |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3619 |
'a' ,* 50 |
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
3620 |
(1 to:4) ,* 5 |
16064
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3621 |
" |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3622 |
! |
493556fe675b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15985
diff
changeset
|
3623 |
|
18934 | 3624 |
concatenate:string1 and:string2 |
3625 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
3626 |
This is equivalent to self , string1 , string2 |
|
3627 |
- generated by stc compiler when such a construct is detected and the receiver |
|
3628 |
is known to be a string." |
|
3629 |
||
3630 |
^ self , string1 , string2 |
|
3631 |
! |
|
3632 |
||
3633 |
concatenate:string1 and:string2 and:string3 |
|
3634 |
"return the concatenation of myself and the string arguments. |
|
3635 |
This is equivalent to self , string1 , string2 , string3 |
|
3636 |
- generated by stc compiler when such a construct is detected and the receiver |
|
3637 |
is known to be a string." |
|
3638 |
||
3639 |
^ self , string1 , string2 , string3 |
|
3640 |
! |
|
3641 |
||
18882 | 3642 |
copyAfter: anElement |
3643 |
"Answer a copy of the receiver from after the first occurrence |
|
3644 |
of anElement up to the end. If no such element exists, answer |
|
3645 |
an empty copy." |
|
3646 |
||
3647 |
|idx| |
|
3648 |
||
3649 |
idx := self indexOf:anElement. |
|
3650 |
idx == 0 ifTrue:[idx := self size]. |
|
3651 |
^ self copyFrom:idx + 1 |
|
3652 |
||
3653 |
" |
|
3654 |
'hello world' copyAfter:$l |
|
3655 |
'123456123456' copyAfter:$2 |
|
3656 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
3657 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
3658 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
3659 |
" |
|
3660 |
! |
|
3661 |
||
3662 |
copyAfterLast:element |
|
3663 |
"return a copy of the receiver from (but excluding) the last occurrence |
|
3664 |
of element to the end; uses = for comparison" |
|
3665 |
||
3666 |
|idx| |
|
3667 |
||
3668 |
idx := self lastIndexOf:element. |
|
3669 |
idx == 0 ifTrue:[idx := self size]. |
|
3670 |
^ self copyFrom:idx + 1 |
|
3671 |
||
3672 |
" |
|
3673 |
'hello world' copyAfterLast:$l |
|
3674 |
'123456123456' copyAfterLast:$2 |
|
3675 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
3676 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
3677 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
3678 |
" |
|
3679 |
! |
|
3680 |
||
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3681 |
copyButFirst |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3682 |
"return a new collection consisting of the receiver's elements |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3683 |
except for the first element. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3684 |
Raises an error if the receiver is empty. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3685 |
Differs from #allButFirst in its error behavior." |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3686 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3687 |
self isEmpty ifTrue:[self notEnoughElementsError]. |
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3688 |
^ self copyFrom:2 |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3689 |
|
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3690 |
" |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3691 |
#($a $b $c $d $e $f $g) copyButFirst |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3692 |
'1234567890' copyButFirst |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3693 |
'1' copyButFirst |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3694 |
'' copyButFirst |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3695 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3696 |
'' allButFirst |
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3697 |
" |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3698 |
! |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3699 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3700 |
copyButFirst:count |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3701 |
"return a new collection consisting of the receiver's elements |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3702 |
except for the first count elements |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3703 |
Raises an error if the receiver is empty. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3704 |
Differs from #allButFirst: in its error behavior." |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3705 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3706 |
self size < count ifTrue:[self notEnoughElementsError]. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3707 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3708 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3709 |
" |
8874 | 3710 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3711 |
'1234567890' copyButFirst:2 |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3712 |
'12' copyButFirst:2 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3713 |
'' copyButFirst:2 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3714 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3715 |
'' allButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3716 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3717 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3718 |
|
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3719 |
copyButLast |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3720 |
"return a new collection consisting of the receiver's elements |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3721 |
except for the last element. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3722 |
Raises an error if the receiver is empty. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3723 |
Differs from #allButLast in its error behavior." |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3724 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3725 |
self isEmpty ifTrue:[self notEnoughElementsError]. |
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3726 |
^ self copyTo:(self size - 1) |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3727 |
|
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3728 |
" |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3729 |
#($a $b $c $d $e $f $g) copyButLast |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3730 |
'1234567890' copyButLast |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3731 |
'1' copyButLast |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3732 |
'' copyButLast |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3733 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3734 |
'' allButLast |
16694
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3735 |
" |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3736 |
! |
ddb4534bf499
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16611
diff
changeset
|
3737 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3738 |
copyButLast:count |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3739 |
"return a new collection consisting of the receiver's elements |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3740 |
except for the last count elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3741 |
Raises an error if there are not at least count elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3742 |
Differs from #allButLast: in its error behavior." |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3743 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3744 |
count > self size ifTrue:[self notEnoughElementsError]. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3745 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3746 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3747 |
" |
8874 | 3748 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3749 |
'1234567890' copyButLast:2 |
|
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3750 |
'12' copyButLast:2 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3751 |
'1' copyButLast:2 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3752 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3753 |
'1' allButLast:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3754 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3755 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3756 |
|
602 | 3757 |
copyFirst:count |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3758 |
"return a new collection consisting of the receiver's first count elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3759 |
Raises an error if there are not enough elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3760 |
Differs from #first: in its error behavior." |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3761 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3762 |
count > self size ifTrue:[self notEnoughElementsError]. |
602 | 3763 |
^ self copyFrom:1 to:count |
3764 |
||
3765 |
" |
|
3766 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3767 |
'1234567890' copyFirst:4 |
|
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3768 |
'1234' copyFirst:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3769 |
'12' copyFirst:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3770 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3771 |
'1234567890' first:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3772 |
'123' first:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3773 |
'' first:4 |
602 | 3774 |
" |
3775 |
! |
|
3776 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3777 |
copyFrom:startIndex |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3778 |
"return a new collection consisting of receiver's elements from startIndex to the end of the collection. |
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3779 |
Return an empty collection, if startIndex is beyond the receiver's size." |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3780 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3781 |
^ self copyFrom:startIndex to:(self size) |
602 | 3782 |
|
3783 |
" |
|
3784 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3785 |
'1234567890' copyFrom:2 |
|
8874 | 3786 |
(10 to:19) copyFrom:5 |
602 | 3787 |
" |
3788 |
! |
|
3789 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3790 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3791 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3792 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3793 |
(i.e. extract a slice). |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3794 |
Returns an empty collection if startIndex is beyond the receiver's size. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3795 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3796 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3797 |
^ self |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3798 |
copyFrom:startIndex |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3799 |
to:(startIndex + numberOfElements - 1) |
8874 | 3800 |
|
3801 |
" |
|
3802 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3803 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3804 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3805 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3806 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3807 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3808 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3809 |
copyFrom:startIndex to:stopIndex |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3810 |
"return a new collection consisting of receiver's elements |
4403 | 3811 |
between start and stop. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
3812 |
Returns an empty collection if startIndex is beyond the receiver's size. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3813 |
Raise an error, if stopIndex is out of range." |
602 | 3814 |
|
3815 |
|newCollection newSize| |
|
3816 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3817 |
newSize := stopIndex - startIndex + 1. |
18821 | 3818 |
newSize <= 0 ifTrue:[ |
3819 |
^ self copyEmpty:0. |
|
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3820 |
]. |
602 | 3821 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3822 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
18821 | 3823 |
|
602 | 3824 |
^ newCollection |
3825 |
||
3826 |
" |
|
3827 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3828 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3829 |
(1 to:10) copyFrom:2 to:5 |
3830 |
||
3831 |
'1234567890' copyFrom:2 to:15 |
|
18821 | 3832 |
(1 to:10) copyFrom:12 to:1 |
602 | 3833 |
" |
3834 |
! |
|
3835 |
||
3836 |
copyLast:count |
|
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3837 |
"return a new collection consisting of the receiver's last count elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3838 |
Raises an error if there are not enough elements. |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3839 |
Differs from #last: in its error behavior." |
602 | 3840 |
|
3841 |
|sz| |
|
3842 |
||
3843 |
sz := self size. |
|
3844 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3845 |
||
3846 |
" |
|
3847 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3848 |
'1234567890' copyLast:4 |
|
18712
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3849 |
'12345' copyLast:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3850 |
'1234' copyLast:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3851 |
'123' copyLast:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3852 |
'' copyLast:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3853 |
|
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3854 |
'123' last:4 |
8e39130d65c8
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18711
diff
changeset
|
3855 |
'' last:4 |
602 | 3856 |
" |
3857 |
! |
|
3858 |
||
11841 | 3859 |
copyMapping:map |
3860 |
"return a copy, where elements all replaced via the given mapping. |
|
3861 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
|
3862 |
Otherwise, it is left unchanged." |
|
3863 |
||
3864 |
|s| |
|
3865 |
||
3866 |
s := WriteStream on:self species new. |
|
3867 |
self do:[:el | |
|
3868 |
s nextPut:(map at:el ifAbsent:el) |
|
3869 |
]. |
|
3870 |
^ s contents |
|
3871 |
||
3872 |
" |
|
3873 |
|map| |
|
3874 |
||
3875 |
map := Dictionary new |
|
3876 |
at:1 put:'one'; |
|
3877 |
at:2 put:'two'; |
|
3878 |
yourself. |
|
3879 |
||
3880 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
|
3881 |
" |
|
3882 |
! |
|
3883 |
||
3884 |
copyMappingFrom:inMap to:outMap |
|
3885 |
"return a copy, where elements all replaced via the given mapping. |
|
3886 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
|
3887 |
Otherwise, it is left unchanged." |
|
3888 |
||
3889 |
|d| |
|
3890 |
||
3891 |
d := Dictionary new. |
|
3892 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
|
3893 |
^ self copyMapping:d |
|
3894 |
||
3895 |
" |
|
3896 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
|
3897 |
'hello' copyMappingFrom:'eo' to:'**' |
|
3898 |
" |
|
3899 |
! |
|
3900 |
||
3653 | 3901 |
copyReplaceAll:oldElement with:newElement |
3902 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3903 |
have been replaced by newElement." |
|
3904 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3905 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3906 |
^ self copy replaceAll:oldElement with:newElement |
3907 |
||
3908 |
" |
|
8874 | 3909 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3910 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3911 |
" |
3912 |
||
3913 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3914 |
! |
|
3915 |
||
16758
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3916 |
copyReplaceAll:oldElement with:newElement ifNone:valueIfNoneFound |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3917 |
"return a copy of the receiver, where all elements equal to oldElement |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3918 |
have been replaced by newElement. |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3919 |
If none is found, return valueIfNoneFound. |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3920 |
Usually this is used as: |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3921 |
foo copyReplaceAll:oldElement with:newElement ifNone:foo" |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3922 |
|
17581
7b7b91d6aae5
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17574
diff
changeset
|
3923 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
16758
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3924 |
(self includes:oldElement) ifFalse:[ ^ valueIfNoneFound value ]. |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3925 |
^ self copyReplaceAll:oldElement with:newElement |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3926 |
|
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3927 |
" |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3928 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 ifNone:nil |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3929 |
'hello world' copyReplaceAll:$l with:$* ifNone:'oops' |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3930 |
'hello world' copyReplaceAll:$x with:$* ifNone:'oops' |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3931 |
" |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3932 |
! |
6e6379aeea83
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16716
diff
changeset
|
3933 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3934 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3935 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3936 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3937 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3938 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3939 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3940 |
|
5489 | 3941 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3942 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3943 |
"/ '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
|
3944 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3945 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3946 |
self do:[:el | |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3947 |
el = oldObject ifTrue:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3948 |
s nextPutAll:aCollection |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3949 |
] ifFalse:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3950 |
s nextPut:el |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3951 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3952 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3953 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3954 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3955 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3956 |
args: oldObject : <object> |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3957 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3958 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3959 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3960 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3961 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3962 |
" |
8874 | 3963 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3964 |
#(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
|
3965 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3966 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3967 |
|
14947
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3968 |
copyReplaceAny:collectionOfOldElements with:newElement |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3969 |
"return a copy of the receiver, where all elements equal to any in collectionOfOldElements |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3970 |
have been replaced by newElement." |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3971 |
|
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3972 |
^ self copy replaceAny:collectionOfOldElements with:newElement |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3973 |
|
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3974 |
" |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3975 |
#(1 2 3 1 2 3 1 2 3 4 1 2 3 1 2 3) copyReplaceAny:#(1 2) with:99 |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3976 |
'hello world' copyReplaceAny:'eo' with:$* |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3977 |
" |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3978 |
! |
e9a0049c0ef1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14893
diff
changeset
|
3979 |
|
602 | 3980 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3981 |
"return a copy of the receiver, where the elements from startIndex to |
|
15985
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3982 |
endIndex have been replaced by the elements of aCollection. |
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3983 |
Returns a plain copy, if startIndex is beyond the receiver's size" |
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3984 |
|
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3985 |
|newColl sz mySize replSize| |
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3986 |
|
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3987 |
mySize := self size. |
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3988 |
startIndex > mySize ifTrue:[^ self copy]. |
602 | 3989 |
|
3990 |
replSize := aCollection size. |
|
15985
8cfe18d25ee4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15813
diff
changeset
|
3991 |
sz := mySize - (endIndex - startIndex + 1) + replSize. |
602 | 3992 |
newColl := self copyEmptyAndGrow:sz. |
3993 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3994 |
newColl replaceFrom:startIndex with:aCollection. |
|
3995 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3996 |
^ newColl |
|
3997 |
||
3998 |
" |
|
3999 |
#(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 | 4000 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
14265
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4001 |
'one two three' copyReplaceFrom:5 to:7 with:'zwei' |
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4002 |
|
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4003 |
the following is something we want, not an unexpected side feature: |
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4004 |
'12345' copyReplaceFrom:5 to:4 with:'xxx' |
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4005 |
" |
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4006 |
|
f9f9a9d170a1
comment/format in: #copyReplaceFrom:to:with:
Claus Gittinger <cg@exept.de>
parents:
14238
diff
changeset
|
4007 |
"Modified (comment): / 27-07-2012 / 16:39:17 / cg" |
602 | 4008 |
! |
4009 |
||
18882 | 4010 |
copyReplaceFrom:startIndex to:endIndex withObject:anObject |
4011 |
"return a copy of the receiver, where the elements from startIndex to |
|
4012 |
endIndex have been replaced by anObject. |
|
4013 |
Returns a plain copy, if startIndex is beyond the receiver's size" |
|
4014 |
||
4015 |
|newColl mySize| |
|
4016 |
||
4017 |
mySize := self size. |
|
4018 |
startIndex > mySize ifTrue:[^ self copy]. |
|
4019 |
||
4020 |
newColl := self copyEmptyAndGrow:mySize. |
|
4021 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
4022 |
newColl from:startIndex to:endIndex put:anObject. |
|
4023 |
newColl replaceFrom:(endIndex + 1) with:self startingAt:(endIndex + 1). |
|
4024 |
^ newColl |
|
4025 |
||
4026 |
" |
|
4027 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 to:6 withObject:#foo |
|
4028 |
'hello world' copyReplaceFrom:1 to:5 withObject:$* |
|
4029 |
'hello world' copyReplaceFrom:6 to:8 withObject:$* |
|
4030 |
" |
|
4031 |
! |
|
4032 |
||
602 | 4033 |
copyReplaceFrom:startIndex with:aCollection |
4034 |
"return a copy of the receiver, where the elements from startIndex to |
|
4035 |
the end have been replaced by the elements of aCollection" |
|
4036 |
||
4037 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
4038 |
||
4039 |
" |
|
8874 | 4040 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
4041 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 4042 |
" |
4043 |
! |
|
4044 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4045 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4046 |
"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
|
4047 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4048 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4049 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4050 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4051 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4052 |
" |
8874 | 4053 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
4054 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4055 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4056 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
4057 |
|
602 | 4058 |
copyThrough:element |
4059 |
"return a new collection consisting of the receiver elements |
|
8874 | 4060 |
up-to (AND including) the first occurrence of element." |
602 | 4061 |
|
4062 |
|idx| |
|
4063 |
||
4064 |
idx := self indexOf:element. |
|
4065 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
4066 |
^ self copyFrom:1 to:idx |
|
4067 |
||
4068 |
" |
|
4069 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
4070 |
'1234567890' copyThrough:$5 |
|
4071 |
'1234567890' copyThrough:$a |
|
4072 |
'1234567890' copyThrough:$1 |
|
4073 |
" |
|
4074 |
! |
|
4075 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
4076 |
copyTo:stopIndex |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4077 |
"Return a new collection consisting of receiver's elements from 1 up to (including) stopIndex. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
4078 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
4079 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
4080 |
^ self copyFrom:1 to:stopIndex |
602 | 4081 |
|
4082 |
" |
|
4083 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
4084 |
'1234567890' copyTo:4 |
|
4085 |
" |
|
4086 |
! |
|
4087 |
||
5526 | 4088 |
copyToMax:stop |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4089 |
"return a new collection consisting of receiver's elements |
8874 | 4090 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 4091 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
4092 |
||
4093 |
^ self copyFrom:1 to:(self size min:stop) |
|
4094 |
||
4095 |
" |
|
4096 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
4097 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
4098 |
" |
|
4099 |
! |
|
4100 |
||
11844 | 4101 |
copyTransliterating:oldElements to:newElements |
4102 |
"return a copy, where elements all transliterated via the given mapping. |
|
4103 |
The transiteraion works like perl's tr operator. |
|
4104 |
||
4105 |
Transliterates all occurrences of the characters found in the search list with the |
|
11880 | 4106 |
corresponding character in the replacement list. |
11844 | 4107 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
4108 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
4109 |
||
4110 |
Note that tr does not do other character escapes such as \n. |
|
4111 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
4112 |
||
4113 |
Note also that the whole range idea is rather unportable between character sets, |
|
4114 |
and even within character sets they may cause results you probably didn't expect. |
|
4115 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
4116 |
case (a-e, A-E), or digits (0-4). |
|
4117 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
4118 |
" |
|
4119 |
||
4120 |
^ self |
|
4121 |
copyTransliterating:oldElements to:newElements |
|
4122 |
complement:false squashDuplicates:false |
|
4123 |
||
4124 |
" |
|
4125 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
4126 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
4127 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
4128 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
4129 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11880 | 4130 |
|
4131 |
can also be used to remove character: |
|
4132 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
|
4133 |
||
4134 |
'abcdefg |
|
4135 |
hijkl1 |
|
4136 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
|
4137 |
||
11844 | 4138 |
" |
4139 |
! |
|
4140 |
||
4141 |
copyTransliterating:oldElements to:newElements |
|
4142 |
complement:complement squashDuplicates:squashDuplicates |
|
4143 |
||
4144 |
"return a copy, where elements all transliterated via the given mapping. |
|
4145 |
The transiteraion works like perl's tr operator. |
|
4146 |
||
4147 |
Transliterates all occurrences of the characters found in the search list with the |
|
4148 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
4149 |
or deleted. |
|
4150 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
4151 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
4152 |
||
4153 |
Note that tr does not do other character escapes such as \n. |
|
4154 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
4155 |
||
4156 |
Note also that the whole range idea is rather unportable between character sets, |
|
4157 |
and even within character sets they may cause results you probably didn't expect. |
|
4158 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
4159 |
case (a-e, A-E), or digits (0-4). |
|
4160 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
4161 |
||
4162 |
If complement is true, non-matching chars are affected. |
|
4163 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
|
4164 |
" |
|
4165 |
||
16099
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4166 |
^ self |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4167 |
copyTransliterating:oldElements to:newElements rawMap:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4168 |
complement:complement squashDuplicates:squashDuplicates |
11844 | 4169 |
|
4170 |
" |
|
11875 | 4171 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
11844 | 4172 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
4173 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
4174 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
4175 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
4176 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11875 | 4177 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
4178 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
|
4179 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
|
11844 | 4180 |
|
4181 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
4182 |
complement:false squashDuplicates:false |
|
4183 |
||
4184 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
4185 |
complement:false squashDuplicates:true |
|
4186 |
||
4187 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
4188 |
complement:false squashDuplicates:false |
|
4189 |
||
4190 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
4191 |
complement:true squashDuplicates:false |
|
4192 |
||
4193 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
4194 |
complement:true squashDuplicates:false |
|
4195 |
||
4196 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
4197 |
complement:false squashDuplicates:false |
|
4198 |
||
4199 |
- delete all a-zA-Z |
|
4200 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
4201 |
complement:false squashDuplicates:false |
|
4202 |
||
4203 |
- delete all except a-zA-Z |
|
4204 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
4205 |
complement:true squashDuplicates:false |
|
4206 |
||
4207 |
- squash multiple spaces |
|
4208 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
|
4209 |
complement:false squashDuplicates:true |
|
4210 |
||
4211 |
- squash multiple digits |
|
4212 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
|
4213 |
complement:false squashDuplicates:true |
|
4214 |
" |
|
4215 |
! |
|
4216 |
||
16099
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4217 |
copyTransliterating:oldElements to:newElements rawMap:rawMap |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4218 |
complement:complement squashDuplicates:squashDuplicates |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4219 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4220 |
"return a copy, where elements all transliterated via the given mapping. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4221 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4222 |
The transiteraion works like perl's tr operator, unless rawMap is true; |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4223 |
otherwise, elements from oldElements and newElements are treated without any processing, |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4224 |
which makes this easier to use, if special characters (such as '-', '[' etc. are to be transliterated). |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4225 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4226 |
Transliterates all occurrences of the characters found in the search list with the |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4227 |
corresponding character in the replacement list. It returns the number of characters replaced |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4228 |
or deleted. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4229 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4230 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4231 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4232 |
Note that tr does not do other character escapes such as \n. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4233 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4234 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4235 |
Note also that the whole range idea is rather unportable between character sets, |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4236 |
and even within character sets they may cause results you probably didn't expect. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4237 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4238 |
case (a-e, A-E), or digits (0-4). |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4239 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4240 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4241 |
If complement is true, non-matching chars are affected. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4242 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4243 |
" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4244 |
|
16518 | 4245 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated last inc rslt willDeleteChars| |
4246 |
||
4247 |
willDeleteChars := false. |
|
16099
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4248 |
map := Dictionary new. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4249 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4250 |
rawMap ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4251 |
1 to:(oldElements size min:newElements size) do:[:i | |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4252 |
map at:(oldElements at:i) put:(newElements at:i) |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4253 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4254 |
"/ remaining are filled with last from newElements |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4255 |
newElements size+1 to:oldElements size do:[:i | |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4256 |
map at:(oldElements at:i) put:(newElements last) |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4257 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4258 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4259 |
s1 := oldElements readStream. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4260 |
s2 := newElements readStream. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4261 |
range1 := range2 := nil. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4262 |
[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4263 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4264 |
range1 notNil or:[ s1 atEnd not ] |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4265 |
] whileTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4266 |
range1 notNil ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4267 |
c1 := range1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4268 |
range1 atEnd ifTrue:[range1 := nil]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4269 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4270 |
c1 := s1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4271 |
s1 peek == $\ ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4272 |
s1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4273 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4274 |
s1 peek == $- ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4275 |
s1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4276 |
last := s1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4277 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4278 |
range1 := (c1 to:last by:inc) readStream. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4279 |
c1 := range1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4280 |
range1 atEnd ifTrue:[range1 := nil]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4281 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4282 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4283 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4284 |
range2 notNil ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4285 |
c2 := range2 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4286 |
range2 atEnd ifTrue:[range2 := nil]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4287 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4288 |
s2 atEnd ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4289 |
"/ keep c2 |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4290 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4291 |
c2 := s2 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4292 |
s2 peek == $\ ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4293 |
s1 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4294 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4295 |
s2 peek == $- ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4296 |
s2 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4297 |
last := s2 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4298 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4299 |
range2 := (c2 to:last by:inc) readStream. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4300 |
c2 := range2 next. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4301 |
range2 atEnd ifTrue:[range2 := nil]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4302 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4303 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4304 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4305 |
]. |
16518 | 4306 |
map at:c1 put:c2. |
4307 |
c2 isNil ifTrue:[ |
|
4308 |
willDeleteChars := true. |
|
4309 |
]. |
|
16099
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4310 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4311 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4312 |
|
16518 | 4313 |
(squashDuplicates not and:[complement not and:[willDeleteChars not]]) ifTrue:[ |
16099
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4314 |
"/ use a faster algorithm |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4315 |
rslt := self species new:self size. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4316 |
1 to:self size do:[:i | |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4317 |
|in| |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4318 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4319 |
in := self at:i. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4320 |
rslt at:i put:(map at:in ifAbsent:in). |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4321 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4322 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4323 |
outStream := WriteStream on:(self species new). |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4324 |
self do:[:in | |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4325 |
|xLated out| |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4326 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4327 |
complement ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4328 |
(map includesKey:in) ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4329 |
xLated := out := in. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4330 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4331 |
out := newElements at:1 ifAbsent:nil. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4332 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4333 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4334 |
(map includesKey:in) ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4335 |
xLated := out := map at:in. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4336 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4337 |
out := in. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4338 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4339 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4340 |
out notNil ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4341 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4342 |
"/ ignore |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4343 |
] ifFalse:[ |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4344 |
outStream nextPut:out. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4345 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4346 |
prevXlated := out. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4347 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4348 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4349 |
rslt := outStream contents |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4350 |
]. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4351 |
^ rslt. |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4352 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4353 |
" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4354 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4355 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4356 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4357 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4358 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4359 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'*' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4360 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4361 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4362 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4363 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4364 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4365 |
'abcdefghijkl12345678--90' copyTransliterating:'-abcz' to:'z-' rawMap:true complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4366 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4367 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4368 |
complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4369 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4370 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4371 |
complement:false squashDuplicates:true |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4372 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4373 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4374 |
complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4375 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4376 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4377 |
complement:true squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4378 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4379 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4380 |
complement:true squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4381 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4382 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4383 |
complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4384 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4385 |
- delete all a-zA-Z |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4386 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4387 |
complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4388 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4389 |
- delete all except a-zA-Z |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4390 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4391 |
complement:true squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4392 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4393 |
- squash multiple spaces |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4394 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4395 |
complement:false squashDuplicates:true |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4396 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4397 |
- squash multiple digits |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4398 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4399 |
complement:false squashDuplicates:true |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4400 |
" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4401 |
! |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4402 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4403 |
copyTransliteratingRaw:oldElements to:newElements |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4404 |
"return a copy, where elements all transliterated via the given mapping" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4405 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4406 |
^ self |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4407 |
copyTransliterating:oldElements to:newElements |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4408 |
rawMap:true |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4409 |
complement:false squashDuplicates:false |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4410 |
|
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4411 |
" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4412 |
'abcdefghijkl1234567890' copyTransliteratingRaw:'abc' to:'*' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4413 |
'abcdefghijkl---1234567890' copyTransliteratingRaw:'-' to:'*' |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4414 |
" |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4415 |
! |
8615e6eae7cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16073
diff
changeset
|
4416 |
|
13697 | 4417 |
copyUpThrough:element |
4418 |
"return a new collection consisting of the receiver elements |
|
4419 |
up-to (and including) the first occurrence of element; |
|
4420 |
or to the end, if element is not included" |
|
4421 |
||
4422 |
|idx| |
|
4423 |
||
4424 |
idx := self indexOf:element. |
|
4425 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
4426 |
^ self copyFrom:1 to:idx |
|
4427 |
||
4428 |
" |
|
4429 |
#($a $b $c $d $e $f $g) copyUpThrough:$d |
|
4430 |
'1234567890' copyUpThrough:$5 |
|
4431 |
'1234567890' copyUpThrough:$a |
|
4432 |
'1234567890' copyUpThrough:$1 |
|
4433 |
'123456789' copyUpThrough:$0 |
|
4434 |
" |
|
4435 |
||
4436 |
"Created: / 14-09-2011 / 16:28:16 / cg" |
|
4437 |
! |
|
4438 |
||
602 | 4439 |
copyUpTo:element |
4440 |
"return a new collection consisting of the receiver elements |
|
8874 | 4441 |
up-to (but excluding) the first occurrence of element; |
602 | 4442 |
or to the end, if element is not included" |
4443 |
||
4444 |
|idx| |
|
4445 |
||
4446 |
idx := self indexOf:element. |
|
4447 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
4448 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
4449 |
^ self copyFrom:1 to:(idx-1) |
|
4450 |
||
4451 |
" |
|
4452 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
4453 |
'1234567890' copyUpTo:$5 |
|
4454 |
'1234567890' copyUpTo:$a |
|
4455 |
'1234567890' copyUpTo:$1 |
|
4456 |
'123456789' copyUpTo:$0 |
|
4457 |
" |
|
4458 |
! |
|
4459 |
||
1 | 4460 |
copyWith:newElement |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4461 |
"return a new collection containing the receiver's elements |
8874 | 4462 |
and the single new element, newElement. |
233 | 4463 |
This is different from concatentation, which expects another collection |
4464 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 4465 |
|
4466 |
|newCollection mySize newSize| |
|
4467 |
||
4468 |
mySize := self size. |
|
4469 |
newSize := mySize + 1. |
|
252 | 4470 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 4471 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
4472 |
newCollection at:newSize put:newElement. |
|
4473 |
^newCollection |
|
44 | 4474 |
|
95 | 4475 |
" |
4476 |
#(1 2 3 4 5) copyWith:$a |
|
4477 |
'abcdefg' copyWith:$h |
|
4478 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
4479 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
4480 |
" |
|
1 | 4481 |
! |
4482 |
||
15709
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4483 |
copyWith:newElement insertedAfterIndex:index |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4484 |
"return a new collection with newElement inserted after index. |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4485 |
With a 0 index, newElement is prepended; |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4486 |
if index is my size, it is appended. |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4487 |
The recevier remains unchanged" |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4488 |
|
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4489 |
^ ((self copyTo:index) copyWith:newElement),(self copyFrom:index+1). |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4490 |
|
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4491 |
" |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4492 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedAfterIndex:1 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4493 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedAfterIndex:0 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4494 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedAfterIndex:14 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4495 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedAfterIndex:15 --> error |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4496 |
" |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4497 |
! |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4498 |
|
15710
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4499 |
copyWith:newElement insertedBeforeIndex:index |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4500 |
"return a new collection with newElement inserted after index. |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4501 |
With a 0 index, newElement is prepended; |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4502 |
if index is my size, it is appended. |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4503 |
The recevier remains unchanged" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4504 |
|
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4505 |
^ self copyWith:newElement insertedAfterIndex:index-1 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4506 |
|
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4507 |
" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4508 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedBeforeIndex:1 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4509 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedBeforeIndex:0 --> error |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4510 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedBeforeIndex:14 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4511 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedBeforeIndex:15 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4512 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWith:#a insertedBeforeIndex:16 --> error |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4513 |
" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4514 |
! |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4515 |
|
15709
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4516 |
copyWithAll:aCollection insertedAfterIndex:index |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4517 |
"return a new collection with aCollection sliced in after index. |
15708
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4518 |
With a 0 index, aString is prepended; |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4519 |
if index is my size, it is appended. |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4520 |
The recevier remains unchanged" |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4521 |
|
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4522 |
"/ tuning only; |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4523 |
"/ the code below would work as well (but create a garbage copy of the receiver) |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4524 |
index == 0 ifTrue:[ ^ aCollection, self]. |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4525 |
index == self size ifTrue:[ ^ self,aCollection]. |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4526 |
|
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4527 |
^ (self copyTo:index),aCollection,(self copyFrom:index+1). |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4528 |
|
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4529 |
" |
15709
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4530 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedAfterIndex:1 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4531 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedAfterIndex:0 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4532 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedAfterIndex:14 |
69a8fa0f6d6c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15708
diff
changeset
|
4533 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedAfterIndex:15 --> error |
15708
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4534 |
" |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4535 |
! |
d1cb0760d407
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15685
diff
changeset
|
4536 |
|
15710
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4537 |
copyWithAll:aCollection insertedBeforeIndex:index |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4538 |
"return a new collection with aCollection sliced in before index. |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4539 |
With a 1 index, aString is prepended; |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4540 |
if index is my size+1, it is appended. |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4541 |
The recevier remains unchanged" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4542 |
|
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4543 |
^ self copyWithAll:aCollection insertedAfterIndex:index-1 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4544 |
|
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4545 |
" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4546 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedBeforeIndex:1 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4547 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedBeforeIndex:0 --> error |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4548 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedBeforeIndex:14 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4549 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedBeforeIndex:15 |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4550 |
#(1 2 3 4 5 6 7 8 9 0 1 2 3 4) copyWithAll:#(a b c) insertedBeforeIndex:16 --> error |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4551 |
" |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4552 |
! |
73bff9f3bc15
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15709
diff
changeset
|
4553 |
|
11855 | 4554 |
copyWithFirst:newFirstElement |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4555 |
"return a new collection containing the receiver's elements |
11855 | 4556 |
and the single new element, newElement up front. |
4557 |
This is different from concatentation, which expects two collections |
|
4558 |
as argument, but equivalent to copy-and-addFirst." |
|
4559 |
||
4560 |
|newCollection mySize newSize| |
|
4561 |
||
4562 |
mySize := self size. |
|
4563 |
newSize := mySize + 1. |
|
4564 |
newCollection := self copyEmptyAndGrow:newSize. |
|
4565 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
|
4566 |
newCollection at:1 put:newFirstElement. |
|
4567 |
^newCollection |
|
4568 |
||
4569 |
" |
|
4570 |
#(1 2 3 4 5) copyWithFirst:$a |
|
4571 |
'abcdefg' copyWithFirst:$h |
|
4572 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
|
4573 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
|
4574 |
" |
|
4575 |
! |
|
4576 |
||
61 | 4577 |
copyWithout:elementToSkip |
4578 |
"return a new collection consisting of a copy of the receiver, with |
|
4579 |
ALL elements equal to elementToSkip are left out. |
|
4580 |
No error is reported, if elementToSkip is not in the collection." |
|
4581 |
||
293 | 4582 |
|n "{ Class: SmallInteger }" |
4583 |
sz "{ Class: SmallInteger }" |
|
4584 |
srcIndex "{ Class: SmallInteger }" |
|
4585 |
dstIndex "{ Class: SmallInteger }" |
|
4586 |
skipIndex "{ Class: SmallInteger }" |
|
4587 |
copy l| |
|
61 | 4588 |
|
8874 | 4589 |
"the code below may look like overkill, |
61 | 4590 |
however, for big collections its better to move data |
4591 |
around in big chunks" |
|
4592 |
||
4593 |
n := self occurrencesOf:elementToSkip. |
|
4594 |
n == 0 ifTrue:[^ self copy]. |
|
4595 |
||
4596 |
sz := self size. |
|
252 | 4597 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 4598 |
|
4599 |
srcIndex := 1. |
|
4600 |
dstIndex := 1. |
|
4601 |
||
4602 |
n timesRepeat:[ |
|
8874 | 4603 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
4604 |
l := skipIndex - srcIndex. |
|
4605 |
l ~~ 0 ifTrue:[ |
|
4606 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
4607 |
with:self startingAt:srcIndex. |
|
4608 |
dstIndex := dstIndex + l |
|
4609 |
]. |
|
4610 |
srcIndex := skipIndex + 1 |
|
61 | 4611 |
]. |
4612 |
l := sz - srcIndex. |
|
4613 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 4614 |
with:self startingAt:srcIndex. |
61 | 4615 |
^ copy |
4616 |
||
95 | 4617 |
" |
4618 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
4619 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
4620 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 4621 |
'abcdefghi' copyWithout:$h |
4622 |
'abcdefg' copyWithout:$h |
|
95 | 4623 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
4624 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
4625 |
#(90 80 70 60 50) copyWithout:70 |
|
4626 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
4627 |
" |
|
61 | 4628 |
! |
4629 |
||
602 | 4630 |
copyWithoutFirst:elementToSkip |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4631 |
"return a new collection consisting of a copy of the receiver's elements |
8874 | 4632 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
4633 |
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
|
4634 |
Do not confuse this with copyButFirst:" |
602 | 4635 |
|
4636 |
|copy skipIndex sz| |
|
4637 |
||
4638 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
4639 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
4640 |
||
4641 |
sz := self size - 1. |
|
4642 |
copy := self copyEmptyAndGrow:sz. |
|
4643 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
4644 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
4645 |
^ copy |
|
4646 |
||
4647 |
" |
|
4648 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
4649 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
4650 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
4651 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
4652 |
" |
|
4653 |
! |
|
4654 |
||
8673 | 4655 |
copyWithoutIdentical:elementToSkip |
4656 |
"return a new collection consisting of a copy of the receiver, with |
|
4657 |
ALL elements identical to elementToSkip are left out. |
|
4658 |
No error is reported, if elementToSkip is not in the collection." |
|
4659 |
||
4660 |
|n "{ Class: SmallInteger }" |
|
4661 |
sz "{ Class: SmallInteger }" |
|
4662 |
srcIndex "{ Class: SmallInteger }" |
|
4663 |
dstIndex "{ Class: SmallInteger }" |
|
4664 |
skipIndex "{ Class: SmallInteger }" |
|
4665 |
copy l| |
|
4666 |
||
8874 | 4667 |
"the code below may look like overkill, |
8673 | 4668 |
however, for big collections its better to move data |
4669 |
around in big chunks" |
|
4670 |
||
4671 |
n := self count:[:el | el == elementToSkip]. |
|
4672 |
n == 0 ifTrue:[^ self copy]. |
|
4673 |
||
4674 |
sz := self size. |
|
4675 |
copy := self copyEmptyAndGrow:(sz - n). |
|
4676 |
||
4677 |
srcIndex := 1. |
|
4678 |
dstIndex := 1. |
|
4679 |
||
4680 |
n timesRepeat:[ |
|
8874 | 4681 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
4682 |
l := skipIndex - srcIndex. |
|
4683 |
l ~~ 0 ifTrue:[ |
|
4684 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
4685 |
with:self startingAt:srcIndex. |
|
4686 |
dstIndex := dstIndex + l |
|
4687 |
]. |
|
4688 |
srcIndex := skipIndex + 1 |
|
8673 | 4689 |
]. |
4690 |
l := sz - srcIndex. |
|
4691 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 4692 |
with:self startingAt:srcIndex. |
8673 | 4693 |
^ copy |
4694 |
||
4695 |
" |
|
8874 | 4696 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
4697 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 4698 |
" |
4699 |
! |
|
4700 |
||
61 | 4701 |
copyWithoutIndex:omitIndex |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4702 |
"return a new collection consisting of receiver's elements |
61 | 4703 |
without the argument stored at omitIndex" |
4704 |
||
4705 |
|copy sz| |
|
4706 |
||
202 | 4707 |
sz := self size - 1. |
252 | 4708 |
copy := self copyEmptyAndGrow:sz. |
293 | 4709 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
4710 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 4711 |
^ copy |
4712 |
||
95 | 4713 |
" |
4714 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
4715 |
'abcdefghijkl' copyWithoutIndex:5 |
|
4716 |
" |
|
1 | 4717 |
! |
4718 |
||
15763 | 4719 |
copyWithoutIndex:firstIndex toIndex:lastIndex |
4720 |
"return a new collection consisting of receiver's elements |
|
4721 |
without the arguments elements stored from firstIndex to lastIndex" |
|
4722 |
||
4723 |
|copy sz| |
|
4724 |
||
4725 |
sz := self size - (lastIndex-firstIndex + 1). |
|
4726 |
copy := self copyEmptyAndGrow:sz. |
|
4727 |
copy replaceFrom:1 to:(firstIndex - 1) with:self startingAt:1. |
|
4728 |
copy replaceFrom:firstIndex to:sz with:self startingAt:(lastIndex + 1). |
|
4729 |
^ copy |
|
4730 |
||
4731 |
" |
|
4732 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 toIndex:5 |
|
4733 |
'abcdefghijkl' copyWithoutIndex:5 toIndex:5 |
|
4734 |
" |
|
4735 |
! |
|
4736 |
||
202 | 4737 |
copyWithoutLast:count |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4738 |
"return a new collection consisting of the receiver's elements |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
4739 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
4740 |
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
|
4741 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
4742 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
4743 |
|
9250 | 4744 |
<resource:#obsolete> |
4745 |
||
15156 | 4746 |
self obsoleteMethodWarning:'use #copyButLast:'. |
4747 |
^ self copyButLast:count. |
|
202 | 4748 |
|
4749 |
" |
|
8874 | 4750 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
4751 |
'1234567890' copyWithoutLast:4 |
|
202 | 4752 |
" |
5231 | 4753 |
! |
4754 |
||
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4755 |
repeatedConcatenation:nTimes |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4756 |
"return a new collection consisting of nTimes the concatenation of the receiver" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4757 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4758 |
|newColl idx| |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4759 |
|
16706
f5d25a583a27
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16704
diff
changeset
|
4760 |
newColl := self species newWithSize:(self size * nTimes). |
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4761 |
idx := 1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4762 |
nTimes timesRepeat:[ |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4763 |
newColl replaceFrom:idx with:self startingAt:1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4764 |
idx := idx + self size. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4765 |
]. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4766 |
^ newColl |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4767 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4768 |
" |
16123
9c9623b5cfda
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16111
diff
changeset
|
4769 |
'hello' repeatedConcatenation:5 |
16703
423c0735a7dd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16702
diff
changeset
|
4770 |
' ' repeatedConcatenation:100 |
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4771 |
#[1 2 3] repeatedConcatenation:3 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4772 |
#(1 2 3) repeatedConcatenation:1 |
16703
423c0735a7dd
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16702
diff
changeset
|
4773 |
#(1 2 3) repeatedConcatenation:0 |
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4774 |
" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4775 |
! |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
4776 |
|
5231 | 4777 |
restAfter:anElement |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4778 |
"return a new collection consisting of the receiver's elements after |
5231 | 4779 |
(but excluding) anElement. |
8366 | 4780 |
If anElement is not in the receiver, the returned collection |
4781 |
will be empty. |
|
4782 |
See also #upTo:." |
|
5231 | 4783 |
|
4784 |
|pos| |
|
4785 |
||
4786 |
pos := self indexOf:anElement. |
|
8366 | 4787 |
pos == 0 ifTrue:[^ self copyEmpty]. |
4788 |
||
5231 | 4789 |
^ self copyFrom:(pos + 1) |
4790 |
||
4791 |
" |
|
8874 | 4792 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
4793 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
4794 |
'hello world' upTo:Character space |
|
4795 |
'hello world' restAfter:Character space |
|
4796 |
'1234.5678' upTo:$. |
|
4797 |
'1234.5678' restAfter:$. |
|
8366 | 4798 |
" |
5231 | 4799 |
! |
4800 |
||
12187 | 4801 |
trimForWhich:aCheckBlock |
4802 |
"return a copy of myself without leading and trailing elements for which aCheckBlock returns true. |
|
4803 |
Normally, this is mostly used with string receivers." |
|
4804 |
||
4805 |
|startIndex "{ Class: SmallInteger }" |
|
4806 |
endIndex "{ Class: SmallInteger }" |
|
4807 |
sz| |
|
4808 |
||
4809 |
sz := self size. |
|
4810 |
startIndex := 1. |
|
4811 |
endIndex := sz. |
|
4812 |
||
12320 | 4813 |
[(startIndex <= endIndex) and:[aCheckBlock value:(self at:startIndex)]] whileTrue:[ |
12187 | 4814 |
startIndex := startIndex + 1 |
4815 |
]. |
|
4816 |
[(endIndex > 1) and:[aCheckBlock value:(self at:endIndex)]] whileTrue:[ |
|
4817 |
endIndex := endIndex - 1 |
|
4818 |
]. |
|
4819 |
startIndex > endIndex ifTrue:[ |
|
4820 |
^ self class new |
|
4821 |
]. |
|
4822 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
4823 |
^ self |
|
4824 |
]. |
|
4825 |
^ self copyFrom:startIndex to:endIndex |
|
4826 |
||
4827 |
" |
|
12320 | 4828 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
4829 |
' ' trimForWhich:[:ch | ch isSeparator] |
|
12187 | 4830 |
#(0 0 0 1 2 3 0 0 0) trimForWhich:[:e | e = 0] |
4831 |
" |
|
4832 |
! |
|
4833 |
||
5231 | 4834 |
upTo:anElement |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4835 |
"return a new collection consisting of the receiver's elements upTo |
5231 | 4836 |
(but excluding) anElement. |
4837 |
If anElement is not in the receiver, the returned collection |
|
4838 |
will consist of all elements of the receiver. |
|
9307 | 4839 |
See also #restAfter: , #copyFrom:index." |
5231 | 4840 |
|
4841 |
|pos| |
|
4842 |
||
4843 |
pos := self indexOf:anElement. |
|
4844 |
pos == 0 ifTrue:[^ self copy]. |
|
4845 |
||
4846 |
^ self copyFrom:1 to:(pos - 1) |
|
4847 |
||
4848 |
" |
|
8874 | 4849 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
4850 |
'hello world' upTo:Character space |
|
4851 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
4852 |
'1234.5678' upTo:$. |
|
4853 |
'1234' upTo:$. |
|
18821 | 4854 |
'.' upTo:$. |
4855 |
||
5231 | 4856 |
|
4857 |
raises an error: |
|
4858 |
||
8874 | 4859 |
(Dictionary new |
9307 | 4860 |
at:#foo put:'foo'; |
4861 |
at:#bar put:'bar'; |
|
4862 |
at:#baz put:'baz'; |
|
4863 |
yourself) upTo:#bar |
|
8874 | 4864 |
|
5231 | 4865 |
" |
4866 |
||
4867 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
4868 |
! |
|
4869 |
||
4870 |
upTo:anElement count:count |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4871 |
"return a new collection consisting of the receiver's elements upTo |
8874 | 4872 |
(but excluding) count found anElements, i.e. the first found count-1 |
4873 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 4874 |
procedure is done from the end of the collection backwards. |
4875 |
If anElement is not in the receiver, the returned collection |
|
4876 |
will consist of all elements of the receiver" |
|
4877 |
||
4878 |
|startPos endPos pos found| |
|
4879 |
||
4880 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4881 |
^ self upTo:anElement |
5231 | 4882 |
]. |
4883 |
||
4884 |
startPos := 1. |
|
4885 |
endPos := self size. |
|
4886 |
found := 0. |
|
4887 |
||
4888 |
count > 1 ifTrue:[ |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4889 |
pos := 0. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4890 |
[pos < self size and: [found < count]] |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4891 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4892 |
found == count ifTrue:[ |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4893 |
endPos := pos - 1 |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4894 |
] |
8874 | 4895 |
]. |
5231 | 4896 |
|
4897 |
count < 0 ifTrue:[ |
|
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4898 |
pos := self size + 1. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4899 |
[pos > 1 and: [found < count abs]] |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4900 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4901 |
found == count abs ifTrue:[ |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4902 |
startPos := pos + 1 |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4903 |
] |
8874 | 4904 |
]. |
5231 | 4905 |
|
4906 |
^ self copyFrom: startPos to: endPos |
|
4907 |
||
4908 |
" |
|
8874 | 4909 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 4910 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
4911 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
4912 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
4913 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
4914 |
" |
|
5563 | 4915 |
! |
4916 |
||
15476
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4917 |
upToAll:aSubCollection |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4918 |
"return a new collection consisting of the receiver's elements upTo |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4919 |
(but excluding) the first occurrence of a subcollection. |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4920 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4921 |
will consist of all elements of the receiver. |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4922 |
See also #upTo:" |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4923 |
|
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4924 |
|pos| |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4925 |
|
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4926 |
pos := self indexOfSubCollection:aSubCollection. |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4927 |
pos == 0 ifTrue:[^ self copy]. |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4928 |
|
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4929 |
^ self copyFrom:1 to:(pos - 1) |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4930 |
|
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4931 |
" |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4932 |
'hello world' upToAll:'wo' |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4933 |
'hello world' upToAll:'bla' |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4934 |
#(1 2 3 4 5 6 7 8) upToAll:#(5) |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4935 |
#(1 2 3 4 5 6 7 8) upToAll:#() |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4936 |
#(1 3 4 2 3 4 5 6 7 8) upToAll:#(3 4 5 6) |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4937 |
#(1 3 4 2 3 4 5 6 7 8) upToAll:#(3 5 6) |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4938 |
" |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4939 |
! |
60b348dd1d49
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15444
diff
changeset
|
4940 |
|
5563 | 4941 |
upToAny:aCollectionOfObjects |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
4942 |
"return a new collection consisting of the receiver's elements upTo |
5563 | 4943 |
(but excluding) any in aCollectionOfObjects. |
4944 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
4945 |
will consist of all elements of the receiver. |
|
4946 |
See also #upTo:" |
|
4947 |
||
4948 |
|pos| |
|
4949 |
||
4950 |
pos := self indexOfAny:aCollectionOfObjects. |
|
4951 |
pos == 0 ifTrue:[^ self copy]. |
|
4952 |
||
4953 |
^ self copyFrom:1 to:(pos - 1) |
|
4954 |
||
4955 |
" |
|
8874 | 4956 |
'hello world' upToAny:#($/ $:) |
4957 |
'hello:world' upToAny:#($/ $:) |
|
4958 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 4959 |
" |
12187 | 4960 |
! |
4961 |
||
16510
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4962 |
upToElementForWhich:aBlock |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4963 |
"return a new collection consisting of the receiver's elements |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4964 |
upTo (but excluding) the first element for which aBlock returns true. |
16606
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4965 |
If none is found, a copy of whole collection is returned. |
16510
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4966 |
See also #upTo:" |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4967 |
|
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4968 |
|pos| |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4969 |
|
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4970 |
pos := self findFirst:aBlock. |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4971 |
pos == 0 ifTrue:[^ self copy]. |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4972 |
|
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4973 |
^ self copyFrom:1 to:(pos - 1) |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4974 |
|
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4975 |
" |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4976 |
'hello world' upToElementForWhich:[:c | c isSeparator] |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4977 |
'hello:world' upToElementForWhich:[:c | c isSeparator] |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4978 |
" |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4979 |
! |
c56ce0737f3b
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16488
diff
changeset
|
4980 |
|
16606
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4981 |
upToMatching:aBlock |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4982 |
"Return the next elements up to but not including the next element |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4983 |
for which aBlock returns true. |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4984 |
If none is found, a copy of whole collection is returned." |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4985 |
|
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4986 |
^ self upToElementForWhich:aBlock |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4987 |
|
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4988 |
" |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4989 |
#(1 2 3 4 5 6 7 8 9) upToMatching:[:el | el even and:[el >= 5]] |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4990 |
'hello world' upToMatching:[:ch | ch isSeparator] |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4991 |
" |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4992 |
! |
477792bafbc3
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16567
diff
changeset
|
4993 |
|
16611
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4994 |
upToSeparator |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4995 |
"Return the next elements up to but not including the next separator. |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4996 |
The next read will return the separator. |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4997 |
If no separator is encountered, the contents up to the end is returned. |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4998 |
The elements are supposed to understand #isSeparator |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
4999 |
(i.e. the receiver is supposed to be a character-stream)." |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5000 |
|
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5001 |
^ self upToElementForWhich:[:ch | ch isSeparator] |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5002 |
|
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5003 |
" |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5004 |
'hello world' upToSeparator |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5005 |
" |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5006 |
! |
fceeb99e31df
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16606
diff
changeset
|
5007 |
|
12187 | 5008 |
withoutLeadingForWhich:aCheckBlock |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
5009 |
"return a copy of myself without leading elements for which aCheckBlock returns true. |
12187 | 5010 |
Returns an empty collection, if the receiver consist only of matching elements. |
5011 |
Normally, this is mostly used with string receivers." |
|
5012 |
||
5013 |
|index sz| |
|
5014 |
||
5015 |
sz := self size. |
|
5016 |
index := 1. |
|
5017 |
[index <= sz] whileTrue:[ |
|
5018 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
5019 |
index == 1 ifTrue:[^ self]. |
|
5020 |
^ self copyFrom:index |
|
5021 |
]. |
|
5022 |
index := index + 1 |
|
5023 |
]. |
|
5024 |
^ self class new |
|
5025 |
||
5026 |
" |
|
5027 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
|
5028 |
#( 0 0 0 1 2 3 4 0 0 0) withoutLeadingForWhich:[:e | e = 0] |
|
5029 |
" |
|
5030 |
! |
|
5031 |
||
5032 |
withoutTrailingForWhich:aCheckBlock |
|
5033 |
"return a copy of myself without trailing characters for which aCheckBlock returns true. |
|
5034 |
Returns an empty collection, if the receiver consist only of matching chars." |
|
5035 |
||
5036 |
|index sz| |
|
5037 |
||
5038 |
index := sz := self size. |
|
5039 |
[index ~~ 0] whileTrue:[ |
|
5040 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
5041 |
index == sz ifTrue:[^ self]. |
|
5042 |
^ self copyTo:index |
|
5043 |
]. |
|
5044 |
index := index - 1 |
|
5045 |
]. |
|
5046 |
^ self class new |
|
5047 |
||
5048 |
" |
|
5049 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
|
5050 |
#( 0 0 0 1 2 3 4 0 0 0) withoutTrailingForWhich:[:e | e = 0] |
|
5051 |
" |
|
602 | 5052 |
! ! |
5053 |
||
5054 |
!SequenceableCollection methodsFor:'enumerating'! |
|
5055 |
||
5056 |
collect:aBlock |
|
5057 |
"evaluate the argument, aBlock for every element in the collection |
|
5058 |
and return a collection of the results" |
|
5059 |
||
5060 |
|newCollection |
|
5061 |
sz "{ Class:SmallInteger }"| |
|
5062 |
||
5063 |
sz := self size. |
|
5064 |
newCollection := self copyEmptyAndGrow:sz. |
|
5065 |
1 to:sz do:[:index | |
|
8874 | 5066 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 5067 |
]. |
5068 |
^ newCollection |
|
5069 |
||
5070 |
" |
|
8874 | 5071 |
#(one two three four five six) collect:[:element | element asUppercase] |
5072 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
5073 |
(1 to:9) collect:[:element | element * element] |
|
602 | 5074 |
" |
202 | 5075 |
! |
5076 |
||
602 | 5077 |
do:aBlock |
5078 |
"evaluate the argument, aBlock for every element in the collection." |
|
5079 |
||
5080 |
|stop "{ Class:SmallInteger }"| |
|
5081 |
||
5082 |
stop := self size. |
|
5083 |
1 to:stop do:[:index | |
|
8874 | 5084 |
aBlock value:(self at:index). |
602 | 5085 |
] |
2927 | 5086 |
|
602 | 5087 |
" |
1422 | 5088 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 5089 |
" |
5090 |
! |
|
5091 |
||
2927 | 5092 |
do:aBlock separatedBy:sepBlock |
5093 |
"evaluate the argument, aBlock for every element in the collection. |
|
5094 |
Between each element (i.e. not before the first element and not after the |
|
5095 |
last element), evaluate sepBlock. |
|
15444
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5096 |
This supports printing of collections with elements separated by some string." |
2927 | 5097 |
|
5098 |
|stop "{ Class:SmallInteger }"| |
|
5099 |
||
15444
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5100 |
"/ redefined to use an inlinable to:do: |
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5101 |
"/ to avoid creation of another block |
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5102 |
|
2927 | 5103 |
stop := self size. |
5104 |
stop < 1 ifTrue:[^ self]. |
|
5105 |
aBlock value:(self at:1). |
|
5106 |
2 to:stop do:[:index | |
|
15444
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5107 |
sepBlock value. |
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5108 |
aBlock value:(self at:index) |
2927 | 5109 |
]. |
5110 |
||
5111 |
" |
|
5112 |
#(one two three four five six) |
|
15444
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5113 |
do:[:each | Transcript show:each] |
29961a2523e4
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15291
diff
changeset
|
5114 |
separatedBy:[Transcript show:' , '] |
2927 | 5115 |
" |
5116 |
! |
|
3003 | 5117 |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5118 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5119 |
"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
|
5120 |
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
|
5121 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5122 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5123 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5124 |
" |
8874 | 5125 |
#(one two three four five six) |
5126 |
from:2 |
|
5127 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5128 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5129 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5130 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5131 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
5132 |
|
14145 | 5133 |
from:start conform:aOneArgBlock |
5134 |
"return true, if the elements starting at the start-index conform to some condition. |
|
5135 |
I.e. return false, if aBlock returns false for any of those elements; |
|
5136 |
true otherwise." |
|
5137 |
||
5138 |
self from:start to:(self size) do:[:element | |
|
5139 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
5140 |
]. |
|
5141 |
^ true |
|
5142 |
||
5143 |
"Created: / 25-05-2012 / 14:02:13 / cg" |
|
5144 |
! |
|
5145 |
||
602 | 5146 |
from:startIndex do:aBlock |
5147 |
"evaluate the argument, aBlock for the elements starting with the |
|
5148 |
element at startIndex to the end." |
|
5149 |
||
5150 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 5151 |
|
95 | 5152 |
" |
8874 | 5153 |
#(one two three four five six) |
5154 |
from:3 |
|
5155 |
do:[:element | Transcript showCR:element] |
|
602 | 5156 |
" |
5157 |
! |
|
5158 |
||
15570
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5159 |
from:startIndex doWithExit:aBlock |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5160 |
"evaluate the argument, aBlock for the elements starting with the |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5161 |
element at startIndex to the end. Passes an additional exitBlock as second |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5162 |
argument, which can be used to exit the loop early." |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5163 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5164 |
^ self from:startIndex to:self size doWithExit:aBlock |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5165 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5166 |
" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5167 |
#(one two three four five six) |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5168 |
from:3 |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5169 |
doWithExit:[:element :exit | |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5170 |
Transcript showCR:element. |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5171 |
element = 'four' ifTrue:[ exit value:nil ] |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5172 |
] |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5173 |
" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5174 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5175 |
"Created: / 28-07-2013 / 22:37:28 / cg" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5176 |
! |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5177 |
|
7490 | 5178 |
from:startIndex keysAndValuesDo:aBlock |
5179 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
5180 |
element at startIndex to the end." |
|
5181 |
||
5182 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
5183 |
||
5184 |
" |
|
8874 | 5185 |
#(one two three four five six) |
5186 |
from:3 |
|
5187 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 5188 |
" |
5189 |
! |
|
5190 |
||
7337 | 5191 |
from:index1 to:index2 by:stepArg do:aBlock |
5192 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
5193 |
||
5194 |
|start "{ Class:SmallInteger }" |
|
5195 |
stop "{ Class:SmallInteger }" |
|
5196 |
step "{ Class:SmallInteger }" | |
|
5197 |
||
5198 |
start := index1. "/ these assignments force type checking... |
|
5199 |
stop := index2. "/ and guarantee inline loop code below. |
|
5200 |
step := stepArg. |
|
5201 |
||
5202 |
start to:stop by:step do:[:index | |
|
8874 | 5203 |
aBlock value:(self at:index). |
7337 | 5204 |
] |
5205 |
||
5206 |
" |
|
8874 | 5207 |
#(one two three four five six seven eight nine ten) |
5208 |
from:2 |
|
5209 |
to:10 |
|
5210 |
by:2 |
|
5211 |
do:[:element | Transcript showCR:element] |
|
7337 | 5212 |
" |
5213 |
! |
|
5214 |
||
602 | 5215 |
from:start to:stop collect:aBlock |
5216 |
"evaluate the argument, aBlock for the elements indexed by start |
|
5217 |
to stop in the collection and return a collection of the results" |
|
5218 |
||
5219 |
|newCollection sz |
|
5220 |
idx "{ Class:SmallInteger }"| |
|
5221 |
||
5222 |
sz := stop - start + 1. |
|
5223 |
newCollection := self copyEmptyAndGrow:sz. |
|
5224 |
idx := 1. |
|
5225 |
start to:stop do:[:index | |
|
8874 | 5226 |
newCollection at:idx put:(aBlock value:(self at:index)). |
5227 |
idx := idx + 1 |
|
602 | 5228 |
]. |
5229 |
^ newCollection |
|
5230 |
||
5231 |
" |
|
8874 | 5232 |
#(one two three four five six) |
5233 |
from:2 |
|
5234 |
to:4 |
|
5235 |
collect:[:element | element asUppercase] |
|
602 | 5236 |
" |
5237 |
! |
|
5238 |
||
14145 | 5239 |
from:start to:end conform:aOneArgBlock |
5240 |
"return true, if the elements from start-index to end-index conform to some condition. |
|
5241 |
I.e. return false, if aBlock returns false for any of those elements; |
|
5242 |
true otherwise." |
|
5243 |
||
5244 |
self from:start to:end do:[:element | |
|
5245 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
5246 |
]. |
|
5247 |
^ true |
|
5248 |
||
5249 |
" |
|
5250 |
#(1 2 3 4 5) from:2 to:2 conform:[:el | el even] |
|
5251 |
#(1 2 2 4 5) from:2 to:4 conform:[:el | el even] |
|
5252 |
#(1 2 2 4 5) from:2 to:5 conform:[:el | el even] |
|
5253 |
#(2 4 6 8 10) conform:[:el | el even] |
|
5254 |
" |
|
5255 |
||
5256 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5257 |
"Created: / 25-05-2012 / 14:00:50 / cg" |
|
5258 |
! |
|
5259 |
||
602 | 5260 |
from:index1 to:index2 do:aBlock |
5261 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
5262 |
index2 in the collection" |
|
5263 |
||
5264 |
|start "{ Class:SmallInteger }" |
|
5265 |
stop "{ Class:SmallInteger }" | |
|
5266 |
||
7337 | 5267 |
start := index1. "/ these assignments force type checking... |
5268 |
stop := index2. "/ and guarantee inline loop code below. |
|
5269 |
||
602 | 5270 |
start to:stop do:[:index | |
13384 | 5271 |
aBlock value:(self at:index). |
602 | 5272 |
] |
5273 |
||
5274 |
" |
|
8874 | 5275 |
#(one two three four five six) |
13384 | 5276 |
from:3 to:5 do:[:element | Transcript showCR:element] |
5277 |
" |
|
5278 |
||
5279 |
"Modified: / 02-06-2011 / 13:23:06 / cg" |
|
602 | 5280 |
! |
5281 |
||
15570
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5282 |
from:index1 to:index2 doWithExit:aBlock |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5283 |
"evaluate the argument, aBlock for the elements with index index1 to |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5284 |
index2 in the collection. Pass an additional exitBlock as second argument, |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5285 |
which can be used to exit the loop early." |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5286 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5287 |
|exitBlock| |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5288 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5289 |
exitBlock := [:value | ^ value]. |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5290 |
^ self from:index1 to:index2 do:[:el | |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5291 |
aBlock value:el value:exitBlock |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5292 |
]. |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5293 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5294 |
" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5295 |
#(one two three four five six) |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5296 |
from:3 to:5 doWithExit:[:element :exit | |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5297 |
Transcript showCR:element. |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5298 |
element = 'four' ifTrue:[ exit value:nil]. |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5299 |
] |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5300 |
" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5301 |
|
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5302 |
"Created: / 28-07-2013 / 22:40:06 / cg" |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5303 |
! |
e537a8a1e428
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15535
diff
changeset
|
5304 |
|
14029
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5305 |
from:index1 to:index2 doWithIndex:aBlock |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5306 |
"Squeak/V'Age compatibility; |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5307 |
like keysAndValuesDo:, but passes the index as second argument." |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5308 |
|
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5309 |
self from:index1 to:index2 keysAndValuesDo:[:index :el | aBlock value:el value:index]. |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5310 |
|
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5311 |
"Created: / 29-02-2012 / 11:47:48 / cg" |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5312 |
! |
9fdd8832d871
added: #from:to:doWithIndex:
Claus Gittinger <cg@exept.de>
parents:
14010
diff
changeset
|
5313 |
|
6868 | 5314 |
from:index1 to:index2 keysAndValuesDo:aBlock |
5315 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
5316 |
index2 in the collection; pass both index and value." |
|
5317 |
||
5318 |
|start "{ Class:SmallInteger }" |
|
5319 |
stop "{ Class:SmallInteger }" | |
|
5320 |
||
7337 | 5321 |
start := index1. "/ these assignments force type checking... |
5322 |
stop := index2. "/ and guarantee inline loop code below. |
|
5323 |
||
6868 | 5324 |
start to:stop do:[:index | |
8874 | 5325 |
aBlock value:index value:(self at:index). |
6868 | 5326 |
] |
5327 |
||
5328 |
" |
|
8874 | 5329 |
#(one two three four five six) |
5330 |
from:3 |
|
5331 |
to:5 |
|
5332 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 5333 |
" |
5334 |
! |
|
5335 |
||
13383 | 5336 |
from:index1 to:index2 orEndDo:aBlock |
5337 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
5338 |
index2 or the end (whichever comes first) in the collection" |
|
5339 |
||
5340 |
self from:index1 to:(index2 min:self size) do:aBlock |
|
5341 |
||
5342 |
" |
|
5343 |
#(one two three four five six) |
|
13384 | 5344 |
from:3 to:10 orEndDo:[:element | Transcript showCR:element] |
13383 | 5345 |
" |
5346 |
||
5347 |
"Created: / 02-06-2011 / 13:21:32 / cg" |
|
5348 |
! |
|
5349 |
||
602 | 5350 |
from:index1 to:index2 reverseDo:aBlock |
5351 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
5352 |
index2 in the collection. Step in reverse order" |
|
5353 |
||
5354 |
|start "{ Class:SmallInteger }" |
|
5355 |
stop "{ Class:SmallInteger }" | |
|
5356 |
||
7337 | 5357 |
start := index1. "/ these assignments force type checking... |
5358 |
stop := index2. "/ and guarantee inline loop code below. |
|
5359 |
||
602 | 5360 |
stop to:start by:-1 do:[:index | |
8874 | 5361 |
aBlock value:(self at:index). |
602 | 5362 |
] |
5363 |
||
5364 |
" |
|
8874 | 5365 |
#(one two three four five six) |
5366 |
from:3 |
|
5367 |
to:5 |
|
5368 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 5369 |
" |
61 | 5370 |
! |
5371 |
||
6027 | 5372 |
from:startIndex to:stopIndex select:aBlock |
5373 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
5374 |
and return a collection of those elements for which the block return true." |
|
5375 |
||
5376 |
|element newColl species needCopy |
|
16254 | 5377 |
sz "{ Class:SmallInteger }"| |
5378 |
||
5379 |
sz := stopIndex - startIndex + 1. |
|
6027 | 5380 |
species := self species. |
14109 | 5381 |
species growIsCheap ifTrue:[ |
16254 | 5382 |
newColl := self copyEmpty:sz. |
14109 | 5383 |
needCopy := false |
14238 | 5384 |
] ifFalse:[ |
16254 | 5385 |
sz == 0 ifTrue:[ |
5386 |
^ (species new:0) postCopyFrom:self. |
|
5387 |
]. |
|
5388 |
newColl := self speciesForAdding new:sz. |
|
14109 | 5389 |
needCopy := true |
6027 | 5390 |
]. |
5391 |
startIndex to:stopIndex do:[:index | |
|
14109 | 5392 |
element := self at:index. |
5393 |
(aBlock value:element) ifTrue:[ |
|
5394 |
newColl add:element |
|
5395 |
]. |
|
6027 | 5396 |
]. |
5397 |
needCopy ifTrue:[ |
|
14109 | 5398 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 5399 |
]. |
5400 |
^ newColl |
|
5401 |
||
5402 |
" |
|
8874 | 5403 |
#(faba one two three four five six) |
14109 | 5404 |
from:2 to:5 select:[:element | element startsWith:'f'] |
6027 | 5405 |
" |
5406 |
! |
|
5407 |
||
13897 | 5408 |
from:startArg to:stopArg with:aSequenceableCollection doWithIndex:aThreeArgBlock |
5409 |
"evaluate the argument, aBlock for successive elements from |
|
5410 |
each the receiver and the argument, aSequenceableCollection. |
|
5411 |
aBlock must be a three-argument block, which get elements from either collection and |
|
5412 |
the index as arguments. |
|
5413 |
The collection argument must implement access via a numeric key." |
|
5414 |
||
5415 |
|start "{ Class: SmallInteger }" |
|
5416 |
stop "{ Class: SmallInteger }" | |
|
5417 |
||
5418 |
start := startArg. |
|
5419 |
stop := stopArg. |
|
5420 |
start to:stop do:[:index | |
|
5421 |
aThreeArgBlock |
|
5422 |
value:(self at:index) |
|
5423 |
value:(aSequenceableCollection at:index) |
|
5424 |
value:index. |
|
5425 |
] |
|
5426 |
||
5427 |
" |
|
5428 |
#(one two three four five six) |
|
5429 |
from:2 to:5 |
|
5430 |
with:(1 to:10) |
|
5431 |
doWithIndex:[:el1 :el2 :idx| Transcript show:idx; space; show:el1; space; showCR:el2] |
|
5432 |
" |
|
5433 |
||
5434 |
"Created: / 08-01-2012 / 17:18:34 / cg" |
|
5435 |
! |
|
5436 |
||
10150 | 5437 |
inGroupsOf:n collect:anNArgBlock |
5438 |
"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
|
5439 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5440 |
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
|
5441 |
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
|
5442 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5443 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5444 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5445 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5446 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5447 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5448 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5449 |
|
10150 | 5450 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
5451 |
"/ 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
|
5452 |
n == 2 ifTrue:[ |
10150 | 5453 |
1 to:stop by:n do:[:index | |
5454 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
5455 |
newCollection at:dstIdx put:rslt. |
|
5456 |
dstIdx := dstIdx + 1. |
|
5457 |
]. |
|
5458 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5459 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5460 |
n == 3 ifTrue:[ |
10150 | 5461 |
1 to:stop by:n do:[:index | |
5462 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
5463 |
newCollection at:dstIdx put:rslt. |
|
5464 |
dstIdx := dstIdx + 1. |
|
5465 |
]. |
|
5466 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5467 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5468 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5469 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5470 |
1 to:stop by:n do:[:index | |
10150 | 5471 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
5472 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
5473 |
newCollection at:dstIdx put:rslt. |
|
5474 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5475 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5476 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5477 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5478 |
"for groups of 2, this is the same as: |
8874 | 5479 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 5480 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 5481 |
|
5482 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 5483 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5484 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5485 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 5486 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
5487 |
" |
|
5488 |
||
5489 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
5490 |
! |
|
5491 |
||
5492 |
inGroupsOf:n do:anNArgBlock |
|
5493 |
"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
|
5494 |
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
|
5495 |
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
|
5496 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5497 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5498 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5499 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5500 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5501 |
|
10150 | 5502 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
5503 |
"/ 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
|
5504 |
n == 2 ifTrue:[ |
10150 | 5505 |
1 to:stop by:n do:[:index | |
5506 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
5507 |
]. |
|
5508 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5509 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5510 |
n == 3 ifTrue:[ |
10150 | 5511 |
1 to:stop by:n do:[:index | |
5512 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
5513 |
]. |
|
5514 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5515 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5516 |
n == 4 ifTrue:[ |
10150 | 5517 |
1 to:stop by:n do:[:index | |
5518 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
5519 |
]. |
|
5520 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5521 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5522 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5523 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5524 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5525 |
1 to:stop by:n do:[:index | |
10150 | 5526 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
5527 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5528 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5529 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5530 |
"for groups of 2, this is the same as: |
8874 | 5531 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 5532 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 5533 |
|
5534 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 5535 |
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
|
5536 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5537 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 5538 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
5539 |
" |
|
5540 |
||
5541 |
"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
|
5542 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5543 |
|
8853 | 5544 |
keysAndValuesCollect:aTwoArgBlock |
5545 |
"evaluate the argument, aBlock for every element in the collection, |
|
5546 |
passing both index and element as arguments. |
|
5547 |
Collect the returned values and return them." |
|
5548 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5549 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5550 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5551 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5552 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5553 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5554 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 5555 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5556 |
|
8853 | 5557 |
^ newCollection |
5558 |
||
5559 |
" |
|
8874 | 5560 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5561 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5562 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 5563 |
|
5564 |
||
5565 |
|d| |
|
5566 |
||
5567 |
d := Dictionary new. |
|
5568 |
d at:'a' put:'A'. |
|
5569 |
d at:'b' put:'B'. |
|
5570 |
d at:'c' put:'C'. |
|
5571 |
d at:'d' put:'D'. |
|
5572 |
d at:'e' put:'E'. |
|
8874 | 5573 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5574 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5575 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
5576 |
ifFalse:[element asLowercase]] |
8853 | 5577 |
" |
5578 |
! |
|
5579 |
||
14686
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5580 |
keysAndValuesConform:aTwoArgBlock |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5581 |
"evaluate the argument, aBlock for every element in the collection, |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5582 |
passing both index and element as arguments. |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5583 |
Return false if any such evaluation returns false, true otherwise." |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5584 |
|
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5585 |
self keysAndValuesDo:[:index :el | |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5586 |
(aTwoArgBlock value:index value:el) ifFalse:[^ false]. |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5587 |
]. |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5588 |
^ true |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5589 |
|
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5590 |
" |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5591 |
#(10 20 30 40) keysAndValuesConform:[:key :element | element = (key * 10) ]. |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5592 |
#(10 20 30 33 40) keysAndValuesConform:[:key :element | element = (key * 10) ]. |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5593 |
" |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5594 |
! |
9192f0a036fb
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14674
diff
changeset
|
5595 |
|
602 | 5596 |
keysAndValuesDo:aTwoArgBlock |
5597 |
"evaluate the argument, aBlock for every element in the collection, |
|
5598 |
passing both index and element as arguments." |
|
5599 |
||
5600 |
|stop "{ Class:SmallInteger }"| |
|
5601 |
||
5602 |
stop := self size. |
|
5603 |
1 to:stop do:[:index | |
|
17383
b0e93027f2cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17372
diff
changeset
|
5604 |
aTwoArgBlock value:index value:(self at:index). |
602 | 5605 |
] |
17383
b0e93027f2cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17372
diff
changeset
|
5606 |
|
602 | 5607 |
" |
8874 | 5608 |
#(one two three four five six) |
17383
b0e93027f2cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17372
diff
changeset
|
5609 |
keysAndValuesDo:[:key :element | |
b0e93027f2cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17372
diff
changeset
|
5610 |
Transcript show:key; space; showCR:element |
b0e93027f2cc
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17372
diff
changeset
|
5611 |
] |
602 | 5612 |
" |
5613 |
! |
|
5614 |
||
1359 | 5615 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 5616 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 5617 |
in the collection, passing both index and element as arguments." |
5618 |
||
5619 |
|stop "{ Class:SmallInteger }"| |
|
5620 |
||
5621 |
stop := self size. |
|
5622 |
stop to:1 by:-1 do:[:index | |
|
8874 | 5623 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 5624 |
] |
5625 |
||
5626 |
" |
|
8874 | 5627 |
#(one two three four five six) |
5628 |
keysAndValuesReverseDo:[:key :element | |
|
5629 |
Transcript show:key; space; showCR:element |
|
5630 |
] |
|
1359 | 5631 |
" |
5632 |
||
5633 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
5634 |
! |
|
5635 |
||
13043 | 5636 |
keysDo:aBlock |
5637 |
"evaluate the argument, aBlock for every key in the collection. |
|
5638 |
That is: enumerate the indices. |
|
5639 |
Here mostly for protocol compatibility." |
|
5640 |
||
13792
5d34db2e46bf
comment/format in: #keysDo:
Claus Gittinger <cg@exept.de>
parents:
13719
diff
changeset
|
5641 |
1 to:(self size) do:aBlock |
13043 | 5642 |
|
5643 |
" |
|
5644 |
#(one two three four five six) |
|
5645 |
keysDo:[:key | Transcript show:key; space; showCR:element |
|
5646 |
] |
|
5647 |
" |
|
5648 |
||
5649 |
"Created: / 24-08-2010 / 10:11:23 / cg" |
|
5650 |
! |
|
5651 |
||
602 | 5652 |
nonNilElementsDo:aBlock |
5653 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
5654 |
||
5655 |
|stop "{ Class:SmallInteger }" |
|
5656 |
element| |
|
5657 |
||
5658 |
stop := self size. |
|
5659 |
1 to:stop do:[:index | |
|
8874 | 5660 |
(element := self at:index) notNil ifTrue:[ |
5661 |
aBlock value:element. |
|
5662 |
] |
|
602 | 5663 |
] |
5664 |
" |
|
1422 | 5665 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 5666 |
" |
5667 |
! |
|
5668 |
||
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5669 |
overlappingPairsCollect: aTwoArgBlock |
18549
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5670 |
"Answer the result of evaluating aBlock with all of the overlapping pairs of my elements. |
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5671 |
Returns an array, because the block may return any object |
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5672 |
(eg, if applied to a string, the returned collection could contain anything)" |
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5673 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5674 |
| mySize "{ Class: SmallInteger }" retval | |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5675 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5676 |
mySize := self size. |
18549
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5677 |
retval := Array ofSize: mySize - 1. |
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5678 |
1 to: mySize - 1 do: [:i | |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5679 |
retval at: i put: (aTwoArgBlock value: (self at: i) value: (self at: i + 1)) |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5680 |
]. |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5681 |
^ retval |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5682 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5683 |
" |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5684 |
#(1 2 3 4) overlappingPairsCollect: [:a :b | a->b] |
18549
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5685 |
|
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5686 |
'hello world how nice' overlappingPairsCollect: [:a :b | a,b] |
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5687 |
|
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5688 |
(('hello worlld aa bb' overlappingPairsCollect: [:a :b | a,b]) |
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5689 |
asBag select:[:p | p asSet size = 1]) |
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
5690 |
valuesSortedByCounts first |
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5691 |
" |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5692 |
! |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5693 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5694 |
overlappingPairsDo: aTwoArgBlock |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5695 |
"Evaluate aBlock with all of the overlapping pairs of my elements." |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5696 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5697 |
|mySize "{ Class: SmallInteger }" | |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5698 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5699 |
mySize := self size. |
16173
2a98fe2d4cef
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16171
diff
changeset
|
5700 |
1 to: mySize-1 do: [:idx | |
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5701 |
aTwoArgBlock value: (self at: idx) value: (self at: idx + 1) |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5702 |
] |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5703 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5704 |
" |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5705 |
#(1 2 3 4) overlappingPairsDo: [:a :b | Transcript show:a; show:' '; showCR:b] |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5706 |
" |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5707 |
! |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5708 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5709 |
overlappingPairsWithIndexDo: aThreeArgBlock |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5710 |
"Evaluate aBlock with all of the overlapping pairs of my elements plus the index." |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5711 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5712 |
|mySize "{ Class: SmallInteger }" | |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5713 |
|
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5714 |
mySize := self size. |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5715 |
1 to: mySize - 1 do: [:idx | |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5716 |
aThreeArgBlock value: (self at: idx) value: (self at: idx + 1) value: idx |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5717 |
] |
16487
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5718 |
|
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5719 |
" |
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5720 |
#(1 2 3 4) overlappingPairsWithIndexDo: [:a :b :idx | |
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5721 |
Transcript show:idx; show:': '; show:a; show:' '; showCR:b |
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5722 |
] |
0fb984d92c6e
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16456
diff
changeset
|
5723 |
" |
16171
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5724 |
! |
886b698e5f10
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16152
diff
changeset
|
5725 |
|
10150 | 5726 |
pairWiseCollect:aTwoArgBlock |
5727 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 5728 |
The block is called with 2 arguments for each 2 elements in the receiver. |
5729 |
An error will be reported, if the number of elements in the receiver |
|
5730 |
is not a multiple of 2. |
|
5731 |
Collect the results and return a new collection containing those." |
|
5732 |
||
10150 | 5733 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 5734 |
|
95 | 5735 |
" |
8874 | 5736 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 5737 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 5738 |
|
5739 |
||
5740 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 5741 |
pairWiseCollect:[:x :y | x@y] |
5742 |
" |
|
5743 |
||
5744 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
5745 |
! |
|
5746 |
||
5747 |
pairWiseDo:aTwoArgBlock |
|
10385 | 5748 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
5749 |
An error will be reported, if the number of elements in the receiver |
|
12346 | 5750 |
is not a multiple of 2. |
5751 |
CONFUSION ATTACK: |
|
16488
9ffcb1851e82
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16487
diff
changeset
|
5752 |
this is different from pairsDo:. |
12346 | 5753 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
5754 |
(sigh: but we were first, so they should have adapted...)" |
|
602 | 5755 |
|
10150 | 5756 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
5757 |
|
602 | 5758 |
" |
8874 | 5759 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 5760 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 5761 |
|
5762 |
||
5763 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 5764 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
5765 |
" |
|
5766 |
||
10385 | 5767 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 5768 |
! |
5769 |
||
602 | 5770 |
reverseDo:aBlock |
5771 |
"evaluate the argument, aBlock for every element in the collection |
|
5772 |
in reverse order" |
|
5773 |
||
5774 |
|sz "{ Class:SmallInteger }"| |
|
5775 |
||
5776 |
sz := self size. |
|
5777 |
sz to:1 by:-1 do:[:index | |
|
8874 | 5778 |
aBlock value:(self at:index). |
602 | 5779 |
] |
5780 |
||
5781 |
" |
|
1422 | 5782 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 5783 |
" |
5784 |
! |
|
5785 |
||
5786 |
select:aBlock |
|
5787 |
"evaluate the argument, aBlock for every element in the collection |
|
5788 |
and return a collection of all elements for which the block return |
|
5789 |
true" |
|
5790 |
||
5791 |
|element newColl species needCopy |
|
5792 |
sz "{ Class:SmallInteger }"| |
|
5793 |
||
5794 |
sz := self size. |
|
5795 |
species := self species. |
|
14109 | 5796 |
species growIsCheap ifTrue:[ |
5797 |
newColl := self copyEmpty:sz. |
|
5798 |
needCopy := false |
|
5799 |
] ifFalse:[ |
|
15097 | 5800 |
sz == 0 ifTrue:[ |
5801 |
^ (species new:0) postCopyFrom:self. |
|
5802 |
]. |
|
5803 |
newColl := self speciesForAdding new:sz. |
|
14109 | 5804 |
needCopy := true |
602 | 5805 |
]. |
5806 |
1 to:sz do:[:index | |
|
14109 | 5807 |
element := self at:index. |
5808 |
(aBlock value:element) ifTrue:[ |
|
5809 |
newColl add:element |
|
5810 |
]. |
|
602 | 5811 |
]. |
5812 |
needCopy ifTrue:[ |
|
14109 | 5813 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 5814 |
]. |
61 | 5815 |
^ newColl |
5816 |
||
95 | 5817 |
" |
8874 | 5818 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
5819 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
15097 | 5820 |
#() select:[:element | element odd] |
8874 | 5821 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
5822 |
(1 to:9) select:[:element | element odd] |
|
5823 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 5824 |
" |
186 | 5825 |
! |
5826 |
||
16069
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5827 |
slicesOf:n collect:aOneArgBlock |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5828 |
"evaluate the argument, aOneArg for every slice of n elements of the collection, |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5829 |
and collect the results as instances of targetContainerClass. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5830 |
The block is called with n element subcollections for groups of n consecutive elements in the receiver. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5831 |
If the number of elements in the receiver is not a multiple of n, the last block evaluation will |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5832 |
get a short slice as argument" |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5833 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5834 |
|out| |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5835 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5836 |
out := self species writeStream. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5837 |
self slicesOf:n do:[:slice | out nextPut:(aOneArgBlock value:slice)]. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5838 |
^ out contents. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5839 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5840 |
": |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5841 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5842 |
slicesOf:2 collect:[:slice | slice map:#asString] |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5843 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5844 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5845 |
slicesOf:5 collect:[:slice | slice map:#asString] |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5846 |
" |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5847 |
! |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5848 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5849 |
slicesOf:n do:aOneArgBlock |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5850 |
"evaluate the argument, aOneArg for every slice of n elements of the collection. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5851 |
The block is called with n element subcollections for groups of n consecutive elements in the receiver. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5852 |
If the number of elements in the receiver is not a multiple of n, the last block evaluation will |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5853 |
get a short slice as argument" |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5854 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5855 |
|i stop| |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5856 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5857 |
i := 1. stop := self size. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5858 |
[i <= stop ] whileTrue:[ |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5859 |
aOneArgBlock value:(self copyFrom:i to:(i+n-1 min:stop)). |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5860 |
i := i + n. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5861 |
]. |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5862 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5863 |
": |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5864 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5865 |
slicesOf:2 do:[:slice | Transcript showCR:slice] |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5866 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5867 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5868 |
slicesOf:5 do:[:slice | Transcript showCR:slice] |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5869 |
|
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5870 |
bad size: |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5871 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21) |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5872 |
slicesOf:5 do:[:slice | Transcript showCR:slice] |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5873 |
" |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5874 |
! |
8a173f8b0a29
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16065
diff
changeset
|
5875 |
|
602 | 5876 |
with:aSequenceableCollection do:aTwoArgBlock |
5877 |
"evaluate the argument, aBlock for successive elements from |
|
5878 |
each the receiver and the argument, aSequenceableCollection. |
|
5879 |
The second argument, aBlock must be a two-argument block. |
|
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
5880 |
The collection argument must implement access via a numeric key |
19026 | 5881 |
and the sizes must be the same (this is a new check!!)." |
602 | 5882 |
|
5883 |
|stop "{ Class: SmallInteger }" | |
|
5884 |
||
5885 |
stop := self size. |
|
13465 | 5886 |
aSequenceableCollection size == stop ifFalse:[ |
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
5887 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
13465 | 5888 |
]. |
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
5889 |
|
602 | 5890 |
1 to:stop do:[:index | |
13465 | 5891 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 5892 |
] |
13465 | 5893 |
|
186 | 5894 |
" |
8874 | 5895 |
#(one two three four five six) |
19026 | 5896 |
with:(1 to:6) |
13465 | 5897 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
5898 |
" |
|
5899 |
||
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
5900 |
"Modified (Format): / 30-06-2011 / 17:39:59 / cg" |
9454 | 5901 |
! |
5902 |
||
5903 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
5904 |
"evaluate the argument, aBlock for successive elements from |
|
5905 |
each the receiver and the argument, aSequenceableCollection. |
|
5906 |
aBlock must be a three-argument block, which get elements from either collection and |
|
5907 |
the index as arguments. |
|
19026 | 5908 |
The collection argument must implement access via a numeric key. |
5909 |
and the sizes must be the same (this is a new check!!)." |
|
9454 | 5910 |
|
5911 |
|stop "{ Class: SmallInteger }" | |
|
5912 |
||
5913 |
stop := self size. |
|
19026 | 5914 |
aSequenceableCollection size == stop ifFalse:[ |
5915 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
|
5916 |
]. |
|
9454 | 5917 |
1 to:stop do:[:index | |
5918 |
aThreeArgBlock |
|
5919 |
value:(self at:index) |
|
5920 |
value:(aSequenceableCollection at:index) |
|
5921 |
value:index. |
|
5922 |
] |
|
13897 | 5923 |
|
9454 | 5924 |
" |
5925 |
#(one two three four five six) |
|
5926 |
with:(1 to:10) |
|
13897 | 5927 |
doWithIndex:[:el1 :el2 :idx| Transcript show:idx; space; show:el1; space; showCR:el2] |
9454 | 5928 |
" |
5929 |
||
5930 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
13897 | 5931 |
"Modified (comment): / 08-01-2012 / 17:18:59 / cg" |
19026 | 5932 |
! |
5933 |
||
5934 |
with:collection2 with:collection3 do:aThreeArgBlock |
|
5935 |
"evaluate the argument, aBlock for successive elements from |
|
5936 |
each the receiver and the collection arguments. |
|
5937 |
The last argument, aBlock must be a three-argument block. |
|
5938 |
The collection arguments must implement access via a numeric key |
|
5939 |
and the sizes must be the same." |
|
5940 |
||
5941 |
|stop "{ Class: SmallInteger }" | |
|
5942 |
||
5943 |
stop := self size. |
|
5944 |
(collection2 size == stop and:[collection3 size == stop]) ifFalse:[ |
|
5945 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
|
5946 |
]. |
|
5947 |
||
5948 |
1 to:stop do:[:index | |
|
5949 |
aThreeArgBlock |
|
5950 |
value:(self at:index) |
|
5951 |
value:(collection2 at:index) |
|
5952 |
value:(collection3 at:index). |
|
5953 |
] |
|
5954 |
||
5955 |
" |
|
5956 |
#(one two three four five six) |
|
5957 |
with:(1 to:6) |
|
5958 |
with:(100 to:600 by:100) |
|
5959 |
do:[:el1 :el2 :el3 | Transcript show:el1; space; show:el2; space; showCR:el3] |
|
5960 |
" |
|
5961 |
||
5962 |
"Modified (Format): / 30-06-2011 / 17:39:59 / cg" |
|
1 | 5963 |
! ! |
5964 |
||
4154 | 5965 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 5966 |
|
602 | 5967 |
atAllPut:anObject |
5968 |
"replace all elements of the collection by the argument, anObject. |
|
5969 |
Notice: This operation modifies the receiver, NOT a copy; |
|
5970 |
therefore the change may affect all others referencing the receiver." |
|
5971 |
||
5972 |
self from:1 to:(self size) put:anObject |
|
5973 |
||
5974 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5975 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5976 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5977 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5978 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5979 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5980 |
" |
602 | 5981 |
(Array new:10) atAllPut:1 |
5982 |
(String new:10) atAllPut:$a |
|
5983 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5984 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5985 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 5986 |
! |
5987 |
||
1 | 5988 |
from:index1 to:index2 put:anObject |
5989 |
"replace the elements from index1 to index2 of the collection |
|
44 | 5990 |
by the argument, anObject. |
5991 |
Notice: This operation modifies the receiver, NOT a copy; |
|
5992 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5993 |
|
5994 |
|index "{ Class: SmallInteger }" |
|
5995 |
end "{ Class: SmallInteger }"| |
|
5996 |
||
5997 |
index := index1. |
|
5998 |
end := index2. |
|
5999 |
[index <= end] whileTrue:[ |
|
8874 | 6000 |
self at:index put:anObject. |
6001 |
index := index + 1 |
|
1 | 6002 |
] |
44 | 6003 |
|
95 | 6004 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6005 |
args: index1 : <integer> |
8874 | 6006 |
index2 : <integer> |
6007 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6008 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6009 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6010 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6011 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6012 |
" |
95 | 6013 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
6014 |
'abcdefghijkl' from:3 to:6 put:$X |
|
6015 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6016 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6017 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 6018 |
! |
6019 |
||
3003 | 6020 |
replaceAll:oldObject by:newObject |
6021 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
6022 |
Pleace do no longer use this one; |
|
6023 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
6024 |
||
5872 | 6025 |
<resource:#obsolete> |
6026 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6027 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 6028 |
^ self replaceAll:oldObject with:newObject |
6029 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6030 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6031 |
args: oldObject : <object> |
8874 | 6032 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6033 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6034 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6035 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6036 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6037 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 6038 |
! |
6039 |
||
6040 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
6041 |
"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
|
6042 |
Please do no longer use this one; |
3003 | 6043 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
6044 |
||
5872 | 6045 |
<resource:#obsolete> |
6046 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6047 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 6048 |
^ 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
|
6049 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6050 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6051 |
args: oldObject : <object> |
8874 | 6052 |
newObject : <object> |
6053 |
startIndex : <integer> |
|
6054 |
stopIndex : <integer> |
|
6055 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6056 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6057 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6058 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6059 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 6060 |
! |
6061 |
||
2927 | 6062 |
replaceAll:oldObject with:newObject |
44 | 6063 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6064 |
|
44 | 6065 |
Notice: This operation modifies the receiver, NOT a copy; |
6066 |
therefore the change may affect all others referencing the receiver." |
|
1 | 6067 |
|
8874 | 6068 |
^ self |
6069 |
replaceAll:oldObject |
|
6070 |
with:newObject |
|
6071 |
from:1 |
|
6072 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6073 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6074 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6075 |
args: oldObject : <object> |
8874 | 6076 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6077 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6078 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6079 |
" |
362 | 6080 |
|
6081 |
" |
|
8874 | 6082 |
'123123abc123' replaceAll:$1 with:$* |
6083 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 6084 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6085 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6086 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 6087 |
! |
293 | 6088 |
|
2927 | 6089 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 6090 |
"replace all oldObjects found between startIndex and endIndex, |
6091 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6092 |
|
362 | 6093 |
Notice: This operation modifies the receiver, NOT a copy; |
6094 |
therefore the change may affect all others referencing the receiver." |
|
6095 |
||
6096 |
|start "{ Class: SmallInteger }" |
|
6097 |
stop "{ Class: SmallInteger }"| |
|
6098 |
||
6099 |
start := startIndex. |
|
6100 |
stop := stopIndex. |
|
6101 |
startIndex to:stopIndex do:[:index | |
|
8874 | 6102 |
(self at:index) = oldObject ifTrue:[ |
6103 |
self at:index put:newObject |
|
6104 |
] |
|
1 | 6105 |
] |
44 | 6106 |
|
95 | 6107 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6108 |
args: oldObject : <object> |
8874 | 6109 |
newObject : <object> |
6110 |
startIndex : <integer> |
|
6111 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6112 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6113 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6114 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6115 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6116 |
" |
2980 | 6117 |
'123123abc123' replaceAll:$1 with:$* |
6118 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
6119 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 6120 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6121 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6122 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 6123 |
! |
6124 |
||
7615 | 6125 |
replaceAllForWhich:aConditionBlock with:newObject |
6126 |
"replace all elements for which aConditionBlock returns true |
|
6127 |
between startIndex and endIndex, by newObject in the receiver. |
|
6128 |
||
6129 |
Notice: This operation modifies the receiver, NOT a copy; |
|
6130 |
therefore the change may affect all others referencing the receiver." |
|
6131 |
||
8874 | 6132 |
self |
6133 |
replaceAllForWhich:aConditionBlock with:newObject |
|
6134 |
from:1 to:(self size) |
|
7615 | 6135 |
|
6136 |
" |
|
6137 |
args: aConditionBlock : <block> |
|
8874 | 6138 |
newObject : <object> |
7615 | 6139 |
|
6140 |
returns: self |
|
6141 |
" |
|
6142 |
! |
|
6143 |
||
6144 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
6145 |
"replace all elements for which aConditionBlock returns true |
|
6146 |
between startIndex and endIndex, by newObject in the receiver. |
|
6147 |
||
6148 |
Notice: This operation modifies the receiver, NOT a copy; |
|
6149 |
therefore the change may affect all others referencing the receiver." |
|
6150 |
||
6151 |
|start "{ Class: SmallInteger }" |
|
6152 |
stop "{ Class: SmallInteger }"| |
|
6153 |
||
6154 |
start := startIndex. |
|
6155 |
stop := stopIndex. |
|
6156 |
startIndex to:stopIndex do:[:index | |
|
8874 | 6157 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
6158 |
self at:index put:newObject |
|
6159 |
] |
|
7615 | 6160 |
] |
6161 |
||
6162 |
" |
|
6163 |
args: aConditionBlock : <block> |
|
8874 | 6164 |
newObject : <object> |
6165 |
startIndex : <integer> |
|
6166 |
stopIndex : <integer> |
|
7615 | 6167 |
|
6168 |
returns: self |
|
6169 |
" |
|
6170 |
! |
|
6171 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6172 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6173 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6174 |
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
|
6175 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6176 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6177 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6178 |
|
8874 | 6179 |
^ self |
6180 |
replaceAllIdentical:oldObject |
|
6181 |
with:newObject |
|
6182 |
from:1 |
|
6183 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6184 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6185 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6186 |
args: oldObject : <object> |
8874 | 6187 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6188 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6189 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6190 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6191 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6192 |
" |
8874 | 6193 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
6194 |
#(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
|
6195 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6196 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6197 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6198 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6199 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6200 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6201 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6202 |
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
|
6203 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6204 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6205 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6206 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6207 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6208 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6209 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6210 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6211 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6212 |
startIndex to:stopIndex do:[:index | |
8874 | 6213 |
(self at:index) == oldObject ifTrue:[ |
6214 |
self at:index put:newObject |
|
6215 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6216 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6217 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6218 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6219 |
args: oldObject : <object> |
8874 | 6220 |
newObject : <object> |
6221 |
startIndex : <integer> |
|
6222 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6223 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6224 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6225 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6226 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6227 |
" |
8874 | 6228 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
6229 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
6230 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 6231 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6232 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
6233 |
|
3003 | 6234 |
replaceAny:aCollection by:newObject |
6235 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 6236 |
Pleace do no longer use this one; |
3003 | 6237 |
instead use #replaceAny:with: for naming consistence." |
6238 |
||
5872 | 6239 |
<resource:#obsolete> |
6240 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6241 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 6242 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 6243 |
|
6244 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6245 |
args: aCollection : <colleciton of <object> > |
8874 | 6246 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6247 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6248 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6249 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6250 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6251 |
" |
3003 | 6252 |
'123123abc123' replaceAny:#($1 $2) with:$* |
6253 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
6254 |
" |
|
6255 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6256 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 6257 |
! |
6258 |
||
3003 | 6259 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
6260 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 6261 |
Pleace do no longer use this one; |
3003 | 6262 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 6263 |
|
5872 | 6264 |
<resource:#obsolete> |
6265 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6266 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 6267 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 6268 |
|
6269 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6270 |
args: aCollection : <colleciton of <object> > |
8874 | 6271 |
newObject : <object> |
6272 |
startIndex : <integer> |
|
6273 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6274 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6275 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6276 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6277 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6278 |
" |
8874 | 6279 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
6280 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 6281 |
" |
6282 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 6284 |
! |
6285 |
||
6286 |
replaceAny:aCollection with:newObject |
|
6287 |
"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
|
6288 |
|
2927 | 6289 |
Notice: This operation modifies the receiver, NOT a copy; |
6290 |
therefore the change may affect all others referencing the receiver." |
|
6291 |
||
8874 | 6292 |
^ self |
10439 | 6293 |
replaceAny:aCollection |
6294 |
with:newObject |
|
6295 |
from:1 |
|
6296 |
to:self size |
|
6297 |
||
6298 |
" |
|
6299 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
6300 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
6301 |
" |
|
6302 |
||
6303 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 6304 |
! |
6305 |
||
2927 | 6306 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 6307 |
"replace all elements within a range, |
6308 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6309 |
|
362 | 6310 |
Notice: This operation modifies the receiver, NOT a copy; |
6311 |
therefore the change may affect all others referencing the receiver." |
|
6312 |
||
6313 |
|start "{ Class: SmallInteger }" |
|
6314 |
stop "{ Class: SmallInteger }"| |
|
6315 |
||
6316 |
start := startIndex. |
|
6317 |
stop := stopIndex. |
|
6318 |
startIndex to:stopIndex do:[:index | |
|
8874 | 6319 |
(aCollection includes:(self at:index)) ifTrue:[ |
6320 |
self at:index put:newObject |
|
6321 |
] |
|
95 | 6322 |
] |
6323 |
||
6324 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6325 |
args: aCollection : <colleciton of <object> > |
8874 | 6326 |
newObject : <object> |
6327 |
startIndex : <integer> |
|
6328 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6329 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6332 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6333 |
" |
8874 | 6334 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
6335 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 6336 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6338 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 6339 |
! |
6340 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6341 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6342 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6343 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6344 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6345 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6346 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6347 |
|
8874 | 6348 |
^ self |
6349 |
replaceFrom:startIndex |
|
6350 |
to:(startIndex + numberOfElements - 1) |
|
6351 |
with:replacementCollection |
|
6352 |
startingAt:1 |
|
6353 |
||
6354 |
" |
|
6355 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6356 |
#($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
|
6357 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6358 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6359 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6360 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6361 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6362 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6363 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6364 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6365 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6366 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6367 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6368 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6369 |
^ self |
8874 | 6370 |
replaceFrom:startIndex |
6371 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
6372 |
with:replacementCollection |
|
6373 |
startingAt:repStartIndex |
|
6374 |
||
6375 |
" |
|
6376 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
6377 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
6378 |
#($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
|
6379 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6380 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6381 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6382 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
6383 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6385 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 6386 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
|
44 | 6388 |
Notice: This operation modifies the receiver, NOT a copy; |
6389 |
therefore the change may affect all others referencing the receiver." |
|
1 | 6390 |
|
8874 | 6391 |
^ self |
6392 |
replaceFrom:startIndex |
|
6393 |
to:stopIndex |
|
6394 |
with:replacementCollection |
|
6395 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6396 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6397 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6398 |
args: startIndex : <integer> |
8874 | 6399 |
stopIndex : <integer> |
6400 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6401 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6402 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6403 |
" |
61 | 6404 |
|
95 | 6405 |
" |
6406 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
6407 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
6408 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6409 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6410 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 6411 |
! |
6412 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6413 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6414 |
"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
|
6415 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 6416 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6417 |
|
44 | 6418 |
Notice: This operation modifies the receiver, NOT a copy; |
6419 |
therefore the change may affect all others referencing the receiver." |
|
1 | 6420 |
|
6421 |
|srcIndex "{ Class: SmallInteger }" |
|
6422 |
dstIndex "{ Class: SmallInteger }" |
|
6423 |
end "{ Class: SmallInteger }" | |
|
6424 |
||
14130
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6425 |
replacementCollection == self ifTrue:[ |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6426 |
repStartIndex == startIndex ifTrue:[ "noting to copy" ^ self ]. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6427 |
|
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6428 |
"beware the overlapping copy" |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6429 |
(repStartIndex < startIndex) ifTrue:[ |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6430 |
" must do reverse copy " |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6431 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6432 |
dstIndex := stopIndex. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6433 |
end := startIndex. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6434 |
[dstIndex >= end] whileTrue:[ |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6435 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6436 |
srcIndex := srcIndex - 1. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6437 |
dstIndex := dstIndex - 1 |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6438 |
]. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6439 |
^ self |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6440 |
]. |
1 | 6441 |
]. |
6442 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6443 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6444 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6445 |
end := stopIndex. |
1 | 6446 |
[dstIndex <= end] whileTrue:[ |
14130
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6447 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6448 |
srcIndex := srcIndex + 1. |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6449 |
dstIndex := dstIndex + 1 |
1 | 6450 |
] |
61 | 6451 |
|
95 | 6452 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6453 |
args: startIndex : <integer> |
14130
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6454 |
stopIndex : <integer> |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6455 |
replacementCollection : <colleciton of <object> > |
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6456 |
repStartIndex : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6457 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6458 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6459 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6460 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6461 |
" |
8874 | 6462 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
6463 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
6464 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 6465 |
|
6466 |
|c| |
|
6467 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 6468 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 6469 |
|
6470 |
|c| |
|
6471 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 6472 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 6473 |
" |
3234 | 6474 |
|
14130
30e79d420a43
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14109
diff
changeset
|
6475 |
"Modified: / 08-05-2012 / 13:23:51 / cg" |
61 | 6476 |
! |
6477 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6478 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6479 |
"replace elements in the receiver starting at startIndex, |
602 | 6480 |
with elements taken from replacementCollection starting at 1 |
6481 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6482 |
|
602 | 6483 |
Notice: This operation modifies the receiver, NOT a copy; |
6484 |
therefore the change may affect all others referencing the receiver." |
|
6485 |
||
8874 | 6486 |
^ self |
6487 |
replaceFrom:startIndex |
|
6488 |
to:(startIndex + replacementCollection size - 1) |
|
6489 |
with:replacementCollection |
|
6490 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6491 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6492 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6493 |
args: startIndex : <integer> |
8874 | 6494 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6495 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6496 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6497 |
" |
602 | 6498 |
|
6499 |
" |
|
6500 |
'1234567890' replaceFrom:5 with:'abc' |
|
6501 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
6502 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6503 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6504 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 6505 |
! |
6506 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6507 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6508 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6509 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 6510 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6511 |
|
602 | 6512 |
Notice: This operation modifies the receiver, NOT a copy; |
6513 |
therefore the change may affect all others referencing the receiver." |
|
6514 |
||
8874 | 6515 |
^ self |
6516 |
replaceFrom:startIndex |
|
6517 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
6518 |
with:replacementCollection |
|
6519 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6520 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6522 |
args: startIndex : <integer> |
8874 | 6523 |
replacementCollection : <colleciton of <object> > |
6524 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6525 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6526 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6527 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6528 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6529 |
" |
8874 | 6530 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 6531 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
6532 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6533 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6534 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 6535 |
! |
6536 |
||
14621
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6537 |
replaceLast:count with:replacementCollection |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6538 |
"replace the last count elements elements in the receiver |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6539 |
with elements taken from replacementCollection. |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6540 |
Return the receiver. |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6541 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6542 |
Notice: This operation modifies the receiver, NOT a copy; |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6543 |
therefore the change may affect all others referencing the receiver." |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6544 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6545 |
self replaceLast:count with:replacementCollection startingAt:1 |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6546 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6547 |
" |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6548 |
'1234567890' replaceLast:5 with:'abcdef' |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6549 |
" |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6550 |
! |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6551 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6552 |
replaceLast:count with:replacementCollection startingAt:repStartIndex |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6553 |
"replace the last count elements elements in the receiver |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6554 |
with elements taken from replacementCollection starting at repStartIndex. |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6555 |
Return the receiver. |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6556 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6557 |
Notice: This operation modifies the receiver, NOT a copy; |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6558 |
therefore the change may affect all others referencing the receiver." |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6559 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6560 |
|sz| |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6561 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6562 |
sz := self size. |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6563 |
self replaceFrom:(sz - count + 1) to:sz with:replacementCollection startingAt:repStartIndex |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6564 |
|
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6565 |
" |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6566 |
'1234567890' replaceLast:5 with:'abcdef' startingAt:1 |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6567 |
" |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6568 |
! |
7f004e0e8998
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14613
diff
changeset
|
6569 |
|
61 | 6570 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
6571 |
"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
|
6572 |
|
61 | 6573 |
Notice: This operation modifies the destination collection, NOT a copy; |
6574 |
therefore the change may affect all others referencing this object." |
|
6575 |
||
8874 | 6576 |
destColl |
6577 |
replaceFrom:destStartIndex |
|
6578 |
to:destEndIndex |
|
6579 |
with:self |
|
6580 |
startingAt:sourceStart |
|
61 | 6581 |
|
6582 |
" |
|
6583 |
|s| |
|
6584 |
s := 'abcdefghijklmnop'. |
|
6585 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
6586 |
s'123defghijklmnop' |
|
6587 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6588 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6589 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 6590 |
! ! |
6591 |
||
14102 | 6592 |
!SequenceableCollection methodsFor:'obsolete'! |
6593 |
||
6594 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
6595 |
"actual randomizedQuicksort worker for sort:with:-message. |
|
6596 |
This exchanges a random element within the partition, to avoid |
|
6597 |
the worst case O-square situation of quickSort. |
|
6598 |
||
6599 |
Notice, that this method has a much worse best- and average case |
|
6600 |
runTime, due to the random number generation. |
|
6601 |
The worst case of quickSort is encountered, if the choosen pivot |
|
6602 |
element lies at either end of the partition, so that a split |
|
6603 |
creates partitions of size 1 and (n-1). |
|
6604 |
Since the middle element is choosen, this worst case is very unlikely |
|
6605 |
to be encountered." |
|
6606 |
||
6607 |
<resource:#obsolete> |
|
6608 |
||
6609 |
self obsoleteMethodWarning:'use quickSortFrom:to:sortBlock:with:'. |
|
6610 |
||
6611 |
"/ code was never used - use regular sort for backward compatibility |
|
6612 |
^ self quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
6613 |
||
6614 |
||
6615 |
"/ |begin "{ Class: SmallInteger }" |
|
6616 |
"/ end "{ Class: SmallInteger }" |
|
6617 |
"/ b "{ Class: SmallInteger }" |
|
6618 |
"/ e "{ Class: SmallInteger }" |
|
6619 |
"/ rnd "{ Class: SmallInteger }" |
|
6620 |
"/ middleElement temp1 temp2 | |
|
6621 |
"/ |
|
6622 |
"/ begin := inBegin. "/ this also does a type-check |
|
6623 |
"/ end := inEnd. |
|
6624 |
"/ |
|
6625 |
"/ b := begin. |
|
6626 |
"/ e := end. |
|
6627 |
"/ |
|
6628 |
"/ "/ randomize that partition |
|
6629 |
"/ "/ by exchanging the first element with any random |
|
6630 |
"/ "/ element. |
|
6631 |
"/ rnd := Random nextIntegerBetween:b and:e. |
|
6632 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
|
6633 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
|
6634 |
"/ aCollection notNil ifTrue:[ |
|
6635 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
|
6636 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
|
6637 |
"/ ]. |
|
6638 |
"/ |
|
6639 |
"/ "/ |
|
6640 |
"/ "/ now proceed as usual |
|
6641 |
"/ "/ |
|
6642 |
"/ middleElement := self at:((b + e) // 2). |
|
6643 |
"/ |
|
6644 |
"/ [b < e] whileTrue:[ |
|
6645 |
"/ [b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
6646 |
"/ [e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
6647 |
"/ |
|
6648 |
"/ (b <= e) ifTrue:[ |
|
6649 |
"/ (b == e) ifFalse:[ |
|
6650 |
"/ temp1 := self at:b. temp2 := self at:e. |
|
6651 |
"/ self at:b put:temp2. self at:e put:temp1. |
|
6652 |
"/ aCollection notNil ifTrue:[ |
|
6653 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
6654 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
6655 |
"/ ]. |
|
6656 |
"/ ]. |
|
6657 |
"/ b := b + 1. |
|
6658 |
"/ e := e - 1 |
|
6659 |
"/ ] |
|
6660 |
"/ ]. |
|
6661 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
6662 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
6663 |
! |
|
6664 |
||
6665 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
|
6666 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
|
6667 |
This exchanges a random element within the partition, to avoid |
|
6668 |
the worst case O-square situation of quickSort. |
|
6669 |
||
6670 |
Notice, that this method has a much worse best- and average case |
|
6671 |
runTime, due to the random number generation. |
|
6672 |
The worst case of quickSort is encountered, if the choosen pivot |
|
6673 |
element lies at either end of the partition, so that a split |
|
6674 |
creates partitions of size 1 and (n-1). |
|
6675 |
Since the middle element is choosen, this worst case is very unlikely |
|
6676 |
to be encountered." |
|
6677 |
||
6678 |
<resource:#obsolete> |
|
6679 |
||
6680 |
self obsoleteMethodWarning:'use quickSortFrom:to:with:'. |
|
6681 |
"/ code was never used - use regular sort for backward compatibility |
|
6682 |
^ self quickSortFrom:inBegin to:inEnd with:aCollection |
|
6683 |
||
6684 |
||
6685 |
"/ |begin "{ Class: SmallInteger }" |
|
6686 |
"/ end "{ Class: SmallInteger }" |
|
6687 |
"/ b "{ Class: SmallInteger }" |
|
6688 |
"/ e "{ Class: SmallInteger }" |
|
6689 |
"/ rnd "{ Class: SmallInteger }" |
|
6690 |
"/ middleElement temp1 temp2 | |
|
6691 |
"/ |
|
6692 |
"/ begin := inBegin. "/ this also does a type-check |
|
6693 |
"/ end := inEnd. |
|
6694 |
"/ |
|
6695 |
"/ b := begin. |
|
6696 |
"/ e := end. |
|
6697 |
"/ |
|
6698 |
"/ "/ randomize that partition |
|
6699 |
"/ "/ by exchanging the first element with any random |
|
6700 |
"/ "/ element. |
|
6701 |
"/ rnd := Random nextIntegerBetween:b and:e. |
|
6702 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
|
6703 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
|
6704 |
"/ aCollection notNil ifTrue:[ |
|
6705 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
|
6706 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
|
6707 |
"/ ]. |
|
6708 |
"/ |
|
6709 |
"/ "/ |
|
6710 |
"/ "/ now proceed as usual |
|
6711 |
"/ "/ |
|
6712 |
"/ middleElement := self at:((b + e) // 2). |
|
6713 |
"/ |
|
6714 |
"/ [b < e] whileTrue:[ |
|
6715 |
"/ [b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
6716 |
"/ [e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
6717 |
"/ |
|
6718 |
"/ (b <= e) ifTrue:[ |
|
6719 |
"/ (b == e) ifFalse:[ |
|
6720 |
"/ temp1 := self at:b. temp2 := self at:e. |
|
6721 |
"/ self at:b put:temp2. self at:e put:temp1. |
|
6722 |
"/ aCollection notNil ifTrue:[ |
|
6723 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
6724 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
6725 |
"/ ]. |
|
6726 |
"/ ]. |
|
6727 |
"/ b := b + 1. |
|
6728 |
"/ e := e - 1 |
|
6729 |
"/ ] |
|
6730 |
"/ ]. |
|
6731 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e with:aCollection]. |
|
6732 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end with:aCollection] |
|
6733 |
"/ |
|
6734 |
"/ "Modified: 21.8.1997 / 18:30:19 / cg" |
|
6735 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
|
6736 |
! |
|
6737 |
||
6738 |
randomizedSort |
|
6739 |
<resource: #obsolete> |
|
6740 |
"sort the collection inplace. The elements are compared using |
|
6741 |
'<' i.e. they should offer a magnitude-like protocol. |
|
6742 |
||
6743 |
This uses the randomized quicksort algorithm, |
|
6744 |
which has a better worstCase behavior than quickSort |
|
6745 |
(bit worse bestCase & averageCase behavior). |
|
6746 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
6747 |
||
6748 |
"/ |stop| |
|
6749 |
"/ |
|
6750 |
"/ stop := self size. |
|
6751 |
"/ (stop > 1) ifTrue:[ |
|
6752 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:nil |
|
6753 |
"/ ] |
|
6754 |
||
6755 |
"/ code was never used - use regular sort for backward compatibility |
|
6756 |
^ self sort |
|
6757 |
||
6758 |
! |
|
6759 |
||
6760 |
randomizedSort:sortBlock |
|
6761 |
<resource: #obsolete> |
|
6762 |
"sort the collection inplace using the 2-arg block sortBlock |
|
6763 |
for comparison. This allows any sort criteria to be implemented. |
|
6764 |
||
6765 |
This uses the randomized quicksort algorithm, |
|
6766 |
which has a better worstCase behavior than quickSort |
|
6767 |
(bit worse bestCase & averageCase behavior). |
|
6768 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
6769 |
||
6770 |
"/ |stop| |
|
6771 |
"/ |
|
6772 |
"/ stop := self size. |
|
6773 |
"/ (stop > 1) ifTrue:[ |
|
6774 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:nil |
|
6775 |
"/ ] |
|
6776 |
||
6777 |
"/ code was never used - use regular sort for backward compatibility |
|
6778 |
^ self sort:sortBlock |
|
6779 |
||
6780 |
! |
|
6781 |
||
6782 |
randomizedSort:sortBlock with:aCollection |
|
6783 |
<resource: #obsolete> |
|
6784 |
"sort the collection inplace using the 2-arg block sortBlock |
|
6785 |
for comparison. Also reorder the elements in aCollection. |
|
6786 |
Use this, when you have a key collection to sort some other collection with. |
|
6787 |
||
6788 |
This uses the randomized quicksort algorithm, |
|
6789 |
which has a better worstCase behavior than quickSort |
|
6790 |
(bit worse bestCase & averageCase behavior). |
|
6791 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
6792 |
||
6793 |
"/ |stop| |
|
6794 |
"/ |
|
6795 |
"/ stop := self size. |
|
6796 |
"/ (stop > 1) ifTrue:[ |
|
6797 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
|
6798 |
"/ ] |
|
6799 |
||
6800 |
"/ code was never used - use regular sort for backward compatibility |
|
6801 |
^ self sort:sortBlock with:aCollection |
|
6802 |
||
6803 |
! |
|
6804 |
||
6805 |
randomizedSortWith:aCollection |
|
6806 |
<resource: #obsolete> |
|
6807 |
"sort the receiver collection inplace, using '<' to compare elements. |
|
6808 |
Also, the elements of aCollection are reordered with it. |
|
6809 |
Use this, when you have a key collection to sort another collection with. |
|
6810 |
||
6811 |
This uses the randomized quicksort algorithm, |
|
6812 |
which has a better worstCase behavior than quickSort |
|
6813 |
(bit worse bestCase & averageCase behavior). |
|
6814 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
6815 |
||
6816 |
"/ |stop| |
|
6817 |
"/ |
|
6818 |
"/ stop := self size. |
|
6819 |
"/ (stop > 1) ifTrue:[ |
|
6820 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:aCollection |
|
6821 |
"/ ] |
|
6822 |
||
6823 |
"/ code was never used - use regular sort for backward compatibility |
|
6824 |
^ self sortWith:aCollection |
|
6825 |
||
6826 |
! ! |
|
6827 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6828 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6829 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6830 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6831 |
"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
|
6832 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6833 |
Elements on the left are filled with padElement. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6834 |
If the receiver's 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
|
6835 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6836 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6837 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6838 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6839 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6840 |
(len < size) ifTrue:[ |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6841 |
s := self species new:size withAll:padElement. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6842 |
s replaceFrom:(size - len + 1) with:self. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6843 |
^ s |
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6844 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6845 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6846 |
" |
8874 | 6847 |
'foo' leftPaddedTo:10 with:$. |
6848 |
'fooBar' leftPaddedTo:5 with:$. |
|
6849 |
123 printString leftPaddedTo:10 with:$. |
|
6850 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
6851 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
6852 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
6853 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
6854 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
6855 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
6856 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6857 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6858 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6859 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6860 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6861 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6862 |
plus pad elements up to length. |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6863 |
If the receiver's 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
|
6864 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6865 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6866 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6867 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6868 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6869 |
len < newSize ifTrue:[ |
14613
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6870 |
s := self species new:newSize withAll:padElement. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6871 |
s replaceFrom:1 to:len with:self. |
a263aaa13092
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
14610
diff
changeset
|
6872 |
^ s |
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6873 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6874 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6875 |
" |
8874 | 6876 |
'foo' paddedTo:10 with:$. |
6877 |
123 printString paddedTo:10 with:$* |
|
6878 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
6879 |
(Float pi printString) paddedTo:15 with:$- |
|
6880 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
6881 |
#[1 2 3 4] paddedTo:6 with:0 |
|
6882 |
#[1 2 3 4] paddedTo:10 with:99 |
|
6883 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6884 |
" |
7116 | 6885 |
! |
6886 |
||
6887 |
paddedToMultipleOf:sizeModulu with:padElement |
|
6888 |
|mySize mod numPad| |
|
6889 |
||
6890 |
mySize := self size. |
|
6891 |
mod := (mySize \\ sizeModulu). |
|
6892 |
mod == 0 ifTrue:[^ self]. |
|
6893 |
||
6894 |
numPad := sizeModulu - mod. |
|
6895 |
^ self , (self species new:numPad withAll:padElement). |
|
6896 |
||
6897 |
" |
|
8874 | 6898 |
#[] paddedToMultipleOf:3 with:0 |
6899 |
#[1] paddedToMultipleOf:3 with:0 |
|
6900 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
6901 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 6902 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
6903 |
||
6904 |
#() paddedToMultipleOf:3 with:nil |
|
6905 |
#(1) paddedToMultipleOf:3 with:nil |
|
6906 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
6907 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
6908 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
6909 |
||
6910 |
'' paddedToMultipleOf:3 with:Character space |
|
6911 |
'a' paddedToMultipleOf:3 with:Character space |
|
6912 |
'ab' paddedToMultipleOf:3 with:Character space |
|
6913 |
'abc' paddedToMultipleOf:3 with:Character space |
|
6914 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
6915 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6916 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
6917 |
|
15791 | 6918 |
!SequenceableCollection methodsFor:'printing & storing'! |
6919 |
||
6920 |
printOn:aStream withSeparator:aSeparatorStringOrCharacter |
|
6921 |
self do:[:eachElement| |
|
6922 |
eachElement printOn:aStream. |
|
6923 |
] separatedBy:[ |
|
6924 |
aSeparatorStringOrCharacter printOn:aStream |
|
6925 |
]. |
|
6926 |
||
6927 |
" |
|
6928 |
#[1 2 3 4 10 17] printOn:Transcript withSeparator:$. |
|
6929 |
" |
|
6930 |
! |
|
6931 |
||
6932 |
printStringWithSeparator:aSeparatorStringOrCharacter |
|
6933 |
|s| |
|
6934 |
||
6935 |
s := CharacterWriteStream on:''. |
|
6936 |
self printOn:s withSeparator:aSeparatorStringOrCharacter. |
|
6937 |
^ s contents. |
|
6938 |
||
6939 |
" |
|
6940 |
#[1 2 3 4 10 17] printStringWithSeparator:$. |
|
6941 |
" |
|
6942 |
! ! |
|
6943 |
||
7260 | 6944 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 6945 |
|
14100 | 6946 |
mergeFirst:first middle:middle last:last into:dst by:aBlock |
8874 | 6947 |
"Private!! |
5358 | 6948 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
14100 | 6949 |
|
6950 |
|i1 i2 val1 val2 out| |
|
5358 | 6951 |
|
6952 |
i1 := first. |
|
14100 | 6953 |
out := first. |
5358 | 6954 |
i2 := middle + 1. |
14100 | 6955 |
val1 := self at:i1. |
6956 |
||
6957 |
i2 <= last ifTrue:[ |
|
6958 |
val2 := self at:i2. |
|
6959 |
"select 'lower' half of the elements based on comparator" |
|
6960 |
[(i1 <= middle) and:[i2 <= last]] whileTrue:[ |
|
6961 |
"this is stable if #< or #> ist used for comparison (and not #<= or #>=)" |
|
6962 |
(aBlock value:val2 value:val1) ifTrue:[ |
|
6963 |
dst at:out put:val2. |
|
6964 |
i2 := i2 + 1. |
|
6965 |
i2 <= last ifTrue:[ |
|
6966 |
val2 := self at:i2 |
|
6967 |
] |
|
6968 |
] ifFalse:[ |
|
6969 |
dst at:out put:val1. |
|
6970 |
i1 := i1 + 1. |
|
6971 |
val1 := self at:i1. |
|
6972 |
]. |
|
6973 |
out := out + 1. |
|
6974 |
]. |
|
6975 |
]. |
|
5358 | 6976 |
|
6977 |
"copy the remaining elements" |
|
14100 | 6978 |
i1 <= middle ifTrue:[ |
6979 |
dst |
|
6980 |
replaceFrom:out |
|
6981 |
to:last |
|
6982 |
with:self |
|
6983 |
startingAt:i1 |
|
6984 |
] ifFalse:[ |
|
6985 |
dst |
|
6986 |
replaceFrom:out |
|
6987 |
to:last |
|
6988 |
with:self |
|
6989 |
startingAt:i2 |
|
6990 |
]. |
|
6991 |
! |
|
6992 |
||
6993 |
mergeSortFrom: first to: last by: aBlock |
|
5358 | 6994 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
6995 |
||
14100 | 6996 |
|size chunkSize idx nextIdx finished temp src dst| |
6997 |
||
6998 |
size := last-first + 1. |
|
6999 |
size <= 1 ifTrue:[^ self]. |
|
7000 |
||
7001 |
"size of the base chunks" |
|
7002 |
chunkSize := 12. |
|
7003 |
idx := first. |
|
7004 |
finished := false. |
|
7005 |
[finished] whileFalse:[ |
|
7006 |
nextIdx := idx + chunkSize. |
|
7007 |
nextIdx > last ifTrue:[ |
|
7008 |
nextIdx := last+1. |
|
7009 |
finished := true. |
|
7010 |
]. |
|
7011 |
self insertionSort:aBlock from:idx to:nextIdx-1. |
|
7012 |
idx := nextIdx. |
|
7013 |
]. |
|
7014 |
||
7015 |
chunkSize < size ifTrue:[ |
|
7016 |
"now merge the chunks" |
|
7017 |
dst := Array new:self size. |
|
7018 |
src := self. |
|
7019 |
||
7020 |
[chunkSize < size] whileTrue:[ |
|
7021 |
"merge pairs of adjecant chunks" |
|
7022 |
idx := first. |
|
7023 |
finished := false. |
|
7024 |
[finished] whileFalse:[ |
|
7025 |
nextIdx := idx + (2*chunkSize). |
|
7026 |
nextIdx > last ifTrue:[ |
|
7027 |
nextIdx := last+1. |
|
7028 |
finished := true. |
|
7029 |
]. |
|
7030 |
src |
|
7031 |
mergeFirst:idx |
|
7032 |
middle:idx+chunkSize-1 |
|
7033 |
last:nextIdx-1 |
|
7034 |
into:dst |
|
7035 |
by:aBlock. |
|
7036 |
idx := nextIdx. |
|
7037 |
]. |
|
7038 |
||
7039 |
chunkSize := chunkSize * 2. |
|
7040 |
||
7041 |
"merged chunks are in dst. Swap source and destination for next step" |
|
7042 |
temp := dst. |
|
7043 |
dst := src. |
|
7044 |
src := temp. |
|
7045 |
]. |
|
7046 |
"note, the latest dst is now src (see above)" |
|
7047 |
src ~~ self ifTrue:[ |
|
7048 |
self replaceFrom:first to:last with:src startingAt:first. |
|
7049 |
]. |
|
7050 |
]. |
|
5358 | 7051 |
! |
7052 |
||
2069 | 7053 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
7054 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
7055 |
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
|
7056 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
7057 |
|
14099 | 7058 |
The algorithm has been extended to introSort, which is quickSort with fallBack |
7059 |
when we find out, that we have a worst case quick sort with O(n*n). |
|
7060 |
||
8874 | 7061 |
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
|
7062 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 7063 |
|
7064 |
|begin "{ Class: SmallInteger }" |
|
7065 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7066 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7067 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7068 |
m "{ Class: SmallInteger }" |
14099 | 7069 |
prevIdx "{ Class: SmallInteger }" |
7070 |
elB elM elE temp stack depthLimit| |
|
7071 |
||
7072 |
inEnd <= inBegin ifTrue:[ |
|
7073 |
"nothing to sort" |
|
7074 |
^ self. |
|
7075 |
]. |
|
7076 |
||
14102 | 7077 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
7078 |
||
7079 |
stack := OrderedCollection new:depthLimit*2. |
|
7080 |
begin := inBegin. |
|
7081 |
end := inEnd. |
|
7082 |
||
16242 | 7083 |
[ |
14102 | 7084 |
"do not sort small chunks, |
7085 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
7086 |
end - begin > 12 ifTrue:[ |
|
7087 |
"ok, her we do the real quickSort..." |
|
7088 |
depthLimit <= 0 ifTrue:[ |
|
7089 |
"this is apparently a degenerated quickSort |
|
7090 |
- abort the quicksort and fall back to heapSort |
|
7091 |
which has O(n * log n) complexity in the worst case" |
|
7092 |
self heapSortFrom:begin to:end. |
|
7093 |
begin := end+1. |
|
7094 |
] ifFalse:[ |
|
7095 |
elB := self at:begin. |
|
7096 |
elE := self at:end. |
|
7097 |
m := (begin + end) // 2. |
|
7098 |
elM := self at:m. |
|
7099 |
"take the median of three as pivot (elM)" |
|
7100 |
(elM < elB) ifTrue:[ |
|
7101 |
temp := self at:begin put:elM. |
|
7102 |
elM := self at:m put:elB. |
|
7103 |
elB := temp. |
|
14099 | 7104 |
]. |
14102 | 7105 |
(elE < elB) ifTrue:[ |
7106 |
self at:begin put:elE. |
|
7107 |
elE := self at:end put:elB. |
|
7108 |
]. |
|
7109 |
(elE < elM) ifTrue:[ |
|
7110 |
self at:end put:elM. |
|
7111 |
elM := self at:m put:elE. |
|
14099 | 7112 |
]. |
14102 | 7113 |
|
7114 |
bRun := begin. |
|
7115 |
eRun := end. |
|
7116 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
7117 |
so STC can optimize" |
|
7118 |
[bRun < eRun] whileTrue:[ |
|
7119 |
[(bRun := bRun+1) <= eRun and:[(self at:bRun) < elM]] whileTrue. |
|
7120 |
[bRun <= (eRun := eRun-1) and:[elM < (self at:eRun)]] whileTrue. |
|
7121 |
||
7122 |
(bRun < eRun) ifTrue:[ |
|
7123 |
temp := self at:bRun. |
|
7124 |
self at:bRun put:(self at:eRun). |
|
7125 |
self at:eRun put:temp. |
|
7126 |
]. |
|
7127 |
]. |
|
7128 |
(bRun < end) ifTrue:[ |
|
7129 |
"remember right part for later processing" |
|
7130 |
stack add:end. |
|
7131 |
stack add:depthLimit-1. |
|
7132 |
]. |
|
7133 |
||
7134 |
"now sort the left part from begin .. new end" |
|
7135 |
depthLimit := depthLimit - 1. |
|
7136 |
end := eRun. |
|
14099 | 7137 |
]. |
7138 |
] ifFalse:[ |
|
14102 | 7139 |
stack isEmpty ifTrue:[ |
7140 |
"we are done. |
|
7141 |
Do a final insertion sort over all the elements, |
|
7142 |
to eventually sort the small, unsorted chunks" |
|
7143 |
begin := inBegin. |
|
7144 |
bRun := begin + 1. |
|
7145 |
end := inEnd. |
|
7146 |
bRun to:end do:[:idx| |
|
7147 |
temp := self at:idx. |
|
7148 |
prevIdx := idx-1. |
|
7149 |
[prevIdx >= begin and:[temp < (self at:prevIdx)]] whileTrue:[ |
|
7150 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
7151 |
prevIdx := prevIdx - 1. |
|
7152 |
]. |
|
7153 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
7154 |
self at:prevIdx+1 put:temp. |
|
7155 |
]. |
|
14099 | 7156 |
]. |
14102 | 7157 |
^ self |
14099 | 7158 |
]. |
14102 | 7159 |
begin := end + 1. |
7160 |
depthLimit := stack removeLast. |
|
7161 |
end := stack removeLast. |
|
14099 | 7162 |
]. |
16242 | 7163 |
] loop. |
2069 | 7164 |
! |
7165 |
||
14099 | 7166 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
7167 |
"actual quicksort worker for sort-message. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
7168 |
Simulates recursion in a stack, to avoid recursion overflow |
14099 | 7169 |
with degenerated collections. |
7170 |
||
7171 |
The algorithm has been extended to introSort, which is quickSort with fallBack |
|
7172 |
when we find out, that we have a worst case quick sort with O(n*n). |
|
7173 |
||
7174 |
Use sortBlock for element comparisons." |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7175 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7176 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7177 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7178 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7179 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7180 |
m "{ Class: SmallInteger }" |
14099 | 7181 |
prevIdx "{ Class: SmallInteger }" |
7182 |
elB elM elE temp stack depthLimit| |
|
7183 |
||
7184 |
inEnd <= inBegin ifTrue:[ |
|
7185 |
"nothing to sort" |
|
7186 |
^ self. |
|
7187 |
]. |
|
7188 |
||
14102 | 7189 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
7190 |
||
7191 |
stack := OrderedCollection new:depthLimit*2. |
|
7192 |
begin := inBegin. |
|
7193 |
end := inEnd. |
|
7194 |
||
16242 | 7195 |
[ |
14102 | 7196 |
"do not sort small chunks, |
7197 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
7198 |
end - begin > 12 ifTrue:[ |
|
7199 |
"ok, her we do the real quickSort..." |
|
7200 |
depthLimit <= 0 ifTrue:[ |
|
7201 |
"this is apparently a degenerated quickSort |
|
7202 |
- abort the quicksort and fall back to heapSort |
|
7203 |
which has O(n * log n) complexity in the worst case" |
|
7204 |
self heapSort:sortBlock from:begin to:end. |
|
7205 |
begin := end+1. |
|
7206 |
] ifFalse:[ |
|
7207 |
elB := self at:begin. |
|
7208 |
elE := self at:end. |
|
7209 |
m := (begin + end) // 2. |
|
7210 |
elM := self at:m. |
|
7211 |
"take the median of three as pivot (elM)" |
|
7212 |
(sortBlock value:elM value:elB) ifTrue:[ |
|
7213 |
temp := self at:begin put:elM. |
|
7214 |
elM := self at:m put:elB. |
|
7215 |
elB := temp. |
|
14099 | 7216 |
]. |
14102 | 7217 |
(sortBlock value:elE value:elB) ifTrue:[ |
7218 |
self at:begin put:elE. |
|
7219 |
elE := self at:end put:elB. |
|
7220 |
]. |
|
7221 |
(sortBlock value:elE value:elM) ifTrue:[ |
|
7222 |
self at:end put:elM. |
|
7223 |
elM := self at:m put:elE. |
|
14099 | 7224 |
]. |
14102 | 7225 |
|
7226 |
bRun := begin. |
|
7227 |
eRun := end. |
|
7228 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
7229 |
so STC can optimize" |
|
7230 |
[bRun < eRun] whileTrue:[ |
|
7231 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
7232 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:elM value:(self at:eRun)]] whileTrue. |
|
7233 |
||
7234 |
(bRun < eRun) ifTrue:[ |
|
7235 |
temp := self at:bRun. |
|
7236 |
self at:bRun put:(self at:eRun). |
|
7237 |
self at:eRun put:temp. |
|
7238 |
]. |
|
7239 |
]. |
|
7240 |
(bRun < end) ifTrue:[ |
|
7241 |
"remember right part for later processing" |
|
7242 |
stack add:end. |
|
7243 |
stack add:depthLimit-1. |
|
7244 |
]. |
|
7245 |
||
7246 |
"now sort the left part from begin .. new end" |
|
7247 |
depthLimit := depthLimit - 1. |
|
7248 |
end := eRun. |
|
7249 |
] |
|
14099 | 7250 |
] ifFalse:[ |
14102 | 7251 |
stack isEmpty ifTrue:[ |
7252 |
"we are done. |
|
7253 |
Do a final insertion sort over all the elements, |
|
7254 |
to eventually sort the small, unsorted chunks" |
|
7255 |
begin := inBegin. |
|
7256 |
bRun := begin + 1. |
|
7257 |
end := inEnd. |
|
7258 |
bRun to:end do:[:idx| |
|
7259 |
temp := self at:idx. |
|
7260 |
prevIdx := idx-1. |
|
7261 |
[prevIdx >= begin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
7262 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
7263 |
prevIdx := prevIdx - 1. |
|
7264 |
]. |
|
7265 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
7266 |
self at:prevIdx+1 put:temp. |
|
7267 |
]. |
|
14099 | 7268 |
]. |
14102 | 7269 |
^ self |
14099 | 7270 |
]. |
14102 | 7271 |
begin := end + 1. |
7272 |
depthLimit := stack removeLast. |
|
7273 |
end := stack removeLast. |
|
14099 | 7274 |
]. |
16242 | 7275 |
] loop. |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7276 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7277 |
|
14099 | 7278 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock policy:policy |
7279 |
"actual quicksort worker for sort-message. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
7280 |
Simulates recursion in a stack, to avoid recursion overflow |
14099 | 7281 |
with degenerated collections. |
7282 |
||
7283 |
Use sortBlock for element comparisons." |
|
2069 | 7284 |
|
7285 |
|begin "{ Class: SmallInteger }" |
|
7286 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7287 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7288 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7289 |
m "{ Class: SmallInteger }" |
14099 | 7290 |
prevIdx "{ Class: SmallInteger }" |
14102 | 7291 |
elB elM elE temp stack depthLimit policySortBlock| |
14099 | 7292 |
|
7293 |
inEnd <= inBegin ifTrue:[ |
|
7294 |
"nothing to sort" |
|
7295 |
^ self. |
|
7296 |
]. |
|
7297 |
||
14102 | 7298 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
7299 |
||
7300 |
stack := OrderedCollection new:depthLimit*2. |
|
7301 |
begin := inBegin. |
|
7302 |
end := inEnd. |
|
7303 |
||
16242 | 7304 |
[ |
14102 | 7305 |
"do not sort small chunks, |
7306 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
7307 |
end - begin > 12 ifTrue:[ |
|
7308 |
"ok, her we do the real quickSort..." |
|
7309 |
depthLimit <= 0 ifTrue:[ |
|
7310 |
"this is apparently a degenerated quickSort |
|
7311 |
- abort the quicksort and fall back to heapSort |
|
7312 |
which has O(n * log n) complexity in the worst case" |
|
7313 |
policySortBlock := [:arg1 :arg2| sortBlock value:policy value:arg1 value:arg2]. |
|
7314 |
self heapSort:policySortBlock from:begin to:end. |
|
7315 |
begin := end+1. |
|
7316 |
] ifFalse:[ |
|
7317 |
elB := self at:begin. |
|
7318 |
elE := self at:end. |
|
7319 |
m := (begin + end) // 2. |
|
7320 |
elM := self at:m. |
|
7321 |
"take the median of three as pivot (elM)" |
|
7322 |
(sortBlock value:policy value:elM value:elB) ifTrue:[ |
|
7323 |
temp := self at:begin put:elM. |
|
7324 |
elM := self at:m put:elB. |
|
7325 |
elB := temp. |
|
7326 |
]. |
|
7327 |
(sortBlock value:policy value:elE value:elB) ifTrue:[ |
|
7328 |
self at:begin put:elE. |
|
7329 |
elE := self at:end put:elB. |
|
7330 |
]. |
|
7331 |
(sortBlock value:policy value:elE value:elM) ifTrue:[ |
|
7332 |
self at:end put:elM. |
|
7333 |
elM := self at:m put:elE. |
|
7334 |
]. |
|
7335 |
||
7336 |
bRun := begin. |
|
7337 |
eRun := end. |
|
7338 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
7339 |
so STC can optimize" |
|
7340 |
[bRun < eRun] whileTrue:[ |
|
7341 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:policy value:(self at:bRun) value:elM]] whileTrue. |
|
7342 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:policy value:elM value:(self at:eRun)]] whileTrue. |
|
7343 |
||
7344 |
(bRun < eRun) ifTrue:[ |
|
7345 |
temp := self at:bRun. |
|
7346 |
self at:bRun put:(self at:eRun). |
|
7347 |
self at:eRun put:temp. |
|
7348 |
]. |
|
14099 | 7349 |
]. |
14102 | 7350 |
(bRun < end) ifTrue:[ |
7351 |
"remember right part for later processing" |
|
7352 |
stack add:end. |
|
7353 |
stack add:depthLimit-1. |
|
7354 |
]. |
|
7355 |
||
7356 |
"now sort the left part from begin .. new end" |
|
7357 |
depthLimit := depthLimit - 1. |
|
7358 |
end := eRun. |
|
14099 | 7359 |
]. |
7360 |
] ifFalse:[ |
|
14102 | 7361 |
stack isEmpty ifTrue:[ |
7362 |
"we are done. |
|
7363 |
Do a final insertion sort over all the elements, |
|
7364 |
to eventually sort the small, unsorted chunks" |
|
7365 |
begin := inBegin. |
|
7366 |
bRun := begin + 1. |
|
7367 |
end := inEnd. |
|
7368 |
bRun to:end do:[:idx| |
|
7369 |
temp := self at:idx. |
|
7370 |
prevIdx := idx-1. |
|
7371 |
[prevIdx >= begin and:[sortBlock value:policy value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
7372 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
7373 |
prevIdx := prevIdx - 1. |
|
7374 |
]. |
|
7375 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
7376 |
self at:prevIdx+1 put:temp. |
|
7377 |
]. |
|
14099 | 7378 |
]. |
14102 | 7379 |
^ self |
14099 | 7380 |
]. |
14102 | 7381 |
begin := end + 1. |
7382 |
depthLimit := stack removeLast. |
|
7383 |
end := stack removeLast. |
|
14099 | 7384 |
]. |
16242 | 7385 |
] loop. |
14099 | 7386 |
! |
7387 |
||
7388 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
7389 |
"actual quicksort worker for sort-message. |
|
7390 |
Simulates recursion in a stack, to avoid recursion overflow |
|
7391 |
with degenerated collections. |
|
7392 |
||
7393 |
Use sortBlock for element comparisons." |
|
7394 |
||
7395 |
|begin "{ Class: SmallInteger }" |
|
7396 |
end "{ Class: SmallInteger }" |
|
7397 |
bRun "{ Class: SmallInteger }" |
|
7398 |
eRun "{ Class: SmallInteger }" |
|
7399 |
m "{ Class: SmallInteger }" |
|
7400 |
prevIdx "{ Class: SmallInteger }" |
|
7401 |
elB elM elE temp temp1 stack depthLimit| |
|
7402 |
||
7403 |
inEnd <= inBegin ifTrue:[ |
|
7404 |
"nothing to sort" |
|
7405 |
^ self. |
|
7406 |
]. |
|
7407 |
||
14102 | 7408 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
7409 |
||
7410 |
stack := OrderedCollection new:depthLimit*2. |
|
7411 |
begin := inBegin. |
|
7412 |
end := inEnd. |
|
7413 |
||
16242 | 7414 |
[ |
14102 | 7415 |
"do not sort small chunks, |
7416 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
7417 |
end - begin > 12 ifTrue:[ |
|
7418 |
"ok, her we do the real quickSort..." |
|
7419 |
"/ heapSort does not support ...with: yet |
|
7420 |
"depthLimit <= 0" false ifTrue:[ |
|
7421 |
"this is apparently a degenerated quickSort |
|
7422 |
- abort the quicksort and fall back to heapSort |
|
7423 |
which has O(n * log n) complexity in the worst case" |
|
7424 |
self heapSort:sortBlock from:begin to:end. |
|
7425 |
begin := end+1. |
|
7426 |
] ifFalse:[ |
|
7427 |
elB := self at:begin. |
|
7428 |
elE := self at:end. |
|
7429 |
m := (begin + end) // 2. |
|
7430 |
elM := self at:m. |
|
7431 |
"take the median of three as pivot (elM)" |
|
7432 |
(sortBlock value:elM value:elB) ifTrue:[ |
|
7433 |
temp := self at:begin put:elM. |
|
7434 |
elM := self at:m put:elB. |
|
7435 |
elB := temp. |
|
7436 |
||
7437 |
temp := aCollection at:begin. |
|
7438 |
aCollection at:begin put:(aCollection at:m). |
|
7439 |
aCollection at:m put:temp. |
|
14099 | 7440 |
]. |
14102 | 7441 |
(sortBlock value:elE value:elB) ifTrue:[ |
7442 |
self at:begin put:elE. |
|
7443 |
elE := self at:end put:elB. |
|
7444 |
||
7445 |
temp := aCollection at:end. |
|
7446 |
aCollection at:end put:(aCollection at:begin). |
|
7447 |
aCollection at:begin put:temp. |
|
7448 |
]. |
|
7449 |
(sortBlock value:elE value:elM) ifTrue:[ |
|
7450 |
self at:end put:elM. |
|
7451 |
elM := self at:m put:elE. |
|
7452 |
||
7453 |
temp := aCollection at:end. |
|
7454 |
aCollection at:end put:(aCollection at:m). |
|
7455 |
aCollection at:m put:temp. |
|
14099 | 7456 |
]. |
14102 | 7457 |
|
7458 |
bRun := begin. |
|
7459 |
eRun := end. |
|
7460 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
7461 |
so STC can optimize" |
|
7462 |
[bRun < eRun] whileTrue:[ |
|
7463 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
7464 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:elM value:(self at:eRun)]] whileTrue. |
|
7465 |
||
7466 |
(bRun < eRun) ifTrue:[ |
|
7467 |
temp := self at:bRun. |
|
7468 |
self at:bRun put:(self at:eRun). |
|
7469 |
self at:eRun put:temp. |
|
7470 |
||
7471 |
temp := aCollection at:bRun. |
|
7472 |
aCollection at:bRun put:(aCollection at:eRun). |
|
7473 |
aCollection at:eRun put:temp. |
|
7474 |
]. |
|
7475 |
]. |
|
7476 |
(bRun < end) ifTrue:[ |
|
7477 |
"remember right part for later processing" |
|
7478 |
stack add:end. |
|
7479 |
stack add:depthLimit-1. |
|
7480 |
]. |
|
7481 |
||
7482 |
"now sort the left part from begin .. new end" |
|
7483 |
depthLimit := depthLimit - 1. |
|
7484 |
end := eRun. |
|
7485 |
] |
|
14099 | 7486 |
] ifFalse:[ |
14102 | 7487 |
stack isEmpty ifTrue:[ |
7488 |
"we are done. |
|
7489 |
Do a final insertion sort over all the elements, |
|
7490 |
to eventually sort the small, unsorted chunks" |
|
7491 |
begin := inBegin. |
|
7492 |
bRun := begin + 1. |
|
7493 |
end := inEnd. |
|
7494 |
bRun to:end do:[:idx| |
|
7495 |
temp := self at:idx. |
|
7496 |
temp1 := aCollection at:idx. |
|
7497 |
prevIdx := idx-1. |
|
7498 |
[prevIdx >= begin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
7499 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
7500 |
aCollection at:prevIdx+1 put:(aCollection at:prevIdx). |
|
7501 |
prevIdx := prevIdx - 1. |
|
7502 |
]. |
|
7503 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
7504 |
self at:prevIdx+1 put:temp. |
|
7505 |
aCollection at:prevIdx+1 put:temp1. |
|
7506 |
]. |
|
14099 | 7507 |
]. |
14102 | 7508 |
^ self |
14099 | 7509 |
]. |
14102 | 7510 |
begin := end + 1. |
7511 |
depthLimit := stack removeLast. |
|
7512 |
end := stack removeLast. |
|
14099 | 7513 |
]. |
16242 | 7514 |
] loop. |
2069 | 7515 |
! |
7516 |
||
7517 |
quickSortFrom:inBegin to:inEnd with:aCollection |
|
14099 | 7518 |
"actual quicksort worker for sort-message. |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
7519 |
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
|
7520 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
7521 |
|
8874 | 7522 |
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
|
7523 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 7524 |
|
7525 |
|begin "{ Class: SmallInteger }" |
|
7526 |
end "{ Class: SmallInteger }" |
|
14099 | 7527 |
bRun "{ Class: SmallInteger }" |
7528 |
eRun "{ Class: SmallInteger }" |
|
7529 |
m "{ Class: SmallInteger }" |
|
7530 |
prevIdx "{ Class: SmallInteger }" |
|
7531 |
elB elM elE temp temp1 stack depthLimit| |
|
7532 |
||
7533 |
inEnd <= inBegin ifTrue:[ |
|
7534 |
"nothing to sort" |
|
7535 |
^ self. |
|
7536 |
]. |
|
7537 |
||
14102 | 7538 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
7539 |
||
7540 |
stack := OrderedCollection new:depthLimit*2. |
|
7541 |
begin := inBegin. |
|
7542 |
end := inEnd. |
|
7543 |
||
16242 | 7544 |
[ |
14102 | 7545 |
"do not sort small chunks, |
7546 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
7547 |
end - begin > 12 ifTrue:[ |
|
7548 |
"ok, her we do the real quickSort..." |
|
7549 |
"/ heapSort does not support ...with: yet |
|
7550 |
"depthLimit <= 0" false ifTrue:[ |
|
7551 |
"this is apparently a degenerated quickSort |
|
7552 |
- abort the quicksort and fall back to heapSort |
|
7553 |
which has O(n * log n) complexity in the worst case" |
|
7554 |
self heapSortFrom:begin to:end. |
|
7555 |
begin := end+1. |
|
7556 |
] ifFalse:[ |
|
7557 |
elB := self at:begin. |
|
7558 |
elE := self at:end. |
|
7559 |
m := (begin + end) // 2. |
|
7560 |
elM := self at:m. |
|
7561 |
"take the median of three as pivot (elM)" |
|
7562 |
(elM < elB) ifTrue:[ |
|
7563 |
temp := self at:begin put:elM. |
|
7564 |
elM := self at:m put:elB. |
|
7565 |
elB := temp. |
|
7566 |
||
7567 |
temp := aCollection at:begin. |
|
7568 |
aCollection at:begin put:(aCollection at:m). |
|
7569 |
aCollection at:m put:temp. |
|
14099 | 7570 |
]. |
14102 | 7571 |
(elE < elB) ifTrue:[ |
7572 |
self at:begin put:elE. |
|
7573 |
elE := self at:end put:elB. |
|
7574 |
||
7575 |
temp := aCollection at:end. |
|
7576 |
aCollection at:end put:(aCollection at:begin). |
|
7577 |
aCollection at:begin put:temp. |
|
7578 |
]. |
|
7579 |
(elE < elM) ifTrue:[ |
|
7580 |
self at:end put:elM. |
|
7581 |
elM := self at:m put:elE. |
|
7582 |
||
7583 |
temp := aCollection at:end. |
|
7584 |
aCollection at:end put:(aCollection at:m). |
|
7585 |
aCollection at:m put:temp. |
|
14099 | 7586 |
]. |
14102 | 7587 |
|
7588 |
bRun := begin. |
|
7589 |
eRun := end. |
|
7590 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
7591 |
so STC can optimize" |
|
7592 |
[bRun < eRun] whileTrue:[ |
|
7593 |
[(bRun := bRun+1) <= eRun and:[(self at:bRun) < elM]] whileTrue. |
|
7594 |
[bRun <= (eRun := eRun-1) and:[elM < (self at:eRun)]] whileTrue. |
|
7595 |
||
7596 |
(bRun < eRun) ifTrue:[ |
|
7597 |
temp := self at:bRun. |
|
7598 |
self at:bRun put:(self at:eRun). |
|
7599 |
self at:eRun put:temp. |
|
7600 |
||
7601 |
temp := aCollection at:bRun. |
|
7602 |
aCollection at:bRun put:(aCollection at:eRun). |
|
7603 |
aCollection at:eRun put:temp. |
|
7604 |
]. |
|
7605 |
]. |
|
7606 |
(bRun < end) ifTrue:[ |
|
7607 |
"remember right part for later processing" |
|
7608 |
stack add:end. |
|
7609 |
stack add:depthLimit-1. |
|
7610 |
]. |
|
7611 |
||
7612 |
"now sort the left part from begin .. new end" |
|
7613 |
depthLimit := depthLimit - 1. |
|
7614 |
end := eRun. |
|
7615 |
] |
|
14099 | 7616 |
] ifFalse:[ |
14102 | 7617 |
stack isEmpty ifTrue:[ |
7618 |
"we are done. |
|
7619 |
Do a final insertion sort over all the elements, |
|
7620 |
to eventually sort the small, unsorted chunks" |
|
7621 |
begin := inBegin. |
|
7622 |
bRun := begin + 1. |
|
7623 |
end := inEnd. |
|
7624 |
bRun to:end do:[:idx| |
|
7625 |
temp := self at:idx. |
|
7626 |
temp1 := aCollection at:idx. |
|
7627 |
prevIdx := idx-1. |
|
7628 |
[prevIdx >= begin and:[temp < (self at:prevIdx)]] whileTrue:[ |
|
7629 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
7630 |
aCollection at:prevIdx+1 put:(aCollection at:prevIdx). |
|
7631 |
prevIdx := prevIdx - 1. |
|
7632 |
]. |
|
7633 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
7634 |
self at:prevIdx+1 put:temp. |
|
7635 |
aCollection at:prevIdx+1 put:temp1. |
|
7636 |
]. |
|
14099 | 7637 |
]. |
14102 | 7638 |
^ self |
14099 | 7639 |
]. |
14102 | 7640 |
begin := end + 1. |
7641 |
depthLimit := stack removeLast. |
|
7642 |
end := stack removeLast. |
|
14099 | 7643 |
]. |
16242 | 7644 |
] loop. |
2069 | 7645 |
! ! |
7646 |
||
602 | 7647 |
!SequenceableCollection methodsFor:'queries'! |
7648 |
||
7649 |
firstIndex |
|
7650 |
"return the first elements index" |
|
7651 |
||
7652 |
^ 1 |
|
7653 |
! |
|
7654 |
||
7655 |
isSequenceable |
|
7656 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
7657 |
||
7658 |
^ true |
|
1 | 7659 |
! |
7660 |
||
5358 | 7661 |
isSorted |
7662 |
"return true. if my elements are sorted (already)" |
|
7663 |
||
7664 |
|lastEl el| |
|
7665 |
||
7666 |
lastEl := self first. |
|
7667 |
2 to:self size do:[:i | |
|
8874 | 7668 |
el := self at:i. |
7669 |
el >= lastEl ifFalse: [^ false]. |
|
7670 |
lastEl := el |
|
5358 | 7671 |
]. |
7672 |
^ true |
|
7673 |
||
7674 |
" |
|
8874 | 7675 |
#(1 2 3 5 10 100) isSorted |
7676 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 7677 |
" |
7678 |
! |
|
7679 |
||
7680 |
isSortedBy:aBlock |
|
7681 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
7682 |
||
7683 |
|lastEl el| |
|
7684 |
||
7685 |
lastEl := self first. |
|
7686 |
2 to:self size do:[:i | |
|
8874 | 7687 |
el := self at:i. |
7688 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
7689 |
lastEl := el |
|
5358 | 7690 |
]. |
7691 |
^ true |
|
7692 |
||
7693 |
" |
|
8874 | 7694 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
7695 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
7696 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
7697 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 7698 |
" |
7699 |
||
7700 |
! |
|
7701 |
||
602 | 7702 |
keys |
7703 |
"return a collection with all keys in the Smalltalk dictionary" |
|
7704 |
||
7705 |
|sz| |
|
7706 |
||
1 | 7707 |
sz := self size. |
602 | 7708 |
sz == 0 ifTrue:[ |
8874 | 7709 |
^ #() |
61 | 7710 |
]. |
602 | 7711 |
^ 1 to:sz |
1 | 7712 |
! |
7713 |
||
602 | 7714 |
lastIndex |
7715 |
"return the last elements index" |
|
7716 |
||
7717 |
^ self size |
|
61 | 7718 |
! |
7719 |
||
602 | 7720 |
size |
7721 |
"return the number of elements in the collection. |
|
7722 |
concrete implementations must define this" |
|
7723 |
||
7724 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7725 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7726 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7727 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7728 |
"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
|
7729 |
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
|
7730 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7731 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7732 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7733 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
7734 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 7735 |
! ! |
7736 |
||
7737 |
!SequenceableCollection methodsFor:'searching'! |
|
7738 |
||
3887 | 7739 |
detect:aBlock startingAt:startIndex |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7740 |
"find the first element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7741 |
Start the search at startIndex. |
3887 | 7742 |
If none does so, report an error" |
7743 |
||
7744 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
7745 |
||
7746 |
" |
|
8874 | 7747 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
7748 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 7749 |
" |
7750 |
||
7751 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
7752 |
! |
|
7753 |
||
7754 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7755 |
"find the first element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7756 |
Start the search at startIndex. |
5717 | 7757 |
If none does so, return the evaluation of exceptionBlock" |
7758 |
||
7759 |
|stop| |
|
3887 | 7760 |
|
7761 |
stop := self size. |
|
5717 | 7762 |
self from:startIndex to:stop do:[:el | |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7763 |
(aBlock value:el) ifTrue:[ |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7764 |
^ el |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7765 |
]. |
3887 | 7766 |
]. |
7767 |
^ exceptionBlock value |
|
7768 |
||
7769 |
" |
|
8874 | 7770 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
7771 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 7772 |
" |
7773 |
||
7774 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
7775 |
! |
|
7776 |
||
18718
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7777 |
detectLast:aBlock |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7778 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7779 |
If none does so, report an error" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7780 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7781 |
^ self detectLast:aBlock startingAt:self size ifNone:[self errorNotFound] |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7782 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7783 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7784 |
#(11 12 13 14) detectLast:[:n | n odd] |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7785 |
#(12 14 16 18) detectLast:[:n | n odd] ifNone:'sorry' |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7786 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7787 |
! |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7788 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7789 |
detectLast:aBlock startingAt:startIndex |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7790 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7791 |
Start the backward search at startIndex. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7792 |
If none does so, report an error" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7793 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7794 |
^ self detectLast:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7795 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7796 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7797 |
#(11 12 13 14) detectLast:[:n | n odd] startingAt:3 |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7798 |
#(12 14 16 18) detectLast:[:n | n odd] startingAt:3 |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7799 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7800 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7801 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7802 |
! |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7803 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7804 |
detectLast:aBlock startingAt:startIndex ifNone:exceptionBlock |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7805 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7806 |
Start the backward search at startIndex. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7807 |
If none does so, return the evaluation of exceptionBlock" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7808 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7809 |
self from:startIndex to:1 by:-1 do:[:el | |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7810 |
(aBlock value:el) ifTrue:[ |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7811 |
^ el |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7812 |
]. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7813 |
]. |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7814 |
^ exceptionBlock value |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7815 |
|
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7816 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7817 |
#(11 12 13 14) detectLast:[:n | n odd] startingAt:3 ifNone:['sorry'] |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7818 |
#(12 14 16 18) detectLast:[:n | n odd] startingAt:3 ifNone:['sorry'] |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7819 |
" |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7820 |
! |
d615f1d3167c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18714
diff
changeset
|
7821 |
|
11705 | 7822 |
findFirst:aBlock ifNone:exceptionalValue |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7823 |
"find the index of the first element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7824 |
Return its index or the value from exceptionalValue if none detected. |
3887 | 7825 |
This is much like #detect, however, here an INDEX is returned, |
7826 |
while #detect returns the element." |
|
602 | 7827 |
|
7828 |
|stop "{ Class: SmallInteger }" | |
|
7829 |
||
32 | 7830 |
stop := self size. |
602 | 7831 |
1 to:stop do:[:index | |
10453 | 7832 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 7833 |
]. |
11705 | 7834 |
^ exceptionalValue value |
7835 |
||
7836 |
" |
|
7837 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
7838 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
7839 |
" |
|
1 | 7840 |
! |
7841 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7842 |
findFirst:aBlock startingAt:startIndex |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7843 |
"find the first element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7844 |
Start the search at startIndex. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7845 |
Return its index or 0 if none detected. |
3887 | 7846 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7847 |
whereas #detect: returns the element." |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7848 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7849 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7850 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7851 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7852 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7853 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7854 |
start to:stop do:[:index | |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7855 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7856 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7857 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7858 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7859 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7860 |
#(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
|
7861 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7862 |
" |
3887 | 7863 |
|
7864 |
"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
|
7865 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7866 |
|
17214
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7867 |
findLast:aBlock ifNone:exceptionalValue |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7868 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
17214
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7869 |
Return its index or the value from exceptionValue if none detected." |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7870 |
|
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7871 |
|start "{ Class: SmallInteger }"| |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7872 |
|
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7873 |
start := self size. |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7874 |
start to:1 by:-1 do:[:index | |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7875 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7876 |
]. |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7877 |
^ exceptionalValue value |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7878 |
|
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7879 |
"notice: these are bad examples, use indexOf/lastIndexOf to search for an equal element. |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7880 |
|
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7881 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] ifNone:-1 |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7882 |
#(1 99 3 99 5 6) findLast:[:x | (x == 100)] ifNone:-1 |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7883 |
'one.two.three' findLast:[:c | (c == $.)] ifNone:-1 |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7884 |
'one.two.three' findLast:[:c | (c == $:)] ifNone:-1 |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7885 |
" |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7886 |
! |
8946d5da8ad1
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16758
diff
changeset
|
7887 |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7888 |
findLast:aBlock startingAt:startIndex |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7889 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7890 |
Start the backward search at startIndex. |
8193 | 7891 |
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
|
7892 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7893 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7894 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7895 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7896 |
start to:1 by:-1 do:[:index | |
15661 | 7897 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7898 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7899 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7900 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7901 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7902 |
#(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
|
7903 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7904 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7905 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7906 |
|
8193 | 7907 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7908 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
7909 |
Start the search at startIndex. |
8193 | 7910 |
End the search at endIndex or when an element is found. |
7911 |
Return its index or 0 if none detected." |
|
7912 |
||
15799 | 7913 |
|start "{ Class: SmallInteger }" |
7914 |
end "{ Class: SmallInteger }"| |
|
8193 | 7915 |
|
7916 |
start := startIndex. |
|
15799 | 7917 |
end := endIndex. |
7918 |
start to:end by:-1 do:[:index | |
|
7919 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
8193 | 7920 |
]. |
7921 |
^ 0 |
|
7922 |
||
7923 |
" |
|
7924 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
7925 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
7926 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
7927 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
7928 |
" |
|
7929 |
! |
|
7930 |
||
368 | 7931 |
indexOfSubCollection:aCollection |
9234 | 7932 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 7933 |
|
17487
7c7c65933390
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17383
diff
changeset
|
7934 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:[0] |
368 | 7935 |
|
7936 |
" |
|
8874 | 7937 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
7938 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
7939 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
7940 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
7941 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
7942 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
7943 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
7944 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
13345
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
7945 |
'foobarbaz' indexOfSubCollection:'bar' |
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
7946 |
" |
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
7947 |
|
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
7948 |
"Modified: / 20-04-2011 / 19:11:20 / cg" |
368 | 7949 |
! |
7950 |
||
602 | 7951 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
7952 |
"find a subcollection. If found, return the index; |
|
7953 |
if not found, return the result of evaluating exceptionBlock." |
|
7954 |
||
7955 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
7956 |
! |
|
7957 |
||
368 | 7958 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 7959 |
"find a subcollection starting at index. |
368 | 7960 |
If found, return the index; if not found, return 0." |
7961 |
||
17487
7c7c65933390
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17383
diff
changeset
|
7962 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:[0] |
368 | 7963 |
! |
7964 |
||
7965 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
7966 |
"find a subcollection, starting at index. If found, return the index; |
|
7967 |
if not found, return the result of evaluating exceptionBlock. |
|
7968 |
This is a q&d hack - not very efficient" |
|
7969 |
||
8874 | 7970 |
|same first |
368 | 7971 |
mySize "{Class: SmallInteger }" |
7972 |
checkIndex "{Class: SmallInteger }" |
|
7973 |
cmpIndex "{Class: SmallInteger }" |
|
7974 |
sz "{Class: SmallInteger }"| |
|
7975 |
||
7976 |
mySize := self size. |
|
7977 |
sz := aCollection size. |
|
7978 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
7979 |
first := aCollection at:1. |
|
7980 |
||
8874 | 7981 |
checkIndex := startIndex - 1. |
16242 | 7982 |
[ |
7983 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
|
7984 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
7985 |
||
7986 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
7987 |
^ 0 |
|
7988 |
]. |
|
7989 |
same := true. |
|
7990 |
cmpIndex := 1. |
|
7991 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
7992 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
7993 |
same := false |
|
7994 |
] ifTrue:[ |
|
7995 |
cmpIndex := cmpIndex + 1 |
|
7996 |
] |
|
7997 |
]. |
|
7998 |
same ifTrue:[^ checkIndex]. |
|
7999 |
] loop. |
|
8874 | 8000 |
|
8001 |
" |
|
8002 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
8003 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
8004 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
8005 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
8006 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
8007 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 8008 |
" |
602 | 8009 |
! |
8010 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8011 |
lastIndexOfSubCollection:aCollection |
8874 | 8012 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8013 |
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
|
8014 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8015 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8016 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8017 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8018 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8019 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8020 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8021 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8022 |
"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
|
8023 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8024 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8025 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8026 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8027 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8028 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8029 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8030 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8031 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 8032 |
"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
|
8033 |
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
|
8034 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8035 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8036 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8037 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8038 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8039 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8040 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8041 |
"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
|
8042 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8043 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8044 |
|
8874 | 8045 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8046 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8047 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8048 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8049 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8050 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8051 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8052 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8053 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8054 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8055 |
|
8874 | 8056 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8057 |
checkIndex > mySize ifTrue:[ |
16242 | 8058 |
checkIndex := mySize + 1 |
8059 |
]. |
|
8060 |
[ |
|
8061 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
|
8062 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
8063 |
||
8064 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
8065 |
^ 0 |
|
8066 |
]. |
|
8067 |
same := true. |
|
8068 |
cmpIndex := 1. |
|
8069 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
8070 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
8071 |
same := false |
|
8072 |
] ifTrue:[ |
|
8073 |
cmpIndex := cmpIndex + 1 |
|
8074 |
] |
|
8075 |
]. |
|
8076 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
8077 |
] loop. |
|
8874 | 8078 |
|
8079 |
" |
|
8080 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
8081 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
8082 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
8083 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
8084 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
8085 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
8086 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
8087 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
8088 |
#(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
|
8089 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8090 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8091 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
15214
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8092 |
! |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8093 |
|
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8094 |
map:values at:key ifAbsent:exceptionValue |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8095 |
"the receiver is interpreted as a collection of keys; |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8096 |
find key in the receiver and return the corresponding value |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8097 |
from the valuesCollection argument." |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8098 |
|
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8099 |
|idx| |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8100 |
|
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8101 |
idx := self indexOf:key. |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8102 |
idx == 0 ifTrue:[^ exceptionValue value]. |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8103 |
^ values at:idx |
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8104 |
|
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8105 |
" |
15215
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8106 |
#(16 32 128 256 512 1024) |
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8107 |
map: #('ipc4' 'ipc5' 'ic07' 'ic08' 'ic09' 'ic10') |
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8108 |
at:128 ifAbsent:nil |
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8109 |
|
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8110 |
#(16 32 128 256 512 1024) |
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8111 |
map: #('ipc4' 'ipc5' 'ic07' 'ic08' 'ic09' 'ic10') |
14ec9c600861
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15214
diff
changeset
|
8112 |
at:64 ifAbsent:nil |
15214
51fa92a56848
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
15174
diff
changeset
|
8113 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8114 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8115 |
|
7264 | 8116 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8117 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8118 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8119 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8120 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8121 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8122 |
|
10194 | 8123 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8124 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8125 |
" |
8874 | 8126 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8127 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8128 |
returns: true - if found |
10194 | 8129 |
false - if not found |
8874 | 8130 |
" |
8131 |
||
8132 |
" |
|
8133 |
#(10 20 30 40 50 60 70) includes:99 |
|
8134 |
#(10 20 30 40 50 60 70) includes:40 |
|
8135 |
#(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
|
8136 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8137 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8138 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8139 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
8140 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8141 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8142 |
"search the collection for anElement; |
602 | 8143 |
if found, return the index otherwise return 0. |
8874 | 8144 |
The comparison is done using = |
602 | 8145 |
(i.e. equality test - not identity test)." |
8146 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8147 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8148 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8149 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8150 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8151 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8152 |
returns: elementIndex - if found |
8874 | 8153 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8154 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8155 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8156 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8157 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8158 |
#(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
|
8159 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8160 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8161 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8162 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8163 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8164 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8165 |
"search the collection for anElement; |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
8166 |
if found, return the index; otherwise return the value of the exceptionBlock. |
8874 | 8167 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8168 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8169 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8170 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8171 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8172 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8173 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8174 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8175 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8176 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8177 |
args: anElement : <object> |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
8178 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8179 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8180 |
returns: elementIndex - if found |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
8181 |
exceptionBlock value - if not found |
8874 | 8182 |
" |
8183 |
||
8184 |
" |
|
8185 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
8186 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
8187 |
#(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
|
8188 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8189 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8190 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8191 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8192 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8193 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 8194 |
"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
|
8195 |
stopping the search at stop. If found, replace the element by replacement |
14144 | 8196 |
and return the index. If not found, return 0. |
8197 |
The comparison is done using = |
|
8198 |
(i.e. equality test - not identity test)." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8199 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8200 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8201 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8202 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8203 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
14144 | 8204 |
self at:idx put:replacement. |
8205 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8206 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8207 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8208 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8209 |
" |
8874 | 8210 |
args: elementToFind : <object> |
14144 | 8211 |
replacement : <integer> |
8212 |
start : <integer> |
|
8213 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8214 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8215 |
returns: elementIndex - if found (and replaced) |
14144 | 8216 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8217 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8218 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8219 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8220 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8221 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8222 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8223 |
(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
|
8224 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8225 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8226 |
|
14144 | 8227 |
"Modified: / 20-05-1998 / 14:59:30 / cg" |
8228 |
"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
|
8229 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8230 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8231 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8232 |
"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
|
8233 |
if found, return the index otherwise return 0. |
8874 | 8234 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8235 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8236 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8237 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8238 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8239 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8240 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8241 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8242 |
startIndex to:stop do:[:index | |
8874 | 8243 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8244 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8245 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8246 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8247 |
" |
8874 | 8248 |
args: anElement : <object> |
8249 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8250 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8251 |
returns: elementIndex - if found |
8874 | 8252 |
0 - if not found |
8253 |
" |
|
8254 |
||
8255 |
" |
|
8256 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
8257 |
#(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
|
8258 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8259 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8260 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8261 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8262 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8263 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8264 |
"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
|
8265 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8266 |
If found (within the range), return the index, otherwise return 0. |
8874 | 8267 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8268 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8269 |
|
602 | 8270 |
|startIndex "{ Class: SmallInteger }" |
8271 |
stopIndex "{ Class: SmallInteger }" | |
|
8272 |
||
8273 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8274 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8275 |
startIndex to:stopIndex do:[:index | |
8874 | 8276 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8277 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8278 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8279 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8280 |
" |
8874 | 8281 |
args: anElement : <object> |
8282 |
start : <integer> |
|
8283 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8284 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8285 |
returns: elementIndex - if found |
8874 | 8286 |
exceptionBlock value - if not found |
8287 |
" |
|
8288 |
||
8289 |
" |
|
8290 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
8291 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
8292 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
8293 |
#(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
|
8294 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8295 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8296 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8297 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8298 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8299 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8300 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8301 |
"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
|
8302 |
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
|
8303 |
exceptionBlock. |
8874 | 8304 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8305 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8306 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8307 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8308 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8309 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8310 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8311 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8312 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8313 |
" |
8874 | 8314 |
args: anElement : <object> |
8315 |
start : <integer> |
|
8316 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8317 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8318 |
returns: elementIndex - if found |
8874 | 8319 |
exceptionBlock value - if not found |
8320 |
" |
|
8321 |
||
8322 |
" |
|
8323 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
8324 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
8325 |
#(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
|
8326 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8327 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8328 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8329 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8330 |
|
17217
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8331 |
indexOfAny:aCollection |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8332 |
"search the collection for an element in aCollection. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8333 |
if found, return the index otherwise return 0. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8334 |
The comparison is done using = |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8335 |
(i.e. equality test - not identity test). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8336 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8337 |
Notice, that for big collections, the runtime of this search |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8338 |
grows proportional to size(receiver) * size(aCollection). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8339 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8340 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8341 |
^ self indexOfAny:aCollection startingAt:1 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8342 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8343 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8344 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8345 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8346 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8347 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8348 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8349 |
indexOfAny:aCollection startingAt:start |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8350 |
"search the collection for an element in aCollection, |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8351 |
starting the search at index start; |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8352 |
if found, return the index otherwise return 0. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8353 |
The comparison is done using = |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8354 |
(i.e. equality test - not identity test). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8355 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8356 |
Notice, that for big collections, the runtime of this search |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8357 |
grows proportional to size(receiver) * size(aCollection). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8358 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8359 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8360 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8361 |
|startIndex "{ Class: SmallInteger }" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8362 |
stop "{ Class: SmallInteger }" | |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8363 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8364 |
startIndex := start. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8365 |
stop := self size. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8366 |
startIndex to:stop do:[:index | |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8367 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8368 |
]. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8369 |
^ 0 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8370 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8371 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8372 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8373 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8374 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8375 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8376 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8377 |
"search the collection for an element in aCollection, |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8378 |
starting the search at index start; |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8379 |
if found, return the index. If not, return the value returned by exceptionBlock. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8380 |
The comparison is done using = |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8381 |
(i.e. equality test - not identity test). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8382 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8383 |
Notice, that for big collections, the runtime of this search |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8384 |
grows proportional to size(receiver) * size(aCollection). |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8385 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8386 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8387 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8388 |
|val| |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8389 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8390 |
val := self indexOfAny:aCollection startingAt:start . |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8391 |
val == 0 ifTrue:[^ exceptionBlock value]. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8392 |
^ val. |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8393 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8394 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8395 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8396 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8397 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(99 88 77) startingAt:5 ifAbsent:['oops'] |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8398 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8399 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8400 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8401 |
indexOfAnyOf:aCollection |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8402 |
"squeak compatibility: same as #indexOfAny:" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8403 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8404 |
^ self indexOfAny:aCollection |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8405 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8406 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8407 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40 30 50) |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8408 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8409 |
'abcdefg' indexOfAnyOf:(CharacterSet newFrom:'cef') |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8410 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8411 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8412 |
"Modified: / 28-01-2011 / 18:03:14 / cg" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8413 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8414 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8415 |
indexOfAnyOf:aCollection startingAt:start |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8416 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8417 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8418 |
^ self indexOfAny:aCollection startingAt:start |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8419 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8420 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8421 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40 30 50) startingAt:2 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8422 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) startingAt:2 |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8423 |
" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8424 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8425 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8426 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8427 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8428 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8429 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8430 |
|
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8431 |
! |
0ce2fc23cd3c
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17214
diff
changeset
|
8432 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8433 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8434 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8435 |
if found, return the index otherwise return 0. |
8874 | 8436 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8437 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8438 |
|
8874 | 8439 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8440 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8441 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8442 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8443 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8444 |
returns: elementIndex - if found |
8874 | 8445 |
0 - if not found |
8446 |
" |
|
8447 |
||
8448 |
" |
|
8449 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
8450 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
8451 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
8452 |
#(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
|
8453 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8454 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8455 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8456 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8457 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8458 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8459 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8460 |
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
|
8461 |
exceptionBlock. |
8874 | 8462 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8463 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8464 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8465 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8466 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8467 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8468 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8469 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8470 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8471 |
" |
8874 | 8472 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
8473 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
8474 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
8475 |
#(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
|
8476 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8477 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8478 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8479 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8480 |
"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
|
8481 |
if found, return the index otherwise return 0. |
8874 | 8482 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8483 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8484 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8485 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8486 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8487 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8488 |
startIndex to:1 by:-1 do:[:index | |
8874 | 8489 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 8490 |
]. |
8491 |
^ 0 |
|
8492 |
||
8493 |
" |
|
8874 | 8494 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
8495 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
8496 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
8497 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
8498 |
#(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
|
8499 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8500 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8501 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8502 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 8503 |
"search the collection backwards for anElement starting the search at |
8504 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8505 |
otherwise return the value of the exceptionBlock. |
8874 | 8506 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8507 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8508 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8509 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8510 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8511 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8512 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8513 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8514 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8515 |
" |
8874 | 8516 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
8517 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
8518 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
8519 |
#(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
|
8520 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8521 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8522 |
|
4788 | 8523 |
lastIndexOfAny:aCollection |
8524 |
"search the collection backwards for any in aCollection; |
|
8525 |
if found, return the index, otherwise return 0. |
|
8874 | 8526 |
The comparison is done using = |
4788 | 8527 |
(i.e. equality test - not identity test)." |
8528 |
||
8874 | 8529 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 8530 |
|
8531 |
" |
|
8532 |
args: aCollection : <collection> |
|
8533 |
||
8534 |
returns: elementIndex - if found |
|
8874 | 8535 |
0 - if not found |
8536 |
" |
|
8537 |
||
8538 |
" |
|
8539 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 8540 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 8541 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
8542 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 8543 |
" |
8544 |
||
8545 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
8546 |
! |
|
8547 |
||
8548 |
lastIndexOfAny:aCollection startingAt:start |
|
8549 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
8550 |
if found, return the index, otherwise return 0. |
|
8874 | 8551 |
The comparison is done using = |
4788 | 8552 |
(i.e. equality test - not identity test)." |
8553 |
||
8554 |
|startIndex "{ Class: SmallInteger }"| |
|
8555 |
||
8556 |
startIndex := start. |
|
8557 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 8558 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 8559 |
]. |
8560 |
^ 0 |
|
8561 |
||
8562 |
" |
|
8874 | 8563 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
8564 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 8565 |
" |
8566 |
! |
|
8567 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8568 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8569 |
"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
|
8570 |
stopping at:stop; |
13719 | 8571 |
if found, return the index otherwise return 0. |
8874 | 8572 |
The comparison is done using = |
13719 | 8573 |
(i.e. equality test - not identity test)." |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8574 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8575 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8576 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8577 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8578 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8579 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8580 |
startIndex to:stop do:[:index | |
13719 | 8581 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8582 |
]. |
13719 | 8583 |
^ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8584 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8585 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8586 |
args: anElement : <object> |
13719 | 8587 |
start : <integer> |
8588 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8589 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8590 |
returns: elementIndex - if found |
13719 | 8591 |
nil - if not found |
8874 | 8592 |
" |
8593 |
||
8594 |
" |
|
8595 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
8596 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
8597 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 8598 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8599 |
|
13719 | 8600 |
"Modified: / 23-09-2011 / 14:03:05 / cg" |
602 | 8601 |
! |
8602 |
||
8603 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
8604 |
"search the collection for anElement, starting the search at index start |
|
8605 |
and stopping at stop; |
|
14144 | 8606 |
if found, return the index otherwise return the value of the exceptionBlock. |
8874 | 8607 |
The comparison is done using = |
602 | 8608 |
(i.e. equality test - not identity test)." |
8609 |
||
8610 |
|index| |
|
8611 |
||
8612 |
index := self nextIndexOf:anElement from:start to:stop. |
|
13719 | 8613 |
index == 0 ifTrue:[^ exceptionBlock value]. |
602 | 8614 |
^ index |
8615 |
||
8616 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8617 |
args: anElement : <object> |
13719 | 8618 |
start : <integer> |
8619 |
stop : <integer> |
|
8620 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8621 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8622 |
returns: elementIndex - if found |
13719 | 8623 |
valueImplementor value - if not found |
8874 | 8624 |
" |
8625 |
||
8626 |
" |
|
8627 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
8628 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
8629 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 8630 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8631 |
|
13719 | 8632 |
"Modified: / 23-09-2011 / 14:03:36 / cg" |
14144 | 8633 |
"Modified (comment): / 23-05-2012 / 13:29:42 / cg" |
2275 | 8634 |
! |
8635 |
||
8636 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
8637 |
"search the collection for anElement, starting the search at index start; |
|
8638 |
ending at stop, going in reverse direction. |
|
13719 | 8639 |
If found (within the range), return the index, otherwise return 0. |
8874 | 8640 |
The comparison is done using = |
13719 | 8641 |
(i.e. equality test - not identity test)." |
2275 | 8642 |
|
8643 |
|startIndex "{ Class: SmallInteger }" |
|
8644 |
stopIndex "{ Class: SmallInteger }" | |
|
8645 |
||
8646 |
startIndex := startSearchIndex. |
|
8647 |
stopIndex := endSearchIndex max:1. |
|
8648 |
startIndex to:stopIndex by:-1 do:[:index | |
|
13719 | 8649 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 8650 |
]. |
13719 | 8651 |
^ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8652 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8653 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8654 |
args: anElement : <object> |
13719 | 8655 |
start : <integer> |
8656 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8657 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8658 |
returns: elementIndex - if found |
13719 | 8659 |
nil - if not found |
8874 | 8660 |
" |
8661 |
||
8662 |
" |
|
8663 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
8664 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
8665 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 8666 |
" |
8667 |
||
13719 | 8668 |
"Modified: / 23-09-2011 / 14:02:36 / cg" |
2275 | 8669 |
! |
8670 |
||
8671 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
8672 |
"search the collection for anElement, starting the search at index start |
|
8673 |
and stopping at stop, doing a reverse search; |
|
8674 |
if found, return the index otherwise return the value of the |
|
8675 |
exceptionBlock. |
|
8874 | 8676 |
The comparison is done using = |
2275 | 8677 |
(i.e. equality test - not identity test)." |
8678 |
||
8679 |
|index| |
|
8680 |
||
8681 |
index := self prevIndexOf:anElement from:start to:stop. |
|
8682 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
8683 |
^ index |
|
8684 |
||
8685 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8686 |
args: anElement : <object> |
8874 | 8687 |
start : <integer> |
8688 |
stop : <integer> |
|
8689 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8690 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8691 |
returns: elementIndex - if found |
8874 | 8692 |
valueImplementor value - if not found |
8693 |
" |
|
8694 |
||
8695 |
" |
|
8696 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
8697 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
8698 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 8699 |
" |
8700 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8701 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8702 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8703 |
|
7264 | 8704 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8705 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8706 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8707 |
"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
|
8708 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8709 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8710 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8711 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8712 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8713 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8714 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8715 |
returns: elementIndex - if found |
8874 | 8716 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8717 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8718 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8719 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8720 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8721 |
#(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
|
8722 |
#(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
|
8723 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8724 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8725 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8726 |
#(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
|
8727 |
#(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
|
8728 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8729 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8730 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8731 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8732 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8733 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8734 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8735 |
"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
|
8736 |
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
|
8737 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8738 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8739 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8740 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8741 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8742 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8743 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8744 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8745 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8746 |
args: anElement : <object> |
8874 | 8747 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8748 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8749 |
returns: elementIndex - if found |
8874 | 8750 |
exceptionBlock value - if not found |
8751 |
" |
|
8752 |
||
8753 |
" |
|
8754 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
8755 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
8756 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
8757 |
#(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
|
8758 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8759 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8760 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8761 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8762 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8763 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8764 |
"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
|
8765 |
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
|
8766 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8767 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8768 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8769 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8770 |
args: anElement : <object> |
8874 | 8771 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8772 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8773 |
returns: elementIndex - if found |
8874 | 8774 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8775 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8776 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8777 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8778 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8779 |
"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
|
8780 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8781 |
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
|
8782 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8783 |
|startIndex "{ Class: SmallInteger }" |
8874 | 8784 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8785 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8786 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8787 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8788 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8789 |
startIndex to:stop do:[:index | |
8874 | 8790 |
el := self at:index. |
8791 |
anElement == el ifTrue:[^ index]. |
|
8792 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8793 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8794 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8795 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8796 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8797 |
args: anElement : <object> |
8874 | 8798 |
anotherElement : <object> |
8799 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8800 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8801 |
returns: elementIndex - if found |
8874 | 8802 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8803 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8804 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8805 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8806 |
#(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
|
8807 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8808 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
8809 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8810 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8811 |
"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
|
8812 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8813 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8814 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8815 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8816 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8817 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8818 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8819 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8820 |
startIndex to:stop do:[:index | |
8874 | 8821 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8822 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8823 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8824 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8825 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8826 |
args: anElement : <object> |
8874 | 8827 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8828 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8829 |
returns: elementIndex - if found |
8874 | 8830 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8831 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8832 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8833 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8834 |
#(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
|
8835 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 8836 |
#(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
|
8837 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8838 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8839 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8840 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8841 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8842 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8843 |
"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
|
8844 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8845 |
If found (within the range), return the index, otherwise return 0. |
14144 | 8846 |
The comparison is done using == |
8847 |
(i.e. identity test - not equality test)." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8848 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8849 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8850 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8851 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8852 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8853 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8854 |
startIndex to:stopIndex do:[:index | |
14144 | 8855 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8856 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8857 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8858 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8859 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8860 |
args: anElement : <object> |
14144 | 8861 |
start : <integer> |
8862 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8863 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8864 |
returns: elementIndex - if found |
14144 | 8865 |
0 - if not found |
8866 |
" |
|
8867 |
||
8868 |
"Created: / 12-04-1996 / 18:23:07 / cg" |
|
8869 |
"Modified: / 20-05-1998 / 15:00:50 / cg" |
|
8870 |
"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
|
8871 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8872 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8873 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8874 |
"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
|
8875 |
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
|
8876 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8877 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8878 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8879 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8880 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8881 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8882 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8883 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8884 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8885 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8886 |
args: anElement : <object> |
8874 | 8887 |
start : <integer> |
8888 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8889 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8890 |
returns: elementIndex - if found |
8874 | 8891 |
exceptionBlock value - if not found |
8892 |
" |
|
8893 |
||
8894 |
" |
|
8895 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
8896 |
#(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
|
8897 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8898 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8899 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8900 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8901 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8902 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8903 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8904 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8905 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8906 |
|
10194 | 8907 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8908 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8909 |
" |
8874 | 8910 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8911 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8912 |
returns: true - if found |
10194 | 8913 |
false - if not found |
8874 | 8914 |
" |
8915 |
||
8916 |
" |
|
8917 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
8918 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
8919 |
#(10 20 30 40 50 60 70) includes:40 |
|
8920 |
#(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
|
8921 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8922 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8923 |
|
8874 | 8924 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
8925 |
#(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
|
8926 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8927 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8928 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8929 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8930 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8931 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8932 |
"search the collection backwards for anElement; |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8933 |
if found, return the index, otherwise return 0. |
8874 | 8934 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8935 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8936 |
|
8874 | 8937 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8938 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8939 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8940 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8941 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8942 |
returns: elementIndex - if found |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8943 |
0 - if not found |
8874 | 8944 |
" |
8945 |
||
8946 |
" |
|
8947 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
8948 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
8949 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
8950 |
#(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
|
8951 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8952 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8953 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8954 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8955 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8956 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8957 |
"search the collection backwards for anElement; |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8958 |
if found, return the index, |
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8959 |
otherwise return the value of the exceptionBlock. |
8874 | 8960 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8961 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8962 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8963 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8964 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8965 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8966 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8967 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8968 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8969 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8970 |
args: anElement : <object> |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8971 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8972 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8973 |
returns: elementIndex - if found |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8974 |
exceptionBlock value - if not found |
8874 | 8975 |
" |
8976 |
||
8977 |
" |
|
8978 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
8979 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
8980 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
8981 |
#(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
|
8982 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8983 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8984 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8985 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8986 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8987 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8988 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8989 |
"search the collection backwards for anElement, starting the search at index start; |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8990 |
if found, return the index, otherwise return 0. |
8874 | 8991 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8992 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8993 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8994 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8995 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8996 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8997 |
startIndex to:1 by:-1 do:[:index | |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
8998 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
8999 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9000 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9001 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9002 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9003 |
args: anElement : <object> |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
9004 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9005 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9006 |
returns: elementIndex - if found |
17542
fd5a173afb50
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
17487
diff
changeset
|
9007 |
0 - if not found |
8874 | 9008 |
" |
9009 |
||
9010 |
" |
|
9011 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
9012 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
9013 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
9014 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
9015 |
#(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
|
9016 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9017 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9018 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9019 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9020 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9021 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 9022 |
"search the collection backwards for anElement starting the search at |
9023 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9024 |
otherwise return the value of the exceptionBlock. |
8874 | 9025 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9026 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9027 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9028 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9029 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9030 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9031 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9032 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9033 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9034 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9035 |
args: anElement : <object> |
8874 | 9036 |
start : <integer> |
9037 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9038 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9039 |
returns: elementIndex - if found |
8874 | 9040 |
exceptionBlock value - if not found |
9041 |
" |
|
9042 |
||
9043 |
" |
|
9044 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
9045 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
9046 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
9047 |
#(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
|
9048 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9049 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9050 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
9051 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 9052 |
! ! |
9053 |
||
9054 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
9055 |
||
6958 | 9056 |
detectFirstInOrder:sortBlock |
9057 |
"find the first element of the collection sorted with sortBlock" |
|
9058 |
||
9059 |
|selectedForNow start| |
|
9060 |
||
9061 |
start := true. |
|
9062 |
self do:[:element| |
|
8874 | 9063 |
start ifTrue:[ |
9064 |
start := false. |
|
9065 |
selectedForNow := element. |
|
9066 |
] ifFalse:[ |
|
9067 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
9068 |
selectedForNow := element. |
|
9069 |
]. |
|
9070 |
]. |
|
6958 | 9071 |
]. |
9072 |
||
9073 |
^ selectedForNow |
|
9074 |
||
9075 |
" |
|
8874 | 9076 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
9077 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
9078 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
9079 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 9080 |
" |
9081 |
! |
|
9082 |
||
14102 | 9083 |
randomShuffle |
9084 |
"random shuffle my elements in place. |
|
9085 |
This is a destructive algorithm. |
|
9086 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
9087 |
See 'The Art of Computer Programming'." |
|
9088 |
||
9089 |
self size to:2 by:-1 do:[:endIndex | |
|
9090 |
|rndIndex t| |
|
9091 |
||
9092 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
9093 |
"/ exchange |
|
9094 |
t := self at:rndIndex. |
|
9095 |
self at:rndIndex put:(self at:endIndex). |
|
9096 |
self at:endIndex put:t. |
|
9097 |
]. |
|
9098 |
||
9099 |
" |
|
9100 |
(1 to:10) asOrderedCollection randomShuffle |
|
9101 |
||
9102 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9103 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9104 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9105 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9106 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9107 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
9108 |
" |
|
9109 |
||
9110 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
|
9111 |
||
9112 |
|a score| |
|
9113 |
||
9114 |
a := 'abcd'. |
|
9115 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
9116 |
||
9117 |
1000000 times do:[ |
|
9118 |
|sorted| |
|
9119 |
sorted := a copy randomShuffle. |
|
9120 |
score at:sorted put:(score at:sorted)+1. |
|
9121 |
]. |
|
9122 |
||
9123 |
score keys asSortedCollection do:[:k | |
|
9124 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
9125 |
]. |
|
9126 |
" |
|
9127 |
||
9128 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
9129 |
! |
|
9130 |
||
9131 |
reverse |
|
9132 |
"destructively reverse the order of the elements inplace. |
|
9133 |
WARNING: this is a destructive operation, which modifies the receiver. |
|
9134 |
Please use reversed (with a d) for a functional version." |
|
9135 |
||
9136 |
|lowIndex "{ Class: SmallInteger }" |
|
9137 |
hiIndex "{ Class: SmallInteger }" |
|
9138 |
t1 t2| |
|
9139 |
||
9140 |
hiIndex := self size. |
|
9141 |
lowIndex := 1. |
|
9142 |
[lowIndex < hiIndex] whileTrue:[ |
|
9143 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
|
9144 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
9145 |
||
9146 |
lowIndex := lowIndex + 1. |
|
9147 |
hiIndex := hiIndex - 1 |
|
9148 |
] |
|
9149 |
||
9150 |
" |
|
9151 |
#(4 5 6 7 7) reverse |
|
9152 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
9153 |
" |
|
9154 |
! |
|
9155 |
||
9156 |
reversed |
|
9157 |
"return a copy with elements in reverse order" |
|
9158 |
||
9159 |
^ self copy reverse |
|
9160 |
||
9161 |
" |
|
9162 |
#(4 5 6 7 7) reversed |
|
9163 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
|
9164 |
" |
|
9165 |
! |
|
9166 |
||
9167 |
sort |
|
9168 |
"sort the collection inplace. The elements are compared using |
|
9169 |
'<' i.e. they should offer a magnitude-like protocol. |
|
16566
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9170 |
WARNING: this is a destructive operation, which modifies the receiver. |
14102 | 9171 |
|
9172 |
The implementation uses the quicksort algorithm, which may not be |
|
9173 |
the best possible for all situations (quickSort has O-square worst |
|
9174 |
case behavior)." |
|
9175 |
||
9176 |
self quickSort |
|
9177 |
||
9178 |
" |
|
9179 |
#(1 16 7 98 3 19 4 0) sort |
|
9180 |
||
9181 |
|data| |
|
9182 |
data := Random new next:100000. |
|
9183 |
Transcript show:'sort random '; showCR:(Time millisecondsToRun:[data sort]). |
|
9184 |
Transcript show:'sort sorted '; showCR:(Time millisecondsToRun:[data sort]). |
|
9185 |
data reverse. |
|
9186 |
Transcript show:'sort reverse '; showCR:(Time millisecondsToRun:[data sort]). |
|
9187 |
" |
|
9188 |
||
9189 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
|
9190 |
! |
|
9191 |
||
9192 |
sort:sortBlock |
|
9193 |
"sort the collection inplace using the 2-arg block sortBlock |
|
9194 |
for comparison. This allows any sort criteria to be implemented. |
|
9195 |
||
9196 |
The implementation uses the quicksort algorithm, which may not be |
|
9197 |
the best possible for all situations (quickSort has O-square worst |
|
9198 |
case behavior)." |
|
9199 |
||
9200 |
self quickSort:sortBlock |
|
9201 |
||
9202 |
" |
|
9203 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
9204 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
9205 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
9206 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
9207 |
" |
|
9208 |
||
9209 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
|
9210 |
! |
|
9211 |
||
9212 |
sort:sortBlock with:aCollection |
|
9213 |
"sort the collection inplace using the 2-arg block sortBlock |
|
9214 |
for comparison. Also reorder the elements in aCollection. |
|
9215 |
Use this, when you have a key collection to sort some other collection with. |
|
9216 |
||
9217 |
The implementation uses the quicksort algorithm, which may not be |
|
9218 |
the best possible for all situations (quickSort has O-square worst |
|
9219 |
case behavior)." |
|
9220 |
||
9221 |
self quickSort:sortBlock with:aCollection |
|
9222 |
||
9223 |
" |
|
9224 |
|c1 c2| |
|
9225 |
c1 := #(1 16 7 9). |
|
9226 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
9227 |
c1 sort:[:a :b | a > b] with:c2. |
|
9228 |
c1 printNL. |
|
9229 |
c2 printNL |
|
9230 |
" |
|
9231 |
||
9232 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
|
9233 |
! |
|
9234 |
||
9235 |
sortByApplying:aBlock |
|
9236 |
"Sort my contents inplace based on the value of what aBlock returns for each element. |
|
9237 |
Similar to, but even more flexible than sortBySelector." |
|
9238 |
||
9239 |
^ self sort:[:a :b | (aBlock value:a) < (aBlock value:b)] |
|
9240 |
||
9241 |
" |
|
9242 |
replace all uses of sort as in: |
|
9243 |
... sort:[:a :b | a foo < b foo] |
|
9244 |
by: |
|
9245 |
... sortByApplying:[:each | each foo] |
|
9246 |
" |
|
9247 |
! |
|
9248 |
||
9249 |
sortBySelector:aSelector |
|
9250 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
9251 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
9252 |
||
9253 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
9254 |
||
9255 |
" |
|
9256 |
replace all uses of sort as in: |
|
9257 |
... sort:[:a :b | a foo < b foo] |
|
9258 |
by: |
|
9259 |
... sortBySelector:#foo |
|
9260 |
||
9261 |
find these by searching for code matching (code-search in the browsers method list): |
|
9262 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
9263 |
" |
|
9264 |
! |
|
9265 |
||
9266 |
sortWith:aCollection |
|
9267 |
"sort the receiver collection inplace, using '<' to compare elements. |
|
9268 |
Also, the elements of aCollection are reordered with it. |
|
9269 |
Use this, when you have a key collection to sort another collection with. |
|
9270 |
||
9271 |
The implementation uses the quicksort algorithm, which may not be |
|
9272 |
the best possible for all situations (quickSort has O-square worst |
|
9273 |
case behavior)." |
|
9274 |
||
9275 |
self quickSortWith:aCollection |
|
9276 |
||
9277 |
" |
|
9278 |
|indices names| |
|
9279 |
||
9280 |
names := #('nine' 'five' 'eight' 'one' 'four' 'two') copy. |
|
9281 |
indices := #(9 5 8 1 4 2) copy. |
|
9282 |
indices sortWith:names. |
|
9283 |
names. |
|
9284 |
indices |
|
9285 |
" |
|
9286 |
||
9287 |
"Modified (comment): / 18-01-2012 / 11:30:01 / cg" |
|
9288 |
! |
|
9289 |
||
9290 |
stableSort |
|
9291 |
"sort the collection inplace. The elements are compared using |
|
9292 |
'<=' i.e. they should offer a magnitude-like protocol. |
|
9293 |
||
9294 |
Use a stable sort algorithm - i.e. elements having an equal key will keep |
|
9295 |
their previous order." |
|
9296 |
||
9297 |
self mergeSort |
|
9298 |
||
9299 |
" |
|
9300 |
#(1 16 7 98 3 19 4 0) stableSort |
|
9301 |
||
9302 |
|data| |
|
9303 |
data := Random new next:100000. |
|
9304 |
Transcript show:'sort random '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
9305 |
Transcript show:'sort sorted '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
9306 |
data reverse. |
|
9307 |
Transcript show:'sort reverse '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
9308 |
" |
|
9309 |
! |
|
9310 |
||
9311 |
stableSort:sortBlock |
|
9312 |
"sort the collection inplace using the 2-arg block sortBlock |
|
9313 |
for comparison. This allows any sort criteria to be implemented. |
|
9314 |
||
9315 |
Use a stable sort algorithm - i.e. elements having an equal key will keep |
|
9316 |
their previous order. |
|
9317 |
||
9318 |
NOTE: the sort algorithm will be stable, if the sortblock uses #< or #> for comparison!! |
|
9319 |
Do not use #<= of #>= if you want stable behavior." |
|
9320 |
||
9321 |
self mergeSort:sortBlock |
|
9322 |
||
9323 |
" |
|
9324 |
The 4@bla points keep their order: |
|
9325 |
{(4@1). (8@2). (4@2). (3@3). (4@3). (-1@4). (17@17). (19@19). |
|
9326 |
(12 @ 12). (13@13). (14@14). (15@15). (10@10). (8@8).} stableSort:[:a :b | a x < b x] |
|
9327 |
||
9328 |
But not with quickSort: |
|
9329 |
{(4@1). (8@2). (4@2). (3@3). (4@3). (-1@4). (17@17). (19@19). |
|
9330 |
(12 @ 12). (13@13). (14@14). (15@15). (10@10). (8@8).} sort:[:a :b | a x < b x] |
|
9331 |
" |
|
9332 |
! |
|
9333 |
||
9334 |
topologicalSort:sortBlock |
|
9335 |
"sort the collection inplace using a sloooow sort algorithm. |
|
9336 |
This algorithm has O-square runtime behavior and should be used only |
|
9337 |
in special situations. |
|
9338 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
9339 |
a>c (for example, to sort classes by inheritance) |
|
9340 |
||
9341 |
In all other situations, use #sort; which implements the quicksort algorithm. |
|
9342 |
||
9343 |
If there are cycles, this algorithm may loop endless!! |
|
9344 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
9345 |
" |
|
9346 |
||
9347 |
|smallestIndex "{ Class: SmallInteger }" |
|
9348 |
end "{ Class: SmallInteger }" |
|
9349 |
index2 "{ Class: SmallInteger }" |
|
9350 |
smallest thisOne| |
|
9351 |
||
9352 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
9353 |
||
9354 |
end := self size. |
|
9355 |
1 to:end-1 do:[:index | |
|
9356 |
smallest := self at:index. |
|
9357 |
smallestIndex := index. |
|
9358 |
||
9359 |
index2 := index + 1. |
|
9360 |
[ |
|
9361 |
thisOne := self at:index2. |
|
9362 |
(index2 ~~ smallestIndex |
|
9363 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
9364 |
smallestIndex := index2. |
|
9365 |
smallest := thisOne. |
|
9366 |
index2 := index + 1. |
|
9367 |
] ifFalse:[ |
|
9368 |
index2 := index2 + 1. |
|
9369 |
]. |
|
9370 |
] doWhile:[index2 <= end]. |
|
9371 |
||
9372 |
"/ (index + 1) to:end do:[:index2 | |
|
9373 |
"/ thisOne := self at:index2. |
|
9374 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
9375 |
"/ smallestIndex := index2. |
|
9376 |
"/ smallest := thisOne |
|
9377 |
"/ ]. |
|
9378 |
"/ ]. |
|
9379 |
||
9380 |
(smallestIndex ~~ index) ifTrue:[ |
|
9381 |
thisOne := self at:index. |
|
9382 |
self at:index put:smallest. |
|
9383 |
self at:smallestIndex put:thisOne |
|
9384 |
]. |
|
9385 |
] |
|
9386 |
||
9387 |
" |
|
9388 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
|
9389 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
9390 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
|
9391 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
9392 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
9393 |
" |
|
9394 |
||
9395 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
|
9396 |
! ! |
|
9397 |
||
9398 |
!SequenceableCollection methodsFor:'sorting algorithms'! |
|
9399 |
||
9400 |
heapSort |
|
9401 |
"sort the collection inplace. The elements are compared using |
|
9402 |
'<' i.e. they should offer a magnitude-like protocol. |
|
9403 |
||
9404 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
9405 |
don't keep their order. |
|
9406 |
||
9407 |
The implementation uses the heapsort algorithm, which has a complexity |
|
9408 |
of O(n*log(n)) for both average and worst case." |
|
9409 |
||
9410 |
self heapSortFrom:1 to:self size |
|
9411 |
||
9412 |
" |
|
9413 |
'franz jagt im komplett verwahrlosten taxi quer durch deutschland' copy heapSort |
|
9414 |
#( 1 2 3 4 5 6 7 8 9) heapSort |
|
9415 |
#( 1 2 3 4 5 6 7 8 9) reversed heapSort |
|
9416 |
||
9417 |
|data| |
|
9418 |
data := Random new next:500000. |
|
9419 |
Transcript show:'heap random '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
9420 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
9421 |
Transcript show:'heap sorted '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
9422 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
9423 |
data reverse. |
|
9424 |
Transcript show:'heap reverse '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
9425 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
9426 |
" |
|
9427 |
! |
|
9428 |
||
9429 |
heapSort:sortBlock |
|
9430 |
"sort the collection inplace. The elements are compared using |
|
9431 |
sortBlock. |
|
9432 |
||
9433 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
9434 |
don't keep their order. |
|
9435 |
||
9436 |
The implementation uses the heapsort algorithm, which has a complexity |
|
9437 |
of O(n*log(n)) for both average and worst case." |
|
9438 |
||
9439 |
self heapSort:sortBlock from:1 to:self size |
|
9440 |
! |
|
9441 |
||
9442 |
heapSort:sortBlock from:begin to:end |
|
9443 |
"sort the collection inplace. The elements are compared using |
|
9444 |
the sortBlock. |
|
9445 |
||
9446 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
9447 |
don't keep their order. |
|
9448 |
||
9449 |
The implementation uses the heapsort algorithm, which has a complexity |
|
9450 |
of O(n*log(n)) for average and worst case." |
|
9451 |
||
9452 |
|cRun "{Class: SmallInteger}" |
|
9453 |
parent "{Class: SmallInteger}" |
|
9454 |
child "{Class: SmallInteger}" |
|
9455 |
eRun "{Class: SmallInteger}" |
|
9456 |
childE childERight temp done size| |
|
9457 |
||
9458 |
eRun := end. |
|
9459 |
size := eRun+1-begin. |
|
9460 |
size <= 1 ifTrue:[ |
|
9461 |
^ self. |
|
9462 |
]. |
|
9463 |
||
9464 |
cRun := size // 2 + 1. "position of the last parent node" |
|
9465 |
||
16242 | 9466 |
[ |
14102 | 9467 |
cRun > begin ifTrue:[ "Phase 1: do the heap creation" |
9468 |
cRun := cRun - 1. |
|
9469 |
temp := self at:cRun. "parent value is in temp" |
|
9470 |
] ifFalse:[ "Phase 2: heap selection" |
|
9471 |
temp := self at:eRun. "save the last value on the heap" |
|
9472 |
self at:eRun put:(self at:begin). "put the largest value so far to the end of the heap" |
|
9473 |
eRun := eRun - 1. "make new heap smaller" |
|
9474 |
eRun == begin ifTrue:[ |
|
9475 |
"the heap is empty - we are done" |
|
9476 |
self at:begin put:temp. |
|
9477 |
^ self. |
|
9478 |
]. |
|
9479 |
]. |
|
9480 |
||
9481 |
parent := cRun. |
|
9482 |
child := parent * 2. "parents left child" |
|
9483 |
||
9484 |
"Sift operation - push temp down the heap" |
|
9485 |
done := false. |
|
9486 |
[done not and:[child <= eRun]] whileTrue:[ |
|
9487 |
childE := self at:child. |
|
9488 |
"choose the largest child" |
|
9489 |
(child < eRun and:[sortBlock value:childE value:(childERight := self at:child+1)]) ifTrue:[ |
|
9490 |
child := child + 1. "the right child is larger" |
|
9491 |
childE := childERight. |
|
9492 |
]. |
|
9493 |
(sortBlock value:temp value:childE) ifTrue:[ |
|
9494 |
self at:parent put:childE. "move the largest child up" |
|
9495 |
parent := child. "make child the new parent" |
|
9496 |
child := parent * 2. "get next child" |
|
9497 |
] ifFalse:[ |
|
9498 |
done := true. "now we know where to put temp" |
|
9499 |
]. |
|
9500 |
]. |
|
9501 |
self at:parent put:temp. "save temp in heap" |
|
16242 | 9502 |
] loop. |
14102 | 9503 |
! |
9504 |
||
9505 |
heapSortFrom:begin to:end |
|
9506 |
"sort the collection inplace. The elements are compared using |
|
9507 |
'<' i.e. they should offer a magnitude-like protocol. |
|
9508 |
||
9509 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
9510 |
don't keep their order. |
|
9511 |
||
9512 |
The implementation uses the heapsort algorithm, which has a complexity |
|
9513 |
of O(n*log(n)) for average and worst case." |
|
9514 |
||
9515 |
|cRun "{Class: SmallInteger}" |
|
9516 |
parent "{Class: SmallInteger}" |
|
9517 |
child "{Class: SmallInteger}" |
|
9518 |
eRun "{Class: SmallInteger}" |
|
9519 |
childE childERight temp done size| |
|
9520 |
||
9521 |
eRun := end. |
|
9522 |
size := eRun+1-begin. |
|
9523 |
size <= 1 ifTrue:[ |
|
9524 |
^ self. |
|
9525 |
]. |
|
9526 |
||
9527 |
cRun := size // 2 + 1. "position of the last parent node" |
|
9528 |
||
16242 | 9529 |
[ |
14102 | 9530 |
cRun > begin ifTrue:[ "Phase 1: do the heap creation" |
9531 |
cRun := cRun - 1. |
|
9532 |
temp := self at:cRun. "parent value is in temp" |
|
9533 |
] ifFalse:[ "Phase 2: heap selection" |
|
9534 |
temp := self at:eRun. "save the last value on the heap" |
|
9535 |
self at:eRun put:(self at:begin). "put the largest value so far to the end of the heap" |
|
9536 |
eRun := eRun - 1. "make new heap smaller" |
|
9537 |
eRun == begin ifTrue:[ |
|
9538 |
"the heap is empty - we are done" |
|
9539 |
self at:begin put:temp. |
|
9540 |
^ self. |
|
9541 |
]. |
|
9542 |
]. |
|
9543 |
||
9544 |
parent := cRun. |
|
9545 |
child := parent * 2. "parents left child" |
|
9546 |
||
9547 |
"Sift operation - push temp down the heap" |
|
9548 |
done := false. |
|
9549 |
[done not and:[child <= eRun]] whileTrue:[ |
|
9550 |
childE := self at:child. |
|
9551 |
"choose the largest child" |
|
9552 |
(child < eRun and:[childE < (childERight := self at:child+1)]) ifTrue:[ |
|
9553 |
child := child + 1. "the right child is larger" |
|
9554 |
childE := childERight. |
|
9555 |
]. |
|
9556 |
temp < childE ifTrue:[ |
|
9557 |
self at:parent put:childE. "move the largest child up" |
|
9558 |
parent := child. "make child the new parent" |
|
9559 |
child := parent * 2. "get next child" |
|
9560 |
] ifFalse:[ |
|
9561 |
done := true. "now we know where to put temp" |
|
9562 |
]. |
|
9563 |
]. |
|
9564 |
self at:parent put:temp. "save temp in heap" |
|
16242 | 9565 |
] loop. |
14102 | 9566 |
|
9567 |
||
9568 |
" |
|
9569 |
'franz jagt im komplett verwahrlosten taxi quer durch deutschland' copy heapSort |
|
9570 |
#( 1 2 3 4 5 6 7 8 9) copy heapSort |
|
9571 |
#( 1 2 3 4 5 6 7 8 9) reversed heapSort |
|
9572 |
||
9573 |
|data| |
|
9574 |
data := Random new next:500000. |
|
9575 |
Transcript show:'heap random '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
9576 |
Transcript show:'heap sorted '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
9577 |
data reverse. |
|
9578 |
Transcript show:'heap reverse '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
9579 |
" |
|
9580 |
! |
|
9581 |
||
14100 | 9582 |
insertionSort |
9583 |
"sort the collection using a insertionSort algorithm. |
|
9584 |
The elements are compared using'#<' |
|
9585 |
i.e. they should offer a magnitude-like protocol. |
|
9586 |
||
9587 |
Insertion sort sort is a stable sorting algorithm, i.e. elements with the same sort key |
|
9588 |
keep their order (if you use e.g. #<) for comparison. |
|
9589 |
||
9590 |
The implementation uses the insertionSort algorithm, |
|
9591 |
which is slow for large collections O(n*n), but good for small or |
|
9592 |
almost sorted collections O(N). |
|
9593 |
||
9594 |
See also #quickSort for other sort algorithms |
|
9595 |
with different worst- and average case behavior)" |
|
9596 |
||
9597 |
self insertionSort:[:a :b | a < b] |
|
9598 |
! |
|
9599 |
||
9600 |
insertionSort:sortBlock |
|
9601 |
|stop| |
|
9602 |
||
9603 |
stop := self size. |
|
9604 |
(stop > 1) ifTrue:[ |
|
9605 |
self insertionSort:sortBlock from:1 to:stop |
|
9606 |
]. |
|
9607 |
! |
|
9608 |
||
9609 |
insertionSort:sortBlock from:inBegin to:inEnd |
|
9610 |
"binary insertion sort. |
|
9611 |
The implementation uses the insertionSort algorithm, |
|
9612 |
which is slow for large collections O(n*n), but good for small or |
|
9613 |
almost sorted collections O(N)." |
|
9614 |
||
9615 |
|begin "{Class: SmallInteger}" |
|
9616 |
end "{Class: SmallInteger}" |
|
9617 |
prevIdx "{Class: SmallInteger}" |
|
9618 |
temp| |
|
9619 |
||
9620 |
begin := inBegin+1. |
|
9621 |
end := inEnd. |
|
9622 |
begin to:end do:[:idx| |
|
9623 |
temp := self at:idx. |
|
9624 |
prevIdx := idx-1. |
|
9625 |
"this is stable if #< or #> ist used for comparison (and not #<= or #>=)" |
|
9626 |
[prevIdx >= inBegin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
9627 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
9628 |
prevIdx := prevIdx - 1. |
|
9629 |
]. |
|
9630 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
9631 |
self at:prevIdx+1 put:temp. |
|
9632 |
]. |
|
9633 |
]. |
|
9634 |
||
9635 |
" |
|
9636 |
|data| |
|
9637 |
data := Random new next:1000. |
|
9638 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9639 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9640 |
data reverse. |
|
9641 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9642 |
||
9643 |
data := Random new next:1000. |
|
9644 |
Transcript show:'insert random '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
9645 |
Transcript show:'insert sorted '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
9646 |
data reverse. |
|
9647 |
Transcript show:'insert reverse '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
9648 |
" |
|
9649 |
! |
|
9650 |
||
5358 | 9651 |
mergeSort |
8874 | 9652 |
"sort the collection using a mergeSort algorithm. |
14100 | 9653 |
The elements are compared using '#<' |
5358 | 9654 |
i.e. they should offer a magnitude-like protocol. |
9655 |
||
14099 | 9656 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
9657 |
keep their order. |
|
9658 |
||
5358 | 9659 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 9660 |
the best possible for all situations |
14099 | 9661 |
See also #quickSort for other sort algorithms |
5358 | 9662 |
with different worst- and average case behavior)" |
9663 |
||
12888 | 9664 |
self mergeSort:[:a :b | a < b] |
5358 | 9665 |
|
9666 |
" |
|
9667 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
12888 | 9668 |
#(1 16 7 98 7 3 19 4 0) mergeSort |
5358 | 9669 |
|
14099 | 9670 |
|random data| |
9671 |
||
9672 |
random := Random new next:500000. |
|
9673 |
||
9674 |
data := random copy. |
|
9675 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9676 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9677 |
data := data reverse. |
|
9678 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
9679 |
||
9680 |
data := random copy. |
|
14100 | 9681 |
Transcript show:'quick block random '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
9682 |
Transcript show:'quick block sorted '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
|
9683 |
data := data reverse. |
|
9684 |
Transcript show:'quick block reverse '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
|
9685 |
||
9686 |
data := random copy. |
|
14099 | 9687 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
9688 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
9689 |
data := data reverse. |
|
9690 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
5358 | 9691 |
" |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9692 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9693 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9694 |
mergeSort:sortBlock |
8874 | 9695 |
"sort the collection using a mergeSort algorithm. |
14100 | 9696 |
The elements are compared using sortBlock |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9697 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9698 |
|
14099 | 9699 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
14100 | 9700 |
keep their order (if you use e.g. #< for comparison). |
14099 | 9701 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9702 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 9703 |
the best possible for all situations |
14099 | 9704 |
See also #quickSort for other sort algorithms |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9705 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9706 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9707 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9708 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9709 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9710 |
(stop > 1) ifTrue:[ |
14099 | 9711 |
self mergeSort:sortBlock from:1 to:stop |
14100 | 9712 |
]. |
9713 |
||
9714 |
" |
|
9715 |
|random data| |
|
9716 |
||
9717 |
random := Random new next:500000. |
|
9718 |
||
9719 |
data := random copy. |
|
9720 |
Transcript show:'merge block random '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
9721 |
Transcript show:'merge block sorted '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
9722 |
data := data reverse. |
|
9723 |
Transcript show:'merge block reverse '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
9724 |
||
9725 |
data := random copy. |
|
9726 |
Transcript show:'quick block random '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
9727 |
Transcript show:'quick block sorted '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
9728 |
data := data reverse. |
|
9729 |
Transcript show:'quick block reverse '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
9730 |
" |
|
9731 |
! |
|
9732 |
||
9733 |
mergeSort:aBlock from:startIndex to:stopIndex |
|
8874 | 9734 |
"Sort the given range of indices using the mergesort algorithm. |
9735 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
14099 | 9736 |
as many comparisons as heapsort or quicksort. |
9737 |
||
9738 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
|
14100 | 9739 |
keep their order (if you use e.g. #< or #> for comparison)." |
5358 | 9740 |
|
8874 | 9741 |
"Details: recursively split the range to be sorted into two halves, |
9742 |
mergesort each half, then merge the two halves together. |
|
9743 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
9744 |
and forth between the receiver and this copy. |
|
9745 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 9746 |
resulting in the receiver being completely sorted." |
14100 | 9747 |
|
9748 |
|mySize| |
|
9749 |
||
9750 |
mySize := self size. |
|
9751 |
mySize <= 1 ifTrue:[ |
|
9752 |
^ self |
|
9753 |
]. |
|
9754 |
startIndex = stopIndex ifTrue:[ |
|
9755 |
^ self |
|
9756 |
]. |
|
9757 |
(startIndex >= 1 and:[ startIndex < stopIndex ]) ifFalse:[ |
|
9758 |
self error:'bad start index' |
|
9759 |
]. |
|
9760 |
stopIndex > mySize ifTrue:[ |
|
9761 |
self error:'bad stop index' |
|
9762 |
]. |
|
9763 |
self |
|
9764 |
mergeSortFrom:startIndex |
|
9765 |
to:stopIndex |
|
9766 |
by:aBlock. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9767 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9768 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9769 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9770 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9771 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9772 |
|
14099 | 9773 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
9774 |
don't keep their order. |
|
9775 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9776 |
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
|
9777 |
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
|
9778 |
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
|
9779 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9780 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9781 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9782 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9783 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9784 |
(stop > 1) ifTrue:[ |
14099 | 9785 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9786 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9787 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9788 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9789 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 9790 |
|
9791 |
|data| |
|
14099 | 9792 |
data := Random new next:500001. |
9793 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
9794 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
9795 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8874 | 9796 |
data reverse. |
14099 | 9797 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
9798 |
||
9799 |
|data rg| |
|
9800 |
rg := Random new. |
|
9801 |
data := Array new:500001. |
|
9802 |
1 to:data size do:[:i | |
|
9803 |
data at:i put:(rg nextIntegerBetween:1 and:100). |
|
9804 |
]. |
|
9805 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
9806 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
9807 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
9808 |
data reverse. |
|
9809 |
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
|
9810 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9811 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9812 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9813 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9814 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9815 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9816 |
"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
|
9817 |
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
|
9818 |
|
14099 | 9819 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
9820 |
don't keep their order. |
|
9821 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9822 |
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
|
9823 |
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
|
9824 |
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
|
9825 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9826 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9827 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9828 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9829 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9830 |
(stop > 1) ifTrue:[ |
14099 | 9831 |
sortBlock numArgs == 3 ifTrue:[ |
9832 |
"/ TODO: pass a collating policy to aBlock |
|
9833 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
9834 |
] ifFalse:[ |
|
9835 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
9836 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9837 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9838 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9839 |
" |
8874 | 9840 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
9841 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
9842 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
9843 |
#(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
|
9844 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9845 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9846 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9847 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9848 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9849 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9850 |
"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
|
9851 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9852 |
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
|
9853 |
|
14099 | 9854 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
9855 |
don't keep their order. |
|
9856 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9857 |
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
|
9858 |
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
|
9859 |
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
|
9860 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9861 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9862 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9863 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9864 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9865 |
(stop > 1) ifTrue:[ |
14099 | 9866 |
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
|
9867 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9868 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9869 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9870 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9871 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9872 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9873 |
c1 sort:[:a :b | a > b] with:c2. |
14102 | 9874 |
Transcript showCR:c1; showCR:c2. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9875 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9876 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9877 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9878 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9879 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9880 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9881 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9882 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9883 |
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
|
9884 |
|
14099 | 9885 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
9886 |
don't keep their order. |
|
9887 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9888 |
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
|
9889 |
the best possible for all situations (quickSort has O-square worst |
14099 | 9890 |
case behavior)." |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9891 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9892 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9893 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9894 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9895 |
(stop > 1) ifTrue:[ |
14099 | 9896 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9897 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9898 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
9899 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
16566
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9900 |
! |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9901 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9902 |
sortSmallSized |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9903 |
"for small sizes, specialized sorts with minimum number |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9904 |
of comparisons are used. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9905 |
This is mostly useful if many small collections are to be sorted." |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9906 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9907 |
|n e1 e2 e3 e4 t middle1 middle2| |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9908 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9909 |
n := self size. |
16567
50923ffea206
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16566
diff
changeset
|
9910 |
n <= 1 ifTrue:[ |
50923ffea206
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16566
diff
changeset
|
9911 |
^ self |
50923ffea206
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16566
diff
changeset
|
9912 |
]. |
50923ffea206
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16566
diff
changeset
|
9913 |
|
16566
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9914 |
e1 := self at:1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9915 |
e2 := self at:2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9916 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9917 |
n == 2 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9918 |
e2 < e1 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9919 |
self at:1 put:e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9920 |
self at:2 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9921 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9922 |
^ self. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9923 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9924 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9925 |
e3 := self at:3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9926 |
n == 3 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9927 |
e2 < e1 ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9928 |
e3 < e2 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9929 |
e1 < e3 ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9930 |
self at:1 put:e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9931 |
self at:2 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9932 |
] ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9933 |
self at:1 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9934 |
self at:2 put:e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9935 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9936 |
self at:3 put:e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9937 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9938 |
^ self. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9939 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9940 |
e3 < e1 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9941 |
e2 < e3 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9942 |
self at:1 put:e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9943 |
self at:2 put:e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9944 |
] ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9945 |
self at:1 put:e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9946 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9947 |
self at:3 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9948 |
^ self. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9949 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9950 |
self at:1 put:e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9951 |
self at:2 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9952 |
^ self. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9953 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9954 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9955 |
e4 := self at:4. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9956 |
n == 4 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9957 |
e1 < e2 ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9958 |
t := e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9959 |
e1 := e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9960 |
e2 := t. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9961 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9962 |
e3 < e4 ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9963 |
t := e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9964 |
e3 := e4. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9965 |
e4 := t. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9966 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9967 |
e1 < e3 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9968 |
middle1 := e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9969 |
] ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9970 |
t := e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9971 |
e1 := e3. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9972 |
middle1 := t. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9973 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9974 |
e4 < e2 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9975 |
t := e4. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9976 |
e4 := e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9977 |
middle2 := t. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9978 |
] ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9979 |
middle2 := e2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9980 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9981 |
self at:1 put:e1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9982 |
middle1 < middle2 ifTrue:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9983 |
self at:2 put:middle1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9984 |
self at:3 put:middle2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9985 |
] ifFalse:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9986 |
self at:2 put:middle2. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9987 |
self at:3 put:middle1. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9988 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9989 |
self at:4 put:e4. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9990 |
^ self. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9991 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9992 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9993 |
self quickSortFrom:1 to:n |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9994 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9995 |
" |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9996 |
self assert: {1 . 2} sortSmallSized = #(1 2) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9997 |
self assert: {2 . 1} sortSmallSized = #(1 2) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9998 |
self assert: {1 . 1.0} sortSmallSized = #(1 1.0) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
9999 |
self assert: {1.0 . 1} sortSmallSized = #(1.0 1) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10000 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10001 |
#( 1 2 3) permutationsDo:[:p | |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10002 |
self assert: p copy sortSmallSized = #(1 2 3) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10003 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10004 |
#( 1 2 3 4) permutationsDo:[:p | |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10005 |
self assert: p copy sortSmallSized = #(1 2 3 4) |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10006 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10007 |
" |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10008 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10009 |
" |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10010 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10011 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10012 |
{ 1 . 2 } quickSort |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10013 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10014 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10015 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10016 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10017 |
{ 1 . 2 } sortSmallSized |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10018 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10019 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10020 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10021 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10022 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10023 |
{ 1 . 2 . 3} quickSort |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10024 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10025 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10026 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10027 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10028 |
{ 1 . 2 . 3} sortSmallSized |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10029 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10030 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10031 |
|
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10032 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10033 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10034 |
{ 1 . 2 . 3 . 4} quickSort |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10035 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10036 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10037 |
Time millisecondsToRun:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10038 |
1000000 timesRepeat:[ |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10039 |
{ 1 . 2 . 3 . 4} sortSmallSized |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10040 |
] |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10041 |
]. |
0e6883929d02
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
16518
diff
changeset
|
10042 |
" |
1 | 10043 |
! ! |
602 | 10044 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10045 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10046 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10047 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10048 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10049 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 10050 |
however, having a common protocol with indexed collections |
10051 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10052 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10053 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10054 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10055 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10056 |
" |
8874 | 10057 |
#(1 2 3) includesKey:4 |
10058 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10059 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10060 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
10061 |
|
19028 | 10062 |
!SequenceableCollection methodsFor:'vector arithmetic'! |
10063 |
||
10064 |
dot:aFloatVector |
|
10065 |
"Return the dot product of the receiver and the argument. |
|
19030 | 10066 |
Raises an error, if the argument is not of the same size as the receiver." |
19028 | 10067 |
|
10068 |
|mySize result| |
|
10069 |
||
10070 |
mySize := self size. |
|
19030 | 10071 |
mySize = aFloatVector size ifFalse:[ |
10072 |
^ self error:'Must be of equal size' |
|
10073 |
]. |
|
19028 | 10074 |
result := 0.0. |
10075 |
1 to: mySize do:[:i| |
|
10076 |
result := result + ((self at: i) * (aFloatVector at: i)). |
|
10077 |
]. |
|
10078 |
^result |
|
10079 |
||
10080 |
" |
|
10081 |
|v| |
|
10082 |
v := #(2.0 2.0 1.0). |
|
10083 |
v dot:v. |
|
10084 |
||
10085 |
|v| |
|
10086 |
v := #(2.0 2.0 1.0) asDoubleArray. |
|
10087 |
v dot:v. |
|
10088 |
" |
|
10089 |
! |
|
10090 |
||
10091 |
hornerMultiplyAndAdd:x |
|
10092 |
"fallback for horner's-method computation of polynomials. |
|
10093 |
The vector is interpreted as providing the factors for a polynomial, |
|
10094 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
|
10095 |
where the ai are the elements of the Array. |
|
10096 |
The highest rank factor is at the first position, the 0-rank constant at last." |
|
10097 |
||
10098 |
|mySize result| |
|
10099 |
||
10100 |
mySize := self size. |
|
10101 |
result := self at:1. |
|
10102 |
2 to: mySize do:[:i| |
|
10103 |
result := (result * x) + (self at: i). |
|
10104 |
]. |
|
10105 |
^ result |
|
10106 |
||
10107 |
" |
|
10108 |
|v| |
|
10109 |
'compute 2*10^2 + 3*10 + 4'. |
|
10110 |
v := #(2 3 4). |
|
10111 |
v hornerMultiplyAndAdd:10. |
|
10112 |
||
10113 |
|v| |
|
10114 |
'compute 2*10^2 + 3*10 + 4'. |
|
10115 |
v := #(2 3 4) asFloatArray. |
|
10116 |
v hornerMultiplyAndAdd:10. |
|
10117 |
||
10118 |
|v| |
|
10119 |
'compute 2*10^2 + 3*10 + 4'. |
|
10120 |
v := #(2 3 4) asDoubleArray. |
|
10121 |
v hornerMultiplyAndAdd:10. |
|
10122 |
" |
|
10123 |
! |
|
10124 |
||
10125 |
squaredVectorLength |
|
10126 |
"Return the squared length of the receiver interpreted as vector. |
|
10127 |
Some algorithms get along without the square root, and run faster." |
|
10128 |
||
10129 |
^ self dot: self |
|
10130 |
||
10131 |
" |
|
10132 |
#(10.0 10.0 10.0) vectorLength |
|
10133 |
#(10.0 10.0 10.0) squaredVectorLength |
|
10134 |
||
10135 |
#(10.0 10.0 10.0) asFloatArray vectorLength |
|
10136 |
#(10.0 10.0 10.0) asFloatArray squaredVectorLength |
|
10137 |
" |
|
10138 |
! |
|
10139 |
||
10140 |
vectorLength |
|
10141 |
"Return the length of the receiver interpreted as vector |
|
10142 |
That is the length of the vector from 0.0 @ 0.0 @ ... @ 0.0 |
|
10143 |
to the point in the n-dimensional space represented by the receiver. |
|
10144 |
In 2D, that is plain old pythagoras." |
|
10145 |
||
10146 |
^ self squaredVectorLength sqrt |
|
10147 |
||
10148 |
" |
|
10149 |
#(10.0 10.0) vectorLength |
|
10150 |
#(10.0 10.0 10.0) vectorLength |
|
10151 |
||
10152 |
#(10.0 10.0) asFloatArray vectorLength |
|
10153 |
#(10.0 10.0 10.0) asFloatArray vectorLength |
|
10154 |
" |
|
10155 |
! ! |
|
10156 |
||
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10157 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10158 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10159 |
acceptVisitor:aVisitor with:aParameter |
16716 | 10160 |
"dispatch for visitor pattern; send #visitSequenceableCollection:with: to aVisitor." |
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10161 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10162 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10163 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
10164 |
|
2069 | 10165 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 10166 |
|
14138
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
10167 |
version |
18549
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
10168 |
^ '$Header$' |
14138
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
10169 |
! |
fbae7edde573
added: #replaceFrom:to:withObject:
Claus Gittinger <cg@exept.de>
parents:
14130
diff
changeset
|
10170 |
|
12051 | 10171 |
version_CVS |
18549
98986a4fcc20
class: SequenceableCollection
Claus Gittinger <cg@exept.de>
parents:
18195
diff
changeset
|
10172 |
^ '$Header$' |
628 | 10173 |
! ! |
6820 | 10174 |
|
14610 | 10175 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
10176 |
SequenceableCollection initialize! |