MappedCollection.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2013 13:23:52 +0200
changeset 3018 8abd4a4fdf06
parent 2990 cf16ba07221c
child 3862 d8bdc6819c50
permissions -rw-r--r--
added isFixedSize query
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
claus
parents:
diff changeset
     1
"
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
     3
	      All Rights Reserved
1
claus
parents:
diff changeset
     4
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    10
 hereby transferred.
claus
parents:
diff changeset
    11
"
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    13
19
c25a529288cf *** empty log message ***
claus
parents: 2
diff changeset
    14
Collection subclass:#MappedCollection
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    15
	instanceVariableNames:'domain map'
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    16
	classVariableNames:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    17
	poolDictionaries:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    18
	category:'Collections-Sequenceable'
1
claus
parents:
diff changeset
    19
!
claus
parents:
diff changeset
    20
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    21
!MappedCollection class methodsFor:'documentation'!
1
claus
parents:
diff changeset
    22
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    23
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    24
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    25
 COPYRIGHT (c) 1993 by Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
    26
	      All Rights Reserved
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    27
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    28
 This software is furnished under a license and may be used
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    29
 only in accordance with the terms of that license and with the
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    31
 be provided or otherwise made available to, or used by, any
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    32
 other person.  No title to or ownership of the software is
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    33
 hereby transferred.
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    34
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    35
!
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    36
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    37
documentation
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    38
"
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    39
    MappedCollections represent collections of objects that are indirectly indexed by names.
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    40
    There are really two collections involved: domain and a map.  The map maps
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    41
    between external names and indices into domain, which contains the
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    42
    real association.  In order to work properly, the domain and map objects must
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    43
    be instances of a subclass of SequenceableCollection or Dictionary. 
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    44
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    45
    [author:]
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    46
        Claus Gittinger
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    47
"
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    48
!
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    49
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    50
examples
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    51
"
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    52
                                            [exBegin]
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    53
    |mapped dict|
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    54
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    55
    dict := Dictionary new
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    56
                at:'uno' put:1;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    57
                at:'due' put:2;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    58
                at:'tre' put:3;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    59
                at:'quattro' put:4;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    60
                yourself.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    61
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    62
    mapped := MappedCollection
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    63
                    collection:#(one two three four)
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    64
                    map:dict.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    65
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    66
    mapped at:'tre'.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    67
    mapped select:[:each| each ~= 'two']
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    68
"
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    69
! !
1
claus
parents:
diff changeset
    70
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    71
!MappedCollection class methodsFor:'instance creation'!
1
claus
parents:
diff changeset
    72
claus
parents:
diff changeset
    73
collection:domainCollection map:mapCollection
claus
parents:
diff changeset
    74
    "return a new MappedCollection"
claus
parents:
diff changeset
    75
claus
parents:
diff changeset
    76
    ^ super new setCollection:domainCollection map:mapCollection
claus
parents:
diff changeset
    77
!
claus
parents:
diff changeset
    78
claus
parents:
diff changeset
    79
new
claus
parents:
diff changeset
    80
    "report an error; mappedCollections may not be created using new"
claus
parents:
diff changeset
    81
claus
parents:
diff changeset
    82
    self error:'use collection:map: to create a MappedCollection'
claus
parents:
diff changeset
    83
! !
claus
parents:
diff changeset
    84
claus
parents:
diff changeset
    85
!MappedCollection methodsFor:'accessing'!
claus
parents:
diff changeset
    86
claus
parents:
diff changeset
    87
at:key
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    88
    "retrieve an element"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    89
1
claus
parents:
diff changeset
    90
    ^ domain at:(map at:key)
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    91
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    92
    "Modified: 2.8.1996 / 21:04:01 / cg"
1
claus
parents:
diff changeset
    93
!
claus
parents:
diff changeset
    94
claus
parents:
diff changeset
    95
at:key put:anObject
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    96
    "store an element"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    97
1
claus
parents:
diff changeset
    98
    ^ domain at:(map at:key) put:anObject
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    99
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   100
    "Modified: 2.8.1996 / 21:04:06 / cg"
1
claus
parents:
diff changeset
   101
!
claus
parents:
diff changeset
   102
claus
parents:
diff changeset
   103
contents
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   104
    "return the contents as a bag"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   105
1
claus
parents:
diff changeset
   106
    |contents|
2990
cf16ba07221c class: MappedCollection
Claus Gittinger <cg@exept.de>
parents: 2967
diff changeset
   107
1
claus
parents:
diff changeset
   108
    contents := Bag new.
49
ec5828a97593 *** empty log message ***
claus
parents: 48
diff changeset
   109
    map do:[:key | contents add:(domain at:key)].
1
claus
parents:
diff changeset
   110
    ^ contents
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   111
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   112
    "Modified: 2.8.1996 / 21:04:10 / cg"
1
claus
parents:
diff changeset
   113
! !
claus
parents:
diff changeset
   114
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   115
!MappedCollection methodsFor:'adding & removing'!
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   116
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   117
add:anObject
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   118
    "report an error; mappedCollections cannot add elements (without a key)"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   119
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   120
    self shouldNotImplement
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   121
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   122
    "Modified: 2.8.1996 / 21:03:57 / cg"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   123
! !
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   124
42
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   125
!MappedCollection methodsFor:'copying'!
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   126
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   127
postCopy
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   128
    domain := domain copy.
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   129
    map := map copy
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   130
! !
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   131
1
claus
parents:
diff changeset
   132
!MappedCollection methodsFor:'enumerating'!
claus
parents:
diff changeset
   133
claus
parents:
diff changeset
   134
collect:aBlock
claus
parents:
diff changeset
   135
    "for each element in the receiver, evaluate the argument, aBlock
claus
parents:
diff changeset
   136
     and return a new collection with the results"
claus
parents:
diff changeset
   137
claus
parents:
diff changeset
   138
    |newCollection|
claus
parents:
diff changeset
   139
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   140
    newCollection := self speciesForAdding new:self size.
1
claus
parents:
diff changeset
   141
    self do:[:each | newCollection add:(aBlock value:each)].
claus
parents:
diff changeset
   142
    ^ newCollection
claus
parents:
diff changeset
   143
!
claus
parents:
diff changeset
   144
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   145
do:aBlock
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   146
    "evaluate the argument, aBlock for each element"
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   147
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   148
    map do:[:mapValue | aBlock value:(domain at:mapValue)].
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   149
!
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   150
1
claus
parents:
diff changeset
   151
select:aBlock
claus
parents:
diff changeset
   152
    "return a new collection with all elements from the receiver, for which
claus
parents:
diff changeset
   153
     the argument aBlock evaluates to true"
claus
parents:
diff changeset
   154
claus
parents:
diff changeset
   155
    |newCollection|
claus
parents:
diff changeset
   156
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   157
    newCollection := self speciesForAdding new:self size.
1
claus
parents:
diff changeset
   158
    self do:[:each |
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   159
        (aBlock value:each) ifTrue:[newCollection add:each].
1
claus
parents:
diff changeset
   160
    ].
claus
parents:
diff changeset
   161
    ^ newCollection
claus
parents:
diff changeset
   162
! !
claus
parents:
diff changeset
   163
claus
parents:
diff changeset
   164
!MappedCollection methodsFor:'private'!
claus
parents:
diff changeset
   165
claus
parents:
diff changeset
   166
setCollection:domainCollection map:mapCollection
claus
parents:
diff changeset
   167
    domain := domainCollection.
claus
parents:
diff changeset
   168
    map := mapCollection
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   169
! !
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   170
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   171
!MappedCollection methodsFor:'queries'!
1
claus
parents:
diff changeset
   172
3018
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   173
isFixedSize
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   174
    "return true if the receiver cannot grow"
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   175
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   176
    ^ false
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   177
!
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   178
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   179
size
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   180
    "return the number of elements in the receiver"
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   181
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   182
    ^ map size
906
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   183
!
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   184
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   185
species
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   186
    ^ domain species
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   187
!
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   188
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   189
speciesForAdding
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   190
    ^ domain speciesForAdding
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   191
! !
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   192
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   193
!MappedCollection class methodsFor:'documentation'!
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   194
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   195
version
3018
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   196
    ^ '$Header: /cvs/stx/stx/libbasic2/MappedCollection.st,v 1.21 2013-06-25 11:23:52 cg Exp $'
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   197
! !
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   198