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