author | Claus Gittinger <cg@exept.de> |
Thu, 21 Mar 1996 16:33:40 +0100 | |
changeset 1113 | 840b03d131d7 |
parent 1112 | 0d8dd47d386a |
child 1163 | ad6f6532c54b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Object subclass:#Collection |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
14 |
instanceVariableNames:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
15 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
16 |
NotEnoughElementsSignal' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
17 |
poolDictionaries:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
18 |
category:'Collections-Abstract' |
1 | 19 |
! |
20 |
||
88 | 21 |
!Collection class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
Abstract superclass for all collections. |
|
40 |
This abstract class provides functionality common to all collections, |
|
41 |
without knowing how the concrete class implements things. Thus, all |
|
42 |
methods found here depend on some basic mechanisms to be defined in the |
|
43 |
concrete class. |
|
44 |
These basic methods are usually defined as #subclassResponsibility here. |
|
45 |
Some methods are also redefined for better performance. |
|
46 |
" |
|
47 |
! ! |
|
1 | 48 |
|
10 | 49 |
!Collection class methodsFor:'initialization'! |
50 |
||
51 |
initialize |
|
52 |
"setup the signal" |
|
53 |
||
54 | 54 |
InvalidKeySignal isNil ifTrue:[ |
302 | 55 |
InvalidKeySignal := ErrorSignal newSignalMayProceed:true. |
155 | 56 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
345 | 57 |
InvalidKeySignal notifierString:'invalid key:'. |
58 |
||
59 |
ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true. |
|
60 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
|
61 |
ValueNotFoundSignal notifierString:'value not found:'. |
|
155 | 62 |
|
348 | 63 |
NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true. |
64 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
|
65 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
|
66 |
||
67 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
|
155 | 68 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
69 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
|
54 | 70 |
] |
10 | 71 |
! ! |
72 |
||
602 | 73 |
!Collection class methodsFor:'instance creation'! |
155 | 74 |
|
602 | 75 |
new:size withAll:element |
76 |
"return a new COllection of size, where all elements are |
|
77 |
initialized to element" |
|
348 | 78 |
|
602 | 79 |
|newCollection| |
92 | 80 |
|
602 | 81 |
newCollection := self new:size. |
82 |
size timesRepeat:[newCollection add:element] |
|
83 |
! |
|
1 | 84 |
|
85 |
with:anObject |
|
86 |
"return a new Collection with one element:anObject" |
|
87 |
||
88 |
|newCollection| |
|
89 |
||
90 |
newCollection := self new. |
|
91 |
newCollection add:anObject. |
|
92 |
^ newCollection |
|
93 |
! |
|
94 |
||
95 |
with:firstObject with:secondObject |
|
96 |
"return a new Collection with two elements:firstObject and secondObject" |
|
97 |
||
98 |
|newCollection| |
|
99 |
||
100 |
newCollection := self new. |
|
1112 | 101 |
newCollection add:firstObject; add:secondObject. |
1 | 102 |
^ newCollection |
103 |
! |
|
104 |
||
105 |
with:firstObject with:secondObject with:thirdObject |
|
106 |
"return a new Collection with three elements" |
|
107 |
||
108 |
|newCollection| |
|
109 |
||
110 |
newCollection := self new. |
|
1112 | 111 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 112 |
^ newCollection |
113 |
! |
|
114 |
||
115 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
116 |
"return a new Collection with four elements" |
|
117 |
||
118 |
|newCollection| |
|
119 |
||
120 |
newCollection := self new. |
|
1112 | 121 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 122 |
^ newCollection |
123 |
! |
|
124 |
||
345 | 125 |
with:one with:two with:three with:four with:five |
126 |
"return a new Collection with five elements" |
|
127 |
||
128 |
|newCollection| |
|
129 |
||
130 |
newCollection := self new. |
|
1112 | 131 |
newCollection add:one; add:two; add:three; add:four; add:five. |
345 | 132 |
^ newCollection |
133 |
! |
|
134 |
||
1 | 135 |
withAll:aCollection |
136 |
"return a new Collection with all elements taken from the argument, |
|
137 |
aCollection" |
|
138 |
||
139 |
|newCollection| |
|
140 |
||
141 |
newCollection := self new. |
|
142 |
newCollection addAll:aCollection. |
|
143 |
^newCollection |
|
144 |
! ! |
|
145 |
||
602 | 146 |
!Collection class methodsFor:'Signal constants'! |
10 | 147 |
|
602 | 148 |
emptyCollectionSignal |
149 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 150 |
|
602 | 151 |
^ EmptyCollectionSignal |
308 | 152 |
! |
153 |
||
602 | 154 |
invalidKeySignal |
155 |
"return the signal used to report bad key usage" |
|
308 | 156 |
|
602 | 157 |
^ InvalidKeySignal |
201 | 158 |
! |
159 |
||
602 | 160 |
notEnoughElementsSignal |
161 |
"return the signal used to report attempts for an operation, for which |
|
162 |
there are not enough elements in the collection" |
|
249 | 163 |
|
602 | 164 |
^ NotEnoughElementsSignal |
404 | 165 |
! |
166 |
||
602 | 167 |
valueNotFoundSignal |
168 |
"return the signal used to report a nonexisting element." |
|
404 | 169 |
|
602 | 170 |
^ ValueNotFoundSignal |
171 |
! ! |
|
404 | 172 |
|
602 | 173 |
!Collection class methodsFor:'queries'! |
404 | 174 |
|
602 | 175 |
growIsCheap |
176 |
"return true, if this collection can easily grow |
|
177 |
(i.e. without a need for become:). |
|
178 |
Returns true here; this method is redefined in fix-size |
|
179 |
collections" |
|
404 | 180 |
|
602 | 181 |
^ true |
257 | 182 |
! ! |
183 |
||
1 | 184 |
!Collection methodsFor:'accessing'! |
185 |
||
602 | 186 |
anElement |
187 |
"return any element from the collection, or nil if there is none" |
|
188 |
||
189 |
self do: [:each | ^ each]. |
|
190 |
^ nil |
|
191 |
! |
|
192 |
||
68 | 193 |
first |
194 |
"return the first element of the collection. |
|
195 |
This should be redefined in subclasses." |
|
196 |
||
197 |
self do:[:e | ^ e]. |
|
198 |
||
199 |
"error if collection is empty" |
|
155 | 200 |
^ self emptyCollectionError |
68 | 201 |
! |
202 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
203 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
204 |
"return the first element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
205 |
If its empty, return the excveptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
206 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
207 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
208 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
209 |
^ self first |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
210 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
211 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
212 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
213 |
|
68 | 214 |
last |
215 |
"return the last element of the collection. |
|
216 |
This should be redefined in subclasses." |
|
217 |
||
218 |
|theLastOne any| |
|
219 |
||
220 |
any := false. |
|
221 |
self do:[:e | any := true. theLastOne := e]. |
|
222 |
any ifTrue:[ |
|
155 | 223 |
^ theLastOne |
68 | 224 |
]. |
225 |
||
226 |
"error if collection is empty" |
|
155 | 227 |
^ self emptyCollectionError |
68 | 228 |
! |
229 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
230 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
231 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
232 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
233 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
234 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
235 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
236 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
237 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
238 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
239 |
"Created: 6.2.1996 / 15:27:49 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
240 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
241 |
|
328 | 242 |
upTo:anElement |
243 |
"return a new collection consisting of the receivers elements upTo |
|
244 |
(but excluding) anElement. |
|
245 |
If anElement is not in the receiver, the returned collection |
|
246 |
will consist of all elements of the receiver" |
|
247 |
||
248 |
|s| |
|
249 |
||
250 |
s := ReadStream on:self. |
|
251 |
^ s upTo:anElement |
|
252 |
||
253 |
" |
|
254 |
'test.text' upTo:$. |
|
255 |
#(1 2 3 4 5) upTo:4 |
|
256 |
" |
|
1 | 257 |
! ! |
258 |
||
259 |
!Collection methodsFor:'adding & removing'! |
|
260 |
||
261 |
add:anObject |
|
88 | 262 |
"add the argument, anObject to the receiver. |
263 |
If the receiver is ordered, the position of the new element is undefined |
|
264 |
(i.e. dont depend on where it will be put)." |
|
1 | 265 |
|
266 |
^ self subclassResponsibility |
|
267 |
! |
|
268 |
||
269 |
addAll:aCollection |
|
88 | 270 |
"add all elements of the argument, aCollection to the receiver." |
1 | 271 |
|
272 |
aCollection do:[:element | |
|
155 | 273 |
self add:element |
1 | 274 |
]. |
275 |
^ aCollection |
|
88 | 276 |
|
277 |
" |
|
278 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
279 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
280 |
" |
|
1 | 281 |
! |
282 |
||
12 | 283 |
addAllFirst:aCollection |
284 |
"insert all elements of the argument, aCollection at the beginning |
|
285 |
of the receiver" |
|
286 |
||
287 |
aCollection reverseDo:[:element | |
|
155 | 288 |
self addFirst:element |
12 | 289 |
]. |
290 |
^ aCollection |
|
291 |
||
292 |
" |
|
293 |
|c| |
|
294 |
c := #(4 3 2 1) asOrderedCollection. |
|
295 |
c addAllFirst:#(9 8 7 6 5) |
|
296 |
" |
|
297 |
! |
|
298 |
||
602 | 299 |
addAllLast:aCollection |
300 |
"add all elements of the argument, aCollection to the receiver" |
|
301 |
||
302 |
aCollection do:[:element | |
|
303 |
self addLast:element |
|
304 |
]. |
|
305 |
^ aCollection |
|
306 |
||
307 |
" |
|
308 |
|c| |
|
309 |
c := #(4 3 2 1) asOrderedCollection. |
|
310 |
c addAllLast:#(9 8 7 6 5) |
|
311 |
" |
|
312 |
! |
|
313 |
||
314 |
addFirst:anObject |
|
315 |
"add the argument, anObject to the receiver. |
|
316 |
If the receiver is ordered, the new element will be added at the beginning." |
|
1 | 317 |
|
318 |
^ self subclassResponsibility |
|
319 |
! |
|
320 |
||
602 | 321 |
addLast:anObject |
322 |
"add the argument, anObject to the receiver. |
|
323 |
If the receiver is ordered, the new element will be added at the end." |
|
324 |
||
325 |
^ self add:anObject |
|
326 |
! |
|
327 |
||
1 | 328 |
remove:anObject |
348 | 329 |
"remove the (first occurrence of) argument, anObject from the receiver, |
330 |
and return it. |
|
331 |
If it was not in the collection, raise an error." |
|
1 | 332 |
|
345 | 333 |
self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
1 | 334 |
! |
335 |
||
602 | 336 |
remove:anObject ifAbsent:exceptionBlock |
337 |
"remove the (first occurrence of) argument, anObject from the receiver, |
|
338 |
and return it. |
|
339 |
If it was not in the collection, return the the value of the exceptionBlock" |
|
340 |
||
341 |
^ self subclassResponsibility |
|
342 |
! |
|
343 |
||
134 | 344 |
removeAll |
345 |
"remove all elements from the receiver. |
|
346 |
This should be reimplemented in subclasses for better |
|
347 |
performance." |
|
348 |
||
349 |
[self notEmpty] whileTrue:[ |
|
155 | 350 |
self removeFirst |
134 | 351 |
]. |
352 |
! |
|
353 |
||
1 | 354 |
removeAll:aCollection |
360 | 355 |
"remove all elements of the argument, aCollection from the receiver. |
356 |
Notice: for some collections (those not tuned for |
|
357 |
resizing themself) this may be very slow. |
|
358 |
If the number of removed elements is big compared to to |
|
359 |
the receivers size, it may be better to copy the |
|
360 |
ones which are not to be removed into a new collection." |
|
1 | 361 |
|
362 |
aCollection do:[:element | self remove:element]. |
|
363 |
^ aCollection |
|
348 | 364 |
! |
365 |
||
366 |
removeFirst |
|
367 |
"remove the first element from the receiver. |
|
368 |
Return the removed element." |
|
369 |
||
370 |
^ self subclassResponsibility |
|
371 |
! |
|
372 |
||
373 |
removeFirst:n |
|
374 |
"remove the first n elements from the receiver. |
|
360 | 375 |
Return an array filled with the removed elements. |
376 |
Notice: for some collections (those not tuned for |
|
377 |
resizing themself) this may be very slow." |
|
348 | 378 |
|
379 |
|ret| |
|
380 |
||
381 |
self size < n ifTrue:[ |
|
382 |
^ self notEnoughElementsError |
|
383 |
]. |
|
384 |
ret := Array new:n. |
|
385 |
1 to:n do:[:i | |
|
386 |
ret at:i put:(self removeFirst). |
|
387 |
]. |
|
388 |
^ ret |
|
389 |
! |
|
390 |
||
602 | 391 |
removeLast |
392 |
"remove the last element from the receiver. |
|
393 |
Return the removed element." |
|
394 |
||
395 |
^ self subclassResponsibility |
|
396 |
! |
|
397 |
||
348 | 398 |
removeLast:n |
399 |
"remove the last n elements from the receiver. |
|
360 | 400 |
Return an array filled with the removed elements. |
401 |
Notice: for some collections (those not tuned for |
|
402 |
resizing themself) this may be very slow." |
|
348 | 403 |
|
404 |
|ret| |
|
405 |
||
406 |
self size < n ifTrue:[ |
|
407 |
^ self notEnoughElementsError |
|
408 |
]. |
|
409 |
ret := Array new:n. |
|
410 |
n to:1 by:-1 do:[:i | |
|
411 |
ret at:i put:(self removeLast). |
|
412 |
]. |
|
413 |
^ ret |
|
1 | 414 |
! ! |
415 |
||
602 | 416 |
!Collection methodsFor:'converting'! |
417 |
||
418 |
asArray |
|
419 |
"return a new Array with the collections elements" |
|
420 |
||
421 |
|anArray |
|
422 |
index "{ Class: SmallInteger }" | |
|
1 | 423 |
|
602 | 424 |
anArray := Array new:(self size). |
425 |
index := 1. |
|
426 |
self do:[:each | |
|
427 |
anArray at:index put:each. |
|
428 |
index := index + 1 |
|
429 |
]. |
|
430 |
^ anArray |
|
1 | 431 |
! |
432 |
||
602 | 433 |
asBag |
434 |
"return a new Bag with the receiver collections elements" |
|
1 | 435 |
|
602 | 436 |
^ self addAllTo:(Bag new) |
1 | 437 |
! |
438 |
||
602 | 439 |
asByteArray |
440 |
"return a new ByteArray with the collections elements |
|
441 |
(which must convert to integers in the range 0..255)." |
|
1 | 442 |
|
602 | 443 |
|aByteArray |
444 |
index "{ Class: SmallInteger }" | |
|
1 | 445 |
|
602 | 446 |
aByteArray := ByteArray new:(self size). |
447 |
index := 1. |
|
448 |
self do:[:each | |
|
449 |
aByteArray at:index put:each asInteger. |
|
450 |
index := index + 1 |
|
451 |
]. |
|
452 |
^ aByteArray |
|
1 | 453 |
! |
454 |
||
602 | 455 |
asDoubleArray |
456 |
"return a new DoubleArray with the collections elements |
|
457 |
(which must convert to floats)." |
|
458 |
||
459 |
|aDoubleArray |
|
460 |
index "{ Class: SmallInteger }" | |
|
461 |
||
462 |
aDoubleArray := DoubleArray new:(self size). |
|
463 |
index := 1. |
|
464 |
self do:[:each | |
|
465 |
aDoubleArray at:index put:each asFloat. |
|
466 |
index := index + 1 |
|
467 |
]. |
|
468 |
^ aDoubleArray |
|
469 |
! |
|
68 | 470 |
|
602 | 471 |
asFloatArray |
472 |
"return a new FloatArray with the collections elements |
|
473 |
(which must convert to floats)." |
|
474 |
||
475 |
|aFloatArray |
|
476 |
index "{ Class: SmallInteger }" | |
|
477 |
||
478 |
aFloatArray := FloatArray new:(self size). |
|
479 |
index := 1. |
|
480 |
self do:[:each | |
|
481 |
aFloatArray at:index put:each asFloat. |
|
482 |
index := index + 1 |
|
483 |
]. |
|
484 |
^ aFloatArray |
|
485 |
! |
|
486 |
||
487 |
asIdentitySet |
|
488 |
"return a new IdentitySet with the receiver collections elements" |
|
489 |
||
490 |
^ self addAllTo:(IdentitySet new:self size) |
|
68 | 491 |
! |
492 |
||
602 | 493 |
asOrderedCollection |
494 |
"return a new OrderedCollection with the receiver collections elements" |
|
495 |
||
496 |
^ self addAllTo:(OrderedCollection new:self size) |
|
497 |
! |
|
498 |
||
499 |
asSet |
|
500 |
"return a new Set with the receiver collections elements" |
|
501 |
||
502 |
^ self addAllTo:(Set new:self size) |
|
503 |
! |
|
504 |
||
505 |
asSortedCollection |
|
506 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 507 |
|
602 | 508 |
|aSortedCollection| |
509 |
||
510 |
aSortedCollection := SortedCollection new:self size. |
|
511 |
aSortedCollection addAll:self. |
|
512 |
^ aSortedCollection |
|
513 |
! |
|
514 |
||
515 |
asSortedCollection:sortBlock |
|
516 |
"return a new SortedCollection with the receiver collections elements, |
|
517 |
using sortBlock for comparing" |
|
518 |
||
519 |
|aSortedCollection| |
|
520 |
||
521 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
522 |
aSortedCollection addAll:self. |
|
523 |
^ aSortedCollection |
|
1 | 524 |
! |
525 |
||
602 | 526 |
asString |
527 |
"return a String with the collections elements |
|
528 |
(which must convert to characters)" |
|
529 |
||
530 |
|aString |
|
531 |
index "{ Class: SmallInteger }" | |
|
360 | 532 |
|
602 | 533 |
aString := String new:(self size). |
534 |
index := 1. |
|
535 |
self do:[:each | |
|
536 |
aString at:index put:each asCharacter. |
|
537 |
index := index + 1 |
|
360 | 538 |
]. |
602 | 539 |
^ aString |
540 |
! |
|
541 |
||
542 |
asStringCollection |
|
543 |
"return a new Text-object (collection of lines) with the elements printstings" |
|
544 |
||
545 |
^ self addAllTo:(StringCollection new) |
|
360 | 546 |
! |
547 |
||
602 | 548 |
asText |
549 |
"return a Text-object (collection of lines) from myself. |
|
550 |
BIG warning: asText is totally misnamed here |
|
551 |
- ST/X's asText has nothing to do with PP's asText. |
|
552 |
Therefore it will be removed/renamed soon. |
|
553 |
Please use #asStringCollection." |
|
1 | 554 |
|
602 | 555 |
self obsoleteMethodWarning:'use #asStringCollection; #asText will change its meaning'. |
556 |
||
557 |
^ self asStringCollection |
|
558 |
! |
|
559 |
||
560 |
readStream |
|
561 |
"return a stream for reading from the receiver" |
|
562 |
||
563 |
^ ReadStream on:self |
|
88 | 564 |
|
565 |
" |
|
602 | 566 |
|s| |
567 |
||
568 |
s := 'hello world' readStream. |
|
569 |
s next:5. |
|
570 |
s next. |
|
571 |
(s next:5) inspect |
|
88 | 572 |
" |
1 | 573 |
! |
574 |
||
602 | 575 |
writeStream |
576 |
"return a stream for writing onto the receiver" |
|
577 |
||
578 |
^ WriteStream on:self |
|
2 | 579 |
|
602 | 580 |
" |
581 |
|s| |
|
582 |
||
583 |
s := #() copy writeStream. |
|
584 |
s nextPut:1. |
|
585 |
s nextPut:2. |
|
586 |
s nextPut:3. |
|
587 |
s contents inspect |
|
588 |
" |
|
88 | 589 |
|
590 |
" |
|
602 | 591 |
|s| |
2 | 592 |
|
602 | 593 |
s := OrderedCollection new writeStream. |
594 |
s nextPut:1. |
|
595 |
s nextPut:2. |
|
596 |
s nextPut:3. |
|
597 |
s contents inspect |
|
598 |
" |
|
599 |
! ! |
|
1 | 600 |
|
602 | 601 |
!Collection methodsFor:'copying'! |
602 |
||
603 |
copyEmpty |
|
604 |
"return a copy of the receiver with no elements. |
|
605 |
This is used by copying and enumeration methods |
|
606 |
to get a new instance which is similar to the receiver." |
|
607 |
||
608 |
^ self species new |
|
1 | 609 |
! |
610 |
||
602 | 611 |
copyEmpty:size |
612 |
"return a copy of the receiver with no elements, but space for |
|
613 |
size elements. This is used by copying and enumeration methods |
|
614 |
to get a new instance which is similar to the receiver. |
|
615 |
This method should be redefined in subclasses with instance |
|
616 |
variables, which should be put into the copy too. |
|
617 |
For example, SortedCollection has to copy its sortBlock into the |
|
618 |
new collection." |
|
1 | 619 |
|
602 | 620 |
^ self species new:size |
360 | 621 |
! |
622 |
||
602 | 623 |
copyEmptyAndGrow:size |
624 |
"return a copy of the receiver with size nil elements. |
|
625 |
This is used by copying and enumeration methods |
|
626 |
to get a new instance which is similar to the receiver." |
|
155 | 627 |
|
602 | 628 |
^ (self copyEmpty:size) grow:size |
1 | 629 |
! ! |
630 |
||
631 |
!Collection methodsFor:'enumerating'! |
|
632 |
||
602 | 633 |
addAllTo:aCollection |
634 |
"add all elements of the receiver, to aCollection. |
|
635 |
Return aCollection." |
|
77 | 636 |
|
602 | 637 |
self do:[:each | aCollection add:each]. |
638 |
^ aCollection |
|
77 | 639 |
! |
640 |
||
1 | 641 |
collect:aBlock |
642 |
"for each element in the receiver, evaluate the argument, aBlock |
|
643 |
and return a new collection with the results" |
|
644 |
||
645 |
|newCollection| |
|
646 |
||
201 | 647 |
newCollection := self species new. |
648 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 649 |
^ newCollection |
68 | 650 |
|
651 |
" |
|
652 |
#(1 2 3 4) collect:[:n | n * 2] |
|
653 |
" |
|
1 | 654 |
! |
655 |
||
602 | 656 |
conform:aBlock |
657 |
"return true, if every element conforms to some condition. |
|
658 |
I.e. return false, if aBlock returns false for any element; |
|
659 |
true otherwise." |
|
77 | 660 |
|
602 | 661 |
self do:[:element | (aBlock value:element) ifFalse:[^ false]]. |
662 |
^ true |
|
663 |
! |
|
77 | 664 |
|
602 | 665 |
count:aBlock |
666 |
"count elements, for which aBlock returns true. |
|
667 |
Return the sum." |
|
668 |
||
669 |
^ self inject:0 into:[:sumSoFar :element | |
|
670 |
(aBlock value:element) ifTrue:[sumSoFar + 1] |
|
671 |
ifFalse:[sumSoFar] |
|
672 |
] |
|
77 | 673 |
|
674 |
" |
|
602 | 675 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
676 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
|
77 | 677 |
" |
678 |
! |
|
679 |
||
1 | 680 |
detect:aBlock |
681 |
"evaluate the argument, aBlock for each element in the receiver until |
|
682 |
the block returns true; in this case return the element which caused |
|
683 |
the true evaluation. |
|
345 | 684 |
If none of the evaluations returns true, report an error" |
1 | 685 |
|
686 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 687 |
|
688 |
" |
|
689 |
#(1 2 3 4) detect:[:n | n odd] |
|
690 |
#(2 4 6 8) detect:[:n | n odd] |
|
691 |
" |
|
1 | 692 |
! |
693 |
||
694 |
detect:aBlock ifNone:exceptionBlock |
|
695 |
"evaluate the argument, aBlock for each element in the receiver until |
|
696 |
the block returns true; in this case return the element which caused |
|
697 |
the true evaluation. |
|
698 |
If none of the evaluations returns true, return the result of the |
|
699 |
evaluation of the exceptionBlock" |
|
700 |
||
701 |
self do:[:each | |
|
155 | 702 |
(aBlock value:each) ifTrue:[^ each]. |
1 | 703 |
]. |
704 |
^ exceptionBlock value |
|
68 | 705 |
|
706 |
" |
|
707 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
708 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
709 |
" |
|
1 | 710 |
! |
711 |
||
421 | 712 |
detectLast:aBlock |
713 |
"evaluate the argument, aBlock for each element in the receiver until |
|
714 |
the block returns true; in this case return the element which caused |
|
715 |
the true evaluation. The elements are processed in reverse order. |
|
716 |
If none of the evaluations returns true, report an error" |
|
717 |
||
718 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
719 |
||
720 |
" |
|
721 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
722 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
723 |
" |
|
724 |
! |
|
725 |
||
726 |
detectLast:aBlock ifNone:exceptionBlock |
|
727 |
"evaluate the argument, aBlock for each element in the receiver until |
|
728 |
the block returns true; in this case return the element which caused |
|
729 |
the true evaluation. The elements are processed in reverse order. |
|
730 |
If none of the evaluations returns true, return the result of the |
|
731 |
evaluation of the exceptionBlock" |
|
732 |
||
733 |
self reverseDo:[:each | |
|
734 |
(aBlock value:each) ifTrue:[^ each]. |
|
735 |
]. |
|
736 |
^ exceptionBlock value |
|
737 |
||
738 |
" |
|
739 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
740 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
741 |
" |
|
742 |
! |
|
743 |
||
602 | 744 |
do:aBlock |
745 |
"evaluate the argument, aBlock for each element" |
|
746 |
||
747 |
^ self subclassResponsibility |
|
748 |
! |
|
749 |
||
1 | 750 |
inject:thisValue into:binaryBlock |
751 |
|nextValue| |
|
752 |
||
753 |
nextValue := thisValue. |
|
754 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
755 |
^ nextValue |
|
68 | 756 |
|
757 |
" |
|
758 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
|
759 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
760 |
" |
|
1 | 761 |
! |
762 |
||
602 | 763 |
keysAndValuesDo:aTwoArgBlock |
764 |
"evaluate the argument, aBlock for every element in the collection, |
|
765 |
passing both index and element as arguments." |
|
766 |
||
767 |
^ self errorNotKeyed |
|
768 |
! |
|
769 |
||
770 |
pairsDo:aBlock |
|
771 |
"evaluate the argument, aBlock for every element in the collection, |
|
772 |
which is supposed to consist of 2-element collections. |
|
773 |
The block is called with 2 arguments for each collection in the receiver." |
|
774 |
||
775 |
self do:[:aPair | |
|
776 |
aBlock value:(aPair at:1) value:(aPair at:2). |
|
777 |
] |
|
778 |
" |
|
779 |
#( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) |
|
780 |
pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym] |
|
781 |
||
782 |
||
783 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
784 |
pairsDo:[:x :y | Transcript showCr:x@y] |
|
785 |
" |
|
786 |
! |
|
787 |
||
1 | 788 |
reject:aBlock |
789 |
"return a new collection with all elements from the receiver, for which |
|
790 |
the argument aBlock evaluates to false" |
|
791 |
||
792 |
^ self select:[:element | (aBlock value:element) == false] |
|
68 | 793 |
|
794 |
" |
|
795 |
#(1 2 3 4) reject:[:e | e odd] |
|
796 |
(1 to:10) reject:[:e | e even] |
|
797 |
" |
|
1 | 798 |
! |
799 |
||
602 | 800 |
reverseDo:aBlock |
801 |
"evaluate the argument, aBlock for each element in reverse order." |
|
802 |
||
803 |
"it could be defined in terms of do: - but very inefficient. |
|
804 |
Better force programmer to define a better version ..." |
|
805 |
||
806 |
^ self subclassResponsibility |
|
807 |
! |
|
808 |
||
1 | 809 |
select:aBlock |
810 |
"return a new collection with all elements from the receiver, for which |
|
811 |
the argument aBlock evaluates to true" |
|
812 |
||
813 |
|newCollection| |
|
814 |
||
201 | 815 |
newCollection := self species new. |
1 | 816 |
self do:[:each | |
155 | 817 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 818 |
]. |
819 |
^ newCollection |
|
68 | 820 |
|
821 |
" |
|
822 |
#(1 2 3 4) select:[:e | e odd] |
|
823 |
(1 to:10) select:[:e | e even] |
|
824 |
" |
|
825 |
! |
|
826 |
||
827 |
select:aBlock ifNone:exceptionBlock |
|
828 |
"try a new collection with all elements from the receiver, for which |
|
829 |
the argument aBlock evaluates to true. If none of the elements passes |
|
830 |
the check of aBlock, return the result of evaluating exceptionBlock." |
|
831 |
||
832 |
|newCollection| |
|
833 |
||
834 |
newCollection := self select:aBlock. |
|
835 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
836 |
^ newCollection |
|
837 |
||
838 |
" |
|
839 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
840 |
" |
|
12 | 841 |
! |
842 |
||
362 | 843 |
triplesDo:aBlock |
844 |
"evaluate the argument, aBlock for every element in the collection, |
|
845 |
which is supposed to consist of 3-element collections. |
|
846 |
The block is called with 3 arguments for each collection in the receiver." |
|
847 |
||
848 |
self do:[:aTriple | |
|
849 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
|
850 |
] |
|
851 |
" |
|
852 |
#( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fünf') (6 six sechs)) |
|
853 |
triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCr:sym2] |
|
854 |
" |
|
1 | 855 |
! |
856 |
||
602 | 857 |
with:aCollection collect:aTwoArgBlock |
858 |
"evaluate the argument, aBlock for successive elements from |
|
859 |
each the receiver and the argument, aSequenceableCollection; |
|
860 |
The second argument, aBlock must be a two-argument block, which is |
|
861 |
evaluated for each element-pair. |
|
862 |
Collect the results and return a collection containing them. |
|
863 |
This method fails if neither the receiver nor aCollection is |
|
864 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 865 |
|
602 | 866 |
|index "{ Class: SmallInteger }" |
867 |
newCollection| |
|
1 | 868 |
|
602 | 869 |
newCollection := self species new. |
870 |
index := 1. |
|
871 |
aCollection isSequenceable ifFalse:[ |
|
872 |
self isSequenceable ifFalse:[ |
|
873 |
^ self error:'neither collection is sequenceable'. |
|
874 |
]. |
|
875 |
aCollection do:[:element | |
|
876 |
newCollection add:(aTwoArgBlock value:(self at:index) |
|
877 |
value:element). |
|
878 |
index := index + 1 |
|
879 |
] |
|
880 |
] ifTrue:[ |
|
881 |
self do:[:element | |
|
882 |
newCollection add:(aTwoArgBlock value:element |
|
883 |
value:(aCollection at:index)). |
|
884 |
index := index + 1 |
|
885 |
] |
|
886 |
]. |
|
887 |
^ newCollection |
|
201 | 888 |
|
889 |
" |
|
602 | 890 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
891 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
201 | 892 |
" |
2 | 893 |
! |
894 |
||
602 | 895 |
with:aCollection do:aTwoArgBlock |
896 |
"evaluate the argument, aBlock for successive elements from |
|
897 |
each the receiver and the argument, aSequenceableCollection. |
|
898 |
The second argument, aBlock must be a two-argument block. |
|
899 |
This method fails if neither the receiver nor aCollection is |
|
900 |
a sequenceable collection (i.e. implements numeric key access)." |
|
901 |
||
902 |
|index "{ Class: SmallInteger }"| |
|
2 | 903 |
|
602 | 904 |
index := 1. |
905 |
aCollection isSequenceable ifFalse:[ |
|
906 |
self isSequenceable ifFalse:[ |
|
907 |
^ self error:'neither collection is sequenceable'. |
|
908 |
]. |
|
909 |
aCollection do:[:element | |
|
910 |
aTwoArgBlock value:(self at:index) value:element. |
|
911 |
index := index + 1 |
|
912 |
] |
|
913 |
] ifTrue:[ |
|
914 |
self do:[:element | |
|
915 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
916 |
index := index + 1 |
|
917 |
] |
|
918 |
] |
|
201 | 919 |
|
920 |
" |
|
602 | 921 |
(1 to:3) with:#(one two three) do:[:num :sym | |
922 |
Transcript showCr:(num->sym) |
|
923 |
] |
|
924 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
925 |
Transcript showCr:(num->sym) |
|
926 |
]. |
|
927 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
928 |
Transcript showCr:(num->sym) |
|
929 |
] |
|
201 | 930 |
" |
1 | 931 |
! ! |
932 |
||
602 | 933 |
!Collection methodsFor:'error handling'! |
934 |
||
935 |
emptyCheck |
|
936 |
"check if the receiver is empty; report an error if so" |
|
937 |
||
938 |
self size == 0 ifTrue:[ |
|
939 |
^ self emptyCollectionError. |
|
940 |
]. |
|
941 |
! |
|
942 |
||
943 |
emptyCollectionError |
|
944 |
"report an error that the operation is not allowed for |
|
945 |
empty collections" |
|
946 |
||
947 |
^ EmptyCollectionSignal raise |
|
948 |
! |
|
949 |
||
950 |
errorInvalidKey:aKey |
|
951 |
"report an error that the given key was invalid" |
|
952 |
||
953 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
954 |
! |
|
955 |
||
956 |
errorNotKeyed |
|
957 |
"report an error that keyed access methods are not allowed" |
|
958 |
||
959 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
960 |
! |
|
961 |
||
962 |
errorValueNotFound:anObject |
|
963 |
"report an error that an object was not found in the collection" |
|
964 |
||
965 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
|
966 |
! |
|
967 |
||
968 |
notEnoughElementsError |
|
969 |
"report an error that the operation is not allowed, |
|
970 |
since not enough elements are in the collection" |
|
971 |
||
972 |
^ NotEnoughElementsSignal raise |
|
973 |
! ! |
|
974 |
||
975 |
!Collection methodsFor:'growing'! |
|
976 |
||
977 |
grow |
|
978 |
"make the receiver larger" |
|
979 |
||
980 |
self grow:(self size + self growSize) |
|
981 |
! |
|
982 |
||
983 |
grow:howBig |
|
984 |
"change the receivers size" |
|
985 |
||
986 |
^ self subclassResponsibility |
|
987 |
! |
|
988 |
||
989 |
growSize |
|
990 |
"return a suitable size increment for growing. |
|
991 |
The default returned here may be (and is) redefined in subclasses." |
|
992 |
||
993 |
^ self size max:2 |
|
994 |
! ! |
|
995 |
||
1 | 996 |
!Collection methodsFor:'printing & storing'! |
997 |
||
602 | 998 |
displayString |
999 |
"return a printed representation of the receiver for display in inspectors etc." |
|
1000 |
||
1001 |
^ self printOrDisplayStringUsing:#displayString |
|
1002 |
! |
|
1003 |
||
1 | 1004 |
maxPrint |
169 | 1005 |
"the print-limit; printOn: will try to not produce more output |
88 | 1006 |
than the limit defined here." |
1007 |
||
1 | 1008 |
^ 5000 |
1009 |
! |
|
1010 |
||
1011 |
printOn:aStream |
|
88 | 1012 |
"append a user readable representation of the receiver to aStream. |
1013 |
The text appended is not meant to be read back for reconstruction of |
|
169 | 1014 |
the receiver. Also, this method limits the size of generated string. |
88 | 1015 |
" |
1016 |
||
169 | 1017 |
|limit firstOne string| |
1 | 1018 |
|
44 | 1019 |
thisContext isRecursive ifTrue:[ |
155 | 1020 |
Transcript showCr:'Error: printOn: of self referencing collection.'. |
1021 |
aStream nextPutAll:'#("recursive")'. |
|
1022 |
^ self |
|
44 | 1023 |
]. |
1024 |
||
1025 |
limit := self maxPrint. |
|
1 | 1026 |
aStream nextPutAll:self class name. |
1027 |
aStream nextPut:$(. |
|
1028 |
firstOne := true. |
|
169 | 1029 |
|
1 | 1030 |
self do:[:element | |
169 | 1031 |
firstOne ifFalse:[ |
1032 |
aStream space |
|
1033 |
] ifTrue:[ |
|
1034 |
firstOne := false |
|
1035 |
]. |
|
1036 |
(limit <= 0) ifTrue:[ |
|
1037 |
aStream nextPutAll:'...etc...)'. |
|
1038 |
^ self |
|
1039 |
] ifFalse:[ |
|
92 | 1040 |
"/ old code, worked only on positionable streams |
1041 |
"/ |
|
1042 |
"/ pos1 := aStream position. |
|
1043 |
"/ element printOn:aStream. |
|
1044 |
"/ limit := limit - (aStream position - pos1) |
|
169 | 1045 |
|
1046 |
string := element printString. |
|
1047 |
aStream nextPutAll:string. |
|
1048 |
limit := limit - string size. |
|
155 | 1049 |
]. |
1 | 1050 |
]. |
1051 |
aStream nextPut:$) |
|
44 | 1052 |
|
88 | 1053 |
" |
1054 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
169 | 1055 |
(Array new:100000) printOn:Transcript |
1056 |
(Array new:100000) printString size |
|
88 | 1057 |
(Dictionary new at:#hello put:'world'; yourself) printOn:Transcript |
169 | 1058 |
" |
1059 |
" |
|
88 | 1060 |
|a| |
1061 |
a := Array new:3. |
|
1062 |
a at:2 put:a. |
|
1063 |
a printOn:Transcript |
|
1064 |
" |
|
1 | 1065 |
! |
1066 |
||
602 | 1067 |
printOrDisplayStringUsing:aSelector |
1068 |
"common code for printString and displayString; they only differ in |
|
1069 |
the print-message sent to the elements" |
|
1070 |
||
1071 |
|thisString buffer count string noneYet total limit| |
|
1072 |
||
1073 |
thisContext isRecursive ifTrue:[ |
|
1074 |
Transcript showCr:'Error: print/storeString of self referencing collection.'. |
|
1075 |
^ '#("recursive")' |
|
1076 |
]. |
|
1077 |
||
1078 |
string := (self class name) , '('. |
|
1079 |
noneYet := true. |
|
1080 |
buffer := ''. |
|
1081 |
count := 0. |
|
1082 |
total := 0. |
|
1083 |
limit := self maxPrint. |
|
1084 |
||
1085 |
self do: [:element | |
|
1086 |
thisString := element perform:aSelector. |
|
1087 |
noneYet ifTrue:[ |
|
1088 |
noneYet := false. |
|
1089 |
buffer := buffer , thisString |
|
1090 |
] ifFalse:[ |
|
1091 |
buffer := buffer , (' ' , thisString) |
|
1092 |
]. |
|
1093 |
count := count + 1. |
|
1094 |
(count == 20) ifTrue:[ |
|
1095 |
string := string , buffer. |
|
1096 |
buffer := ''. |
|
1097 |
count := 0 |
|
1098 |
]. |
|
1099 |
total := total + 1. |
|
1100 |
(total > limit) ifTrue:[ |
|
1101 |
string := string , buffer , '... )'. |
|
1102 |
^ string |
|
1103 |
] |
|
1104 |
]. |
|
1105 |
string := string , buffer , ')'. |
|
1106 |
^ string |
|
1107 |
! |
|
1108 |
||
1 | 1109 |
storeOn:aStream |
88 | 1110 |
"output a printed representation onto the argument, aStream. |
1111 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
1112 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
1113 |
(use storeBinaryOn: to handle those)." |
|
1 | 1114 |
|
1115 |
|isEmpty| |
|
1116 |
||
44 | 1117 |
thisContext isRecursive ifTrue:[ |
155 | 1118 |
Transcript showCr:'Error: storeOn: of self referencing collection.'. |
293 | 1119 |
aStream nextPutAll:'#recursive'. |
155 | 1120 |
^ self |
44 | 1121 |
]. |
1122 |
||
1123 |
aStream nextPutAll:'('. |
|
1 | 1124 |
aStream nextPutAll:(self class name). |
44 | 1125 |
aStream nextPutAll:' new'. |
1 | 1126 |
isEmpty := true. |
1127 |
self do:[:element | |
|
155 | 1128 |
aStream nextPutAll:' add:'. |
1129 |
element storeOn:aStream. |
|
1130 |
aStream nextPutAll:';'. |
|
1131 |
isEmpty := false |
|
1 | 1132 |
]. |
1133 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1134 |
aStream nextPut:$) |
|
44 | 1135 |
|
88 | 1136 |
" |
1137 |
OrderedCollection new storeOn:Transcript |
|
1138 |
(1 to:10) storeOn:Transcript |
|
1139 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
1140 |
" |
|
293 | 1141 |
" |
1142 |
|s| |
|
1143 |
||
1144 |
s := Set new. |
|
1145 |
s add:1; add:'hello'; add:s. |
|
1146 |
s storeOn:Transcript |
|
1147 |
" |
|
1 | 1148 |
! ! |
602 | 1149 |
|
1150 |
!Collection methodsFor:'queries'! |
|
1151 |
||
1152 |
isCollection |
|
1153 |
"return true, if the receiver is some kind of collection; |
|
1154 |
true is returned here - the method is redefined from Object." |
|
1155 |
||
1156 |
^ true |
|
1157 |
! |
|
1158 |
||
1159 |
longestCommonPrefix |
|
1160 |
|try allMatching matchLen| |
|
1161 |
||
1162 |
" |
|
1163 |
find the longest common prefix |
|
1164 |
" |
|
1165 |
matchLen := 0. |
|
1166 |
self do:[:aName | |
|
1167 |
aName size > matchLen ifTrue:[ |
|
1168 |
matchLen := aName size. |
|
1169 |
try := aName |
|
1170 |
] |
|
1171 |
]. |
|
1172 |
||
1173 |
allMatching := true. |
|
1174 |
||
1175 |
[true] whileTrue:[ |
|
1176 |
allMatching := true. |
|
1177 |
self do:[:aName | |
|
1178 |
(aName startsWith:try) ifFalse:[ |
|
1179 |
allMatching := false |
|
1180 |
] |
|
1181 |
]. |
|
1182 |
allMatching ifTrue:[ |
|
1183 |
^ try |
|
1184 |
]. |
|
1185 |
matchLen := matchLen - 1. |
|
1186 |
try := try copyTo:matchLen. |
|
1187 |
]. |
|
1188 |
^ try |
|
1189 |
||
1190 |
" |
|
1191 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1192 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1193 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
1194 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
1195 |
" |
|
1196 |
! ! |
|
1197 |
||
1198 |
!Collection methodsFor:'testing'! |
|
1199 |
||
1200 |
capacity |
|
1201 |
"return the number of elements, that the receiver is |
|
1202 |
prepared to take. For most collections, this is the actual |
|
1203 |
size. However, some have more space preallocated to allow |
|
1204 |
for faster adding of elements. |
|
1205 |
Not used by the system; added for ST-80 compatibility." |
|
1206 |
||
1207 |
^ self size |
|
1208 |
! |
|
1209 |
||
1210 |
includes:anElement |
|
1211 |
"return true, if an object equal to the argument, anObject is in the list. |
|
1212 |
This compares using #= (i.e. it does not look for the object itself, |
|
1213 |
instead, one that compares equal). |
|
1214 |
See #includesIdentical: when identity is asked for." |
|
1215 |
||
1216 |
self do:[:element | |
|
1217 |
(anElement = element) ifTrue:[^ true]. |
|
1218 |
]. |
|
1219 |
^ false |
|
1220 |
! |
|
1221 |
||
1222 |
includesAll:aCollection |
|
1223 |
"return true, if the the receiver includes all elements of |
|
1224 |
the argument, aCollection; false if any is missing. |
|
1225 |
Notice: this method has O-square runtime behavior and may be |
|
1226 |
slow for big receivers/args. Think about using a Set, |
|
1227 |
or Dictionary." |
|
1228 |
||
1229 |
aCollection do:[:element | |
|
1230 |
(self includes:element) ifFalse:[^ false]. |
|
1231 |
]. |
|
1232 |
^ true |
|
1233 |
||
1234 |
" |
|
1235 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
1236 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
1237 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
1238 |
" |
|
1239 |
! |
|
1240 |
||
1241 |
includesAny:aCollection |
|
1242 |
"return true, if the the receiver includes any elements of |
|
1243 |
the argument, aCollection; false if it includes none. |
|
1244 |
Notice: this method has O-square runtime behavior and may be |
|
1245 |
slow for big receivers/args. Think about using a Set, |
|
1246 |
or Dictionary. Speedup is possible, by arrangy highly |
|
1247 |
probable elements towards the beginning of aCollection, |
|
1248 |
to avoid useless searches." |
|
1249 |
||
1250 |
aCollection do:[:element | |
|
1251 |
(self includes:element) ifTrue:[^ true]. |
|
1252 |
]. |
|
1253 |
^ false |
|
1254 |
||
1255 |
" |
|
1256 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
1257 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
1258 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
1259 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
1260 |
" |
|
1261 |
! |
|
1262 |
||
1263 |
includesIdentical:anElement |
|
1264 |
"return true, if the argument, anObject is in the list. |
|
1265 |
This compares using #== (i.e. object identity). |
|
1266 |
See #includes: when equality is asked for." |
|
1267 |
||
1268 |
self do:[:element | |
|
1269 |
(anElement == element) ifTrue:[^ true]. |
|
1270 |
]. |
|
1271 |
^ false |
|
1272 |
! |
|
1273 |
||
1274 |
isEmpty |
|
1275 |
"return true, if the receiver is empty" |
|
1276 |
||
1277 |
^ self size == 0 |
|
1278 |
! |
|
1279 |
||
1280 |
max |
|
1281 |
"return the maximum value in the receiver collection" |
|
1282 |
||
1283 |
^ self inject:nil |
|
1284 |
into:[:maxSoFar :this | |
|
1285 |
(maxSoFar isNil |
|
1286 |
or:[this > maxSoFar]) ifTrue:[this] |
|
1287 |
ifFalse:[maxSoFar]] |
|
1288 |
||
1289 |
" |
|
1290 |
#(15 1 -9 10 5) max |
|
1291 |
(1 to:15) max |
|
1292 |
" |
|
1293 |
! |
|
1294 |
||
1295 |
min |
|
1296 |
"return the minimum value in the receiver collection" |
|
1297 |
||
1298 |
^ self inject:nil |
|
1299 |
into:[:minSoFar :this | |
|
1300 |
(minSoFar isNil |
|
1301 |
or:[this < minSoFar]) ifTrue:[this] |
|
1302 |
ifFalse:[minSoFar]] |
|
1303 |
||
1304 |
" |
|
1305 |
#(15 1 -9 10 5) min |
|
1306 |
(1 to:15) min |
|
1307 |
" |
|
1308 |
! |
|
1309 |
||
1310 |
notEmpty |
|
1311 |
"return true, if the receiver is not empty" |
|
1312 |
||
1313 |
^ self isEmpty not |
|
1314 |
! |
|
1315 |
||
1316 |
occurrencesOf:anElement |
|
1317 |
"return the number of occurrences of the argument, anElement in |
|
1318 |
the receiver" |
|
1319 |
||
1320 |
|count "{ Class: SmallInteger }" | |
|
1321 |
||
1322 |
count := 0. |
|
1323 |
self do:[:element | |
|
1324 |
(anElement = element) ifTrue:[ |
|
1325 |
count := count + 1 |
|
1326 |
]. |
|
1327 |
]. |
|
1328 |
^ count |
|
1329 |
! |
|
1330 |
||
1331 |
size |
|
1332 |
"return the number of elements in the receiver. |
|
1333 |
This is usually redefined in subclasses for more performance." |
|
1334 |
||
1335 |
|count "{ Class: SmallInteger }" | |
|
1336 |
||
1337 |
count := 0. |
|
1338 |
self do:[:element | |
|
1339 |
count := count + 1 |
|
1340 |
]. |
|
1341 |
^ count |
|
1342 |
! ! |
|
1343 |
||
628 | 1344 |
!Collection class methodsFor:'documentation'! |
1345 |
||
1346 |
version |
|
1112 | 1347 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.46 1996-03-21 14:30:45 cg Exp $' |
628 | 1348 |
! ! |
602 | 1349 |
Collection initialize! |