1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:''
|
10
|
15 |
classVariableNames:'InvalidKeySignal'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Abstract'
|
|
18 |
!
|
|
19 |
|
|
20 |
Collection comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
Abstract superclass for all collections
|
|
26 |
|
44
|
27 |
$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.9 1994-01-16 03:40:14 claus Exp $
|
1
|
28 |
'!
|
|
29 |
|
10
|
30 |
!Collection class methodsFor:'initialization'!
|
|
31 |
|
|
32 |
initialize
|
|
33 |
"setup the signal"
|
|
34 |
|
|
35 |
InvalidKeySignal := (Signal new) mayProceed:true.
|
|
36 |
InvalidKeySignal notifierString:'invalid key'.
|
|
37 |
! !
|
|
38 |
|
|
39 |
!Collection class methodsFor:'signal access'!
|
|
40 |
|
|
41 |
invalidKeySignal
|
|
42 |
"return the signal used to report bad key usage"
|
|
43 |
|
|
44 |
^ InvalidKeySignal
|
|
45 |
! !
|
|
46 |
|
1
|
47 |
!Collection class methodsFor:'instance creation'!
|
|
48 |
|
|
49 |
with:anObject
|
|
50 |
"return a new Collection with one element:anObject"
|
|
51 |
|
|
52 |
|newCollection|
|
|
53 |
|
|
54 |
newCollection := self new.
|
|
55 |
newCollection add:anObject.
|
|
56 |
^ newCollection
|
|
57 |
!
|
|
58 |
|
|
59 |
with:firstObject with:secondObject
|
|
60 |
"return a new Collection with two elements:firstObject and secondObject"
|
|
61 |
|
|
62 |
|newCollection|
|
|
63 |
|
|
64 |
newCollection := self new.
|
|
65 |
newCollection add:firstObject.
|
|
66 |
newCollection add:secondObject.
|
|
67 |
^ newCollection
|
|
68 |
!
|
|
69 |
|
|
70 |
with:firstObject with:secondObject with:thirdObject
|
|
71 |
"return a new Collection with three elements"
|
|
72 |
|
|
73 |
|newCollection|
|
|
74 |
|
|
75 |
newCollection := self new.
|
|
76 |
newCollection add:firstObject.
|
|
77 |
newCollection add:secondObject.
|
|
78 |
newCollection add:thirdObject.
|
|
79 |
^ newCollection
|
|
80 |
!
|
|
81 |
|
|
82 |
with:firstObject with:secondObject with:thirdObject with:fourthObject
|
|
83 |
"return a new Collection with four elements"
|
|
84 |
|
|
85 |
|newCollection|
|
|
86 |
|
|
87 |
newCollection := self new.
|
|
88 |
newCollection add:firstObject.
|
|
89 |
newCollection add:secondObject.
|
|
90 |
newCollection add:thirdObject.
|
|
91 |
newCollection add:fourthObject.
|
|
92 |
^ newCollection
|
|
93 |
!
|
|
94 |
|
|
95 |
new:size withAll:element
|
|
96 |
"return a new COllection of size, where all elements are
|
|
97 |
initialized to element"
|
|
98 |
|
|
99 |
|newCollection|
|
|
100 |
|
|
101 |
newCollection := self new:size.
|
|
102 |
size timesRepeat:[newCollection add:element]
|
|
103 |
!
|
|
104 |
|
|
105 |
withAll:aCollection
|
|
106 |
"return a new Collection with all elements taken from the argument,
|
|
107 |
aCollection"
|
|
108 |
|
|
109 |
|newCollection|
|
|
110 |
|
|
111 |
newCollection := self new.
|
|
112 |
newCollection addAll:aCollection.
|
|
113 |
^newCollection
|
|
114 |
! !
|
|
115 |
|
|
116 |
!Collection methodsFor:'error handling'!
|
|
117 |
|
|
118 |
errorNotKeyed
|
|
119 |
"report an error that keyed access methods are not allowed"
|
|
120 |
|
|
121 |
self error:(self class name, 's do not respond to keyed accessing messages')
|
10
|
122 |
!
|
|
123 |
|
|
124 |
errorInvalidKey
|
|
125 |
"report an error that the given key was invalid"
|
|
126 |
|
|
127 |
InvalidKeySignal raise
|
1
|
128 |
! !
|
|
129 |
|
|
130 |
!Collection methodsFor:'accessing'!
|
|
131 |
|
|
132 |
anElement
|
|
133 |
"return any element from the collection"
|
|
134 |
|
|
135 |
self do: [:each | ^ each].
|
|
136 |
^ nil
|
|
137 |
! !
|
|
138 |
|
|
139 |
!Collection methodsFor:'adding & removing'!
|
|
140 |
|
|
141 |
add:anObject
|
|
142 |
"add the argument, anObject to the receiver"
|
|
143 |
|
|
144 |
^ self subclassResponsibility
|
|
145 |
!
|
|
146 |
|
|
147 |
addLast:anObject
|
|
148 |
"add the argument, anObject to the receiver"
|
|
149 |
|
|
150 |
^ self add:anObject
|
|
151 |
!
|
|
152 |
|
12
|
153 |
addFirst:anObject
|
|
154 |
"add the argument, anObject to the receiver"
|
|
155 |
|
|
156 |
^ self subclassResponsibility
|
|
157 |
!
|
|
158 |
|
1
|
159 |
addAll:aCollection
|
|
160 |
"add all elements of the argument, aCollection to the receiver"
|
|
161 |
|
|
162 |
aCollection do:[:element |
|
|
163 |
self add:element
|
|
164 |
].
|
|
165 |
^ aCollection
|
|
166 |
!
|
|
167 |
|
12
|
168 |
addAllLast:aCollection
|
|
169 |
"add all elements of the argument, aCollection to the receiver"
|
|
170 |
|
|
171 |
^ self addAll:aCollection
|
|
172 |
!
|
|
173 |
|
|
174 |
addAllFirst:aCollection
|
|
175 |
"insert all elements of the argument, aCollection at the beginning
|
|
176 |
of the receiver"
|
|
177 |
|
|
178 |
aCollection reverseDo:[:element |
|
|
179 |
self addFirst: element
|
|
180 |
].
|
|
181 |
^ aCollection
|
|
182 |
|
|
183 |
"
|
|
184 |
|c|
|
|
185 |
c := #(4 3 2 1) asOrderedCollection.
|
|
186 |
c addAllFirst:#(9 8 7 6 5)
|
|
187 |
"
|
|
188 |
!
|
|
189 |
|
1
|
190 |
remove:anObject ifAbsent:exceptionBlock
|
|
191 |
"remove the argument, anObject from the receiver - if it was not
|
|
192 |
in the collection returns the the value of the exceptionBlock"
|
|
193 |
|
|
194 |
^ self subclassResponsibility
|
|
195 |
!
|
|
196 |
|
|
197 |
remove:anObject
|
|
198 |
"remove the argument, anObject from the receiver"
|
|
199 |
|
|
200 |
self remove:anObject ifAbsent:[self errorNotFound]
|
|
201 |
!
|
|
202 |
|
|
203 |
removeAll:aCollection
|
|
204 |
"remove all elements of the argument, aCollection from the receiver"
|
|
205 |
|
|
206 |
aCollection do:[:element | self remove:element].
|
|
207 |
^ aCollection
|
|
208 |
! !
|
|
209 |
|
|
210 |
!Collection methodsFor:'growing'!
|
|
211 |
|
|
212 |
growSize
|
|
213 |
"return a suitable size increment for growing"
|
|
214 |
|
|
215 |
^ self size max:2
|
|
216 |
!
|
|
217 |
|
|
218 |
grow
|
|
219 |
"make the receiver larger"
|
|
220 |
|
|
221 |
self grow:(self size + self growSize)
|
|
222 |
!
|
|
223 |
|
|
224 |
grow:howBig
|
|
225 |
"change the receivers size"
|
|
226 |
|
|
227 |
^ self subclassResponsibility
|
|
228 |
! !
|
|
229 |
|
|
230 |
!Collection methodsFor:'testing'!
|
|
231 |
|
|
232 |
isEmpty
|
|
233 |
"return true, if the receiver is empty"
|
|
234 |
|
|
235 |
^ self size == 0
|
|
236 |
!
|
|
237 |
|
|
238 |
includes:anElement
|
|
239 |
"return true, if the argument, anObject is in the list"
|
|
240 |
|
|
241 |
self do:[:element |
|
|
242 |
(anElement = element) ifTrue:[^ true].
|
|
243 |
].
|
|
244 |
^ false
|
|
245 |
!
|
|
246 |
|
|
247 |
includesAll:aCollection
|
|
248 |
"return true, if the the receiver includes all elements of
|
|
249 |
the argument, aCollection; false if any is missing"
|
|
250 |
|
|
251 |
aCollection do:[:element |
|
|
252 |
(self includes:element) ifFalse:[^ false].
|
|
253 |
].
|
|
254 |
^ true
|
|
255 |
!
|
|
256 |
|
2
|
257 |
includesAny:aCollection
|
|
258 |
"return true, if the the receiver includes any elements of
|
|
259 |
the argument, aCollection; false if it includes none"
|
|
260 |
|
|
261 |
aCollection do:[:element |
|
|
262 |
(self includes:element) ifTrue:[^ true].
|
|
263 |
].
|
|
264 |
^ false
|
|
265 |
!
|
|
266 |
|
1
|
267 |
occurrencesOf:anElement
|
|
268 |
"return the number of occurrences of the argument, anElement in
|
|
269 |
the receiver"
|
|
270 |
|
|
271 |
|count "<SmallInteger>" |
|
|
272 |
|
|
273 |
count := 0.
|
|
274 |
self do:[:element |
|
|
275 |
(anElement = element) ifTrue:[
|
|
276 |
count := count + 1
|
|
277 |
].
|
|
278 |
].
|
|
279 |
^ count
|
|
280 |
!
|
|
281 |
|
|
282 |
size
|
|
283 |
"return the number of elements in the receiver"
|
|
284 |
|
|
285 |
|count "<SmallInteger>" |
|
|
286 |
|
|
287 |
count := 0.
|
|
288 |
self do:[:element |
|
|
289 |
count := count + 1
|
|
290 |
].
|
|
291 |
^ count
|
|
292 |
! !
|
|
293 |
|
|
294 |
!Collection methodsFor:'enumerating'!
|
|
295 |
|
|
296 |
do:aBlock
|
|
297 |
"evaluate the argument, aBlock for each element"
|
|
298 |
|
|
299 |
^ self subclassResponsibility
|
|
300 |
!
|
|
301 |
|
3
|
302 |
keysAndValuesDo:aTwoArgBlock
|
|
303 |
"evaluate the argument, aBlock for every element in the collection,
|
|
304 |
passing both index and element as arguments."
|
|
305 |
|
|
306 |
^ self errorNotKeyed
|
|
307 |
!
|
|
308 |
|
1
|
309 |
collect:aBlock
|
|
310 |
"for each element in the receiver, evaluate the argument, aBlock
|
|
311 |
and return a new collection with the results"
|
|
312 |
|
|
313 |
|newCollection|
|
|
314 |
|
|
315 |
newCollection := self species new.
|
|
316 |
self do:[:each | newCollection add:(aBlock value:each)].
|
|
317 |
^ newCollection
|
|
318 |
!
|
|
319 |
|
|
320 |
detect:aBlock
|
|
321 |
"evaluate the argument, aBlock for each element in the receiver until
|
|
322 |
the block returns true; in this case return the element which caused
|
|
323 |
the true evaluation.
|
|
324 |
If none of the evaluations return true, report an error"
|
|
325 |
|
|
326 |
^ self detect:aBlock ifNone:[self errorNotFound]
|
|
327 |
!
|
|
328 |
|
|
329 |
detect:aBlock ifNone:exceptionBlock
|
|
330 |
"evaluate the argument, aBlock for each element in the receiver until
|
|
331 |
the block returns true; in this case return the element which caused
|
|
332 |
the true evaluation.
|
|
333 |
If none of the evaluations returns true, return the result of the
|
|
334 |
evaluation of the exceptionBlock"
|
|
335 |
|
|
336 |
self do:[:each |
|
|
337 |
(aBlock value:each) ifTrue:[^ each].
|
|
338 |
].
|
|
339 |
^ exceptionBlock value
|
|
340 |
!
|
|
341 |
|
|
342 |
inject:thisValue into:binaryBlock
|
|
343 |
|nextValue|
|
|
344 |
|
|
345 |
nextValue := thisValue.
|
|
346 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each].
|
|
347 |
^ nextValue
|
|
348 |
!
|
|
349 |
|
|
350 |
reject:aBlock
|
|
351 |
"return a new collection with all elements from the receiver, for which
|
|
352 |
the argument aBlock evaluates to false"
|
|
353 |
|
|
354 |
^ self select:[:element | (aBlock value:element) == false]
|
|
355 |
!
|
|
356 |
|
|
357 |
select:aBlock
|
|
358 |
"return a new collection with all elements from the receiver, for which
|
|
359 |
the argument aBlock evaluates to true"
|
|
360 |
|
|
361 |
|newCollection|
|
|
362 |
|
|
363 |
newCollection := self species new.
|
|
364 |
self do:[:each |
|
|
365 |
(aBlock value:each) ifTrue:[newCollection add:each].
|
|
366 |
].
|
|
367 |
^ newCollection
|
12
|
368 |
!
|
|
369 |
|
|
370 |
addAllTo:aCollection
|
|
371 |
"add all elements of the receiver, to aCollection.
|
|
372 |
Return aCollection."
|
|
373 |
|
|
374 |
self do:[:each | aCollection add:each].
|
|
375 |
^ aCollection
|
1
|
376 |
! !
|
|
377 |
|
|
378 |
!Collection methodsFor:'converting'!
|
|
379 |
|
|
380 |
asArray
|
|
381 |
"return a new Array with the collections elements"
|
|
382 |
|
|
383 |
|anArray
|
|
384 |
index "<SmallInteger>" |
|
|
385 |
|
|
386 |
anArray := Array new:(self size).
|
|
387 |
index := 1.
|
|
388 |
self do:[:each |
|
|
389 |
anArray at:index put:each.
|
|
390 |
index := index + 1
|
|
391 |
].
|
|
392 |
^ anArray
|
|
393 |
!
|
|
394 |
|
|
395 |
asByteArray
|
34
|
396 |
"return a new ByteArray with the collections elements
|
|
397 |
(which must convert to integers in the range 0..255)."
|
1
|
398 |
|
|
399 |
|aByteArray
|
|
400 |
index "<SmallInteger>" |
|
|
401 |
|
|
402 |
aByteArray := ByteArray new:(self size).
|
|
403 |
index := 1.
|
|
404 |
self do:[:each |
|
|
405 |
aByteArray at:index put:each asInteger.
|
|
406 |
index := index + 1
|
|
407 |
].
|
|
408 |
^ aByteArray
|
|
409 |
!
|
|
410 |
|
|
411 |
asString
|
|
412 |
"return a String with the collections elements
|
34
|
413 |
(which must convert to characters)"
|
1
|
414 |
|
|
415 |
|aString
|
|
416 |
index "<SmallInteger>" |
|
|
417 |
|
|
418 |
aString := String new:(self size).
|
|
419 |
index := 1.
|
|
420 |
self do:[:each |
|
|
421 |
aString at:index put:each asCharacter.
|
|
422 |
index := index + 1
|
|
423 |
].
|
|
424 |
^ aString
|
|
425 |
!
|
|
426 |
|
|
427 |
asText
|
|
428 |
"return a new Text-object with the elements printstings"
|
|
429 |
|
34
|
430 |
|aText|
|
1
|
431 |
|
34
|
432 |
aText := Text new.
|
1
|
433 |
self do:[:each |
|
34
|
434 |
aText add:(each printString).
|
1
|
435 |
].
|
|
436 |
^ aText
|
|
437 |
!
|
|
438 |
|
|
439 |
asBag
|
|
440 |
"return a new Bag with the receiver collections elements"
|
|
441 |
|
12
|
442 |
^ self addAllTo:(Bag new)
|
1
|
443 |
!
|
|
444 |
|
|
445 |
asOrderedCollection
|
|
446 |
"return a new OrderedCollection with the receiver collections elements"
|
|
447 |
|
12
|
448 |
^ self addAllTo:(OrderedCollection new:self size)
|
1
|
449 |
!
|
|
450 |
|
|
451 |
asSet
|
|
452 |
"return a new Set with the receiver collections elements"
|
|
453 |
|
12
|
454 |
^ self addAllTo:(Set new:self size)
|
|
455 |
!
|
1
|
456 |
|
12
|
457 |
asIdentitySet
|
|
458 |
"return a new IdentitySet with the receiver collections elements"
|
|
459 |
|
|
460 |
^ self addAllTo:(IdentitySet new:self size)
|
1
|
461 |
!
|
|
462 |
|
|
463 |
asSortedCollection
|
|
464 |
"return a new SortedCollection with the receiver collections elements"
|
|
465 |
|
|
466 |
|aSortedCollection|
|
|
467 |
|
|
468 |
aSortedCollection := SortedCollection new:self size.
|
|
469 |
aSortedCollection addAll:self.
|
|
470 |
^ aSortedCollection
|
|
471 |
!
|
|
472 |
|
|
473 |
asSortedCollection:sortBlock
|
|
474 |
"return a new SortedCollection with the receiver collections elements,
|
|
475 |
using sortBlock for comparing"
|
|
476 |
|
|
477 |
|aSortedCollection|
|
|
478 |
|
|
479 |
aSortedCollection := SortedCollection sortBlock:sortBlock.
|
|
480 |
aSortedCollection addAll:self.
|
|
481 |
^ aSortedCollection
|
2
|
482 |
!
|
|
483 |
|
|
484 |
writeStream
|
|
485 |
"return a stream for writing onto the receiver"
|
|
486 |
|
|
487 |
^ WriteStream on:self
|
|
488 |
!
|
|
489 |
|
|
490 |
readStream
|
|
491 |
"return a stream for reading from the receiver"
|
|
492 |
|
|
493 |
^ ReadStream on:self
|
1
|
494 |
! !
|
|
495 |
|
|
496 |
!Collection methodsFor:'printing & storing'!
|
|
497 |
|
|
498 |
maxPrint
|
|
499 |
^ 5000
|
|
500 |
!
|
|
501 |
|
12
|
502 |
printOrDisplayStringUsing:aSelector
|
|
503 |
"common code for printString and displayString; they only differ in
|
|
504 |
the print-message sent to the elements"
|
1
|
505 |
|
|
506 |
|thisString buffer count string noneYet total|
|
|
507 |
|
25
|
508 |
thisContext isRecursive ifTrue:[
|
|
509 |
Transcript showCr:'Error: print/storeString of self referencing collection.'.
|
|
510 |
^ '#("recursive")'
|
|
511 |
].
|
|
512 |
|
1
|
513 |
string := (self class name) , '('.
|
|
514 |
noneYet := true.
|
|
515 |
buffer := ''.
|
|
516 |
count := 0.
|
|
517 |
total := 0.
|
|
518 |
self do: [:element |
|
12
|
519 |
thisString := element perform:aSelector.
|
1
|
520 |
noneYet ifTrue:[
|
|
521 |
noneYet := false.
|
|
522 |
buffer := buffer , thisString
|
|
523 |
] ifFalse:[
|
|
524 |
buffer := buffer , (' ' , thisString)
|
|
525 |
].
|
|
526 |
count := count + 1.
|
|
527 |
(count == 20) ifTrue:[
|
|
528 |
string := string , buffer.
|
|
529 |
buffer := ''.
|
|
530 |
count := 0
|
|
531 |
].
|
|
532 |
total := total + 1.
|
|
533 |
(total > 5000) ifTrue:[
|
|
534 |
string := string , buffer , '... )'.
|
|
535 |
^string
|
|
536 |
]
|
|
537 |
].
|
|
538 |
string := string , buffer , ')'.
|
|
539 |
^string
|
|
540 |
!
|
|
541 |
|
12
|
542 |
printString
|
|
543 |
"return a printed representation of the receiver"
|
1
|
544 |
|
12
|
545 |
^ self printOrDisplayStringUsing:#printString
|
|
546 |
!
|
|
547 |
|
|
548 |
displayString
|
|
549 |
"return a printed representation of the receiver for display in inspectors etc."
|
|
550 |
|
|
551 |
^ self printOrDisplayStringUsing:#displayString
|
1
|
552 |
!
|
|
553 |
|
|
554 |
printOn:aStream
|
44
|
555 |
|limit firstOne noMore s|
|
1
|
556 |
|
44
|
557 |
thisContext isRecursive ifTrue:[
|
|
558 |
Transcript showCr:'Error: printOn: of self referencing collection.'.
|
|
559 |
aStream nextPutAll:'#("recursive")'.
|
|
560 |
^ self
|
|
561 |
].
|
|
562 |
|
|
563 |
limit := self maxPrint.
|
1
|
564 |
aStream nextPutAll:self class name.
|
|
565 |
aStream nextPut:$(.
|
|
566 |
firstOne := true.
|
|
567 |
noMore := false.
|
|
568 |
self do:[:element |
|
|
569 |
noMore ifFalse:[
|
|
570 |
firstOne ifFalse:[
|
44
|
571 |
aStream space
|
1
|
572 |
] ifTrue:[
|
|
573 |
firstOne := false
|
|
574 |
].
|
44
|
575 |
(limit <= 0) ifTrue:[
|
1
|
576 |
aStream nextPutAll:'...etc...)'.
|
|
577 |
noMore := true
|
|
578 |
] ifFalse:[
|
44
|
579 |
s := element printString.
|
|
580 |
aStream nextPutAll:s.
|
|
581 |
limit := limit - s size
|
1
|
582 |
]
|
|
583 |
].
|
|
584 |
].
|
|
585 |
aStream nextPut:$)
|
44
|
586 |
|
|
587 |
"#(1 2 3 'hello' $a) printOn:Transcript"
|
|
588 |
"(Dictionary new at:#hello put:'world'; yourself) printOn:Transcript"
|
|
589 |
|
1
|
590 |
!
|
|
591 |
|
|
592 |
storeOn:aStream
|
|
593 |
"output a printed representation (which can be re-read)
|
|
594 |
onto the argument aStream"
|
|
595 |
|
|
596 |
|isEmpty|
|
|
597 |
|
44
|
598 |
thisContext isRecursive ifTrue:[
|
|
599 |
Transcript showCr:'Error: printOn: of self referencing collection.'.
|
|
600 |
aStream nextPutAll:'#("recursive")'.
|
|
601 |
^ self
|
|
602 |
].
|
|
603 |
|
|
604 |
aStream nextPutAll:'('.
|
1
|
605 |
aStream nextPutAll:(self class name).
|
44
|
606 |
aStream nextPutAll:' new'.
|
1
|
607 |
isEmpty := true.
|
|
608 |
self do:[:element |
|
44
|
609 |
aStream nextPutAll:' add:'.
|
1
|
610 |
element storeOn:aStream.
|
44
|
611 |
aStream nextPutAll:';'.
|
1
|
612 |
isEmpty := false
|
|
613 |
].
|
|
614 |
isEmpty ifFalse:[aStream nextPutAll:' yourself'].
|
|
615 |
aStream nextPut:$)
|
44
|
616 |
|
|
617 |
"OrderedCollection new storeOn:Transcript"
|
|
618 |
"(1 to:10) storeOn:Transcript"
|
|
619 |
"(Set new add:1; add:'hello'; yourself) storeOn:Transcript"
|
1
|
620 |
! !
|