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