1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
345
|
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 |
|
|
13 |
Collection subclass:#Bag
|
609
|
14 |
instanceVariableNames:'contents'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
1
|
18 |
!
|
|
19 |
|
54
|
20 |
!Bag class methodsFor:'documentation'!
|
|
21 |
|
88
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
345
|
25 |
All Rights Reserved
|
88
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
54
|
36 |
documentation
|
|
37 |
"
|
88
|
38 |
Bag implements collections whose elements are unordered and have no
|
154
|
39 |
external key. Elements may occur more than once in a bag. There is no defined
|
|
40 |
order within a bag.
|
345
|
41 |
The default implementation uses a dictionary to store each objects occurence
|
|
42 |
count, using the object itself as key (i.e. using = and hash for inclusion
|
|
43 |
tests).
|
399
|
44 |
|
345
|
45 |
There is also an instance creation variant (#identityNew:) creating a
|
|
46 |
bag which compares using #== and hashes using #identityHash.
|
399
|
47 |
(I'd say that an IdentityBag was a better thing to implement ...
|
|
48 |
... but for compatibility ... we do it here as well)
|
1
|
49 |
|
88
|
50 |
Instance variables:
|
1
|
51 |
|
345
|
52 |
contents <Dictionary> for each element, the number of occurrences
|
54
|
53 |
"
|
609
|
54 |
!
|
|
55 |
|
|
56 |
version
|
|
57 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.15 1995-11-23 01:24:57 cg Exp $'
|
54
|
58 |
! !
|
1
|
59 |
|
|
60 |
!Bag class methodsFor:'instance creation'!
|
|
61 |
|
345
|
62 |
equalityNew:size
|
|
63 |
"return a new empty Bag with initial space for size elements.
|
|
64 |
Elements will be compared using equality compare (i.e. #= not #== identity)."
|
1
|
65 |
|
|
66 |
^ super new initContents:size
|
345
|
67 |
!
|
|
68 |
|
|
69 |
identityNew:size
|
|
70 |
"return a new empty Bag with initial space for size elements.
|
|
71 |
Elements will be compared using identity compare (i.e. #== not #= equality)."
|
|
72 |
|
|
73 |
^ super new initContentsForIdentity:size
|
1
|
74 |
!
|
|
75 |
|
609
|
76 |
new
|
|
77 |
"return a new empty Bag which compares for equality (i.e. not identity)"
|
1
|
78 |
|
609
|
79 |
^ super new initContents
|
345
|
80 |
!
|
|
81 |
|
609
|
82 |
new:size
|
|
83 |
"return a new empty Bag with initial space for size elements.
|
|
84 |
Elements will be compared using equality compare (i.e. #= not #== identity)."
|
345
|
85 |
|
609
|
86 |
^ self equalityNew:size
|
1
|
87 |
! !
|
|
88 |
|
|
89 |
!Bag methodsFor:'accessing'!
|
|
90 |
|
|
91 |
at:index
|
|
92 |
"report an error: at: is not allowed for Bags"
|
|
93 |
|
|
94 |
^ self errorNotKeyed
|
|
95 |
!
|
|
96 |
|
|
97 |
at:index put:anObject
|
|
98 |
"report an error: at:put: is not allowed for Bags"
|
|
99 |
|
|
100 |
^ self errorNotKeyed
|
345
|
101 |
!
|
|
102 |
|
|
103 |
contents
|
|
104 |
"return the dictionary which associates occurrence-counts
|
|
105 |
to the bags elements."
|
|
106 |
|
|
107 |
^ contents
|
1
|
108 |
! !
|
|
109 |
|
|
110 |
!Bag methodsFor:'adding & removing'!
|
|
111 |
|
54
|
112 |
add:newObject
|
345
|
113 |
"add the argument, anObject to the receiver.
|
|
114 |
Returns the object."
|
1
|
115 |
|
54
|
116 |
|n|
|
|
117 |
|
|
118 |
n := contents at:newObject ifAbsent:[0].
|
|
119 |
contents at:newObject put:(n + 1).
|
|
120 |
^ newObject
|
1
|
121 |
!
|
|
122 |
|
|
123 |
add:newObject withOccurences:anInteger
|
345
|
124 |
"add the argument, anObject anInteger times to the receiver.
|
|
125 |
Returns the object."
|
1
|
126 |
|
54
|
127 |
|n|
|
|
128 |
|
|
129 |
n := contents at:newObject ifAbsent:[0].
|
|
130 |
contents at:newObject put:(n + anInteger).
|
1
|
131 |
^ newObject
|
|
132 |
!
|
|
133 |
|
|
134 |
remove:oldObject ifAbsent:anExceptionBlock
|
345
|
135 |
"Remove oldObject from the collection.
|
|
136 |
If it was not present, return the value of the exceptionBlock;
|
|
137 |
otherwise return the removed object."
|
1
|
138 |
|
|
139 |
|count|
|
|
140 |
|
54
|
141 |
count := contents at:oldObject ifAbsent:[0].
|
1
|
142 |
(count == 0) ifTrue:[^ anExceptionBlock value].
|
|
143 |
(count == 1) ifTrue:[
|
345
|
144 |
contents removeKey:oldObject
|
1
|
145 |
] ifFalse:[
|
345
|
146 |
contents at:oldObject put:(count - 1)
|
1
|
147 |
].
|
|
148 |
^ oldObject
|
345
|
149 |
!
|
|
150 |
|
|
151 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
|
|
152 |
"Remove all occurrences of oldObject from the collection.
|
|
153 |
If it was not present, return the value of the exceptionBlock;
|
|
154 |
otherwise return the number of removes."
|
|
155 |
|
|
156 |
|count|
|
|
157 |
|
|
158 |
count := contents at:oldObject ifAbsent:[0].
|
|
159 |
(count == 0) ifTrue:[^ anExceptionBlock value].
|
|
160 |
contents removeKey:oldObject.
|
|
161 |
^ oldObject
|
1
|
162 |
! !
|
|
163 |
|
609
|
164 |
!Bag methodsFor:'converting'!
|
|
165 |
|
|
166 |
asBag
|
|
167 |
"return the receiver as a bag"
|
|
168 |
|
|
169 |
"could be an instance of a subclass..."
|
|
170 |
self class == Bag ifTrue:[
|
|
171 |
^ self
|
|
172 |
].
|
|
173 |
^ super asBag
|
|
174 |
! !
|
|
175 |
|
|
176 |
!Bag methodsFor:'copying'!
|
|
177 |
|
|
178 |
postCopy
|
|
179 |
"must copy the contents as well"
|
|
180 |
|
|
181 |
contents := contents copy
|
|
182 |
! !
|
|
183 |
|
1
|
184 |
!Bag methodsFor:'enumerating'!
|
|
185 |
|
|
186 |
do:aBlock
|
345
|
187 |
"evaluate the block for all elements in the collection."
|
1
|
188 |
|
54
|
189 |
contents keysAndValuesDo:[:key :value|
|
345
|
190 |
value timesRepeat:[
|
|
191 |
aBlock value:key
|
|
192 |
]
|
1
|
193 |
]
|
345
|
194 |
!
|
|
195 |
|
|
196 |
valuesAndCountsDo:aTwoArgBlock
|
|
197 |
"evaluate the block for all distinct elements in the collection,
|
|
198 |
passing both the element and the occurence count as arguments."
|
|
199 |
|
|
200 |
^ contents keysAndValuesDo:aTwoArgBlock
|
1
|
201 |
! !
|
609
|
202 |
|
|
203 |
!Bag methodsFor:'private'!
|
|
204 |
|
|
205 |
initContents
|
|
206 |
"set the contents to be an empty Dictionary"
|
|
207 |
|
|
208 |
contents := Dictionary new
|
|
209 |
!
|
|
210 |
|
|
211 |
initContents:size
|
|
212 |
"set the contents to be an empty Dictionary with initial size"
|
|
213 |
|
|
214 |
contents := Dictionary new:size
|
|
215 |
!
|
|
216 |
|
|
217 |
initContentsForIdentity:size
|
|
218 |
"set the contents to be an empty IdentityDictionary with initial size"
|
|
219 |
|
|
220 |
contents := IdentityDictionary new:size
|
|
221 |
! !
|
|
222 |
|
|
223 |
!Bag methodsFor:'testing'!
|
|
224 |
|
|
225 |
includes:anObject
|
|
226 |
"return true, if anObject is in the receiver"
|
|
227 |
|
|
228 |
^ contents includesKey:anObject
|
|
229 |
!
|
|
230 |
|
|
231 |
occurrencesOf:anObject
|
|
232 |
"return how many times anObject is in the receiver"
|
|
233 |
|
|
234 |
^ contents at:anObject ifAbsent:[0]
|
|
235 |
!
|
|
236 |
|
|
237 |
size
|
|
238 |
"return the number of bag elements"
|
|
239 |
|
|
240 |
|count|
|
|
241 |
|
|
242 |
count := 0.
|
|
243 |
contents do:[:element | count := count + element].
|
|
244 |
^ count
|
|
245 |
! !
|
|
246 |
|