MappedCollection.st
changeset 1 85d662a8509f
child 2 07d9ee98e092
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MappedCollection.st	Mon Oct 04 11:32:27 1993 +0100
@@ -0,0 +1,119 @@
+"
+ COPYRIGHT (c) 1993 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.
+"
+
+Object subclass:#MappedCollection
+       instanceVariableNames:'domain map'
+       classVariableNames:''
+       poolDictionaries:''
+       category:'Collections-Ordered'
+!
+
+MappedCollection comment:'
+
+COPYRIGHT (c) 1993 by Claus Gittinger
+              All Rights Reserved
+
+MappedCollections represent collections of objects that are indirectly indexed by names.
+There are really two collections involved: domain and a map.  The map maps
+between external names and indices into domain, which contains the
+real association.  In order to work properly, the domain and map objects must
+be instances of a subclass of SequenceableCollection or Dictionary. 
+
+
+%W% %E%
+'!
+
+!MappedCollection class methodsFor:'instance creation'!
+
+collection:domainCollection map:mapCollection
+    "return a new MappedCollection"
+
+    ^ super new setCollection:domainCollection map:mapCollection
+!
+
+new
+    "report an error; mappedCollections may not be created using new"
+
+    self error:'use collection:map: to create a MappedCollection'
+! !
+
+!MappedCollection methodsFor:'accessing'!
+
+at:key
+    ^ domain at:(map at:key)
+!
+
+at:key put:anObject
+    ^ domain at:(map at:key) put:anObject
+!
+
+contents
+    |contents|
+    contents := Bag new.
+    map do:[:key | contents add:domain at:key].
+    ^ contents
+!
+
+add:anObject
+    self shouldNotImplement
+! !
+
+!MappedCollection methodsFor:'testing'!
+
+size
+    "return the number of elements in the receiver"
+
+    ^ map size
+! !
+
+!MappedCollection methodsFor:'enumerating'!
+
+do:aBlock
+    "evaluate the argument, aBlock for each element"
+
+    map do:[:mapValue | aBlock value:(domain at:mapValue)].
+!
+
+collect:aBlock
+    "for each element in the receiver, evaluate the argument, aBlock
+     and return a new collection with the results"
+
+    |newCollection|
+
+    newCollection := self species new:self size.
+    self do:[:each | newCollection add:(aBlock value:each)].
+    ^ newCollection
+!
+
+select:aBlock
+    "return a new collection with all elements from the receiver, for which
+     the argument aBlock evaluates to true"
+
+    |newCollection|
+
+    newCollection := self species new:self size.
+    self do:[:each |
+        (aBlock value:each) ifTrue:[newCollection add:each].
+    ].
+    ^ newCollection
+! !
+
+!MappedCollection methodsFor:'private'!
+
+setCollection:domainCollection map:mapCollection
+    domain := domainCollection.
+    map := mapCollection
+!
+
+species
+    ^ domain species
+! !