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