1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
48
|
3 |
All Rights Reserved
|
1
|
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 |
|
904
|
13 |
"{ Package: 'stx:libbasic2' }"
|
|
14 |
|
19
|
15 |
Collection subclass:#MappedCollection
|
254
|
16 |
instanceVariableNames:'domain map'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Collections-Sequenceable'
|
1
|
20 |
!
|
|
21 |
|
904
|
22 |
!MappedCollection class methodsFor:'documentation'!
|
1
|
23 |
|
31
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
48
|
27 |
All Rights Reserved
|
31
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
28
|
38 |
documentation
|
|
39 |
"
|
|
40 |
MappedCollections represent collections of objects that are indirectly indexed by names.
|
|
41 |
There are really two collections involved: domain and a map. The map maps
|
|
42 |
between external names and indices into domain, which contains the
|
|
43 |
real association. In order to work properly, the domain and map objects must
|
|
44 |
be instances of a subclass of SequenceableCollection or Dictionary.
|
254
|
45 |
|
|
46 |
[author:]
|
|
47 |
Claus Gittinger
|
28
|
48 |
"
|
|
49 |
! !
|
1
|
50 |
|
904
|
51 |
!MappedCollection class methodsFor:'instance creation'!
|
1
|
52 |
|
|
53 |
collection:domainCollection map:mapCollection
|
|
54 |
"return a new MappedCollection"
|
|
55 |
|
|
56 |
^ super new setCollection:domainCollection map:mapCollection
|
|
57 |
!
|
|
58 |
|
|
59 |
new
|
|
60 |
"report an error; mappedCollections may not be created using new"
|
|
61 |
|
|
62 |
self error:'use collection:map: to create a MappedCollection'
|
|
63 |
! !
|
|
64 |
|
|
65 |
!MappedCollection methodsFor:'accessing'!
|
|
66 |
|
|
67 |
at:key
|
426
|
68 |
"retrieve an element"
|
|
69 |
|
1
|
70 |
^ domain at:(map at:key)
|
426
|
71 |
|
|
72 |
"Modified: 2.8.1996 / 21:04:01 / cg"
|
1
|
73 |
!
|
|
74 |
|
|
75 |
at:key put:anObject
|
426
|
76 |
"store an element"
|
|
77 |
|
1
|
78 |
^ domain at:(map at:key) put:anObject
|
426
|
79 |
|
|
80 |
"Modified: 2.8.1996 / 21:04:06 / cg"
|
1
|
81 |
!
|
|
82 |
|
|
83 |
contents
|
426
|
84 |
"return the contents as a bag"
|
|
85 |
|
1
|
86 |
|contents|
|
|
87 |
contents := Bag new.
|
49
|
88 |
map do:[:key | contents add:(domain at:key)].
|
1
|
89 |
^ contents
|
426
|
90 |
|
|
91 |
"Modified: 2.8.1996 / 21:04:10 / cg"
|
1
|
92 |
! !
|
|
93 |
|
904
|
94 |
!MappedCollection methodsFor:'adding & removing'!
|
|
95 |
|
|
96 |
add:anObject
|
|
97 |
"report an error; mappedCollections cannot add elements (without a key)"
|
|
98 |
|
|
99 |
self shouldNotImplement
|
|
100 |
|
|
101 |
"Modified: 2.8.1996 / 21:03:57 / cg"
|
|
102 |
! !
|
|
103 |
|
42
|
104 |
!MappedCollection methodsFor:'copying'!
|
|
105 |
|
|
106 |
postCopy
|
|
107 |
domain := domain copy.
|
|
108 |
map := map copy
|
|
109 |
! !
|
|
110 |
|
1
|
111 |
!MappedCollection methodsFor:'enumerating'!
|
|
112 |
|
|
113 |
collect:aBlock
|
|
114 |
"for each element in the receiver, evaluate the argument, aBlock
|
|
115 |
and return a new collection with the results"
|
|
116 |
|
|
117 |
|newCollection|
|
|
118 |
|
|
119 |
newCollection := self species new:self size.
|
|
120 |
self do:[:each | newCollection add:(aBlock value:each)].
|
|
121 |
^ newCollection
|
|
122 |
!
|
|
123 |
|
123
|
124 |
do:aBlock
|
|
125 |
"evaluate the argument, aBlock for each element"
|
|
126 |
|
|
127 |
map do:[:mapValue | aBlock value:(domain at:mapValue)].
|
|
128 |
!
|
|
129 |
|
1
|
130 |
select:aBlock
|
|
131 |
"return a new collection with all elements from the receiver, for which
|
|
132 |
the argument aBlock evaluates to true"
|
|
133 |
|
|
134 |
|newCollection|
|
|
135 |
|
|
136 |
newCollection := self species new:self size.
|
|
137 |
self do:[:each |
|
48
|
138 |
(aBlock value:each) ifTrue:[newCollection add:each].
|
1
|
139 |
].
|
|
140 |
^ newCollection
|
|
141 |
! !
|
|
142 |
|
|
143 |
!MappedCollection methodsFor:'private'!
|
|
144 |
|
|
145 |
setCollection:domainCollection map:mapCollection
|
|
146 |
domain := domainCollection.
|
|
147 |
map := mapCollection
|
48
|
148 |
! !
|
|
149 |
|
|
150 |
!MappedCollection methodsFor:'queries'!
|
1
|
151 |
|
123
|
152 |
size
|
|
153 |
"return the number of elements in the receiver"
|
|
154 |
|
|
155 |
^ map size
|
906
|
156 |
!
|
|
157 |
|
|
158 |
species
|
|
159 |
^ domain species
|
123
|
160 |
! !
|
|
161 |
|
904
|
162 |
!MappedCollection class methodsFor:'documentation'!
|
131
|
163 |
|
|
164 |
version
|
906
|
165 |
^ '$Header: /cvs/stx/stx/libbasic2/MappedCollection.st,v 1.18 2000-08-22 13:57:10 cg Exp $'
|
131
|
166 |
! !
|