MappedCollection.st
author claus
Mon, 10 Oct 1994 01:53:55 +0100
changeset 42 506596f9a1a8
parent 36 d046fe84ea67
child 48 18b9353c9d07
permissions -rw-r--r--
*** empty log message ***

"
 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.
"

Collection subclass:#MappedCollection
       instanceVariableNames:'domain map'
       classVariableNames:''
       poolDictionaries:''
       category:'Collections-Sequenceable'
!

MappedCollection comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

$Header: /cvs/stx/stx/libbasic2/MappedCollection.st,v 1.7 1994-10-10 00:53:54 claus Exp $
'!

!MappedCollection class methodsFor:'documentation'!

copyright
"
 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.
"
!

version
"
$Header: /cvs/stx/stx/libbasic2/MappedCollection.st,v 1.7 1994-10-10 00:53:54 claus Exp $
"
!

documentation
"
    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. 
"
! !

!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:'copying'!

postCopy
    domain := domain copy.
    map := map copy
! !

!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
! !