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