MappedCollection.st
changeset 1 85d662a8509f
child 2 07d9ee98e092
equal deleted inserted replaced
0:1cf8d1747859 1:85d662a8509f
       
     1 "
       
     2  COPYRIGHT (c) 1993 by Claus Gittinger
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 
       
    13 Object subclass:#MappedCollection
       
    14        instanceVariableNames:'domain map'
       
    15        classVariableNames:''
       
    16        poolDictionaries:''
       
    17        category:'Collections-Ordered'
       
    18 !
       
    19 
       
    20 MappedCollection comment:'
       
    21 
       
    22 COPYRIGHT (c) 1993 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 MappedCollections represent collections of objects that are indirectly indexed by names.
       
    26 There are really two collections involved: domain and a map.  The map maps
       
    27 between external names and indices into domain, which contains the
       
    28 real association.  In order to work properly, the domain and map objects must
       
    29 be instances of a subclass of SequenceableCollection or Dictionary. 
       
    30 
       
    31 
       
    32 %W% %E%
       
    33 '!
       
    34 
       
    35 !MappedCollection class methodsFor:'instance creation'!
       
    36 
       
    37 collection:domainCollection map:mapCollection
       
    38     "return a new MappedCollection"
       
    39 
       
    40     ^ super new setCollection:domainCollection map:mapCollection
       
    41 !
       
    42 
       
    43 new
       
    44     "report an error; mappedCollections may not be created using new"
       
    45 
       
    46     self error:'use collection:map: to create a MappedCollection'
       
    47 ! !
       
    48 
       
    49 !MappedCollection methodsFor:'accessing'!
       
    50 
       
    51 at:key
       
    52     ^ domain at:(map at:key)
       
    53 !
       
    54 
       
    55 at:key put:anObject
       
    56     ^ domain at:(map at:key) put:anObject
       
    57 !
       
    58 
       
    59 contents
       
    60     |contents|
       
    61     contents := Bag new.
       
    62     map do:[:key | contents add:domain at:key].
       
    63     ^ contents
       
    64 !
       
    65 
       
    66 add:anObject
       
    67     self shouldNotImplement
       
    68 ! !
       
    69 
       
    70 !MappedCollection methodsFor:'testing'!
       
    71 
       
    72 size
       
    73     "return the number of elements in the receiver"
       
    74 
       
    75     ^ map size
       
    76 ! !
       
    77 
       
    78 !MappedCollection methodsFor:'enumerating'!
       
    79 
       
    80 do:aBlock
       
    81     "evaluate the argument, aBlock for each element"
       
    82 
       
    83     map do:[:mapValue | aBlock value:(domain at:mapValue)].
       
    84 !
       
    85 
       
    86 collect:aBlock
       
    87     "for each element in the receiver, evaluate the argument, aBlock
       
    88      and return a new collection with the results"
       
    89 
       
    90     |newCollection|
       
    91 
       
    92     newCollection := self species new:self size.
       
    93     self do:[:each | newCollection add:(aBlock value:each)].
       
    94     ^ newCollection
       
    95 !
       
    96 
       
    97 select:aBlock
       
    98     "return a new collection with all elements from the receiver, for which
       
    99      the argument aBlock evaluates to true"
       
   100 
       
   101     |newCollection|
       
   102 
       
   103     newCollection := self species new:self size.
       
   104     self do:[:each |
       
   105         (aBlock value:each) ifTrue:[newCollection add:each].
       
   106     ].
       
   107     ^ newCollection
       
   108 ! !
       
   109 
       
   110 !MappedCollection methodsFor:'private'!
       
   111 
       
   112 setCollection:domainCollection map:mapCollection
       
   113     domain := domainCollection.
       
   114     map := mapCollection
       
   115 !
       
   116 
       
   117 species
       
   118     ^ domain species
       
   119 ! !