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