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