MappedCollection.st
author Claus Gittinger <cg@exept.de>
Fri, 26 Apr 2013 11:46:00 +0200
changeset 2990 cf16ba07221c
parent 2967 9ebb5e9a705a
child 3018 8abd4a4fdf06
permissions -rw-r--r--
class: MappedCollection comment/format in: #contents

"
 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.
"
"{ Package: 'stx:libbasic2' }"

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

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

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. 

    [author:]
        Claus Gittinger
"
!

examples
"
                                            [exBegin]
    |mapped dict|

    dict := Dictionary new
                at:'uno' put:1;
                at:'due' put:2;
                at:'tre' put:3;
                at:'quattro' put:4;
                yourself.

    mapped := MappedCollection
                    collection:#(one two three four)
                    map:dict.

    mapped at:'tre'.
    mapped select:[:each| each ~= 'two']
"
! !

!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
    "retrieve an element"

    ^ domain at:(map at:key)

    "Modified: 2.8.1996 / 21:04:01 / cg"
!

at:key put:anObject
    "store an element"

    ^ domain at:(map at:key) put:anObject

    "Modified: 2.8.1996 / 21:04:06 / cg"
!

contents
    "return the contents as a bag"

    |contents|

    contents := Bag new.
    map do:[:key | contents add:(domain at:key)].
    ^ contents

    "Modified: 2.8.1996 / 21:04:10 / cg"
! !

!MappedCollection methodsFor:'adding & removing'!

add:anObject
    "report an error; mappedCollections cannot add elements (without a key)"

    self shouldNotImplement

    "Modified: 2.8.1996 / 21:03:57 / cg"
! !

!MappedCollection methodsFor:'copying'!

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

!MappedCollection methodsFor:'enumerating'!

collect:aBlock
    "for each element in the receiver, evaluate the argument, aBlock
     and return a new collection with the results"

    |newCollection|

    newCollection := self speciesForAdding new:self size.
    self do:[:each | newCollection add:(aBlock value:each)].
    ^ newCollection
!

do:aBlock
    "evaluate the argument, aBlock for each element"

    map do:[:mapValue | aBlock value:(domain at:mapValue)].
!

select:aBlock
    "return a new collection with all elements from the receiver, for which
     the argument aBlock evaluates to true"

    |newCollection|

    newCollection := self speciesForAdding 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
! !

!MappedCollection methodsFor:'queries'!

size
    "return the number of elements in the receiver"

    ^ map size
!

species
    ^ domain species
!

speciesForAdding
    ^ domain speciesForAdding
! !

!MappedCollection class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/MappedCollection.st,v 1.20 2013-04-26 09:46:00 cg Exp $'
! !