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