author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 19 Jan 2012 11:46:00 +0000 | |
branch | jv |
changeset 17911 | a99f15c5efa5 |
parent 17910 | 8d796ca8bd1d |
child 17928 | 8e8dad2e6269 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
6135 | 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 |
" |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
13 |
|
1 | 14 |
Object subclass:#Collection |
11331 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
|
17 |
NotEnoughElementsSignal RecursiveCollectionStoreStringSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2097 | 22 |
!Collection class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6135 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
Abstract superclass for all collections. |
|
41 |
This abstract class provides functionality common to all collections, |
|
42 |
without knowing how the concrete class implements things. Thus, all |
|
43 |
methods found here depend on some basic mechanisms to be defined in the |
|
44 |
concrete class. |
|
45 |
These basic methods are usually defined as #subclassResponsibility here. |
|
46 |
Some methods are also redefined for better performance. |
|
1289 | 47 |
|
1325 | 48 |
Subclasses should at least implement: |
49 |
do: - enumerate elements |
|
50 |
||
51 |
they should implement one of the following set of access messages: |
|
52 |
keyed collections: |
|
53 |
at:ifAbsent: - fetching an element |
|
54 |
at: - fetching an element |
|
55 |
at:put: - storing an element |
|
56 |
||
57 |
unkeyed collections: |
|
58 |
add: - add an element |
|
59 |
remove:ifAbsent: - remove an element |
|
60 |
||
61 |
||
1289 | 62 |
[author:] |
63 |
Claus Gittinger |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
2097 | 67 |
!Collection class methodsFor:'initialization'! |
10 | 68 |
|
69 |
initialize |
|
70 |
"setup the signal" |
|
71 |
||
54 | 72 |
InvalidKeySignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
73 |
InvalidKeySignal := Error newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
74 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
75 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 76 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
77 |
ValueNotFoundSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
78 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
79 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 80 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
81 |
NotEnoughElementsSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 84 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
85 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
54 | 88 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
89 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
90 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 91 |
! ! |
92 |
||
2097 | 93 |
!Collection class methodsFor:'instance creation'! |
155 | 94 |
|
17800 | 95 |
combiningEach:collection1 withEach:collection2 using:aTwoArgBlock |
96 |
"evaluate aTwoArgBlock for each combination of elements from collection1 |
|
97 |
and collection2 and return an instance of the receiver containing all those elements" |
|
98 |
||
99 |
|newColl idx useIndex newEl| |
|
100 |
||
101 |
newColl := self new:(collection1 size * collection2 size). |
|
102 |
useIndex := newColl isFixedSize. |
|
103 |
idx := 1. |
|
104 |
collection1 do:[:e1 | |
|
105 |
collection2 do:[:e2 | |
|
106 |
newEl := aTwoArgBlock value:e1 value:e2. |
|
107 |
||
108 |
useIndex ifTrue:[ |
|
109 |
newColl at:idx put:(aTwoArgBlock value:e1 value:e2). |
|
110 |
idx := idx + 1. |
|
111 |
] ifFalse:[ |
|
112 |
newColl add:(aTwoArgBlock value:e1 value:e2) |
|
113 |
]. |
|
114 |
] |
|
115 |
]. |
|
116 |
^ newColl |
|
117 |
||
118 |
" |
|
119 |
Set combiningEach:#(1 2 3 4 5) withEach:(10 to:100 by:10) using:[:a :b | a * b] |
|
120 |
Set combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] |
|
121 |
" |
|
122 |
||
123 |
"Created: / 25-08-2010 / 17:21:47 / cg" |
|
124 |
! |
|
125 |
||
602 | 126 |
new:size withAll:element |
5626 | 127 |
"return a new Collection of size, where all elements are |
602 | 128 |
initialized to element" |
348 | 129 |
|
602 | 130 |
|newCollection| |
92 | 131 |
|
602 | 132 |
newCollection := self new:size. |
133 |
size timesRepeat:[newCollection add:element] |
|
134 |
! |
|
1 | 135 |
|
5626 | 136 |
newFrom:aCollection |
137 |
"Return an instance of me containing the same elements as aCollection." |
|
138 |
||
139 |
^ self withAll:aCollection |
|
140 |
||
141 |
" |
|
142 |
Bag newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
143 |
Set newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
144 |
" |
|
145 |
! |
|
146 |
||
1 | 147 |
with:anObject |
148 |
"return a new Collection with one element:anObject" |
|
149 |
||
150 |
|newCollection| |
|
151 |
||
152 |
newCollection := self new. |
|
153 |
newCollection add:anObject. |
|
154 |
^ newCollection |
|
155 |
! |
|
156 |
||
157 |
with:firstObject with:secondObject |
|
158 |
"return a new Collection with two elements:firstObject and secondObject" |
|
159 |
||
160 |
|newCollection| |
|
161 |
||
162 |
newCollection := self new. |
|
1112 | 163 |
newCollection add:firstObject; add:secondObject. |
1 | 164 |
^ newCollection |
165 |
! |
|
166 |
||
167 |
with:firstObject with:secondObject with:thirdObject |
|
168 |
"return a new Collection with three elements" |
|
169 |
||
170 |
|newCollection| |
|
171 |
||
172 |
newCollection := self new. |
|
1112 | 173 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 174 |
^ newCollection |
175 |
! |
|
176 |
||
177 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
178 |
"return a new Collection with four elements" |
|
179 |
||
180 |
|newCollection| |
|
181 |
||
182 |
newCollection := self new. |
|
1112 | 183 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 184 |
^ newCollection |
185 |
! |
|
186 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
187 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 188 |
"return a new Collection with five elements" |
189 |
||
190 |
|newCollection| |
|
191 |
||
192 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
193 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
194 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
195 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
196 |
"Modified: 22.1.1997 / 19:34:01 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
197 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
198 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
199 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
200 |
"return a new Collection with size elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
201 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
202 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
203 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
204 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
205 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 206 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
207 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
208 |
"Created: 22.1.1997 / 19:34:14 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
209 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
210 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
211 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
212 |
"return a new Collection with seven elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
213 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
214 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
215 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
216 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
217 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
218 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
219 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
220 |
"Created: 22.1.1997 / 19:34:24 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
221 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
222 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
223 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 with:a8 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
224 |
"return a new Collection with eight elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
225 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
226 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
227 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
228 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
229 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7; add:a8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
230 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
231 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
232 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 233 |
! |
234 |
||
1 | 235 |
withAll:aCollection |
236 |
"return a new Collection with all elements taken from the argument, |
|
237 |
aCollection" |
|
238 |
||
239 |
|newCollection| |
|
240 |
||
241 |
newCollection := self new. |
|
242 |
newCollection addAll:aCollection. |
|
243 |
^newCollection |
|
9159 | 244 |
! |
245 |
||
246 |
withSize:n |
|
247 |
"return a new collection which really provides space for n elements. |
|
248 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
249 |
||
250 |
^ self new:n |
|
251 |
! ! |
|
252 |
||
253 |
!Collection class methodsFor:'Compatibility-Squeak'! |
|
254 |
||
255 |
ofSize:n |
|
256 |
"return a new collection which really provides space for n elements. |
|
257 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
258 |
||
259 |
^ self withSize:n |
|
1 | 260 |
! ! |
261 |
||
17862 | 262 |
|
2097 | 263 |
!Collection class methodsFor:'Signal constants'! |
10 | 264 |
|
602 | 265 |
emptyCollectionSignal |
266 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 267 |
|
602 | 268 |
^ EmptyCollectionSignal |
308 | 269 |
! |
270 |
||
602 | 271 |
invalidKeySignal |
272 |
"return the signal used to report bad key usage" |
|
308 | 273 |
|
602 | 274 |
^ InvalidKeySignal |
201 | 275 |
! |
276 |
||
602 | 277 |
notEnoughElementsSignal |
278 |
"return the signal used to report attempts for an operation, for which |
|
279 |
there are not enough elements in the collection" |
|
249 | 280 |
|
602 | 281 |
^ NotEnoughElementsSignal |
404 | 282 |
! |
283 |
||
602 | 284 |
valueNotFoundSignal |
285 |
"return the signal used to report a nonexisting element." |
|
404 | 286 |
|
602 | 287 |
^ ValueNotFoundSignal |
288 |
! ! |
|
404 | 289 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
290 |
!Collection class methodsFor:'misc ui support'! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
291 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
292 |
iconInBrowserSymbol |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
293 |
<resource: #programImage> |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
294 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
295 |
^ #containerClassBrowserIcon |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
296 |
! ! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
297 |
|
2097 | 298 |
!Collection class methodsFor:'queries'! |
404 | 299 |
|
602 | 300 |
growIsCheap |
301 |
"return true, if this collection can easily grow |
|
302 |
(i.e. without a need for become:). |
|
303 |
Returns true here; this method is redefined in fix-size |
|
304 |
collections" |
|
404 | 305 |
|
602 | 306 |
^ true |
8893 | 307 |
! |
308 |
||
309 |
isAbstract |
|
11231 | 310 |
"Return if this class is an abstract class. |
311 |
True is returned for Collection here; false for subclasses. |
|
312 |
Abstract subclasses must redefine again." |
|
313 |
||
8893 | 314 |
^ self == Collection |
257 | 315 |
! ! |
316 |
||
17865 | 317 |
|
318 |
||
7261 | 319 |
!Collection methodsFor:'Compatibility-Dolphin'! |
6411 | 320 |
|
321 |
identityIncludes:anObject |
|
7807 | 322 |
"return true, if the argument, anObject is in the collection. |
323 |
Same as #includesIdentical for Dolphin compatibility." |
|
324 |
||
6411 | 325 |
^ self includesIdentical:anObject. |
6869 | 326 |
! |
327 |
||
328 |
includesAnyOf:aCollection |
|
329 |
"same as #includesAny for Dolphin compatibility." |
|
330 |
||
331 |
^ self includesAny:aCollection |
|
332 |
||
333 |
" |
|
334 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
335 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
336 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
337 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
338 |
" |
|
17735 | 339 |
! |
340 |
||
341 |
symmetricDifference:aCollection |
|
342 |
"return a new set containing all elements, |
|
343 |
which are contained in either the receiver or aCollection, but not in both. |
|
344 |
Same as xor: - for compatibility" |
|
345 |
||
346 |
^ self xor:aCollection |
|
347 |
||
348 |
" |
|
349 |
|c1 c2| |
|
350 |
||
351 |
c1 := #( foo bar baz baloo ). |
|
352 |
c2 := #( foe bar banana baloo ). |
|
353 |
c1 symmetricDifference:c2. |
|
354 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
355 |
" |
|
6411 | 356 |
! ! |
357 |
||
7261 | 358 |
!Collection methodsFor:'Compatibility-Squeak'! |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
359 |
|
17862 | 360 |
, aCollection |
361 |
^ self copy addAll: aCollection; yourself |
|
362 |
||
363 |
"Created: / 14-09-2011 / 16:32:06 / cg" |
|
364 |
! |
|
365 |
||
9354 | 366 |
addIfNotPresent:anObject |
7011 | 367 |
"Include anObject as one of the receiver's elements, but only if there |
368 |
is no such element already. Anwser anObject." |
|
369 |
||
370 |
(self includes: anObject) ifFalse: [self add: anObject]. |
|
371 |
^ anObject |
|
372 |
! |
|
373 |
||
6149 | 374 |
anyOne |
8858 | 375 |
"return any element from the collection. |
376 |
Report an error if there is none. |
|
6149 | 377 |
Same as #anElement - for Squeak compatibility" |
378 |
||
379 |
^ self anElement |
|
380 |
! |
|
381 |
||
17862 | 382 |
associationsDo: aBlock |
383 |
"cg: I think this is bad, but - well..." |
|
384 |
||
385 |
self do: aBlock |
|
386 |
||
387 |
"Created: / 12-09-2011 / 09:21:58 / cg" |
|
388 |
! |
|
389 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
390 |
difference: aCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
391 |
"Answer the set-theoretic difference of two collections." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
392 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
393 |
^ self reject:[:each | aCollection includes: each] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
394 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
395 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
396 |
#(0 2 4 6 8) difference:#(2 4) |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
397 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
398 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
399 |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
400 |
gather:aBlock |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
401 |
"return an Array, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
402 |
containing all elements as returned from applying aBlock to each element if the receiver, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
403 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
404 |
This could also be called: collectAllAsArray:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
405 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
406 |
^ self gather:aBlock as:Array |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
407 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
408 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
409 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
410 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
411 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
412 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
413 |
gather:aBlock as:aClass |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
414 |
"return an instance of the collection-class aClass, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
415 |
containing all elements as returned from applying aBlock to each element if the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
416 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
417 |
This could also be called: collectAll:as:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
418 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
419 |
^ aClass streamContents:[:s | |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
420 |
self do: [:ea | s nextPutAll: (aBlock value: ea)] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
421 |
] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
422 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
423 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
424 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] as:OrderedCollection |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
425 |
(Set withAll:#(10 20 30 10 20 40)) collectAll:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
426 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
427 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
428 |
|
9354 | 429 |
groupBy:keyBlock having:selectBlock |
17742 | 430 |
"Like in SQL operation - Split the receiver's contents into collections of |
9354 | 431 |
elements for which keyBlock returns the same results, and return those |
432 |
collections allowed by selectBlock. " |
|
433 |
||
9319 | 434 |
|result key| |
17742 | 435 |
|
9319 | 436 |
result := OrderedDictionary new. |
17742 | 437 |
self do:[:e | |
438 |
key := keyBlock value: e. |
|
439 |
(result includesKey: key) ifFalse: [ |
|
440 |
result at: key put: OrderedCollection new |
|
441 |
]. |
|
442 |
(result at: key) add: e |
|
443 |
]. |
|
9319 | 444 |
^ result := result select: selectBlock |
445 |
||
9354 | 446 |
" |
447 |
#(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a |true] |
|
448 |
" |
|
9319 | 449 |
! |
450 |
||
9065 | 451 |
ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
452 |
|action| |
|
453 |
||
454 |
action := self isEmpty ifTrue:[ ifEmptyValue ] ifFalse:[ ifNotEmptyValue ]. |
|
455 |
||
456 |
action isBlock ifTrue:[ |
|
457 |
action numArgs == 1 ifTrue:[ |
|
458 |
^ action value:self |
|
459 |
] |
|
460 |
]. |
|
461 |
^ action value |
|
462 |
! |
|
463 |
||
464 |
ifEmpty:ifEmptyValue ifNotEmptyDo:ifNotEmptyValue |
|
465 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
466 |
! |
|
467 |
||
468 |
ifEmptyDo:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
469 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
470 |
! |
|
471 |
||
472 |
ifNotEmpty:ifNotEmptyValue |
|
473 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
474 |
! |
|
475 |
||
476 |
ifNotEmptyDo:ifNotEmptyValue |
|
477 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
478 |
! |
|
479 |
||
480 |
ifNotEmptyDo:ifNotEmptyValue ifEmpty:ifEmptyValue |
|
481 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
482 |
! |
|
483 |
||
11280 | 484 |
intersection:aCollection |
485 |
^ self intersect:aCollection |
|
486 |
||
487 |
"Created: / 22-10-2008 / 21:29:27 / cg" |
|
488 |
! |
|
489 |
||
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
490 |
withIndexDo:aTwoArgBlock |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
491 |
"evaluate the argument, aBlock for every element in the collection, |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
492 |
passing both element and index as arguments." |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
493 |
|
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
494 |
^ self keysAndValuesDo:[:key :index | |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
495 |
aTwoArgBlock value:index value:key |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
496 |
]. |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
497 |
! ! |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
498 |
|
17862 | 499 |
|
1 | 500 |
!Collection methodsFor:'accessing'! |
501 |
||
602 | 502 |
anElement |
6966 | 503 |
"return any element from the collection, |
504 |
report an error if there is none" |
|
602 | 505 |
|
506 |
self do: [:each | ^ each]. |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
507 |
self emptyCollectionError. |
602 | 508 |
! |
509 |
||
17797 | 510 |
at:aKey ifNilOrAbsentPut:valueBlock |
511 |
"try to fetch the element at aKey. If either the key is invalid (as in a Dictionary) |
|
512 |
or there is no element stored under that key (as in an Array), set the element |
|
513 |
from the valueBlock and return it. |
|
514 |
Useful for lazy initialization of collections." |
|
515 |
||
516 |
|val| |
|
517 |
||
518 |
val := self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ]. |
|
519 |
val isNil ifTrue:[ |
|
520 |
self at:aKey put:(val := valueBlock value). |
|
521 |
]. |
|
522 |
^ val |
|
523 |
||
524 |
" |
|
525 |
|d| |
|
526 |
||
527 |
d := Dictionary new. |
|
528 |
d at:#foo ifNilOrAbsentPut:[ 'hello' ]. |
|
529 |
d |
|
530 |
" |
|
531 |
||
532 |
" |
|
533 |
|a| |
|
534 |
||
535 |
a := Array new:10. |
|
536 |
a at:1 ifNilOrAbsentPut:[ 'hello' ]. |
|
537 |
a |
|
538 |
" |
|
539 |
||
540 |
"Created: / 24-08-2010 / 17:07:21 / cg" |
|
541 |
! |
|
542 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
543 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
544 |
"put anObject into all indexes from indexCollection in the receiver. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
545 |
This abstract implementation requires that the receiver supports |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
546 |
access via a key (and indexCollection contains valid keys). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
547 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
548 |
Notice: This operation modifies the receiver, NOT a copy; |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
549 |
therefore the change may affect all others referencing the receiver." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
550 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
551 |
indexCollection do:[:index | self at:index put:anObject] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
552 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
553 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
554 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
555 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
556 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
557 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
558 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
559 |
(Array new:10) atAll:(1 to:5) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
560 |
(Array new:10) atAll:#(1 5 6 9) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
561 |
(Dictionary new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
562 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
563 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
564 |
(Set new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
565 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
566 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
567 |
"Modified: / 20.5.1998 / 14:50:46 / cg" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
568 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
569 |
|
4985 | 570 |
fifth |
571 |
"return the fifth element of the collection. |
|
572 |
For unordered collections, this simply returns the fifth |
|
573 |
element when enumerating them. |
|
574 |
This should be redefined in subclasses." |
|
575 |
||
576 |
^ self nth:5 |
|
577 |
||
578 |
" |
|
579 |
#(1 2 3 4 5) fifth |
|
580 |
" |
|
581 |
! |
|
582 |
||
68 | 583 |
first |
584 |
"return the first element of the collection. |
|
4855 | 585 |
For unordered collections, this simply returns the first |
586 |
element when enumerating them. |
|
68 | 587 |
This should be redefined in subclasses." |
588 |
||
589 |
self do:[:e | ^ e]. |
|
590 |
||
591 |
"error if collection is empty" |
|
155 | 592 |
^ self emptyCollectionError |
68 | 593 |
! |
594 |
||
5887 | 595 |
first:n |
596 |
"return the n first elements of the collection. |
|
597 |
Raises an error if there are not enough elements in the receiver. |
|
598 |
For unordered collections, this simply returns the first |
|
599 |
n elements when enumerating them |
|
600 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
601 |
This should be redefined in subclasses." |
|
602 |
||
603 |
|coll remain| |
|
604 |
||
605 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
606 |
||
607 |
coll := OrderedCollection new. |
|
608 |
remain := n. |
|
609 |
self do:[:e | |
|
17892 | 610 |
coll add:e. |
611 |
remain := remain - 1. |
|
612 |
remain == 0 ifTrue:[ |
|
613 |
^ coll |
|
614 |
]. |
|
615 |
]. |
|
5887 | 616 |
|
617 |
"error if collection has not enough elements" |
|
618 |
^ self notEnoughElementsError |
|
619 |
||
620 |
" |
|
621 |
#(1 2 3 4 5) first:3 |
|
622 |
#(1 2 3 4 5) asSet first:3 |
|
623 |
" |
|
17892 | 624 |
|
625 |
"Modified (format): / 29-09-2011 / 10:16:49 / cg" |
|
5887 | 626 |
! |
627 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
628 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
629 |
"return the first element of the collection. |
10593 | 630 |
If its empty, return the exceptionValue. |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
631 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
632 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
633 |
self isEmpty ifTrue:[^ exceptionValue value]. |
10593 | 634 |
^ self first |
635 |
||
636 |
"Modified: / 04-06-2007 / 22:36:14 / cg" |
|
637 |
! |
|
638 |
||
639 |
firstOrNil |
|
640 |
"return the first element of the collection. |
|
641 |
If its empty, return nil. |
|
642 |
(i.e. dont trigger an error as done in #first)" |
|
643 |
||
644 |
^ self firstIfEmpty:nil |
|
645 |
||
646 |
"Created: / 04-06-2007 / 22:36:07 / cg" |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
647 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
648 |
|
4985 | 649 |
fourth |
650 |
"return the fourth element of the collection. |
|
651 |
For unordered collections, this simply returns the fourth |
|
652 |
element when enumerating them. |
|
653 |
This should be redefined in subclasses." |
|
654 |
||
655 |
^ self nth:4 |
|
656 |
||
657 |
" |
|
658 |
#(1 2 3 4) fourth |
|
659 |
" |
|
660 |
! |
|
661 |
||
68 | 662 |
last |
663 |
"return the last element of the collection. |
|
664 |
This should be redefined in subclasses." |
|
665 |
||
666 |
|theLastOne any| |
|
667 |
||
668 |
any := false. |
|
669 |
self do:[:e | any := true. theLastOne := e]. |
|
670 |
any ifTrue:[ |
|
6135 | 671 |
^ theLastOne |
68 | 672 |
]. |
673 |
||
674 |
"error if collection is empty" |
|
155 | 675 |
^ self emptyCollectionError |
68 | 676 |
! |
677 |
||
5887 | 678 |
last:n |
679 |
"return the n last elements of the collection. |
|
680 |
Raises an error if there are not enough elements in the receiver. |
|
681 |
For unordered collections, this simply returns the last |
|
682 |
n elements when enumerating them |
|
683 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
684 |
This should be redefined in subclasses since the implementation here is VERY inefficient." |
|
685 |
||
686 |
|coll remain| |
|
687 |
||
688 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
689 |
||
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
690 |
coll := OrderedCollection new:n. |
5887 | 691 |
remain := n. |
692 |
self do:[:e | |
|
17892 | 693 |
remain > 0 ifTrue:[ |
694 |
remain := remain - 1. |
|
695 |
] ifFalse:[ |
|
696 |
coll removeFirst. |
|
697 |
]. |
|
698 |
coll add:e. |
|
699 |
]. |
|
5887 | 700 |
|
701 |
remain ~~ 0 ifTrue:[ |
|
702 |
"error if collection has not enough elements" |
|
703 |
^ self notEnoughElementsError |
|
704 |
]. |
|
705 |
^ coll |
|
706 |
||
707 |
" |
|
708 |
#(1 2 3 4 5) last:3 |
|
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
709 |
#(1 2 3 4 5 6 7 8 9 0) asSet last:3 |
5887 | 710 |
'hello world' last:5 |
711 |
" |
|
17892 | 712 |
|
713 |
"Modified (format): / 29-09-2011 / 10:16:22 / cg" |
|
5887 | 714 |
! |
715 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
716 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
717 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
718 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
719 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
720 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
721 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
722 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
723 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
724 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
725 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 726 |
! |
727 |
||
9073 | 728 |
median |
729 |
"Return the middle element, or as close as we can get." |
|
730 |
||
731 |
^ self asSortedCollection median |
|
732 |
||
733 |
" |
|
734 |
#(10 35 20 45 30 5) median |
|
735 |
" |
|
736 |
! |
|
737 |
||
4985 | 738 |
nth:n |
739 |
"return the nth element of the collection. |
|
740 |
For unordered collections, this simply returns the nth |
|
741 |
element when enumerating them. |
|
742 |
This should be redefined in subclasses." |
|
743 |
||
744 |
|count| |
|
745 |
||
746 |
count := 1. |
|
747 |
self do:[:e | |
|
748 |
count == n ifTrue:[^ e]. |
|
749 |
count := count + 1 |
|
750 |
]. |
|
751 |
||
5266 | 752 |
"error if collection is smaller" |
753 |
^ self notEnoughElementsError |
|
4985 | 754 |
|
755 |
" |
|
756 |
#(1 2 3 4) nth:3 |
|
5266 | 757 |
#(1 2 3 4) nth:5 |
758 |
||
759 |
#(1 2 3 4) asSet nth:3 |
|
760 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 761 |
" |
762 |
! |
|
763 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
764 |
order |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
765 |
"report an error that only OrderedXXX's have an order" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
766 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
767 |
self shouldNotImplement |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
768 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
769 |
|
4855 | 770 |
second |
771 |
"return the second element of the collection. |
|
772 |
For unordered collections, this simply returns the second |
|
773 |
element when enumerating them. |
|
7129 | 774 |
This should be redefined in subclasses." |
775 |
||
776 |
^ self nth:2 |
|
4855 | 777 |
|
4856 | 778 |
" |
779 |
#(1 2 3) second |
|
780 |
" |
|
6135 | 781 |
! |
782 |
||
783 |
seventh |
|
784 |
"return the seventh element of the collection. |
|
785 |
For unordered collections, this simply returns the sixth |
|
786 |
element when enumerating them. |
|
787 |
This should be redefined in subclasses." |
|
788 |
||
789 |
^ self nth:7 |
|
790 |
||
791 |
" |
|
792 |
#(1 2 3 4 5 6 7) seventh |
|
793 |
" |
|
794 |
||
795 |
"Created: / 1.11.2001 / 16:43:29 / cg" |
|
4856 | 796 |
! |
797 |
||
4985 | 798 |
sixth |
799 |
"return the sixth element of the collection. |
|
800 |
For unordered collections, this simply returns the sixth |
|
801 |
element when enumerating them. |
|
802 |
This should be redefined in subclasses." |
|
803 |
||
804 |
^ self nth:6 |
|
805 |
||
806 |
" |
|
807 |
#(1 2 3 4 5 6 7) sixth |
|
808 |
" |
|
809 |
! |
|
810 |
||
4856 | 811 |
third |
812 |
"return the third element of the collection. |
|
813 |
For unordered collections, this simply returns the third |
|
814 |
element when enumerating them. |
|
815 |
This should be redefined in subclasses." |
|
816 |
||
4985 | 817 |
^ self nth:3 |
4856 | 818 |
|
819 |
" |
|
820 |
#(1 2 3) third |
|
821 |
" |
|
17892 | 822 |
|
823 |
"Modified (format): / 29-09-2011 / 10:17:25 / cg" |
|
17747 | 824 |
! |
825 |
||
826 |
values |
|
827 |
"return a collection containing all values of the receiver. |
|
828 |
This is to make value access to an OrderedDictionary compatible with any-Collection" |
|
829 |
||
830 |
|aCollection| |
|
831 |
||
832 |
aCollection := OrderedCollection new. |
|
833 |
self do:[:value| aCollection add:value]. |
|
834 |
^ aCollection |
|
1 | 835 |
! ! |
836 |
||
837 |
!Collection methodsFor:'adding & removing'! |
|
838 |
||
839 |
add:anObject |
|
88 | 840 |
"add the argument, anObject to the receiver. |
841 |
If the receiver is ordered, the position of the new element is undefined |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
842 |
(i.e. don't depend on where it will be put). |
2351 | 843 |
An error is raised here - it is to be implemented by a concrete subclass." |
1 | 844 |
|
845 |
^ self subclassResponsibility |
|
2351 | 846 |
|
847 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 848 |
! |
849 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
850 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
851 |
"add the argument, anObject anInteger times to the receiver. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
852 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
853 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
854 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
855 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
856 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
857 |
"Created: 11.5.1996 / 12:13:48 / cg" |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
858 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
859 |
|
1 | 860 |
addAll:aCollection |
1163 | 861 |
"add all elements of the argument, aCollection to the receiver. |
7752 | 862 |
Returns the argument, aCollection (sigh)." |
1 | 863 |
|
864 |
aCollection do:[:element | |
|
1163 | 865 |
self add:element |
1 | 866 |
]. |
867 |
^ aCollection |
|
88 | 868 |
|
869 |
" |
|
870 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
871 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
872 |
" |
|
1163 | 873 |
|
874 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 875 |
! |
876 |
||
12 | 877 |
addAllFirst:aCollection |
878 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 879 |
of the receiver. Returns the argument, aCollection." |
12 | 880 |
|
881 |
aCollection reverseDo:[:element | |
|
1163 | 882 |
self addFirst:element |
12 | 883 |
]. |
884 |
^ aCollection |
|
885 |
||
886 |
" |
|
887 |
|c| |
|
888 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 889 |
c addAllFirst:#(9 8 7 6 5). |
890 |
c |
|
12 | 891 |
" |
1163 | 892 |
|
893 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 894 |
! |
895 |
||
602 | 896 |
addAllLast:aCollection |
1163 | 897 |
"add all elements of the argument, aCollection to the receiver. |
898 |
Returns the argument, aCollection." |
|
602 | 899 |
|
900 |
aCollection do:[:element | |
|
1163 | 901 |
self addLast:element |
602 | 902 |
]. |
903 |
^ aCollection |
|
904 |
||
905 |
" |
|
906 |
|c| |
|
907 |
c := #(4 3 2 1) asOrderedCollection. |
|
908 |
c addAllLast:#(9 8 7 6 5) |
|
909 |
" |
|
1163 | 910 |
|
911 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 912 |
! |
913 |
||
914 |
addFirst:anObject |
|
915 |
"add the argument, anObject to the receiver. |
|
2351 | 916 |
If the receiver is ordered, the new element will be added at the beginning. |
917 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 918 |
|
919 |
^ self subclassResponsibility |
|
2351 | 920 |
|
921 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 922 |
! |
923 |
||
602 | 924 |
addLast:anObject |
925 |
"add the argument, anObject to the receiver. |
|
1163 | 926 |
If the receiver is ordered, the new element will be added at the end. |
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
927 |
Return the argument, anObject. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
928 |
|
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
929 |
This usually has the same semantics as #add:. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
930 |
OrderedSet and OrderedDictionary redefine this, to move anObject to |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
931 |
the end, even if it is already present in the collection." |
602 | 932 |
|
933 |
^ self add:anObject |
|
1163 | 934 |
|
935 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 936 |
! |
937 |
||
3124 | 938 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
939 |
"set my contents from aCollection |
5265 | 940 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 941 |
|
6042
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
942 |
aCollection == self ifTrue:[ |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
943 |
"/ self error:'should not happen'. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
944 |
^ self |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
945 |
]. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
946 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
947 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
948 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
949 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
950 |
] |
3124 | 951 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
952 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 953 |
! |
954 |
||
1 | 955 |
remove:anObject |
2351 | 956 |
"search for the first element, which is equal to anObject; |
957 |
if found, remove and return it. |
|
958 |
If not found, report a 'value not found'-error. |
|
959 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 960 |
|
1163 | 961 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
962 |
||
2351 | 963 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 964 |
! |
965 |
||
602 | 966 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 967 |
"search for the first element, which is equal to anObject; |
968 |
if found, remove and return it. |
|
969 |
If not found, return the the value of the exceptionBlock. |
|
970 |
Uses equality compare (=) to search for the occurrence. |
|
971 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 972 |
|
973 |
^ self subclassResponsibility |
|
2351 | 974 |
|
975 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 976 |
! |
977 |
||
134 | 978 |
removeAll |
1163 | 979 |
"remove all elements from the receiver. Returns the receiver. |
134 | 980 |
This should be reimplemented in subclasses for better |
981 |
performance." |
|
982 |
||
2383 | 983 |
[self isEmpty] whileFalse:[ |
1163 | 984 |
self removeFirst |
134 | 985 |
]. |
1163 | 986 |
|
2383 | 987 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 988 |
! |
989 |
||
1 | 990 |
removeAll:aCollection |
17795 | 991 |
"remove all elements from the receiver which are equal to any in aCollection. |
1163 | 992 |
Return the argument, aCollection. |
5267 | 993 |
Raises an error, if some element-to-remove is not in the receiver. |
994 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 995 |
|
360 | 996 |
Notice: for some collections (those not tuned for |
1163 | 997 |
resizing themself) this may be very slow. |
998 |
If the number of removed elements is big compared to to |
|
999 |
the receivers size, it may be better to copy the |
|
1000 |
ones which are not to be removed into a new collection." |
|
1 | 1001 |
|
1002 |
aCollection do:[:element | self remove:element]. |
|
1003 |
^ aCollection |
|
1163 | 1004 |
|
5268 | 1005 |
" |
1006 |
|coll| |
|
1007 |
||
1008 |
coll := #(1 2 3 4 5 6) asSet. |
|
1009 |
coll removeAll:#(4 5 6). |
|
1010 |
coll |
|
1011 |
" |
|
1012 |
||
1013 |
"raises an error: |
|
1014 |
|coll| |
|
1015 |
||
1016 |
coll := #(1 2 3 4 5 6) asSet. |
|
1017 |
coll removeAll:#(4 5 6 7 8). |
|
1018 |
coll |
|
1019 |
" |
|
1020 |
||
1021 |
"no error raised: |
|
1022 |
|coll| |
|
1023 |
||
1024 |
coll := #(1 2 3 4 5 6) asSet. |
|
1025 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1026 |
coll |
|
1027 |
" |
|
1028 |
||
17795 | 1029 |
"Modified: / 05-08-2010 / 13:50:33 / cg" |
348 | 1030 |
! |
1031 |
||
5265 | 1032 |
removeAllFoundIn:aCollection |
17795 | 1033 |
"remove all elements from the receiver which are equal to any in aCollection. |
5267 | 1034 |
No error is raised, if some element-to-remove is not in the receiver. |
1035 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 1036 |
|
1037 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 1038 |
^ aCollection |
5268 | 1039 |
|
1040 |
" |
|
1041 |
|coll| |
|
1042 |
||
1043 |
coll := #(1 2 3 4 5 6) asSet. |
|
1044 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1045 |
coll |
|
1046 |
" |
|
1047 |
||
1048 |
"raises an error: |
|
1049 |
|coll| |
|
1050 |
||
1051 |
coll := #(1 2 3 4 5 6) asSet. |
|
1052 |
coll removeAll:#(4 5 6 7 8). |
|
1053 |
coll |
|
1054 |
" |
|
1055 |
||
17795 | 1056 |
"Modified: / 05-08-2010 / 13:51:05 / cg" |
1057 |
! |
|
1058 |
||
1059 |
removeAllIdentical:aCollection |
|
1060 |
"remove all elements from the receiver which are in aCollection. |
|
1061 |
Return the argument, aCollection. |
|
1062 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1063 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1064 |
||
1065 |
Notice: for some collections (those not tuned for |
|
1066 |
resizing themself) this may be very slow. |
|
1067 |
If the number of removed elements is big compared to to |
|
1068 |
the receivers size, it may be better to copy the |
|
1069 |
ones which are not to be removed into a new collection." |
|
1070 |
||
1071 |
aCollection do:[:element | self removeIdentical:element]. |
|
1072 |
^ aCollection |
|
1073 |
||
1074 |
" |
|
1075 |
|coll| |
|
1076 |
||
1077 |
coll := #(1 2 3 4 5 6) asSet. |
|
1078 |
coll removeAll:#(4 5 6). |
|
1079 |
coll |
|
1080 |
" |
|
1081 |
||
1082 |
"raises an error: |
|
1083 |
|coll| |
|
1084 |
||
1085 |
coll := #(1 2 3 4 5 6) asSet. |
|
1086 |
coll removeAll:#(4 5 6 7 8). |
|
1087 |
coll |
|
1088 |
" |
|
1089 |
||
1090 |
"no error raised: |
|
1091 |
|coll| |
|
1092 |
||
1093 |
coll := #(1 2 3 4 5 6) asSet. |
|
1094 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1095 |
coll |
|
1096 |
" |
|
1097 |
||
1098 |
"Created: / 05-08-2010 / 13:51:51 / cg" |
|
1099 |
! |
|
1100 |
||
1101 |
removeAllIdenticalFoundIn:aCollection |
|
1102 |
"remove all elements from the receiver which are in aCollection. |
|
1103 |
No error is raised, if some element-to-remove is not in the receiver. |
|
1104 |
(see also: #removeAll:, which does raise an error)." |
|
1105 |
||
1106 |
aCollection do:[:each | self removeIdentical:each ifAbsent:[]]. |
|
1107 |
^ aCollection |
|
1108 |
||
1109 |
" |
|
1110 |
|coll| |
|
1111 |
||
1112 |
coll := #(1 2 3 4 5 6) asSet. |
|
1113 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1114 |
coll |
|
1115 |
" |
|
1116 |
||
1117 |
"raises an error: |
|
1118 |
|coll| |
|
1119 |
||
1120 |
coll := #(1 2 3 4 5 6) asSet. |
|
1121 |
coll removeAll:#(4 5 6 7 8). |
|
1122 |
coll |
|
1123 |
" |
|
1124 |
||
1125 |
"Created: / 05-08-2010 / 13:52:21 / cg" |
|
4980 | 1126 |
! |
1127 |
||
17746 | 1128 |
removeAllKeys:aCollection |
1129 |
"remove all keys of the argument, aCollection from the receiver. |
|
1130 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1131 |
Notice: only works for keyed collections, such as dictionaries." |
|
1132 |
||
1133 |
aCollection do:[:element | self removeKey:element]. |
|
1134 |
! |
|
1135 |
||
5265 | 1136 |
removeAllSuchThat:aBlock |
4980 | 1137 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 1138 |
Return a collection of removed elements. |
5267 | 1139 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 1140 |
|
5267 | 1141 |
|removedElements| |
4980 | 1142 |
|
5267 | 1143 |
removedElements := OrderedCollection new. |
1144 |
self do:[:element | |
|
4980 | 1145 |
(aBlock value:element) ifTrue: [ |
5267 | 1146 |
removedElements add:element |
4980 | 1147 |
] |
1148 |
]. |
|
5267 | 1149 |
self removeAll:removedElements. |
1150 |
^ removedElements |
|
1151 |
||
1152 |
" |
|
1153 |
|coll| |
|
1154 |
||
1155 |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
1156 |
coll removeAllSuchThat:[:el | el even]. |
|
1157 |
coll |
|
1158 |
" |
|
1159 |
" |
|
1160 |
|coll| |
|
1161 |
||
1162 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
1163 |
coll removeAllSuchThat:[:el | el even]. |
|
1164 |
coll |
|
1165 |
" |
|
1166 |
||
4980 | 1167 |
! |
1168 |
||
348 | 1169 |
removeFirst |
1170 |
"remove the first element from the receiver. |
|
1171 |
Return the removed element." |
|
1172 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1173 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1174 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1175 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1176 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1177 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1178 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1179 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1180 |
(Set with:3 with:2 with:1) removeFirst |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1181 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1182 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1183 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 1184 |
! |
1185 |
||
1186 |
removeFirst:n |
|
1187 |
"remove the first n elements from the receiver. |
|
5265 | 1188 |
Return a collection of removed elements. |
360 | 1189 |
Notice: for some collections (those not tuned for |
5265 | 1190 |
resizing themself) this may be very slow." |
348 | 1191 |
|
1192 |
|ret| |
|
1193 |
||
1194 |
self size < n ifTrue:[ |
|
5265 | 1195 |
^ self notEnoughElementsError |
348 | 1196 |
]. |
1197 |
ret := Array new:n. |
|
1198 |
1 to:n do:[:i | |
|
5265 | 1199 |
ret at:i put:(self removeFirst). |
348 | 1200 |
]. |
1201 |
^ ret |
|
1202 |
! |
|
1203 |
||
2351 | 1204 |
removeIdentical:anObject |
1205 |
"search for the first element, which is identical to anObject; |
|
1206 |
if found, remove and return it. |
|
1207 |
If not found, report a 'value not found'-error. |
|
1208 |
Uses identity compare (==) to search for the occurrence." |
|
1209 |
||
1210 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
1211 |
||
1212 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1213 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
1214 |
! |
|
1215 |
||
1216 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
1217 |
"search for the first element, which is identical to anObject; |
|
1218 |
if found, remove and return it. |
|
1219 |
If not found, return the the value of the exceptionBlock. |
|
1220 |
Uses identity compare (==) to search for the occurrence. |
|
1221 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1222 |
||
1223 |
^ self subclassResponsibility |
|
1224 |
||
1225 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
1226 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
1227 |
! |
|
1228 |
||
602 | 1229 |
removeLast |
1230 |
"remove the last element from the receiver. |
|
2351 | 1231 |
Return the removed element. |
1232 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1233 |
|
1234 |
^ self subclassResponsibility |
|
2351 | 1235 |
|
1236 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 1237 |
! |
1238 |
||
348 | 1239 |
removeLast:n |
11331 | 1240 |
"remove the last n elements from the receiver collection. |
5265 | 1241 |
Return a collection of removed elements. |
11331 | 1242 |
Notice: for some collections this may be very slow |
1243 |
(those not tuned for resizing themself)." |
|
348 | 1244 |
|
1245 |
|ret| |
|
1246 |
||
1247 |
self size < n ifTrue:[ |
|
5265 | 1248 |
^ self notEnoughElementsError |
348 | 1249 |
]. |
1250 |
ret := Array new:n. |
|
1251 |
n to:1 by:-1 do:[:i | |
|
5265 | 1252 |
ret at:i put:(self removeLast). |
348 | 1253 |
]. |
1254 |
^ ret |
|
1 | 1255 |
! ! |
1256 |
||
8858 | 1257 |
!Collection methodsFor:'bulk operations'! |
1258 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1259 |
abs |
17834 | 1260 |
"absolute value of all elements in the collection" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1261 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1262 |
^ self collect: [:a | a abs] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1263 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1264 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1265 |
TestCase assert:( { 1. -2. -3. 4 } abs = { 1. 2. 3. 4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1266 |
" |
17834 | 1267 |
|
1268 |
"Modified: / 18-03-2011 / 10:33:50 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1269 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1270 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1271 |
negated |
17834 | 1272 |
"negated value of all elements in the collection" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1273 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1274 |
^ self collect:[:a | a negated] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1275 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1276 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1277 |
TestCase assert:( { 1. -2. -3. 4 } negated = { -1. 2. 3. -4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1278 |
" |
17834 | 1279 |
|
1280 |
"Modified: / 18-03-2011 / 10:33:53 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1281 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1282 |
|
11468 | 1283 |
product |
11498 | 1284 |
"multiply up all elements." |
8858 | 1285 |
|
11468 | 1286 |
self emptyCheck. |
1287 |
^ self |
|
11469 | 1288 |
inject:(self first class unity) |
1289 |
into:[:accum :each | accum * each]. |
|
8858 | 1290 |
|
1291 |
" |
|
11468 | 1292 |
TestCase should:[ Array new sum ] raise:Error. |
1293 |
||
1294 |
TestCase assert:( { 1 } product == 1). |
|
1295 |
TestCase assert:( { 6 } product == 6). |
|
1296 |
TestCase assert:( { 1. 2. 3. 4. 5. } product = 5 factorial ) |
|
1297 |
" |
|
1298 |
! |
|
1299 |
||
1300 |
sum |
|
11498 | 1301 |
"sum up all elements; return 0 for an empty collection." |
11468 | 1302 |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1303 |
self isEmpty ifTrue:[^ 0]. |
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1304 |
|
11468 | 1305 |
^ self |
1306 |
inject:(self first class zero) |
|
1307 |
into:[:accum :each | accum + each]. |
|
1308 |
||
1309 |
||
1310 |
" |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1311 |
TestCase assert: ( { } sum = 0 ). |
11468 | 1312 |
TestCase assert: ( { 1 } sum = 1 ). |
1313 |
TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
|
1314 |
TestCase assert: ( (1 to:10) sum = 55 ). |
|
1315 |
TestCase assert: ( 'abc' asByteArray sum = 294 ). |
|
1316 |
TestCase assert: ( { 10 +/- 2. |
|
1317 |
20 +/- 4. |
|
1318 |
100 +/- 10 } sum = (130 +/- 16) ). |
|
1319 |
||
1320 |
TestCase assert: ( { (1 / 9). |
|
1321 |
(1 / 7). |
|
1322 |
} sum = (16 / 63) ). |
|
8858 | 1323 |
" |
11514 | 1324 |
! |
1325 |
||
1326 |
sum:aBlock |
|
17797 | 1327 |
"for each element in the receiver, evaluate the argument, aBlock and sum up the results. |
1328 |
Return the total sum or 0 for an empty collection. |
|
11514 | 1329 |
Similar to (self collect...) sum, but avoids creation of an intermediate collection." |
1330 |
||
1331 |
|sum| |
|
1332 |
||
1333 |
self do:[:element | |
|
1334 |
|thisValue| |
|
1335 |
||
1336 |
thisValue := aBlock value:element. |
|
1337 |
sum isNil ifTrue:[ |
|
1338 |
sum := thisValue |
|
1339 |
] ifFalse:[ |
|
1340 |
sum := sum + thisValue |
|
1341 |
]. |
|
1342 |
]. |
|
1343 |
^ sum ? 0 |
|
1344 |
||
1345 |
" |
|
1346 |
((1 to:10) collect:[:n | n squared]) sum |
|
1347 |
((1 to:10) sum:[:n | n squared]) |
|
1348 |
" |
|
17797 | 1349 |
|
1350 |
"Modified: / 23-08-2010 / 18:19:42 / cg" |
|
8858 | 1351 |
! ! |
1352 |
||
602 | 1353 |
!Collection methodsFor:'converting'! |
1354 |
||
1355 |
asArray |
|
1356 |
"return a new Array with the collections elements" |
|
1357 |
||
1358 |
|anArray |
|
1359 |
index "{ Class: SmallInteger }" | |
|
1 | 1360 |
|
602 | 1361 |
anArray := Array new:(self size). |
1362 |
index := 1. |
|
1363 |
self do:[:each | |
|
6135 | 1364 |
anArray at:index put:each. |
1365 |
index := index + 1 |
|
602 | 1366 |
]. |
1367 |
^ anArray |
|
1 | 1368 |
! |
1369 |
||
602 | 1370 |
asBag |
1371 |
"return a new Bag with the receiver collections elements" |
|
1 | 1372 |
|
602 | 1373 |
^ self addAllTo:(Bag new) |
1 | 1374 |
! |
1375 |
||
602 | 1376 |
asByteArray |
1377 |
"return a new ByteArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1378 |
(which must convert to 8bit integers in the range 0..255)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1379 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1380 |
^ self asIntegerArray:ByteArray |
1 | 1381 |
! |
1382 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1383 |
asCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1384 |
"return myself as a Collection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1385 |
I am already a Collection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1386 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1387 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1388 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1389 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1390 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1391 |
|
7906 | 1392 |
asDictionary |
1393 |
"return a new Dictionary with the receiver collections elements" |
|
1394 |
||
1395 |
|d| |
|
1396 |
||
1397 |
d := Dictionary new. |
|
1398 |
self keysAndValuesDo:[:k :v | d at:k put:v]. |
|
1399 |
^ d |
|
1400 |
||
1401 |
" |
|
1402 |
#(10 20 30 40 50 60 70 80 90) asDictionary |
|
1403 |
" |
|
1404 |
! |
|
1405 |
||
602 | 1406 |
asDoubleArray |
1407 |
"return a new DoubleArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1408 |
(which must convert to 64bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1409 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1410 |
^ self asIntegerArray:DoubleArray |
602 | 1411 |
! |
68 | 1412 |
|
11412 | 1413 |
asFlatOrderedCollection |
17834 | 1414 |
"return a new ordered collection containing all elements of the receiver |
1415 |
and recursively of all collectionelements from the receiver" |
|
1416 |
||
11412 | 1417 |
|coll| |
1418 |
||
1419 |
coll := OrderedCollection new. |
|
1420 |
self flatDo:[:el | coll add:el]. |
|
1421 |
^ coll. |
|
1422 |
||
1423 |
" |
|
1424 |
#( |
|
1425 |
(1 2 3) |
|
1426 |
4 5 |
|
1427 |
(6) |
|
1428 |
7 |
|
17834 | 1429 |
(8 (9 10) 11 12 (13 (14 (15) 16))) |
1430 |
) asFlatOrderedCollection |
|
11412 | 1431 |
" |
17834 | 1432 |
|
1433 |
"Modified: / 20-03-2011 / 22:02:36 / cg" |
|
11412 | 1434 |
! |
1435 |
||
602 | 1436 |
asFloatArray |
1437 |
"return a new FloatArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1438 |
(which must convert to 32bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1439 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1440 |
^ self asIntegerArray:FloatArray |
602 | 1441 |
! |
1442 |
||
1443 |
asIdentitySet |
|
1444 |
"return a new IdentitySet with the receiver collections elements" |
|
1445 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1446 |
^ self addAllNonNilElementsTo:(IdentitySet new:self size) |
68 | 1447 |
! |
1448 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1449 |
asIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1450 |
"return a new IntegerArray with the collection's elements |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1451 |
(which must convert to 32bit integers in the range 0..16rFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1452 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1453 |
^ self asIntegerArray:IntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1454 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1455 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1456 |
asIntegerArray:arrayClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1457 |
"return a new Array with the collection's elements" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1458 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1459 |
|anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1460 |
index "{ Class: SmallInteger }" | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1461 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1462 |
anIntegerArray := arrayClass new:(self size). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1463 |
index := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1464 |
self do:[:each | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1465 |
anIntegerArray at:index put:each asInteger. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1466 |
index := index + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1467 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1468 |
^ anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1469 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1470 |
|
2167 | 1471 |
asList |
1472 |
"return a new List with the receiver collections elements" |
|
1473 |
||
1474 |
^ self addAllTo:(List new:self size) |
|
1475 |
||
1476 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
1477 |
! |
|
1478 |
||
8050 | 1479 |
asLongIntegerArray |
1480 |
"return a new LongIntegerArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1481 |
(which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1482 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1483 |
^ self asIntegerArray:LongIntegerArray |
8050 | 1484 |
! |
1485 |
||
602 | 1486 |
asOrderedCollection |
1487 |
"return a new OrderedCollection with the receiver collections elements" |
|
1488 |
||
1489 |
^ self addAllTo:(OrderedCollection new:self size) |
|
1490 |
! |
|
1491 |
||
11469 | 1492 |
asRunArray |
1493 |
"return a new RunArray with the collections elements" |
|
1494 |
||
1495 |
^ RunArray from:self. |
|
1496 |
||
1497 |
"/ |runs lastElement occurrences| |
|
1498 |
"/ |
|
1499 |
"/ runs := RunArray new. |
|
1500 |
"/ occurrences := 0. |
|
1501 |
"/ self do:[:each | |
|
1502 |
"/ each == lastElement ifTrue:[ |
|
1503 |
"/ occurrences := occurrences + 1 |
|
1504 |
"/ ] ifFalse:[ |
|
1505 |
"/ runs add:lastElement withOccurrences:occurrences. |
|
1506 |
"/ occurrences := 1. |
|
1507 |
"/ lastElement := each |
|
1508 |
"/ ]. |
|
1509 |
"/ ]. |
|
1510 |
"/ occurrences ~~ 0 ifTrue:[ |
|
1511 |
"/ runs add:lastElement withOccurrences:occurrences |
|
1512 |
"/ ]. |
|
1513 |
"/ ^ runs |
|
1394 | 1514 |
|
1515 |
" |
|
11469 | 1516 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
1394 | 1517 |
" |
11469 | 1518 |
|
1519 |
"Modified: / 7.4.1998 / 09:50:54 / cg" |
|
1394 | 1520 |
! |
1521 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1522 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1523 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1524 |
I am already a Collection, but not sequenceable." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1525 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1526 |
^ self asArray |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1527 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1528 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1529 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1530 |
|
602 | 1531 |
asSet |
1532 |
"return a new Set with the receiver collections elements" |
|
1533 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1534 |
^ self addAllNonNilElementsTo:(Set new:self size) |
602 | 1535 |
! |
1536 |
||
9353 | 1537 |
asSharedCollection |
1538 |
"return a shared collection on the receiver. |
|
1539 |
This implements synchronized access to me." |
|
1540 |
||
1541 |
^ SharedCollection for:self. |
|
1542 |
! |
|
1543 |
||
17892 | 1544 |
asSignedIntegerArray |
1545 |
"return a new SignedIntegerArray with the collection's elements |
|
1546 |
(which must convert to 32bit signed integers in the range -16r80000000..16r7FFFFFFF)." |
|
1547 |
||
1548 |
^ self asIntegerArray:SignedIntegerArray |
|
1549 |
||
1550 |
"Created: / 07-10-2011 / 13:14:01 / cg" |
|
1551 |
! |
|
1552 |
||
602 | 1553 |
asSortedCollection |
1554 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 1555 |
|
602 | 1556 |
|aSortedCollection| |
1557 |
||
1558 |
aSortedCollection := SortedCollection new:self size. |
|
1559 |
aSortedCollection addAll:self. |
|
1560 |
^ aSortedCollection |
|
1561 |
! |
|
1562 |
||
1563 |
asSortedCollection:sortBlock |
|
1564 |
"return a new SortedCollection with the receiver collections elements, |
|
1565 |
using sortBlock for comparing" |
|
1566 |
||
1567 |
|aSortedCollection| |
|
1568 |
||
1569 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
1570 |
aSortedCollection addAll:self. |
|
1571 |
^ aSortedCollection |
|
1 | 1572 |
! |
1573 |
||
2919 | 1574 |
asSortedStrings |
1575 |
"Create & return a SortedCollection that sorts the receivers |
|
1576 |
elements according to the locales collating policy. |
|
1577 |
This is currently not really support - strings are sorted |
|
1578 |
without caring for the locale." |
|
1579 |
||
1580 |
|aSortedCollection| |
|
1581 |
||
1582 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1583 |
aSortedCollection addAll:self. |
|
1584 |
^ aSortedCollection |
|
1585 |
||
1586 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
1587 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
1588 |
! |
|
1589 |
||
1590 |
asSortedStrings:sortBlock |
|
1591 |
"Create & return a SortedCollection that sorts the receivers |
|
3061 | 1592 |
elements using sortBlock and according to the locales collating policy, |
1593 |
which is passed as first arg to sortBlock. |
|
2919 | 1594 |
This is currently not really support - strings are sorted |
1595 |
without caring for the locale." |
|
1596 |
||
1597 |
|aSortedCollection| |
|
1598 |
||
1599 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1600 |
aSortedCollection sortBlock:sortBlock. |
|
1601 |
aSortedCollection addAll:self. |
|
1602 |
^ aSortedCollection |
|
1603 |
||
3061 | 1604 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
1605 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 1606 |
! |
1607 |
||
1608 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
1609 |
"Create & return a SortedCollection that sorts the receivers |
|
1610 |
elements using sortBlock and according to the specified locales collating policy. |
|
1611 |
This is currently not really support - strings are sorted |
|
1612 |
without caring for the locale." |
|
1613 |
||
1614 |
|aSortedCollection| |
|
1615 |
||
1616 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1617 |
aSortedCollection sortBlock:sortBlock. |
|
1618 |
aSortedCollection addAll:self. |
|
1619 |
^ aSortedCollection |
|
1620 |
||
1621 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
1622 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
1623 |
! |
|
1624 |
||
1625 |
asSortedStringsWith: aCollationPolicy |
|
1626 |
"Create & return a SortedCollection that sorts the receivers |
|
1627 |
elements according to the specified locales collating policy. |
|
1628 |
This is currently not really support - strings are sorted |
|
1629 |
without caring for the locale." |
|
1630 |
||
1631 |
|aSortedCollection| |
|
1632 |
||
1633 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1634 |
aSortedCollection addAll:self. |
|
1635 |
^ aSortedCollection |
|
1636 |
||
1637 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
1638 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
1639 |
! |
|
1640 |
||
602 | 1641 |
asString |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1642 |
"return a String with the collection's elements |
602 | 1643 |
(which must convert to characters)" |
1644 |
||
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1645 |
|string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1646 |
index "{ Class: SmallInteger }" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1647 |
char bitsPerCharacter| |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1648 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1649 |
string := String new:(self size). |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1650 |
bitsPerCharacter := string bitsPerCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1651 |
|
602 | 1652 |
index := 1. |
1653 |
self do:[:each | |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1654 |
char := each asCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1655 |
char bitsPerCharacter > bitsPerCharacter ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1656 |
char bitsPerCharacter == 16 ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1657 |
string := Unicode16String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1658 |
] ifFalse:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1659 |
string := Unicode32String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1660 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1661 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1662 |
string at:index put:char. |
6135 | 1663 |
index := index + 1 |
360 | 1664 |
]. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1665 |
^ string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1666 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1667 |
" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1668 |
#(80 81 82) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1669 |
#(16r8000 16r8001 16r8002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1670 |
#(16r800000 16r800001 16r800002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1671 |
" |
602 | 1672 |
! |
1673 |
||
1674 |
asStringCollection |
|
1413 | 1675 |
"return a new string collection containing the elements; |
1676 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 1677 |
|
1678 |
^ self addAllTo:(StringCollection new) |
|
1413 | 1679 |
|
1680 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 1681 |
! |
1682 |
||
8160 | 1683 |
asUnicodeString |
1684 |
"return a String with the collections elements |
|
1685 |
(which must convert to characters)" |
|
1686 |
||
1687 |
|aString |
|
1688 |
index "{ Class: SmallInteger }" | |
|
1689 |
||
1690 |
aString := UnicodeString uninitializedNew:self size. |
|
1691 |
index := 1. |
|
1692 |
self do:[:each | |
|
1693 |
aString at:index put:each asCharacter. |
|
1694 |
index := index + 1 |
|
1695 |
]. |
|
1696 |
^ aString |
|
1697 |
||
1698 |
" |
|
1699 |
#(16r440 16r443 16r441 16r441 16r43A 16r438 16r439 16r20 16r44F 16r437 16r44B 16r43A) asUnicodeString |
|
1700 |
" |
|
1701 |
! |
|
1702 |
||
7885 | 1703 |
asWordArray |
1704 |
"return a new WordArray with the collections elements |
|
1705 |
(which must convert to integers in the range 0..16rFFFF)." |
|
1706 |
||
1707 |
|aWordArray |
|
1708 |
index "{ Class: SmallInteger }" | |
|
1709 |
||
1710 |
aWordArray := WordArray new:(self size). |
|
1711 |
index := 1. |
|
1712 |
self do:[:each | |
|
1713 |
aWordArray at:index put:each asInteger. |
|
1714 |
index := index + 1 |
|
1715 |
]. |
|
1716 |
^ aWordArray |
|
1717 |
! |
|
1718 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1719 |
copyAs:collectionClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1720 |
"return a new instance of collectionClass with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1721 |
This is similar to copy as:collectionClass, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1722 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1723 |
instance of collectionClass." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1724 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1725 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1726 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1727 |
newColl := self as:collectionClass. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1728 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1729 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1730 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1731 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1732 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1733 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1734 |
|coll1 coll2| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1735 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1736 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1737 |
coll2 := coll1 copyAs:Array. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1738 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1739 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1740 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1741 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1742 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate an OC'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1743 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1744 |
self assert:(coll1 isSameSequenceAs: coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1745 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1746 |
coll1 := #(1 2 3 4 5) asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1747 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1748 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1749 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1750 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1751 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1752 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1753 |
copyAsOrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1754 |
"return a new OrderedCollection with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1755 |
This is similar to copy asOrderedCollection, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1756 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1757 |
OrderedCollection." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1758 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1759 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1760 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1761 |
newColl := self asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1762 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1763 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1764 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1765 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1766 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1767 |
|
7905 | 1768 |
keysAndValues |
1769 |
|assocs| |
|
1770 |
||
1771 |
assocs := OrderedCollection new. |
|
1772 |
self keysAndValuesDo:[:k :v | |
|
1773 |
assocs add:(k -> v). |
|
1774 |
]. |
|
1775 |
^ assocs. |
|
1776 |
||
1777 |
" |
|
1778 |
#(10 20 30 40 50 60) keysAndValues |
|
1779 |
" |
|
1780 |
! |
|
1781 |
||
9410 | 1782 |
literalArrayEncoding |
1783 |
"encode myself as an array literal, from which a copy of the receiver |
|
1784 |
can be reconstructed with #decodeAsLiteralArray." |
|
1785 |
||
1786 |
|encoding idx| |
|
1787 |
||
1788 |
encoding := Array new:self size + 1. |
|
1789 |
encoding at:1 put:self class name. |
|
1790 |
idx := 2. |
|
1791 |
self do:[:element| |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1792 |
encoding at:idx put:element literalArrayEncoding. |
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1793 |
idx := idx + 1. |
9410 | 1794 |
]. |
1795 |
^ encoding |
|
1796 |
||
1797 |
||
1798 |
" |
|
1799 |
(Array with:(Color red:50 green:50 blue:50) |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1800 |
with:(1 @ 2) |
9410 | 1801 |
) literalArrayEncoding decodeAsLiteralArray |
1802 |
" |
|
1803 |
||
1804 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
1805 |
! |
|
1806 |
||
602 | 1807 |
readStream |
1808 |
"return a stream for reading from the receiver" |
|
1809 |
||
1810 |
^ ReadStream on:self |
|
88 | 1811 |
|
1812 |
" |
|
602 | 1813 |
|s| |
1814 |
||
1815 |
s := 'hello world' readStream. |
|
1816 |
s next:5. |
|
1817 |
s next. |
|
1818 |
(s next:5) inspect |
|
88 | 1819 |
" |
1 | 1820 |
! |
1821 |
||
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1822 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1823 |
"return a stream for reading from the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1824 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1825 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1826 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1827 |
^ self readStream |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1828 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1829 |
|
8631 | 1830 |
readWriteStream |
1831 |
"return a stream for reading and writing from/to the receiver" |
|
1832 |
||
1833 |
^ ReadWriteStream with:self |
|
1834 |
||
1835 |
" |
|
11436 | 1836 |
'hello world' readWriteStream |
1837 |
nextPutAll:'+Foo'; |
|
1838 |
contents |
|
1839 |
||
1840 |
'hello world' readWriteStream |
|
1841 |
setToEnd; |
|
1842 |
nextPutAll:'+Foo'; |
|
1843 |
contents |
|
8631 | 1844 |
" |
1845 |
! |
|
1846 |
||
602 | 1847 |
writeStream |
1848 |
"return a stream for writing onto the receiver" |
|
1849 |
||
1850 |
^ WriteStream on:self |
|
2 | 1851 |
|
602 | 1852 |
" |
1853 |
|s| |
|
1854 |
||
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1855 |
s := #() writeStream. |
602 | 1856 |
s nextPut:1. |
1857 |
s nextPut:2. |
|
1858 |
s nextPut:3. |
|
1859 |
s contents inspect |
|
1860 |
" |
|
88 | 1861 |
|
1862 |
" |
|
602 | 1863 |
|s| |
2 | 1864 |
|
602 | 1865 |
s := OrderedCollection new writeStream. |
1866 |
s nextPut:1. |
|
1867 |
s nextPut:2. |
|
1868 |
s nextPut:3. |
|
1869 |
s contents inspect |
|
1870 |
" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1871 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1872 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1873 |
writeStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1874 |
"return a stream for writing onto the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1875 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1876 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1877 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1878 |
^ self writeStream |
602 | 1879 |
! ! |
1 | 1880 |
|
602 | 1881 |
!Collection methodsFor:'copying'! |
1882 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1883 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1884 |
"return a copy of the receiver. |
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
1885 |
Redefined to pass the original as argument to the postCopyFrom method." |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1886 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1887 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1888 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1889 |
"Created: / 19.4.1998 / 20:02:53 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1890 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1891 |
|
602 | 1892 |
copyEmpty |
1893 |
"return a copy of the receiver with no elements. |
|
1894 |
This is used by copying and enumeration methods |
|
1895 |
to get a new instance which is similar to the receiver." |
|
1896 |
||
1897 |
^ self species new |
|
1 | 1898 |
! |
1899 |
||
602 | 1900 |
copyEmpty:size |
1901 |
"return a copy of the receiver with no elements, but space for |
|
1902 |
size elements. This is used by copying and enumeration methods |
|
1903 |
to get a new instance which is similar to the receiver. |
|
1904 |
This method should be redefined in subclasses with instance |
|
1905 |
variables, which should be put into the copy too. |
|
1906 |
For example, SortedCollection has to copy its sortBlock into the |
|
1907 |
new collection." |
|
1 | 1908 |
|
602 | 1909 |
^ self species new:size |
360 | 1910 |
! |
1911 |
||
602 | 1912 |
copyEmptyAndGrow:size |
1913 |
"return a copy of the receiver with size nil elements. |
|
1914 |
This is used by copying and enumeration methods |
|
1915 |
to get a new instance which is similar to the receiver." |
|
155 | 1916 |
|
602 | 1917 |
^ (self copyEmpty:size) grow:size |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1918 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1919 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1920 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1921 |
"sent to a freshly copied object to give it a chance to adjust things. |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1922 |
Notice, that for Sets/Dicts etc. a rehash is not needed, since the copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1923 |
will have the same hash key as the receiver (as long as ST/X provides the |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1924 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1925 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1926 |
"for ST-80 compatibility, we try postCopy here ..." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1927 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1928 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1929 |
"Created: / 19.4.1998 / 19:59:42 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1930 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 1931 |
! ! |
1932 |
||
1933 |
!Collection methodsFor:'enumerating'! |
|
1934 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1935 |
addAllNonNilElementsTo:aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1936 |
"add all nonNil elements of the receiver to aCollection. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1937 |
Return aCollection." |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1938 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1939 |
self do:[:each | each notNil ifTrue:[aCollection add:each]]. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1940 |
^ aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1941 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1942 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1943 |
#(1 2 3 4 5 1 2 3 4 5 nil) asOrderedCollection addAllNonNilElementsTo:Set new |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1944 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1945 |
! |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1946 |
|
602 | 1947 |
addAllTo:aCollection |
1948 |
"add all elements of the receiver, to aCollection. |
|
1949 |
Return aCollection." |
|
77 | 1950 |
|
602 | 1951 |
self do:[:each | aCollection add:each]. |
1952 |
^ aCollection |
|
5254 | 1953 |
|
1954 |
" |
|
1955 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
1956 |
" |
|
1957 |
||
1958 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 1959 |
! |
1960 |
||
1 | 1961 |
collect:aBlock |
1962 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1963 |
and return a new collection with the results" |
|
1964 |
||
1965 |
|newCollection| |
|
1966 |
||
201 | 1967 |
newCollection := self species new. |
1968 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 1969 |
^ newCollection |
68 | 1970 |
|
1971 |
" |
|
1972 |
#(1 2 3 4) collect:[:n | n * 2] |
|
1973 |
" |
|
1 | 1974 |
! |
1975 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1976 |
collect:aBlock as:aClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1977 |
"like collect, but use an instance of aClass to collect the results. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1978 |
Also avoids the need for an extra intermediate collection which is created with |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1979 |
the standard coding: 'self asXXXX collect:[...]" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1980 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1981 |
|newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1982 |
idx "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1983 |
sz "{ Class:SmallInteger }"| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1984 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1985 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1986 |
newCollection := (aClass new:sz) grow:sz. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1987 |
newCollection isSequenceable ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1988 |
idx := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1989 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1990 |
newCollection at:idx put:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1991 |
idx := idx + 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1992 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1993 |
] ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1994 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1995 |
newCollection add:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1996 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1997 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1998 |
^ newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1999 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2000 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2001 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2002 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2003 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2004 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2005 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2006 |
collect:collectBlock thenReject:rejectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2007 |
"combination of collect followed by reject. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2008 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2009 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2010 |
|
17780 | 2011 |
^ self collect:collectBlock thenSelect:[:el | (rejectBlock value:el) not] |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2012 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2013 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2014 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenReject:[:i | i > 10] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2015 |
" |
17780 | 2016 |
|
2017 |
"Modified: / 11-07-2010 / 17:03:16 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2018 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2019 |
|
4890 | 2020 |
collect:collectBlock thenSelect:selectBlock |
2021 |
"combination of collect followed by select. |
|
2022 |
May be redefined by some subclasses for optimal performance |
|
7164 | 2023 |
(avoiding the creation of intermediate garbage)" |
4890 | 2024 |
|
4892 | 2025 |
^ (self collect:collectBlock) select:selectBlock |
2026 |
||
2027 |
" |
|
2028 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
2029 |
" |
|
4890 | 2030 |
! |
2031 |
||
6867 | 2032 |
collectAll:aBlock |
2033 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
9315 | 2034 |
The block is supposed to return a collection, whose elements are collected. |
8608 | 2035 |
The species of the returned collection is that of the first returned |
2036 |
partial result." |
|
6867 | 2037 |
|
2038 |
|result| |
|
2039 |
||
2040 |
self do:[:element | |
|
2041 |
|individualResult| |
|
2042 |
||
2043 |
individualResult := aBlock value:element. |
|
2044 |
result isNil ifTrue:[ |
|
2045 |
result := individualResult speciesForAdding new. |
|
2046 |
]. |
|
2047 |
result addAll:individualResult. |
|
2048 |
]. |
|
2049 |
||
8609
daa0d6682135
collectAll: now returns an empty collection if
Claus Gittinger <cg@exept.de>
parents:
8608
diff
changeset
|
2050 |
^ result ? #() |
6867 | 2051 |
|
2052 |
" |
|
2053 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] |
|
7629 | 2054 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] |
8607 | 2055 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] |
2056 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] |
|
7629 | 2057 |
" |
6867 | 2058 |
! |
2059 |
||
602 | 2060 |
count:aBlock |
2061 |
"count elements, for which aBlock returns true. |
|
2062 |
Return the sum." |
|
2063 |
||
7336 | 2064 |
|count| |
2065 |
||
2066 |
count := 0. |
|
2067 |
self do:[:element | |
|
2068 |
(aBlock value:element) ifTrue:[count := count + 1] |
|
2069 |
]. |
|
2070 |
^ count |
|
77 | 2071 |
|
2072 |
" |
|
602 | 2073 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
7336 | 2074 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
77 | 2075 |
" |
2076 |
! |
|
2077 |
||
1 | 2078 |
detect:aBlock |
2079 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2080 |
the block returns true; in this case return the element which caused |
|
2081 |
the true evaluation. |
|
345 | 2082 |
If none of the evaluations returns true, report an error" |
1 | 2083 |
|
2084 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 2085 |
|
2086 |
" |
|
2087 |
#(1 2 3 4) detect:[:n | n odd] |
|
2088 |
#(2 4 6 8) detect:[:n | n odd] |
|
2089 |
" |
|
1 | 2090 |
! |
2091 |
||
8750 | 2092 |
detect:generatorBlock forWhich:testBlock ifNone:exceptionBlock |
2093 |
"evaluate generatorBlock for each element in the receiver until |
|
2094 |
testBlock returns true for it; |
|
2095 |
in this case return the value from generatorBlock, which caused the true evaluation. |
|
2096 |
If none of the test evaluations returns true, return the result of the |
|
2097 |
evaluation of the exceptionBlock" |
|
2098 |
||
2099 |
self do:[:each | |
|
2100 |
|val| |
|
2101 |
||
2102 |
val := generatorBlock value:each. |
|
2103 |
(testBlock value:val) ifTrue:[^ val]. |
|
2104 |
]. |
|
2105 |
^ exceptionBlock value |
|
2106 |
||
2107 |
" |
|
2108 |
#(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
2109 |
#( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
8751 | 2110 |
|
2111 |
#( 'st' 'c' 'java' ) |
|
2112 |
detect:[:ext | 'Foo' asFilename withSuffix:ext] |
|
2113 |
forWhich:[:fn | fn exists] |
|
2114 |
ifNone:nil |
|
8750 | 2115 |
" |
2116 |
! |
|
2117 |
||
9842 | 2118 |
detect:aOneArgBlock ifNone:exceptionBlock |
1 | 2119 |
"evaluate the argument, aBlock for each element in the receiver until |
2120 |
the block returns true; in this case return the element which caused |
|
2121 |
the true evaluation. |
|
2122 |
If none of the evaluations returns true, return the result of the |
|
2123 |
evaluation of the exceptionBlock" |
|
2124 |
||
2125 |
self do:[:each | |
|
9842 | 2126 |
(aOneArgBlock value:each) ifTrue:[^ each]. |
1 | 2127 |
]. |
2128 |
^ exceptionBlock value |
|
68 | 2129 |
|
2130 |
" |
|
2131 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
2132 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
2133 |
" |
|
9842 | 2134 |
|
2135 |
"Modified: / 13-09-2006 / 11:17:42 / cg" |
|
1 | 2136 |
! |
2137 |
||
421 | 2138 |
detectLast:aBlock |
2139 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2140 |
the block returns true; in this case return the element which caused |
|
2141 |
the true evaluation. The elements are processed in reverse order. |
|
2142 |
If none of the evaluations returns true, report an error" |
|
2143 |
||
2144 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
2145 |
||
2146 |
" |
|
2147 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
2148 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
2149 |
" |
|
2150 |
! |
|
2151 |
||
2152 |
detectLast:aBlock ifNone:exceptionBlock |
|
2153 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2154 |
the block returns true; in this case return the element which caused |
|
2155 |
the true evaluation. The elements are processed in reverse order. |
|
2156 |
If none of the evaluations returns true, return the result of the |
|
2157 |
evaluation of the exceptionBlock" |
|
2158 |
||
2159 |
self reverseDo:[:each | |
|
6135 | 2160 |
(aBlock value:each) ifTrue:[^ each]. |
421 | 2161 |
]. |
2162 |
^ exceptionBlock value |
|
2163 |
||
2164 |
" |
|
2165 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2166 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2167 |
" |
|
2168 |
! |
|
2169 |
||
17851 | 2170 |
detectMax: aBlock |
2171 |
"Evaluate aBlock with each of the receiver's elements as the argument. |
|
2172 |
Answer the element for which aBlock evaluates to the highest magnitude. |
|
2173 |
If collection empty, return nil. This method might also be called elect:." |
|
2174 |
||
2175 |
| maxElement maxValue | |
|
2176 |
self do: [:each | | val | |
|
2177 |
maxValue == nil |
|
2178 |
ifFalse: [ |
|
2179 |
(val := aBlock value: each) > maxValue ifTrue: [ |
|
2180 |
maxElement := each. |
|
2181 |
maxValue := val]] |
|
2182 |
ifTrue: ["first element" |
|
2183 |
maxElement := each. |
|
2184 |
maxValue := aBlock value: each]. |
|
2185 |
"Note that there is no way to get the first element that works |
|
2186 |
for all kinds of Collections. Must test every one."]. |
|
2187 |
^ maxElement |
|
2188 |
||
2189 |
"Created: / 20-08-2011 / 21:34:49 / cg" |
|
2190 |
! |
|
2191 |
||
2192 |
detectMin: aBlock |
|
2193 |
"Evaluate aBlock with each of the receiver's elements as the argument. |
|
2194 |
Answer the element for which aBlock evaluates to the lowest number. |
|
2195 |
If collection empty, return nil." |
|
2196 |
||
2197 |
| minElement minValue | |
|
2198 |
self do: [:each | | val | |
|
2199 |
minValue == nil |
|
2200 |
ifFalse: [ |
|
2201 |
(val := aBlock value: each) < minValue ifTrue: [ |
|
2202 |
minElement := each. |
|
2203 |
minValue := val]] |
|
2204 |
ifTrue: ["first element" |
|
2205 |
minElement := each. |
|
2206 |
minValue := aBlock value: each]. |
|
2207 |
"Note that there is no way to get the first element that works |
|
2208 |
for all kinds of Collections. Must test every one."]. |
|
2209 |
^ minElement |
|
2210 |
||
2211 |
"Created: / 20-08-2011 / 21:35:13 / cg" |
|
2212 |
! |
|
2213 |
||
602 | 2214 |
do:aBlock |
2215 |
"evaluate the argument, aBlock for each element" |
|
2216 |
||
2217 |
^ self subclassResponsibility |
|
2218 |
! |
|
2219 |
||
4394 | 2220 |
do:aBlock inBetweenDo:betweenBlock |
2221 |
"evaluate the argument, aBlock for each element. |
|
2222 |
Between elements (i.e. after each except for the last), |
|
2223 |
evaluate betweenBlock. |
|
5254 | 2224 |
This is a utility helper for collection printers |
2225 |
(for example, to print a space between elements)." |
|
4394 | 2226 |
|
8359 | 2227 |
<resource: #obsolete> |
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2228 |
self obsoleteMethodWarning:'use #do:separatedBy:'. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2229 |
^ self do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2230 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2231 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2232 |
#(1 2 3 4) do:[:el | Transcript show:el] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2233 |
inBetweenDo:[ Transcript show:'-'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2234 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2235 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2236 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2237 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2238 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2239 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2240 |
associations |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2241 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2242 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2243 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2244 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2245 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2246 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2247 |
! |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2248 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2249 |
do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2250 |
"evaluate the argument, aBlock for each element. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2251 |
Between elements (i.e. after each except for the last), |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2252 |
evaluate betweenBlock. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2253 |
This is a utility helper for collection printers |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2254 |
(for example, to print a space between elements)." |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2255 |
|
4394 | 2256 |
|first| |
2257 |
||
2258 |
first := true. |
|
2259 |
self do:[:element | |
|
2260 |
first ifTrue:[ |
|
2261 |
first := false |
|
2262 |
] ifFalse:[ |
|
2263 |
betweenBlock value. |
|
2264 |
]. |
|
2265 |
aBlock value:element |
|
2266 |
]. |
|
2267 |
||
2268 |
" |
|
2269 |
#(1 2 3 4) do:[:el | Transcript show:el] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2270 |
separatedBy:[ Transcript show:'-'] |
5619 | 2271 |
|
2272 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2273 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2274 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2275 |
|
2276 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2277 |
associations |
|
2278 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2279 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2280 |
|
4394 | 2281 |
" |
5254 | 2282 |
|
2283 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 2284 |
! |
2285 |
||
1212 | 2286 |
doWithExit:aBlock |
2287 |
"evaluate the argument, aBlock for each element. |
|
2288 |
Passes an additional exit object, which can be used to leave |
|
2289 |
the loop early, by sending it a #value: message. |
|
2290 |
Returns nil or the value passed to the exit>>value: message. |
|
2291 |
||
8662 | 2292 |
Notice, that this is different to a return statement in the block, |
2293 |
which returns from the enclosed method, NOT only from the block." |
|
2294 |
||
2295 |
|exit| |
|
2296 |
||
2297 |
exit := [:exitValue | ^exitValue]. |
|
2298 |
self do:[:el | |
|
2299 |
aBlock value:el value:exit |
|
2300 |
]. |
|
2301 |
^ nil |
|
1212 | 2302 |
|
2303 |
"Example: |
|
2304 |
search for the first element which is >= 99; return it. |
|
2305 |
remaining elements are not processed: |
|
2306 |
||
2307 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2308 |
doWithExit:[:element :exit | |
|
1422 | 2309 |
Transcript showCR:element. |
8662 | 2310 |
element >= 99 ifTrue:[exit value:element]] |
1212 | 2311 |
|
2312 |
#(1 2 3 4 5 6 7 8 9) |
|
2313 |
doWithExit:[:element :exit | |
|
1422 | 2314 |
Transcript showCR:element. |
1212 | 2315 |
element >= 99 ifTrue:[exit value:element]] |
2316 |
" |
|
2317 |
||
2318 |
"to demonstrate the difference to returning from the block: |
|
2319 |
this works: |
|
2320 |
||
2321 |
|el| |
|
2322 |
||
2323 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2324 |
doWithExit:[:element :exit | |
|
2325 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 2326 |
Transcript showCR:el. |
1212 | 2327 |
|
2328 |
||
1422 | 2329 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 2330 |
|
2331 |
|el| |
|
2332 |
||
2333 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2334 |
do:[:element | |
|
2335 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 2336 |
Transcript showCR:el. |
1212 | 2337 |
" |
2338 |
||
2339 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
2340 |
! |
|
2341 |
||
11412 | 2342 |
flatDo:aBlock |
2343 |
"for each element of the collection, if its a scalar, evaluate aBlock for it; |
|
17815 | 2344 |
otherwise, recursively invoke flatDo on the collection. |
2345 |
Thus implementing a depth-first enumeration" |
|
11412 | 2346 |
|
2347 |
self do:[:each | |
|
2348 |
(each isNonByteCollection) ifTrue:[ |
|
2349 |
each flatDo:aBlock |
|
2350 |
] ifFalse:[ |
|
2351 |
aBlock value:each |
|
2352 |
]. |
|
2353 |
]. |
|
2354 |
||
2355 |
" |
|
2356 |
#( |
|
2357 |
(1 2 3) |
|
2358 |
4 5 |
|
2359 |
(6) |
|
2360 |
7 |
|
2361 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) flatDo:[:el | Transcript showCR:el] |
|
2362 |
" |
|
17815 | 2363 |
|
2364 |
"Modified: / 22-01-2011 / 09:12:22 / cg" |
|
11412 | 2365 |
! |
2366 |
||
17862 | 2367 |
fold: binaryBlock |
2368 |
"Evaluate the block with the first two elements of the receiver, |
|
2369 |
then with the result of the first evaluation and the next element, |
|
2370 |
and so on. Answer the result of the final evaluation. If the receiver |
|
2371 |
is empty, raise an error. If the receiver has a single element, answer |
|
2372 |
that element." |
|
2373 |
||
2374 |
| firstValue nextValue | |
|
2375 |
firstValue := nextValue := Object new. "something that can't be in the receiver" |
|
2376 |
self do: |
|
2377 |
[:each | |
|
2378 |
nextValue := firstValue == nextValue |
|
2379 |
ifTrue: [each] |
|
2380 |
ifFalse: [binaryBlock value: nextValue value: each]]. |
|
2381 |
^nextValue == firstValue |
|
2382 |
ifTrue: [self emptyCollectionError] |
|
2383 |
ifFalse: [nextValue] |
|
2384 |
||
2385 |
" |
|
2386 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b] |
|
2387 |
" |
|
2388 |
||
2389 |
"Created: / 14-09-2011 / 16:29:53 / cg" |
|
2390 |
! |
|
2391 |
||
1 | 2392 |
inject:thisValue into:binaryBlock |
1262 | 2393 |
"starting with thisValue for value, pass this value and each element |
2394 |
to binaryBlock, replacing value with the result returned from the block |
|
2395 |
in the next iteration." |
|
2396 |
||
1 | 2397 |
|nextValue| |
2398 |
||
2399 |
nextValue := thisValue. |
|
2400 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
2401 |
^ nextValue |
|
68 | 2402 |
|
1262 | 2403 |
"sum up the elements of a collection: |
2404 |
||
68 | 2405 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
2406 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 2407 |
|
2408 |
find the minimum: |
|
2409 |
||
2410 |
|coll| |
|
2411 |
coll := #(1 99 -15 20 100). |
|
2412 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 2413 |
" |
1262 | 2414 |
|
2415 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 2416 |
! |
2417 |
||
602 | 2418 |
keysAndValuesDo:aTwoArgBlock |
2419 |
"evaluate the argument, aBlock for every element in the collection, |
|
2420 |
passing both index and element as arguments." |
|
2421 |
||
2422 |
^ self errorNotKeyed |
|
2423 |
! |
|
2424 |
||
1359 | 2425 |
keysAndValuesReverseDo:aTwoArgBlock |
2426 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
2427 |
passing both index and element as arguments." |
|
2428 |
||
2429 |
^ self errorNotKeyed |
|
2430 |
||
2431 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
2432 |
! |
|
2433 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2434 |
keysAndValuesSelect:selectBlockWith2Args thenCollect:collectBlockWith2Args |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2435 |
|collected| |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2436 |
|
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2437 |
collected := OrderedCollection new. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2438 |
self keysAndValuesDo:[:eachKey :eachValue | |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2439 |
(selectBlockWith2Args value:eachKey value:eachValue) ifTrue:[ |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2440 |
collected add:(collectBlockWith2Args value:eachKey value:eachValue) |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2441 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2442 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2443 |
^ collected |
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2444 |
|
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2445 |
" |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2446 |
#(10 20 30 40) keysAndValuesSelect:[:idx :val | idx > 2] thenCollect:[:idx :val | idx->val] |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2447 |
" |
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2448 |
! |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2449 |
|
17797 | 2450 |
keysDo:aBlock |
2451 |
"evaluate the argument, aBlock for every key in the collection." |
|
2452 |
||
2453 |
self keysAndValuesDo:[:k :v | aBlock value:k] |
|
2454 |
||
2455 |
"Created: / 24-08-2010 / 10:12:14 / cg" |
|
2456 |
! |
|
2457 |
||
7454 | 2458 |
map:selector |
2459 |
"for lisp fans - similar to collect" |
|
2460 |
||
2461 |
^ self collect:[:eachElement | eachElement perform:selector]. |
|
2462 |
||
2463 |
" |
|
2464 |
#(1 2 3 4) map:#negated |
|
2465 |
" |
|
2466 |
! |
|
2467 |
||
2468 |
map:selector with:arg |
|
2469 |
"for lisp fans - similar to collect" |
|
2470 |
||
2471 |
^ self collect:[:eachElement | eachElement perform:selector with:arg]. |
|
2472 |
||
2473 |
" |
|
2474 |
#(1 2 3 4) map:#+ with:1 |
|
2475 |
" |
|
2476 |
! |
|
2477 |
||
8863 | 2478 |
nonNilElementsDo:aBlock |
2479 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
2480 |
||
2481 |
self do:[:each | |
|
2482 |
each notNil ifTrue:[ |
|
2483 |
aBlock value:each. |
|
2484 |
] |
|
2485 |
] |
|
17907 | 2486 |
|
2487 |
" |
|
2488 |
#(1 nil 3 nil nil 6 7 nil) |
|
2489 |
nonNilElementsDo:[:el | Transcript showCR:el] |
|
2490 |
" |
|
2491 |
||
2492 |
"Modified (comment): / 21-12-2011 / 15:51:39 / cg" |
|
8863 | 2493 |
! |
2494 |
||
10750 | 2495 |
pairsDo:aTwoArgBlock |
2496 |
"evaluate the argument, aTwoArgBlock for every element in the collection, |
|
602 | 2497 |
which is supposed to consist of 2-element collections. |
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2498 |
The block is called with 2 arguments for each collection in the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2499 |
CONFUSION ATTACK: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2500 |
this is different from pairWiseDo:. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2501 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2502 |
(sigh: but we were first, so they should have adapted...)" |
602 | 2503 |
|
2504 |
self do:[:aPair | |
|
10750 | 2505 |
aTwoArgBlock value:(aPair at:1) value:(aPair at:2). |
602 | 2506 |
] |
10750 | 2507 |
|
602 | 2508 |
" |
6555 | 2509 |
#( |
2510 |
(1 one) |
|
2511 |
(2 two) |
|
2512 |
(3 three) |
|
2513 |
(4 four) |
|
2514 |
(5 five) |
|
2515 |
(6 six) |
|
2516 |
) pairsDo: |
|
2517 |
[:num :sym | |
|
2518 |
Transcript show:num; show:' is: '; showCR:sym] |
|
602 | 2519 |
|
2520 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 2521 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 2522 |
" |
10750 | 2523 |
|
2524 |
"Modified: / 20-10-2007 / 17:17:50 / cg" |
|
602 | 2525 |
! |
2526 |
||
17847 | 2527 |
partition:check into:aTwoArgBlock |
2528 |
"partition the receiver elements into two sets, depending on the outcome |
|
2529 |
of a check block. Evaluate aTwoArgBlock on the two selected and rejected value |
|
2530 |
collections. Also return the selected values as return value" |
|
2531 |
||
2532 |
|selected rejected| |
|
2533 |
||
2534 |
selected := OrderedCollection new. |
|
2535 |
rejected := OrderedCollection new. |
|
2536 |
self do:[:each | ((check value:each) ifTrue:[selected] ifFalse:[rejected]) add:each]. |
|
2537 |
aTwoArgBlock value:selected value:rejected. |
|
2538 |
^ selected |
|
2539 |
||
17907 | 2540 |
" |
2541 |
#(1 2 3 4 5 6 7 8) |
|
2542 |
partition:[:el | el even] |
|
2543 |
into:[:evenElements :oddElements | |
|
2544 |
Transcript show:'even: '; showCR:evenElements. |
|
2545 |
Transcript show:' odd: '; showCR:oddElements. |
|
2546 |
]. |
|
2547 |
" |
|
2548 |
||
17847 | 2549 |
"Created: / 20-07-2011 / 00:54:41 / cg" |
2550 |
! |
|
2551 |
||
2552 |
reduceLeft:aTwoArgBlock |
|
2553 |
"reduce by iteratively applying aTwoArgBlock to an element and the result from the |
|
2554 |
previous reduce. The first evaluation is on the first two elements." |
|
2555 |
||
2556 |
|first| |
|
2557 |
||
2558 |
first := true. |
|
2559 |
^ self |
|
2560 |
inject:nil |
|
2561 |
into:[:accu :el | |
|
2562 |
first ifTrue:[ |
|
2563 |
first := false. |
|
2564 |
el |
|
2565 |
] ifFalse:[ |
|
2566 |
aTwoArgBlock value:accu value:el |
|
2567 |
] |
|
2568 |
] |
|
2569 |
||
2570 |
" |
|
2571 |
#(1 2 3 4 5) reduceLeft:[:sum :el | sum + el] |
|
2572 |
" |
|
2573 |
||
2574 |
"Created: / 20-07-2011 / 01:01:03 / cg" |
|
2575 |
! |
|
2576 |
||
1 | 2577 |
reject:aBlock |
2578 |
"return a new collection with all elements from the receiver, for which |
|
2579 |
the argument aBlock evaluates to false" |
|
2580 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2581 |
^ self select:[:element | (aBlock value:element) not] |
68 | 2582 |
|
2583 |
" |
|
2584 |
#(1 2 3 4) reject:[:e | e odd] |
|
2585 |
(1 to:10) reject:[:e | e even] |
|
2586 |
" |
|
1 | 2587 |
! |
2588 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2589 |
reject:rejectBlock thenCollect:collectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2590 |
"combination of reject followed by collect. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2591 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2592 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2593 |
|
17780 | 2594 |
^ self select:[:el | (rejectBlock value:el) not] thenCollect:collectBlock |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2595 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2596 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2597 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenCollect:[:i | i * 2] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2598 |
" |
17780 | 2599 |
|
2600 |
"Modified: / 11-07-2010 / 17:04:07 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2601 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2602 |
|
602 | 2603 |
reverseDo:aBlock |
2604 |
"evaluate the argument, aBlock for each element in reverse order." |
|
2605 |
||
2606 |
"it could be defined in terms of do: - but very inefficient. |
|
2607 |
Better force programmer to define a better version ..." |
|
2608 |
||
2609 |
^ self subclassResponsibility |
|
2610 |
! |
|
2611 |
||
1 | 2612 |
select:aBlock |
2613 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 2614 |
the argument aBlock evaluates to true. |
2615 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 2616 |
|
17795 | 2617 |
^ self select:aBlock as:(self species) |
2618 |
||
2619 |
" |
|
2620 |
#(1 2 3 4) select:[:e | e odd] |
|
2621 |
(1 to:10) select:[:e | e even] |
|
2622 |
" |
|
2623 |
||
2624 |
"Modified: / 07-08-2010 / 16:26:40 / cg" |
|
2625 |
! |
|
2626 |
||
2627 |
select:aBlock as:aCollectionClass |
|
2628 |
"return a new collection with all elements from the receiver, for which |
|
2629 |
the argument aBlock evaluates to true. |
|
2630 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
2631 |
||
1 | 2632 |
|newCollection| |
2633 |
||
17795 | 2634 |
newCollection := aCollectionClass new. |
1 | 2635 |
self do:[:each | |
5265 | 2636 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 2637 |
]. |
2638 |
^ newCollection |
|
68 | 2639 |
|
2640 |
" |
|
17795 | 2641 |
#(1 2 3 4) select:[:e | e odd] as:OrderedCollection |
2642 |
(1 to:10) select:[:e | e even] as:OrderedCollection |
|
68 | 2643 |
" |
17795 | 2644 |
|
2645 |
"Created: / 07-08-2010 / 16:26:15 / cg" |
|
68 | 2646 |
! |
2647 |
||
2648 |
select:aBlock ifNone:exceptionBlock |
|
2649 |
"try a new collection with all elements from the receiver, for which |
|
2650 |
the argument aBlock evaluates to true. If none of the elements passes |
|
5265 | 2651 |
the check of aBlock, return the result of evaluating exceptionBlock. |
2652 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
68 | 2653 |
|
2654 |
|newCollection| |
|
2655 |
||
2656 |
newCollection := self select:aBlock. |
|
2657 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
2658 |
^ newCollection |
|
2659 |
||
2660 |
" |
|
2661 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
2662 |
" |
|
12 | 2663 |
! |
2664 |
||
4892 | 2665 |
select:selectBlock thenCollect:collectBlock |
2666 |
"combination of select followed by collect. |
|
2667 |
May be redefined by some subclasses for optimal performance |
|
5844 | 2668 |
(avoiding the creation of intermediate garbage)" |
4892 | 2669 |
|
2670 |
^ (self select:selectBlock) collect:collectBlock |
|
2671 |
||
2672 |
" |
|
2673 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
2674 |
" |
|
2675 |
! |
|
2676 |
||
6695 | 2677 |
select:selectBlock thenDo:doBlock |
2678 |
"combination of select followed by do |
|
2679 |
Avoids the creation of intermediate garbage" |
|
2680 |
||
2681 |
self do:[:eachElement | |
|
2682 |
(selectBlock value:eachElement) ifTrue:[ |
|
2683 |
doBlock value:eachElement |
|
2684 |
] |
|
2685 |
]. |
|
2686 |
||
2687 |
" |
|
2688 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
2689 |
" |
|
2690 |
! |
|
2691 |
||
362 | 2692 |
triplesDo:aBlock |
2693 |
"evaluate the argument, aBlock for every element in the collection, |
|
2694 |
which is supposed to consist of 3-element collections. |
|
2695 |
The block is called with 3 arguments for each collection in the receiver." |
|
2696 |
||
2697 |
self do:[:aTriple | |
|
2639 | 2698 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 2699 |
] |
2700 |
" |
|
6555 | 2701 |
#( |
2702 |
(1 one eins) |
|
2703 |
(2 two zwei) |
|
2704 |
(3 three drei) |
|
2705 |
(4 four vier) |
|
2706 |
(5 five #'fuenf') |
|
2707 |
(6 six sechs) |
|
2708 |
) |
|
2709 |
triplesDo:[:num :sym1 :sym2 | |
|
2710 |
Transcript show:num; space; show:sym1; space; showCR:sym2 |
|
2711 |
] |
|
362 | 2712 |
" |
2639 | 2713 |
|
2714 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 2715 |
! |
2716 |
||
602 | 2717 |
with:aCollection collect:aTwoArgBlock |
2718 |
"evaluate the argument, aBlock for successive elements from |
|
2719 |
each the receiver and the argument, aSequenceableCollection; |
|
2720 |
The second argument, aBlock must be a two-argument block, which is |
|
2721 |
evaluated for each element-pair. |
|
2722 |
Collect the results and return a collection containing them. |
|
2723 |
This method fails if neither the receiver nor aCollection is |
|
2724 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 2725 |
|
17747 | 2726 |
|newCollection| |
1 | 2727 |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2728 |
newCollection := self speciesForAdding new. |
17747 | 2729 |
self with:aCollection do:[:el1 :el2 | |
2730 |
newCollection add:(aTwoArgBlock value:el1 value:el2). |
|
602 | 2731 |
]. |
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2732 |
^ newCollection as:self species |
201 | 2733 |
|
2734 |
" |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2735 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
17747 | 2736 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
2737 |
" |
|
2738 |
! |
|
2739 |
||
17845 | 2740 |
with:aCollection contains:aTwoArgBlock |
2741 |
"evaluate the argument, aBlock for successive elements from |
|
2742 |
each the receiver and the argument, aCollection. |
|
2743 |
Return true, if the block returns true for any of these pairs. |
|
2744 |
This method fails if neither the receiver nor aCollection is |
|
2745 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2746 |
||
2747 |
self with:aCollection do:[:a :b | |
|
2748 |
(aTwoArgBlock value:a value:b) ifTrue:[^ true]. |
|
2749 |
]. |
|
2750 |
^ false. |
|
2751 |
||
2752 |
" |
|
2753 |
(1 to:3) with:#(1 2 3 4) contains:[:a :b | a ~= b] --- raises an error |
|
2754 |
(1 to:3) with:#(1 22 3) contains:[:a :b | a ~= b] |
|
2755 |
(1 to:3) with:#(1 2 3) contains:[:a :b | a ~= b] |
|
2756 |
" |
|
2757 |
||
2758 |
"Created: / 30-06-2011 / 12:37:41 / cg" |
|
2759 |
"Modified (Format): / 30-06-2011 / 12:40:38 / cg" |
|
2760 |
! |
|
2761 |
||
17747 | 2762 |
with:aCollection count:aTwoArgBlock |
2763 |
"evaluate the argument, aBlock for successive elements from |
|
2764 |
each the receiver and the argument, aSequenceableCollection. |
|
2765 |
Count, how often the second argument, aTwoArgBlock returns true. |
|
2766 |
This method fails if neither the receiver nor aCollection is |
|
2767 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2768 |
||
2769 |
|count "{ Class: SmallInteger }"| |
|
2770 |
||
2771 |
count := 0. |
|
2772 |
self with:aCollection do:[:el1 :el2 | |
|
2773 |
(aTwoArgBlock value:el1 value:el2) ifTrue:[ |
|
2774 |
count := count + 1 |
|
2775 |
] |
|
2776 |
]. |
|
2777 |
^ count |
|
2778 |
||
2779 |
" |
|
2780 |
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2] |
|
201 | 2781 |
" |
2 | 2782 |
! |
2783 |
||
602 | 2784 |
with:aCollection do:aTwoArgBlock |
2785 |
"evaluate the argument, aBlock for successive elements from |
|
2786 |
each the receiver and the argument, aSequenceableCollection. |
|
2787 |
The second argument, aBlock must be a two-argument block. |
|
2788 |
This method fails if neither the receiver nor aCollection is |
|
17845 | 2789 |
a sequenceable collection (i.e. implements numeric key access) |
2790 |
or (new!!) if the sizes are different." |
|
602 | 2791 |
|
2792 |
|index "{ Class: SmallInteger }"| |
|
2 | 2793 |
|
17845 | 2794 |
aCollection size == self size ifFalse:[ |
2795 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
|
2796 |
]. |
|
2797 |
||
602 | 2798 |
index := 1. |
2799 |
aCollection isSequenceable ifFalse:[ |
|
6135 | 2800 |
self isSequenceable ifFalse:[ |
2801 |
^ self error:'neither collection is sequenceable'. |
|
2802 |
]. |
|
2803 |
aCollection do:[:element | |
|
2804 |
aTwoArgBlock value:(self at:index) value:element. |
|
2805 |
index := index + 1 |
|
2806 |
] |
|
602 | 2807 |
] ifTrue:[ |
6135 | 2808 |
self do:[:element | |
2809 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
2810 |
index := index + 1 |
|
2811 |
] |
|
602 | 2812 |
] |
201 | 2813 |
|
2814 |
" |
|
602 | 2815 |
(1 to:3) with:#(one two three) do:[:num :sym | |
6135 | 2816 |
Transcript showCR:(num->sym) |
602 | 2817 |
] |
2818 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
6135 | 2819 |
Transcript showCR:(num->sym) |
602 | 2820 |
]. |
2821 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
6135 | 2822 |
Transcript showCR:(num->sym) |
602 | 2823 |
] |
201 | 2824 |
" |
17845 | 2825 |
|
2826 |
"Modified: / 30-06-2011 / 17:39:47 / cg" |
|
1 | 2827 |
! ! |
2828 |
||
5874 | 2829 |
!Collection methodsFor:'enumerating-tests'! |
2830 |
||
2831 |
allSatisfy:aBlock |
|
2832 |
"evaluate aBlock for each of the receiver's elements. |
|
2833 |
Return true, if aBlock returns true for all elements, false otherwise |
|
2834 |
(i.e. false if any element fails to satisfy the block-condition). |
|
2835 |
This is an ANSI renomer of #conform:" |
|
2836 |
||
2837 |
^ self conform:aBlock. |
|
2838 |
||
2839 |
" |
|
2840 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
|
2841 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
|
2842 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
|
2843 |
" |
|
2844 |
||
2845 |
! |
|
2846 |
||
2847 |
anySatisfy:aBlock |
|
2848 |
"evaluate aBlock for each of the receiver's elements. |
|
2849 |
Return true, if aBlock ever returns true, false otherwise |
|
2850 |
(i.e. if any element satisfies the block-condition). |
|
2851 |
This is an ANSI renomer of #contains:" |
|
2852 |
||
2853 |
^ self contains:aBlock. |
|
2854 |
||
2855 |
" |
|
2856 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
|
2857 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
|
2858 |
" |
|
2859 |
||
2860 |
! |
|
2861 |
||
9842 | 2862 |
conform:aOneArgBlock |
5874 | 2863 |
"return true, if every element conforms to some condition. |
2864 |
I.e. return false, if aBlock returns false for any element; |
|
2865 |
true otherwise." |
|
2866 |
||
9842 | 2867 |
self do:[:element | |
2868 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
2869 |
]. |
|
5874 | 2870 |
^ true |
9842 | 2871 |
|
2872 |
" |
|
2873 |
#(1 2 3 4 5) conform:[:el | el even] |
|
2874 |
#(2 4 6 8 10) conform:[:el | el even] |
|
2875 |
" |
|
2876 |
||
2877 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5874 | 2878 |
! |
2879 |
||
9842 | 2880 |
contains:aOneArgBlock |
2881 |
"evaluate aOneArgBlock for each of the receiver's elements. |
|
5874 | 2882 |
Return true, if aBlock ever returns true, false otherwise." |
2883 |
||
9842 | 2884 |
self do:[:element | |
2885 |
(aOneArgBlock value:element) ifTrue:[^ true] |
|
2886 |
]. |
|
2887 |
^ false |
|
5874 | 2888 |
|
2889 |
" |
|
2890 |
#(1 2 3 4 5) contains:[:el | el odd] |
|
2891 |
#(2 4 6 8 10) contains:[:el | el odd] |
|
2892 |
" |
|
2893 |
||
9842 | 2894 |
"Modified: / 13-09-2006 / 11:18:36 / cg" |
8577 | 2895 |
! |
2896 |
||
2897 |
noneSatisfy:aBlock |
|
2898 |
"evaluate aBlock for each of the receiver's elements. |
|
2899 |
Return true, if aBlock returns false for all elements, false otherwise |
|
2900 |
(i.e. false if any element satisfies the block-condition)." |
|
2901 |
||
9843 | 2902 |
^ (self contains:aBlock) not |
8577 | 2903 |
|
2904 |
" |
|
2905 |
#(1 2 3 4 5) noneSatisfy:[:el | el odd] |
|
2906 |
#(2 4 6 8 10) noneSatisfy:[:el | el odd] |
|
2907 |
#(2 4 6 8 10) noneSatisfy:[:el | el even] |
|
2908 |
" |
|
9843 | 2909 |
|
2910 |
"Modified: / 13-09-2006 / 11:19:57 / cg" |
|
5874 | 2911 |
! ! |
2912 |
||
602 | 2913 |
!Collection methodsFor:'error handling'! |
2914 |
||
2915 |
emptyCheck |
|
2916 |
"check if the receiver is empty; report an error if so" |
|
2917 |
||
11475
f7e1a355df17
changed #emptyCheck (is isEmpty instead of size == 0)
Claus Gittinger <cg@exept.de>
parents:
11470
diff
changeset
|
2918 |
self isEmpty ifTrue:[ |
6135 | 2919 |
^ self emptyCollectionError. |
602 | 2920 |
]. |
2921 |
! |
|
2922 |
||
2923 |
emptyCollectionError |
|
2924 |
"report an error that the operation is not allowed for |
|
2925 |
empty collections" |
|
2926 |
||
2927 |
^ EmptyCollectionSignal raise |
|
2928 |
! |
|
2929 |
||
2930 |
errorInvalidKey:aKey |
|
2931 |
"report an error that the given key was invalid" |
|
2932 |
||
2933 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
2934 |
! |
|
2935 |
||
2936 |
errorNotKeyed |
|
2937 |
"report an error that keyed access methods are not allowed" |
|
2938 |
||
2939 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
2940 |
! |
|
2941 |
||
2942 |
errorValueNotFound:anObject |
|
2943 |
"report an error that an object was not found in the collection" |
|
2944 |
||
6757 | 2945 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
3075 | 2946 |
|
2947 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 2948 |
! |
2949 |
||
2950 |
notEnoughElementsError |
|
2951 |
"report an error that the operation is not allowed, |
|
2952 |
since not enough elements are in the collection" |
|
2953 |
||
2954 |
^ NotEnoughElementsSignal raise |
|
2955 |
! ! |
|
2956 |
||
2957 |
!Collection methodsFor:'growing'! |
|
2958 |
||
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2959 |
changeCapacityTo:newSize |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2960 |
newSize > self capacity ifTrue:[ |
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2961 |
self grow:newSize |
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2962 |
]. |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2963 |
|
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2964 |
"Created: / 30.10.2001 / 17:56:50 / cg" |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2965 |
! |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2966 |
|
602 | 2967 |
grow |
2968 |
"make the receiver larger" |
|
2969 |
||
2970 |
self grow:(self size + self growSize) |
|
2971 |
! |
|
2972 |
||
2973 |
grow:howBig |
|
2974 |
"change the receivers size" |
|
2975 |
||
2976 |
^ self subclassResponsibility |
|
2977 |
! |
|
2978 |
||
2979 |
growSize |
|
2980 |
"return a suitable size increment for growing. |
|
2981 |
The default returned here may be (and is) redefined in subclasses." |
|
2982 |
||
2983 |
^ self size max:2 |
|
2984 |
! ! |
|
2985 |
||
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
2986 |
|
11412 | 2987 |
!Collection methodsFor:'operations'! |
2988 |
||
2989 |
decrementAt:aKey |
|
17834 | 2990 |
"decrement the value at aKey by one" |
2991 |
||
11412 | 2992 |
self at:aKey put:(self at:aKey) - 1. |
17834 | 2993 |
|
2994 |
"Modified: / 18-03-2011 / 10:32:04 / cg" |
|
11412 | 2995 |
! |
2996 |
||
2997 |
incrementAt:aKey |
|
17834 | 2998 |
"increment the value at aKey by one" |
2999 |
||
11412 | 3000 |
self at:aKey put:(self at:aKey) + 1. |
17834 | 3001 |
|
3002 |
"Modified: / 18-03-2011 / 10:31:57 / cg" |
|
11412 | 3003 |
! ! |
3004 |
||
1 | 3005 |
!Collection methodsFor:'printing & storing'! |
3006 |
||
17807 | 3007 |
displayOn:aGCOrStream |
3008 |
"print a representation of the receiver on aGCOrStream for display in inspectors etc." |
|
3009 |
||
3010 |
|noneYet total limit| |
|
3011 |
||
3012 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
3013 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
3014 |
(aGCOrStream isStream and:[aGCOrStream ~~ Transcript]) ifFalse:[ |
|
3015 |
^ super displayOn:aGCOrStream |
|
3016 |
]. |
|
3017 |
||
3018 |
aGCOrStream nextPutAll:self displayStringName; nextPut:$(. |
|
602 | 3019 |
|
3020 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3021 |
'Collection [error]: displayString of self referencing collection.' errorPrintCR. |
17807 | 3022 |
aGCOrStream nextPutAll:'"recursive")'. |
3023 |
^ self. |
|
602 | 3024 |
]. |
3025 |
||
3026 |
noneYet := true. |
|
3027 |
total := 0. |
|
3028 |
limit := self maxPrint. |
|
3029 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3030 |
self printElementsDo: [:element | |
1413 | 3031 |
noneYet ifTrue:[ |
3032 |
noneYet := false. |
|
3033 |
] ifFalse:[ |
|
17807 | 3034 |
aGCOrStream space |
1413 | 3035 |
]. |
17807 | 3036 |
element displayOn:aGCOrStream. |
1413 | 3037 |
total := total + 1. |
3038 |
(total > limit) ifTrue:[ |
|
17807 | 3039 |
aGCOrStream nextPutAll:'... )'. |
3040 |
^ self |
|
1413 | 3041 |
] |
602 | 3042 |
]. |
17807 | 3043 |
aGCOrStream nextPutAll:')'. |
1413 | 3044 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3045 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3046 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3047 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3048 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3049 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3050 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3051 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3052 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3053 |
"Modified: / 2.2.1999 / 22:39:44 / cg" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3054 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3055 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3056 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3057 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3058 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3059 |
^ (self class name) |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3060 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3061 |
"Created: / 2.2.1999 / 22:39:33 / cg" |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3062 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3063 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3064 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3065 |
"the print-limit; printOn: will try to not produce more output |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3066 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3067 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3068 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3069 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3070 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3071 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3072 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3073 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3074 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3075 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3076 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3077 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3078 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3079 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3080 |
|
11599 | 3081 |
printElementsOn:aStream |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3082 |
"append a user readable representation of the receiver to aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3083 |
The text appended is not meant to be read back for reconstruction of |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3084 |
the receiver. Also, this method limits the size of generated string. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3085 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3086 |
|
3402 | 3087 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3088 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3089 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3090 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3091 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3092 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3093 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3094 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3095 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3096 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3097 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3098 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3099 |
if aStream is not positionable, create an temporary positionable stream |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3100 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3101 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3102 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3103 |
s := aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3104 |
] ifFalse:[ |
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7629
diff
changeset
|
3105 |
s := WriteStream on:(String uninitializedNew:50). |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3106 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
3107 |
limit := s position1Based + self maxPrint. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3108 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3109 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3110 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3111 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3112 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3113 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3114 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
3115 |
(s position1Based >= limit) ifTrue:[ |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3116 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3117 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3118 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3119 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3120 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3121 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3122 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3123 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3124 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3125 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3126 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3127 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3128 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3129 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3130 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3131 |
#(1 2 3 'hello' $a) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3132 |
(Array new:100000) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3133 |
(Array new:100000) printOn:Stdout |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3134 |
(Array new:100000) printString size |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3135 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3136 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3137 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3138 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3139 |
|a| |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3140 |
a := Array new:3. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3141 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3142 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3143 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3144 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3145 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3146 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 3147 |
! |
3148 |
||
11599 | 3149 |
printOn:aStream |
3150 |
"append a user readable representation of the receiver to aStream. |
|
3151 |
The text appended is not meant to be read back for reconstruction of |
|
3152 |
the receiver. Also, this method limits the size of generated string. |
|
3153 |
" |
|
3154 |
||
3155 |
thisContext isRecursive ifTrue:[ |
|
3156 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
|
3157 |
aStream nextPutAll:'#("recursive")'. |
|
3158 |
^ self |
|
3159 |
]. |
|
3160 |
||
3161 |
aStream nextPutAll:self class name. |
|
3162 |
self printElementsOn:aStream. |
|
3163 |
||
3164 |
" |
|
3165 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
3166 |
(Array new:100000) printOn:Transcript |
|
3167 |
(Array new:100000) printOn:Stdout |
|
3168 |
(Array new:100000) printString size |
|
3169 |
(Dictionary new at:#hello put:'world'; |
|
3170 |
at:#foo put:'bar'; yourself) printOn:Transcript |
|
3171 |
" |
|
3172 |
" |
|
3173 |
|a| |
|
3174 |
a := Array new:3. |
|
3175 |
a at:2 put:a. |
|
3176 |
a printOn:Transcript |
|
3177 |
" |
|
3178 |
||
3179 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
|
3180 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
|
3181 |
! |
|
3182 |
||
1 | 3183 |
storeOn:aStream |
88 | 3184 |
"output a printed representation onto the argument, aStream. |
3185 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
3186 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
3187 |
(use storeBinaryOn: to handle those)." |
|
1 | 3188 |
|
5254 | 3189 |
|notEmpty| |
1 | 3190 |
|
44 | 3191 |
thisContext isRecursive ifTrue:[ |
5498 | 3192 |
Object recursiveStoreStringSignal raiseRequestWith:self. |
2289 | 3193 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 3194 |
aStream nextPutAll:'#recursive'. |
3195 |
^ self |
|
44 | 3196 |
]. |
3197 |
||
3198 |
aStream nextPutAll:'('. |
|
1 | 3199 |
aStream nextPutAll:(self class name). |
44 | 3200 |
aStream nextPutAll:' new'. |
5254 | 3201 |
notEmpty := false. |
1 | 3202 |
self do:[:element | |
1413 | 3203 |
aStream nextPutAll:' add:'. |
3204 |
element storeOn:aStream. |
|
3205 |
aStream nextPutAll:';'. |
|
5254 | 3206 |
notEmpty := true |
1 | 3207 |
]. |
5254 | 3208 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 3209 |
aStream nextPut:$) |
44 | 3210 |
|
88 | 3211 |
" |
3212 |
OrderedCollection new storeOn:Transcript |
|
3213 |
(1 to:10) storeOn:Transcript |
|
3214 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
3215 |
" |
|
293 | 3216 |
" |
3217 |
|s| |
|
3218 |
||
3219 |
s := Set new. |
|
3220 |
s add:1; add:'hello'; add:s. |
|
3221 |
s storeOn:Transcript |
|
3222 |
" |
|
1413 | 3223 |
|
5254 | 3224 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 3225 |
! ! |
602 | 3226 |
|
3227 |
!Collection methodsFor:'queries'! |
|
3228 |
||
17841 | 3229 |
arithmeticMean |
3230 |
"arithmetic mean value of all elements in the collection" |
|
3231 |
||
3232 |
^ self sum / self size |
|
3233 |
||
3234 |
" |
|
3235 |
TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5). |
|
3236 |
TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4). |
|
3237 |
TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431). |
|
3238 |
" |
|
3239 |
||
3240 |
"Created: / 13-04-2011 / 16:52:32 / cg" |
|
3241 |
! |
|
3242 |
||
17834 | 3243 |
average |
3244 |
"average value of all elements in the collection" |
|
3245 |
||
17841 | 3246 |
^ self arithmeticMean |
17834 | 3247 |
|
3248 |
" |
|
3249 |
TestCase assert:( { 1. 2. 3. 4 } average = 2.5). |
|
3250 |
" |
|
3251 |
||
3252 |
"Created: / 18-03-2011 / 10:31:04 / cg" |
|
17841 | 3253 |
"Modified: / 13-04-2011 / 17:09:21 / cg" |
17834 | 3254 |
! |
3255 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3256 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3257 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3258 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3259 |
|
17841 | 3260 |
geometricMean |
3261 |
"geometric mean value of all elements in the collection" |
|
3262 |
||
3263 |
^ self product raisedTo:(1/self size) |
|
3264 |
||
3265 |
" |
|
3266 |
TestCase assert:( { 1. 2. 3. 4. } geometricMean closeTo: 2.21336). |
|
3267 |
TestCase assert:( { 1. 2. 3. 4. 5 } geometricMean closeTo: 2.60517). |
|
3268 |
TestCase assert:( { 13. 23. 12. 44. 55 } geometricMean closeTo: 24.41932). |
|
3269 |
" |
|
3270 |
||
3271 |
"Created: / 13-04-2011 / 16:53:44 / cg" |
|
3272 |
! |
|
3273 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3274 |
largest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3275 |
"return the n largest elements" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3276 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3277 |
|mySize loopAction actionForFirstN actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3278 |
nLargest sz_nLargest minInLargest| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3279 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3280 |
mySize := self size. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3281 |
n > mySize ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3282 |
self notEnoughElementsError |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3283 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3284 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3285 |
(n < 50) ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3286 |
n == 1 ifTrue:[ ^ Array with:self max ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3287 |
n == 2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3288 |
|l1 l2| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3289 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3290 |
loopAction := [:el | l1 := el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3291 |
loopAction := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3292 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3293 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3294 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3295 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3296 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3297 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3298 |
loopAction := actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3299 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3300 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3301 |
actionForRest := [:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3302 |
el > l2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3303 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3304 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3305 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3306 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3307 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3308 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3309 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3310 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3311 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3312 |
^ Array with:l2 with:l1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3313 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3314 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3315 |
nLargest := SortedCollection new:n. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3316 |
sz_nLargest := 0. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3317 |
actionForFirstN := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3318 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3319 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3320 |
sz_nLargest := sz_nLargest + 1. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3321 |
sz_nLargest == n ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3322 |
loopAction := actionForRest. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3323 |
minInLargest := nLargest min. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3324 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3325 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3326 |
actionForRest := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3327 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3328 |
el > minInLargest ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3329 |
nLargest removeFirst. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3330 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3331 |
minInLargest := nLargest min |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3332 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3333 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3334 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3335 |
loopAction := actionForFirstN. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3336 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3337 |
^ nLargest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3338 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3339 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3340 |
^ self asSortedCollection copyTo:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3341 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3342 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3343 |
#(10 35 20 45 30 5) largest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3344 |
#(10 35 20 45 30 5) largest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3345 |
#(10 35 20 45 30 5) largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3346 |
#(10 35 20 45 30 5) largest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3347 |
#(10 35 20 45 30 5) largest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3348 |
#(10 35 20 45 30 5) largest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3349 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3350 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3351 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3352 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3353 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3354 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3355 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3356 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3357 |
data asSortedCollection at:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3358 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3359 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3360 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3361 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3362 |
data largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3363 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3364 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3365 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3366 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3367 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3368 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3369 |
|
602 | 3370 |
longestCommonPrefix |
1246 | 3371 |
"return the longest common prefix of my elements. |
3372 |
Typically used with string collections." |
|
3373 |
||
5532 | 3374 |
^ self longestCommonPrefixIgnoreCase:false |
3375 |
||
3376 |
" |
|
3377 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3378 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3379 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
3380 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
3381 |
" |
|
3382 |
||
3383 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
|
3384 |
! |
|
3385 |
||
3386 |
longestCommonPrefixIgnoreCase:ignoreCase |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3387 |
"return the longest common prefix of my elements (which must be sequenceableCollections). |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3388 |
Typically used with string collections, |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3389 |
especially with completion of selectors or filenames." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3390 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3391 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3392 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3393 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3394 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3395 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3396 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3397 |
longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase |
1246 | 3398 |
] |
602 | 3399 |
]. |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3400 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3401 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3402 |
"/ |longest try allMatching matchLen| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3403 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3404 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3405 |
"/ find the longest common prefix |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3406 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3407 |
"/ matchLen := 0. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3408 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3409 |
"/ aName size > matchLen ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3410 |
"/ matchLen := aName size. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3411 |
"/ try := aName |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3412 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3413 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3414 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3415 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3416 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3417 |
"/ [true] whileTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3418 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3419 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3420 |
"/ ((ignoreCase not and:[aName startsWith:try]) |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3421 |
"/ or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3422 |
"/ allMatching := false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3423 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3424 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3425 |
"/ allMatching ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3426 |
"/ ^ try |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3427 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3428 |
"/ matchLen := matchLen - 1. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3429 |
"/ matchLen == 0 ifTrue:[^ '']. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3430 |
"/ try := try copyTo:matchLen. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3431 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3432 |
"/ ^ try |
602 | 3433 |
|
3434 |
" |
|
5532 | 3435 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
3436 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
3437 |
#('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3438 |
#('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3439 |
#('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3440 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3441 |
! |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3442 |
|
6506 | 3443 |
longestCommonSuffix |
3444 |
"return the longest common suffix (tail) of my elements. |
|
3445 |
Typically used with string collections." |
|
3446 |
||
3447 |
^ self longestCommonSuffixIgnoreCase:false |
|
17847 | 3448 |
|
3449 |
" |
|
3450 |
#('abcdefg' '1234cdefg' 'aaaaaadefg') longestCommonSuffix |
|
3451 |
" |
|
3452 |
||
3453 |
"Modified (comment): / 24-07-2011 / 10:32:15 / cg" |
|
6506 | 3454 |
! |
3455 |
||
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3456 |
longestCommonSuffixIgnoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3457 |
"return the longest common suffix (tail) of my elements |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3458 |
(which must be sequenceableCollections)." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3459 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3460 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3461 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3462 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3463 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3464 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3465 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3466 |
longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3467 |
] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3468 |
]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3469 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3470 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3471 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3472 |
#('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3473 |
#('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3474 |
#('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false |
602 | 3475 |
" |
5557 | 3476 |
! |
3477 |
||
7821 | 3478 |
max |
3479 |
"return the maximum value in the receiver collection, |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3480 |
using #< to compare elements. |
7821 | 3481 |
Raises an error, if the receiver is empty." |
3482 |
||
3483 |
self emptyCheck. |
|
3484 |
||
17780 | 3485 |
^ self |
3486 |
inject:nil |
|
3487 |
into:[:maxSoFar :this | |
|
3488 |
(maxSoFar isNil or:[maxSoFar < this]) |
|
3489 |
ifTrue:[this] |
|
3490 |
ifFalse:[maxSoFar] |
|
3491 |
] |
|
7821 | 3492 |
|
3493 |
" |
|
3494 |
#(15 1 -9 10 5) max |
|
3495 |
(1 to:15) max |
|
3496 |
(-1 to:-15 by:-1) max |
|
3497 |
(-1 to:-15 by:-4) max |
|
3498 |
(0 to:15 by:4) max |
|
3499 |
" |
|
3500 |
||
17780 | 3501 |
"Modified: / 11-07-2010 / 17:05:25 / cg" |
7821 | 3502 |
! |
3503 |
||
17797 | 3504 |
maxApplying:aBlock |
3505 |
"return the maximum value from applying aBlock to each element in the receiver collection, |
|
17834 | 3506 |
using aBlock to compare elements. |
17797 | 3507 |
Raises an error, if the receiver is empty." |
3508 |
||
3509 |
self emptyCheck. |
|
3510 |
||
3511 |
^ self |
|
3512 |
inject:nil |
|
3513 |
into:[:maxSoFar :this | |
|
3514 |
|v| |
|
3515 |
||
3516 |
v := aBlock value:this. |
|
3517 |
(maxSoFar isNil or:[maxSoFar < v]) |
|
3518 |
ifTrue:[v] |
|
3519 |
ifFalse:[maxSoFar] |
|
3520 |
] |
|
3521 |
||
3522 |
" |
|
3523 |
#(15 1 -9 -20 10 5) max -> 15 |
|
3524 |
#(15 1 -9 -20 10 5) maxApplying:[:el | el abs] -> 20 |
|
3525 |
" |
|
3526 |
||
3527 |
"Created: / 23-08-2010 / 11:02:50 / cg" |
|
17834 | 3528 |
"Modified: / 18-03-2011 / 10:32:29 / cg" |
17797 | 3529 |
! |
3530 |
||
7821 | 3531 |
min |
3532 |
"return the minimum value in the receiver collection, |
|
3533 |
using < to compare elements. |
|
3534 |
Raises an error, if the receiver is empty." |
|
3535 |
||
3536 |
self emptyCheck. |
|
3537 |
||
17780 | 3538 |
^ self |
3539 |
inject:nil |
|
3540 |
into:[:minSoFar :this | |
|
3541 |
(minSoFar isNil or:[this < minSoFar]) |
|
3542 |
ifTrue:[this] |
|
3543 |
ifFalse:[minSoFar] |
|
3544 |
] |
|
7821 | 3545 |
|
3546 |
" |
|
3547 |
#(15 1 -9 10 5) min |
|
3548 |
(1 to:15) min |
|
3549 |
(-1 to:-15 by:-1) min |
|
3550 |
(-1 to:-15 by:-4) min |
|
3551 |
" |
|
3552 |
||
17780 | 3553 |
"Modified: / 11-07-2010 / 17:06:38 / cg" |
7821 | 3554 |
! |
3555 |
||
17797 | 3556 |
minApplying:aBlock |
3557 |
"return the minimum value from applying aBlock to each element in the receiver collection, |
|
17834 | 3558 |
using aBlock to compare elements. |
17797 | 3559 |
Raises an error, if the receiver is empty." |
3560 |
||
3561 |
self emptyCheck. |
|
3562 |
||
3563 |
^ self |
|
3564 |
inject:nil |
|
3565 |
into:[:minSoFar :this | |
|
3566 |
|v| |
|
3567 |
||
3568 |
v := aBlock value:this. |
|
3569 |
(minSoFar isNil or:[v < minSoFar]) |
|
3570 |
ifTrue:[v] |
|
3571 |
ifFalse:[minSoFar] |
|
3572 |
] |
|
3573 |
||
3574 |
" |
|
3575 |
#(15 -1 -9 10 5) min -> -9 |
|
3576 |
#(15 -1 -9 10 5) minApplying:[:el | el abs] -> 1 |
|
3577 |
" |
|
3578 |
||
3579 |
"Created: / 23-08-2010 / 11:01:42 / cg" |
|
17834 | 3580 |
"Modified: / 18-03-2011 / 10:32:33 / cg" |
17797 | 3581 |
! |
3582 |
||
7821 | 3583 |
minMax |
3584 |
"return the minimum and maximum values in the receiver collection |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3585 |
as a two element array, using #< to compare elements. |
7821 | 3586 |
Raises an error, if the receiver is empty." |
3587 |
||
3588 |
|min max| |
|
3589 |
||
3590 |
self emptyCheck. |
|
3591 |
||
3592 |
min := max := nil. |
|
3593 |
self do:[:each | |
|
3594 |
min isNil ifTrue:[ |
|
3595 |
min := max := each |
|
3596 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3597 |
max < each ifTrue:[ |
7821 | 3598 |
max := each |
3599 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3600 |
each < min ifTrue:[ |
7821 | 3601 |
min := each |
3602 |
] |
|
3603 |
] |
|
3604 |
] |
|
3605 |
]. |
|
3606 |
^ Array with:min with:max |
|
3607 |
||
3608 |
" |
|
3609 |
#(15 1 -9 10 5) minMax |
|
3610 |
(1 to:15) minMax |
|
3611 |
(-1 to:-15 by:-1) minMax |
|
3612 |
(-1 to:-15 by:-4) minMax |
|
3613 |
(0 to:15 by:4) minMax |
|
3614 |
" |
|
3615 |
! |
|
3616 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3617 |
nthLargest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3618 |
"return the n-largest element" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3619 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3620 |
^ (self largest:n) first |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3621 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3622 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3623 |
#(10 35 20 45 30 5) nthLargest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3624 |
#(10 35 20 45 30 5) nthLargest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3625 |
#(10 35 20 45 30 5) nthLargest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3626 |
#(10 35 20 45 30 5) nthLargest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3627 |
#(10 35 20 45 30 5) nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3628 |
#(10 35 20 45 30 5) nthLargest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3629 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3630 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3631 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3632 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3633 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3634 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3635 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3636 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3637 |
data asSortedCollection at:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3638 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3639 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3640 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3641 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3642 |
data nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3643 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3644 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3645 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3646 |
Transcript show:'nthMost -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3647 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3648 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3649 |
|
5557 | 3650 |
size |
3651 |
"return the number of elements in the receiver. |
|
3652 |
This is usually redefined in subclasses for more performance." |
|
3653 |
||
3654 |
|count "{ Class: SmallInteger }" | |
|
3655 |
||
3656 |
count := 0. |
|
3657 |
self do:[:element | |
|
6135 | 3658 |
count := count + 1 |
5557 | 3659 |
]. |
3660 |
^ count |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3661 |
! |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3662 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3663 |
speciesForAdding |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3664 |
"like species, but redefined for collections which cannot grow easily. |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3665 |
Used by functions which create a growing collection (see collect:with:, for example)" |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3666 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3667 |
^ self species |
17841 | 3668 |
! |
3669 |
||
3670 |
standardDeviation |
|
3671 |
"standard deviation value of all elements in the collection" |
|
3672 |
||
3673 |
|m sumDeltaSquares| |
|
3674 |
||
3675 |
m := self arithmeticMean. |
|
3676 |
sumDeltaSquares := self inject:0 into:[:sum :this | sum + ((this - m) squared)]. |
|
3677 |
^ (sumDeltaSquares / (self size - 1)) sqrt |
|
3678 |
||
3679 |
" |
|
3680 |
TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5). |
|
3681 |
TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4). |
|
3682 |
TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431). |
|
3683 |
" |
|
3684 |
||
3685 |
"Created: / 13-04-2011 / 17:58:47 / cg" |
|
602 | 3686 |
! ! |
3687 |
||
11706 | 3688 |
!Collection methodsFor:'searching'! |
3689 |
||
3690 |
findFirst:aBlock |
|
3691 |
"find the index of the first element, for which evaluation of the argument, aBlock |
|
3692 |
returns true; return its index or 0 if none detected. |
|
3693 |
This is much like #detect, however, here an INDEX is returned, |
|
3694 |
while #detect returns the element." |
|
3695 |
||
3696 |
^ self findFirst:aBlock ifNone:0 |
|
3697 |
||
3698 |
" |
|
3699 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
|
3700 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
3701 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
3702 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
3703 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
|
3704 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
3705 |
" |
|
3706 |
||
3707 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
3708 |
! ! |
|
3709 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3710 |
!Collection methodsFor:'set operations'! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3711 |
|
7169 | 3712 |
\ aCollection |
8606 | 3713 |
"return a new set containing all elements of the receiver, |
3714 |
which are NOT also contained in the aCollection |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3715 |
For large collections you better use a Set for aCollection" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3716 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3717 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3718 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3719 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3720 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3721 |
self do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3722 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3723 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3724 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3725 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3726 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3727 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3728 |
" |
7169 | 3729 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet |
3730 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) |
|
3731 |
('hello' \ 'l') asString |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3732 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3733 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3734 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3735 |
intersect:aCollection |
8606 | 3736 |
"return a new set containing all elements of the receiver, |
3737 |
which are also contained in the argument collection. |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3738 |
For large collections you better use a Set for self" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3739 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3740 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3741 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3742 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3743 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3744 |
(self includes:element) ifTrue:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3745 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3746 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3747 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3748 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3749 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3750 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3751 |
#(0 1 2 3 4 5 6 7 8 9) asSet intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3752 |
#(0 1 2 3 4 5 6 7 8 9) intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3753 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3754 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3755 |
|
7169 | 3756 |
union:aCollection |
3757 |
"return a new set containing all elements of the receiver |
|
3758 |
plus those of the aCollection" |
|
3759 |
||
3760 |
|newCollection| |
|
3761 |
||
3762 |
newCollection := self speciesForAdding new. |
|
3763 |
newCollection addAll:self. |
|
3764 |
newCollection addAll:aCollection. |
|
3765 |
^ newCollection |
|
3766 |
||
3767 |
" |
|
3768 |
#(0 2 4 6 8) union:#(1 3 5 7) |
|
11384 | 3769 |
#(0 2 4 6 8) union:#(0 1 3 5 7) |
7169 | 3770 |
" |
3771 |
! |
|
3772 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3773 |
xor:aCollection |
8606 | 3774 |
"return a new set containing all elements, |
3775 |
which are contained in either the receiver or aCollection, but not in both. |
|
3776 |
||
3777 |
For large collections you better use Sets for both self and aCollection" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3778 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3779 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3780 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3781 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3782 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3783 |
(self includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3784 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3785 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3786 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3787 |
self do:[:element| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3788 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3789 |
newCollection add:element. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3790 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3791 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3792 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3793 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3794 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3795 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3796 |
#(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3797 |
" |
17735 | 3798 |
|
3799 |
" |
|
3800 |
|c1 c2| |
|
3801 |
||
3802 |
c1 := #( foo bar baz baloo ). |
|
3803 |
c2 := #( foe bar banana baloo ). |
|
3804 |
c1 symmetricDifference:c2. |
|
3805 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
3806 |
" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3807 |
! ! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3808 |
|
10811 | 3809 |
!Collection methodsFor:'sorting & reordering'! |
3810 |
||
3811 |
topologicalSort |
|
3812 |
"Sort a partial ordered collection. |
|
3813 |
The receiver consists of tupels defining a partial order. |
|
3814 |
Use the algorithm by R. E. Tarjan from 1972. |
|
3815 |
Answer an OrderedCollection containing the sorted items" |
|
3816 |
||
3817 |
|graph roots sorted count| |
|
3818 |
||
3819 |
"create a graph. |
|
3820 |
For each node there is an entry containing the number of references |
|
3821 |
and a list of arcs to other nodes" |
|
3822 |
||
3823 |
graph := Dictionary new. |
|
3824 |
||
3825 |
self do:[:eachTuple| |
|
3826 |
|node1| |
|
3827 |
node1 := eachTuple first. |
|
3828 |
graph at:node1 ifAbsentPut:[OrderedCollection with:0]. |
|
3829 |
1 to:(eachTuple size-1) do:[:i| |
|
3830 |
|node2 n2| |
|
3831 |
||
3832 |
node1 := eachTuple at:i. |
|
3833 |
node2 := eachTuple at:i+1. |
|
3834 |
"add the node" |
|
3835 |
graph at:node2 ifAbsentPut:[OrderedCollection with:0]. |
|
3836 |
"add an arc from node1 to node2" |
|
3837 |
(graph at:node1) add:node2. |
|
3838 |
"increment count on incoming arcs of node2" |
|
3839 |
n2 := graph at:node2. |
|
3840 |
n2 at:1 put:(n2 first + 1). |
|
3841 |
]. |
|
3842 |
]. |
|
3843 |
||
3844 |
"now find the root nodes (having zero incoming arcs)" |
|
3845 |
roots := OrderedCollection new. |
|
3846 |
graph keysAndValuesDo:[:eachKey :eachValue| |
|
3847 |
eachValue first = 0 ifTrue:[ |
|
3848 |
roots add:eachKey |
|
3849 |
]. |
|
3850 |
]. |
|
3851 |
||
3852 |
"for each root, out it to the sorted list and remove it from the graph. |
|
3853 |
This may cause referenced nodes to be moved to the root" |
|
3854 |
||
3855 |
sorted := OrderedCollection new:graph size. |
|
3856 |
[roots notEmpty] whileTrue:[ |
|
3857 |
|root eachEntry| |
|
3858 |
||
3859 |
root := roots removeFirst. |
|
3860 |
sorted add:root. |
|
3861 |
eachEntry := graph removeKey:root. |
|
3862 |
2 to:eachEntry size do:[:i| |
|
3863 |
|eachChild eachChildGraph| |
|
3864 |
eachChild := eachEntry at:i. |
|
3865 |
eachChildGraph := graph at:eachChild. |
|
3866 |
count := eachChildGraph at:1. |
|
3867 |
eachChildGraph at:1 put:(count - 1). |
|
3868 |
count = 1 ifTrue:[ |
|
3869 |
roots add:eachChild. |
|
3870 |
]. |
|
3871 |
]. |
|
3872 |
]. |
|
3873 |
||
3874 |
graph notEmpty ifTrue:[ |
|
3875 |
self error:'cycle in ordering' mayProceed:true. |
|
3876 |
]. |
|
3877 |
||
3878 |
^ sorted |
|
3879 |
||
3880 |
" |
|
3881 |
#((1 2) (1 3) (3 2)) topologicalSort |
|
11384 | 3882 |
#((1 2) (1 3) (3 2) (4)) topologicalSort |
3883 |
||
3884 |
with cycles: |
|
10811 | 3885 |
#((1 2) (1 3) (3 1)) topologicalSort |
11384 | 3886 |
#((1 2) (2 3) (3 1)) topologicalSort |
3887 |
#((1 2) (2 3) (3 4) (4 1)) topologicalSort |
|
11385
c3d908e81ec7
changed #topologicalSort - comment
Stefan Vogel <sv@exept.de>
parents:
11384
diff
changeset
|
3888 |
#((1 1) (1 2) (1 3) (3 2) (4)) topologicalSort |
10811 | 3889 |
|
3890 |
(Smalltalk allClasses collect:[:eachClass| |
|
3891 |
Array with:eachClass superclass with:eachClass |
|
3892 |
]) topologicalSort |
|
3893 |
" |
|
3894 |
! ! |
|
3895 |
||
602 | 3896 |
!Collection methodsFor:'testing'! |
3897 |
||
17907 | 3898 |
allElementsHaveTheSameValue |
3899 |
"true if all elements of the receiver have the same value" |
|
3900 |
||
3901 |
^ self sameValuesComputedBy:[:el | el] |
|
3902 |
||
3903 |
" |
|
3904 |
#(1 2 3 5 6 7 8 9) allElementsHaveTheSameValue |
|
3905 |
#(1 1 1 1 1 1) allElementsHaveTheSameValue |
|
3906 |
#(1 1 1.0 1.0 1) allElementsHaveTheSameValue |
|
3907 |
" |
|
3908 |
||
3909 |
"Created: / 21-12-2011 / 15:54:08 / cg" |
|
3910 |
! |
|
3911 |
||
602 | 3912 |
capacity |
3913 |
"return the number of elements, that the receiver is |
|
3914 |
prepared to take. For most collections, this is the actual |
|
3915 |
size. However, some have more space preallocated to allow |
|
3916 |
for faster adding of elements. |
|
3917 |
Not used by the system; added for ST-80 compatibility." |
|
3918 |
||
3919 |
^ self size |
|
3920 |
! |
|
3921 |
||
3922 |
includes:anElement |
|
3923 |
"return true, if an object equal to the argument, anObject is in the list. |
|
3924 |
This compares using #= (i.e. it does not look for the object itself, |
|
3925 |
instead, one that compares equal). |
|
7807 | 3926 |
See #includesIdentical: when identity is asked for. |
3927 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 3928 |
|
3929 |
self do:[:element | |
|
6135 | 3930 |
(anElement = element) ifTrue:[^ true]. |
602 | 3931 |
]. |
3932 |
^ false |
|
3933 |
! |
|
3934 |
||
3935 |
includesAll:aCollection |
|
3936 |
"return true, if the the receiver includes all elements of |
|
3937 |
the argument, aCollection; false if any is missing. |
|
3938 |
Notice: this method has O-square runtime behavior and may be |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3939 |
slow for big receivers/args. |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3940 |
Think about using a Set, or Dictionary." |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3941 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3942 |
^ aCollection conform:[:element | (self includes:element)] |
602 | 3943 |
|
3944 |
" |
|
3945 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
3946 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
3947 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
3948 |
" |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3949 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3950 |
"Modified: / 13-10-2006 / 12:54:50 / cg" |
602 | 3951 |
! |
3952 |
||
3953 |
includesAny:aCollection |
|
3954 |
"return true, if the the receiver includes any elements of |
|
3955 |
the argument, aCollection; false if it includes none. |
|
7807 | 3956 |
Notice: this method has O^2(N) runtime behavior and may be |
3957 |
slow for big receivers/args. |
|
3958 |
Think about using a Set or Dictionary. |
|
3959 |
Some speedup is also possible, by arranging highly |
|
6135 | 3960 |
probable elements towards the beginning of aCollection, |
3961 |
to avoid useless searches." |
|
602 | 3962 |
|
3963 |
aCollection do:[:element | |
|
6135 | 3964 |
(self includes:element) ifTrue:[^ true]. |
602 | 3965 |
]. |
3966 |
^ false |
|
3967 |
||
3968 |
" |
|
3969 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
3970 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
3971 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
3972 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
3973 |
" |
|
3974 |
! |
|
3975 |
||
10504 | 3976 |
includesAnyIdentical:aCollection |
3977 |
"return true, if the the receiver includes any elements of |
|
3978 |
the argument, aCollection; false if it includes none. |
|
3979 |
Use identity compare for comparing. |
|
3980 |
Notice: this method has O^2(N) runtime behavior and may be |
|
3981 |
slow for big receivers/args. |
|
3982 |
Think about using a Set or Dictionary. |
|
3983 |
Some speedup is also possible, by arranging highly |
|
3984 |
probable elements towards the beginning of aCollection, |
|
3985 |
to avoid useless searches." |
|
3986 |
||
3987 |
aCollection do:[:element | |
|
3988 |
(self includesIdentical:element) ifTrue:[^ true]. |
|
3989 |
]. |
|
3990 |
^ false |
|
3991 |
||
3992 |
" |
|
3993 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(1 2 3) |
|
3994 |
#('hello' 'there' 'world') includesAnyIdentical:#('hello' 'world') |
|
3995 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(7 8 9) |
|
3996 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(8 9 10) |
|
3997 |
" |
|
3998 |
! |
|
3999 |
||
602 | 4000 |
includesIdentical:anElement |
7807 | 4001 |
"return true, if the argument, anObject is in the collection. |
602 | 4002 |
This compares using #== (i.e. object identity). |
7807 | 4003 |
See #includes: when equality is asked for. |
4004 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 4005 |
|
4006 |
self do:[:element | |
|
6135 | 4007 |
(anElement == element) ifTrue:[^ true]. |
602 | 4008 |
]. |
4009 |
^ false |
|
4010 |
! |
|
4011 |
||
9005 | 4012 |
isCollection |
4013 |
"return true, if the receiver is some kind of collection; |
|
4014 |
true is returned here - the method is redefined from Object." |
|
4015 |
||
4016 |
^ true |
|
4017 |
! |
|
4018 |
||
602 | 4019 |
isEmpty |
4020 |
"return true, if the receiver is empty" |
|
4021 |
||
4022 |
^ self size == 0 |
|
4023 |
! |
|
4024 |
||
6181 | 4025 |
isEmptyOrNil |
8833 | 4026 |
"return true if I am nil or an empty collection - true here, if the receivers size is 0, |
4027 |
(from Sqeak)" |
|
6181 | 4028 |
|
4029 |
^ self isEmpty |
|
4030 |
||
4031 |
"Created: / 13.11.2001 / 13:17:12 / cg" |
|
6182 | 4032 |
"Modified: / 13.11.2001 / 13:28:35 / cg" |
6181 | 4033 |
! |
4034 |
||
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4035 |
isNilOrEmptyCollection |
8833 | 4036 |
"return true if I am nil or an empty collection - false here. |
4037 |
Obsolete, use isEmptyOrNil." |
|
4038 |
||
4039 |
<resource:#obsolete> |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4040 |
|
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4041 |
^ self isEmpty |
6182 | 4042 |
|
4043 |
"Modified: / 13.11.2001 / 13:27:55 / cg" |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4044 |
! |
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4045 |
|
9005 | 4046 |
isNonByteCollection |
4047 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
4048 |
true is returned here - the method is redefined from Object." |
|
4049 |
||
4050 |
^ true |
|
4051 |
! |
|
4052 |
||
4053 |
isSorted |
|
4054 |
"return true, if the receiver is sorted. |
|
4055 |
Collections which hold their elements in sorted order |
|
4056 |
should return true. Some algorithms (quicksort) degenerate when |
|
4057 |
operating on sorted collections and can be avoided if this information |
|
4058 |
is given. The default returned here (false) should not hurt. |
|
4059 |
I.e. you should NEVER depend on that in your application." |
|
4060 |
||
4061 |
^ false |
|
4062 |
||
4063 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
4064 |
! |
|
4065 |
||
4066 |
isSortedBy:aBlock |
|
4067 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
|
4068 |
Collections which hold their elements in sorted order |
|
4069 |
should return true. Some algorithms (quicksort) degenerate when |
|
4070 |
operating on sorted collections and can be avoided if this information |
|
4071 |
is given. The default returned here (false) should not hurt. |
|
4072 |
I.e. you should NEVER depend on that in your application." |
|
4073 |
||
4074 |
^ false |
|
4075 |
||
4076 |
! |
|
4077 |
||
4078 |
isSortedCollection |
|
4079 |
"return true, if the receiver is a sortedCollection." |
|
4080 |
||
4081 |
^ false |
|
4082 |
||
4083 |
! |
|
4084 |
||
9247 | 4085 |
isWeakCollection |
4086 |
"return true, if the receiver has weak references to its elements." |
|
4087 |
||
4088 |
^ false |
|
4089 |
! |
|
4090 |
||
602 | 4091 |
notEmpty |
4092 |
"return true, if the receiver is not empty" |
|
4093 |
||
4094 |
^ self isEmpty not |
|
4095 |
! |
|
4096 |
||
6932 | 4097 |
notEmptyOrNil |
4098 |
"Squeak compatibility: |
|
4099 |
return true if I am neither nil nor an empty collection." |
|
4100 |
||
4101 |
^ self notEmpty |
|
4102 |
! |
|
4103 |
||
602 | 4104 |
occurrencesOf:anElement |
4105 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 4106 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 4107 |
|
4108 |
|count "{ Class: SmallInteger }" | |
|
4109 |
||
4110 |
count := 0. |
|
4111 |
self do:[:element | |
|
5048 | 4112 |
(anElement = element) ifTrue:[ |
4113 |
count := count + 1 |
|
4114 |
]. |
|
602 | 4115 |
]. |
4116 |
^ count |
|
8274 | 4117 |
! |
4118 |
||
4119 |
occurrencesOfAny:aCollectionOfElements |
|
4120 |
"return the number of occurrences of any in aCollectionOfElements in the receiver. |
|
4121 |
Uses #= (i.e. equality) compare. |
|
4122 |
Should be redefined in subclass(es) if ever used heavily." |
|
4123 |
||
4124 |
|count "{ Class: SmallInteger }" | |
|
4125 |
||
4126 |
count := 0. |
|
8275 | 4127 |
aCollectionOfElements do:[:element | |
4128 |
count := count + (self occurrencesOf:element) |
|
8274 | 4129 |
]. |
4130 |
^ count |
|
4131 |
||
4132 |
" |
|
4133 |
#(1 4 6 8 4 1) occurrencesOfAny:#(1 4) |
|
4134 |
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5) |
|
4135 |
'hello world' occurrencesOfAny:'hel' |
|
4136 |
" |
|
17907 | 4137 |
! |
4138 |
||
4139 |
sameValuesComputedBy:aBlock |
|
4140 |
"true if aBlock answers the same value for all elements of the receiver" |
|
4141 |
||
4142 |
|first valueForFirstElement| |
|
4143 |
||
4144 |
first := true. |
|
4145 |
self do:[:each | |
|
4146 |
first ifTrue:[ |
|
4147 |
first := false. |
|
4148 |
valueForFirstElement := aBlock value:each. |
|
4149 |
] ifFalse:[ |
|
4150 |
valueForFirstElement = (aBlock value:each) ifFalse:[ |
|
4151 |
^ false. |
|
4152 |
]. |
|
4153 |
]. |
|
4154 |
]. |
|
4155 |
^ true |
|
4156 |
||
4157 |
" |
|
4158 |
#(1 2 3 5 6 7 8 9) sameValuesComputedBy:[:el | el even] |
|
4159 |
#(1 1 1 1 1 1) sameValuesComputedBy:[:el | el even] |
|
4160 |
#(1 1 1.0 1.0 1) sameValuesComputedBy:[:el | el even] |
|
4161 |
#(1 3 3 15 1) sameValuesComputedBy:[:el | el even] |
|
4162 |
" |
|
4163 |
||
4164 |
"Created: / 21-12-2011 / 15:59:19 / cg" |
|
602 | 4165 |
! ! |
4166 |
||
4680 | 4167 |
!Collection methodsFor:'tracing'! |
4168 |
||
4169 |
traceInto:aRequestor level:level from:referrer |
|
4170 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
4171 |
||
4172 |
^ aRequestor traceCollection:self level:level from:referrer |
|
4173 |
||
4174 |
||
4175 |
! ! |
|
4176 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4177 |
!Collection methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4178 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4179 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4180 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4181 |
^ aVisitor visitCollection:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4182 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4183 |
|
2097 | 4184 |
!Collection class methodsFor:'documentation'! |
628 | 4185 |
|
17862 | 4186 |
version |
17907 | 4187 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.270 2011/12/21 14:59:45 cg Exp $' |
17862 | 4188 |
! |
4189 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
4190 |
version_CVS |
17907 | 4191 |
^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.270 2011/12/21 14:59:45 cg Exp ' |
17865 | 4192 |
! |
4193 |
||
4194 |
version_SVN |
|
17911 | 4195 |
^ '$Id: Collection.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
628 | 4196 |
! ! |
6867 | 4197 |
|
17846 | 4198 |
Collection initialize! |
17892 | 4199 |
|
17907 | 4200 |
|
17910 | 4201 |
|
17911 | 4202 |