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