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