ArrayedCollection.st
changeset 1 a27a279701f8
child 2 6526dde5f3ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ArrayedCollection.st	Fri Jul 16 11:39:45 1993 +0200
@@ -0,0 +1,181 @@
+"
+ COPYRIGHT (c) 1989-92 by Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+SequenceableCollection subclass:#ArrayedCollection
+       instanceVariableNames:''
+       classVariableNames:''
+       poolDictionaries:''
+       category:'Collections-Abstract'
+!
+
+ArrayedCollection comment:'
+
+COPYRIGHT (c) 1989-92 by Claus Gittinger
+             All Rights Reserved
+
+ArrayedCollections are collections where the elements can be accessed via an integer index.
+
+%W% %E%
+written spring 89 by claus
+'!
+
+!ArrayedCollection class methodsFor:'instance creation'!
+
+with:element
+    "return a new SequenceableCollection with one element:anObject"
+
+    |newCollection|
+
+    newCollection := self new:1.
+    newCollection at:1 put:element.
+    ^newCollection
+!
+
+with:first with:second
+    "return a new SequenceableCollection with two elements"
+
+    |newCollection|
+
+    newCollection := self new:2.
+    newCollection at:1 put:first.
+    newCollection at:2 put:second.
+    ^newCollection
+!
+
+with:first with:second with:third
+    "return a new SequenceableCollection with three elements"
+
+    |newCollection|
+
+    newCollection := self new:3.
+    newCollection at:1 put:first.
+    newCollection at:2 put:second.
+    newCollection at:3 put:third.
+    ^newCollection
+!
+
+with:first with:second with:third with:forth
+    "return a new SequenceableCollection with four elements"
+
+    |newCollection|
+
+    newCollection := self new:4.
+    newCollection at:1 put:first.
+    newCollection at:2 put:second.
+    newCollection at:3 put:third.
+    newCollection at:4 put:forth.
+    ^newCollection
+!
+
+with:one with:two with:three with:four with:five
+    "return a new SequenceableCollection with five elements"
+
+    |newCollection|
+
+    newCollection := self new:5.
+    newCollection at:1 put:one.
+    newCollection at:2 put:two.
+    newCollection at:3 put:three.
+    newCollection at:4 put:four.
+    newCollection at:5 put:five.
+    ^newCollection
+!
+
+withAll:aCollection
+    "return a new Collection with all elements taken from the argument,
+     aCollection"
+
+    |newCollection newSize
+     index "{ Class: SmallInteger }" |
+
+    newSize := aCollection size.
+    newCollection := self new:newSize.
+    (aCollection isKindOf:SequenceableCollection) ifTrue:[
+        "aCollection has indexed elements"
+        newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1
+    ] ifFalse:[
+        "must enumerate the elements"
+        index := 1.
+        aCollection do:[:element |
+            newCollection at:index put:element.
+            index := index + 1
+        ]
+    ].
+    ^ newCollection
+! !
+
+!ArrayedCollection methodsFor:'error handling'!
+
+indexMustBeInteger
+    "report an error that index must be Integer"
+
+    ^ self error:'index must be integer'
+!
+
+indexOutOfRange:theIndex
+    "report an error that index is out of range"
+
+    ^ self error:'index is out of range'
+!
+
+fixedSizeError
+    "report an error that size of the collection cannot be changed"
+
+    ^ self error:'cannot change size'
+! !
+
+!ArrayedCollection methodsFor:'accessing'!
+
+size
+    "return the ArrayedCollections size - redefined since SequenceableCollection
+     does it in a slow way"
+
+    ^ self basicSize
+!
+
+at:index
+    "return the index's element of the collection"
+
+    ^ self basicAt:index
+!
+
+at:index put:anObject
+    "put the argument as index's element into the collection"
+
+    ^ self basicAt:index put:anObject
+! !
+
+!ArrayedCollection methodsFor:'storing'!
+
+storeOn:aStream
+    "output a printed representation (which can be re-read)
+     onto the argument aStream"
+
+    |index "{ Class: SmallInteger }"|
+
+    aStream nextPutAll:'('.
+    aStream nextPutAll:self class name.
+    aStream nextPutAll:' new:'.
+    self size printOn:aStream.
+    aStream nextPutAll:')'.
+    index := 1.
+    self do:[:element |
+        aStream nextPutAll:' at:'.
+        index printOn:aStream.
+        aStream nextPutAll:' put:'.
+        element storeOn:aStream.
+        aStream nextPut:$;.
+        index := index + 1
+    ].
+    index > 1 ifTrue:[aStream nextPutAll:' yourself'].
+    aStream nextPut:$)
+! !