--- a/Collection.st Sat Dec 11 01:42:02 1993 +0100
+++ b/Collection.st Sat Dec 11 01:46:55 1993 +0100
@@ -24,7 +24,7 @@
Abstract superclass for all collections
-$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.5 1993-11-08 02:29:51 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.6 1993-12-11 00:45:11 claus Exp $
'!
!Collection class methodsFor:'initialization'!
@@ -150,6 +150,12 @@
^ self add:anObject
!
+addFirst:anObject
+ "add the argument, anObject to the receiver"
+
+ ^ self subclassResponsibility
+!
+
addAll:aCollection
"add all elements of the argument, aCollection to the receiver"
@@ -159,6 +165,28 @@
^ aCollection
!
+addAllLast:aCollection
+ "add all elements of the argument, aCollection to the receiver"
+
+ ^ self addAll:aCollection
+!
+
+addAllFirst:aCollection
+ "insert all elements of the argument, aCollection at the beginning
+ of the receiver"
+
+ aCollection reverseDo:[:element |
+ self addFirst: element
+ ].
+ ^ aCollection
+
+ "
+ |c|
+ c := #(4 3 2 1) asOrderedCollection.
+ c addAllFirst:#(9 8 7 6 5)
+ "
+!
+
remove:anObject ifAbsent:exceptionBlock
"remove the argument, anObject from the receiver - if it was not
in the collection returns the the value of the exceptionBlock"
@@ -337,6 +365,14 @@
(aBlock value:each) ifTrue:[newCollection add:each].
].
^ newCollection
+!
+
+addAllTo:aCollection
+ "add all elements of the receiver, to aCollection.
+ Return aCollection."
+
+ self do:[:each | aCollection add:each].
+ ^ aCollection
! !
!Collection methodsFor:'converting'!
@@ -405,31 +441,25 @@
asBag
"return a new Bag with the receiver collections elements"
- |aBag|
-
- aBag := Bag new.
- self do:[:each | aBag add:each].
- ^ aBag
+ ^ self addAllTo:(Bag new)
!
asOrderedCollection
"return a new OrderedCollection with the receiver collections elements"
- |anOrderedCollection|
-
- anOrderedCollection := OrderedCollection new:self size.
- self do:[:each | anOrderedCollection addLast:each].
- ^ anOrderedCollection
+ ^ self addAllTo:(OrderedCollection new:self size)
!
asSet
"return a new Set with the receiver collections elements"
- |aSet|
+ ^ self addAllTo:(Set new:self size)
+!
- aSet := Set new: self size.
- self do:[:each | aSet add:each].
- ^ aSet
+asIdentitySet
+ "return a new IdentitySet with the receiver collections elements"
+
+ ^ self addAllTo:(IdentitySet new:self size)
!
asSortedCollection
@@ -471,11 +501,9 @@
^ 5000
!
-printString
- "return the printString of a big collection can take a long time
- due to long temporary strings - I use a buffer here collecting some
- elements to reduce the GC overhead ...
- "
+printOrDisplayStringUsing:aSelector
+ "common code for printString and displayString; they only differ in
+ the print-message sent to the elements"
|thisString buffer count string noneYet total|
@@ -485,7 +513,7 @@
count := 0.
total := 0.
self do: [:element |
- thisString := element printString.
+ thisString := element perform:aSelector.
noneYet ifTrue:[
noneYet := false.
buffer := buffer , thisString
@@ -508,41 +536,16 @@
^string
!
-displayString
- "return the printString of a big collection can take a long time
- due to long temporary strings - I use a buffer here collecting some
- elements to reduce the GC overhead ...
- "
-
- |thisString buffer count string noneYet total|
+printString
+ "return a printed representation of the receiver"
- string := (self class name) , '('.
- noneYet := true.
- buffer := ''.
- count := 0.
- total := 0.
- self do: [:element |
- thisString := element displayString.
- noneYet ifTrue:[
- noneYet := false.
- buffer := buffer , thisString
- ] ifFalse:[
- buffer := buffer , (' ' , thisString)
- ].
- count := count + 1.
- (count == 20) ifTrue:[
- string := string , buffer.
- buffer := ''.
- count := 0
- ].
- total := total + 1.
- (total > 5000) ifTrue:[
- string := string , buffer , '... )'.
- ^string
- ]
- ].
- string := string , buffer , ')'.
- ^string
+ ^ self printOrDisplayStringUsing:#printString
+!
+
+displayString
+ "return a printed representation of the receiver for display in inspectors etc."
+
+ ^ self printOrDisplayStringUsing:#displayString
!
printOn:aStream