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