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