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