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