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