author | Stefan Vogel <sv@exept.de> |
Tue, 08 Nov 2005 12:08:47 +0100 | |
changeset 8973 | c715c59b633d |
parent 8873 | eecab5af2679 |
child 10835 | e23097df1639 |
permissions | -rw-r--r-- |
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 |
||
5375 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Collection subclass:#Bag |
1057 | 16 |
instanceVariableNames:'contents' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Unordered' |
|
1 | 20 |
! |
21 |
||
54 | 22 |
!Bag class methodsFor:'documentation'! |
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 27 |
All Rights Reserved |
88 | 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 |
||
54 | 38 |
documentation |
39 |
" |
|
88 | 40 |
Bag implements collections whose elements are unordered and have no |
154 | 41 |
external key. Elements may occur more than once in a bag. There is no defined |
8873 | 42 |
order within a bag. |
43 |
The default implementation uses a dictionary to store each objects occurrence |
|
44 |
count, using the object itself as key (i.e. using = and hash for inclusion |
|
345 | 45 |
tests). |
399 | 46 |
|
345 | 47 |
There is also an instance creation variant (#identityNew:) creating a |
48 |
bag which compares using #== and hashes using #identityHash. |
|
8873 | 49 |
(I'd say that an IdentityBag was a better thing to implement ... |
399 | 50 |
... but for compatibility ... we do it here as well) |
1 | 51 |
|
1283 | 52 |
[Instance variables:] |
53 |
||
8873 | 54 |
contents <Dictionary> for each element, the number of occurrences |
1283 | 55 |
|
1 | 56 |
|
1290 | 57 |
[author:] |
8873 | 58 |
Claus Gittinger |
1290 | 59 |
|
1283 | 60 |
[See also:] |
8873 | 61 |
Set IdentitySet |
62 |
Dictionary IdentityDictionary |
|
63 |
OrderedCollection Array |
|
54 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
67 |
!Bag class methodsFor:'instance creation'! |
|
68 |
||
345 | 69 |
equalityNew:size |
70 |
"return a new empty Bag with initial space for size elements. |
|
71 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 72 |
|
5558 | 73 |
^ self basicNew initContents:size |
345 | 74 |
! |
75 |
||
5375 | 76 |
identityNew |
77 |
"return a new empty Bag which compares for identity |
|
78 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
79 |
||
80 |
^ super new initContentsForIdentity |
|
81 |
! |
|
82 |
||
345 | 83 |
identityNew:size |
84 |
"return a new empty Bag with initial space for size elements. |
|
85 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
86 |
||
5558 | 87 |
^ self basicNew initContentsForIdentity:size |
1 | 88 |
! |
89 |
||
609 | 90 |
new |
91 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 92 |
|
5558 | 93 |
^ self basicNew initContents |
345 | 94 |
! |
95 |
||
609 | 96 |
new:size |
97 |
"return a new empty Bag with initial space for size elements. |
|
98 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 99 |
|
609 | 100 |
^ self equalityNew:size |
1 | 101 |
! ! |
102 |
||
7261 | 103 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 104 |
|
105 |
asAssociations |
|
106 |
^ contents |
|
107 |
! ! |
|
108 |
||
1 | 109 |
!Bag methodsFor:'accessing'! |
110 |
||
111 |
at:index |
|
112 |
"report an error: at: is not allowed for Bags" |
|
113 |
||
114 |
^ self errorNotKeyed |
|
115 |
! |
|
116 |
||
117 |
at:index put:anObject |
|
118 |
"report an error: at:put: is not allowed for Bags" |
|
119 |
||
120 |
^ self errorNotKeyed |
|
345 | 121 |
! |
122 |
||
123 |
contents |
|
124 |
"return the dictionary which associates occurrence-counts |
|
125 |
to the bags elements." |
|
126 |
||
127 |
^ contents |
|
1 | 128 |
! ! |
129 |
||
130 |
!Bag methodsFor:'adding & removing'! |
|
131 |
||
54 | 132 |
add:newObject |
345 | 133 |
"add the argument, anObject to the receiver. |
1057 | 134 |
Returns the object. |
135 |
||
136 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 137 |
Iterate over a copy to do this." |
1 | 138 |
|
54 | 139 |
|n| |
140 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
141 |
n := contents at:newObject ifAbsent:0. |
54 | 142 |
contents at:newObject put:(n + 1). |
143 |
^ newObject |
|
1057 | 144 |
|
145 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 146 |
! |
147 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
148 |
add:newObject withOccurrences:anInteger |
345 | 149 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 150 |
Returns the object. |
151 |
||
152 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 153 |
Iterate over a copy to do this." |
1 | 154 |
|
54 | 155 |
|n| |
156 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
157 |
n := contents at:newObject ifAbsent:0. |
54 | 158 |
contents at:newObject put:(n + anInteger). |
1 | 159 |
^ newObject |
1057 | 160 |
|
161 |
"Modified: 1.3.1996 / 21:43:12 / cg" |
|
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
162 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 163 |
! |
164 |
||
165 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 166 |
"Remove oldObject from the collection. |
167 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 168 |
otherwise return the removed object. |
169 |
||
170 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 171 |
Iterate over a copy to do this." |
1 | 172 |
|
173 |
|count| |
|
174 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
175 |
count := contents at:oldObject ifAbsent:0. |
1 | 176 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
177 |
(count == 1) ifTrue:[ |
|
8873 | 178 |
contents removeKey:oldObject |
179 |
] ifFalse:[ |
|
180 |
contents at:oldObject put:(count - 1) |
|
1 | 181 |
]. |
182 |
^ oldObject |
|
1057 | 183 |
|
184 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 185 |
! |
186 |
||
1110 | 187 |
removeAll |
1164 | 188 |
"remove all objects from the receiver collection (i.e. make it empty). |
189 |
Returns the receiver." |
|
1110 | 190 |
|
191 |
contents := contents species new. |
|
192 |
||
193 |
" |
|
194 |
|b| |
|
195 |
||
196 |
b := Bag new. |
|
197 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 198 |
Transcript showCR:b. |
1110 | 199 |
|
200 |
b removeAll. |
|
201 |
||
1422 | 202 |
Transcript showCR:b |
1110 | 203 |
" |
204 |
||
1164 | 205 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 206 |
! |
207 |
||
345 | 208 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
209 |
"Remove all occurrences of oldObject from the collection. |
|
210 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 211 |
otherwise return the number of removes. |
212 |
||
213 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 214 |
Iterate over a copy to do this." |
345 | 215 |
|
216 |
|count| |
|
217 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
218 |
count := contents at:oldObject ifAbsent:0. |
345 | 219 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
220 |
contents removeKey:oldObject. |
|
221 |
^ oldObject |
|
1057 | 222 |
|
223 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 224 |
! ! |
225 |
||
8860 | 226 |
!Bag methodsFor:'bulk operations'! |
227 |
||
228 |
sum |
|
229 |
"sum up all elements." |
|
230 |
||
231 |
|accu| |
|
232 |
||
233 |
accu := 0. |
|
234 |
self valuesAndCountsDo:[:n :count | accu := accu + (count * n)]. |
|
235 |
^ accu |
|
236 |
||
237 |
" |
|
238 |
self assert:(Bag new sum == 0). |
|
239 |
self assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum == 10). |
|
240 |
" |
|
241 |
! ! |
|
242 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
243 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
244 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
245 |
= aBag |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
246 |
aBag species == self species ifFalse:[^ false]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
247 |
self size == aBag size ifFalse:[^ false]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
248 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 249 |
(aBag occurrencesOf:val) == cnt ifFalse:[^ false] |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
250 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
251 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
252 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
253 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
254 |
hash |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
255 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
256 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
257 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
258 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 259 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
260 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
261 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
262 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
263 |
|
609 | 264 |
!Bag methodsFor:'converting'! |
265 |
||
266 |
asBag |
|
267 |
"return the receiver as a bag" |
|
268 |
||
269 |
"could be an instance of a subclass..." |
|
270 |
self class == Bag ifTrue:[ |
|
271 |
^ self |
|
272 |
]. |
|
273 |
^ super asBag |
|
7161 | 274 |
! |
275 |
||
276 |
asSet |
|
277 |
"return the receiver as a set" |
|
278 |
||
279 |
^ contents keys copy |
|
280 |
||
281 |
" |
|
282 |
|b| |
|
283 |
||
284 |
b := Bag new. |
|
285 |
b add:1; add:2; add:3; add:1; add:1. |
|
286 |
b asSet. |
|
287 |
" |
|
609 | 288 |
! ! |
289 |
||
290 |
!Bag methodsFor:'copying'! |
|
291 |
||
292 |
postCopy |
|
293 |
"must copy the contents as well" |
|
294 |
||
295 |
contents := contents copy |
|
296 |
! ! |
|
297 |
||
1 | 298 |
!Bag methodsFor:'enumerating'! |
299 |
||
300 |
do:aBlock |
|
1057 | 301 |
"evaluate the block for all elements in the collection. |
302 |
||
303 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 304 |
Iterate over a copy to do this." |
1 | 305 |
|
54 | 306 |
contents keysAndValuesDo:[:key :value| |
8873 | 307 |
value timesRepeat:[ |
308 |
aBlock value:key |
|
309 |
] |
|
1 | 310 |
] |
1057 | 311 |
|
312 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 313 |
! |
314 |
||
315 |
valuesAndCountsDo:aTwoArgBlock |
|
316 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 317 |
passing both the element and the occurrence count as arguments. |
1057 | 318 |
|
319 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 320 |
Iterate over a copy to do this." |
345 | 321 |
|
322 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 323 |
|
324 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 325 |
! ! |
609 | 326 |
|
327 |
!Bag methodsFor:'private'! |
|
328 |
||
329 |
initContents |
|
330 |
"set the contents to be an empty Dictionary" |
|
331 |
||
332 |
contents := Dictionary new |
|
333 |
! |
|
334 |
||
335 |
initContents:size |
|
336 |
"set the contents to be an empty Dictionary with initial size" |
|
337 |
||
338 |
contents := Dictionary new:size |
|
339 |
! |
|
340 |
||
5375 | 341 |
initContentsForIdentity |
342 |
"set the contents to be an empty IdentityDictionary" |
|
343 |
||
344 |
contents := IdentityDictionary new |
|
345 |
! |
|
346 |
||
609 | 347 |
initContentsForIdentity:size |
348 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
349 |
||
350 |
contents := IdentityDictionary new:size |
|
351 |
! ! |
|
352 |
||
5557 | 353 |
!Bag methodsFor:'queries'! |
354 |
||
355 |
size |
|
356 |
"return the number of bag elements" |
|
357 |
||
358 |
|count| |
|
359 |
||
360 |
count := 0. |
|
361 |
contents do:[:element | count := count + element]. |
|
362 |
^ count |
|
363 |
! ! |
|
364 |
||
609 | 365 |
!Bag methodsFor:'testing'! |
366 |
||
367 |
includes:anObject |
|
368 |
"return true, if anObject is in the receiver" |
|
369 |
||
370 |
^ contents includesKey:anObject |
|
371 |
! |
|
372 |
||
373 |
occurrencesOf:anObject |
|
374 |
"return how many times anObject is in the receiver" |
|
375 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
376 |
^ contents at:anObject ifAbsent:0 |
609 | 377 |
! ! |
378 |
||
633 | 379 |
!Bag class methodsFor:'documentation'! |
380 |
||
381 |
version |
|
8873 | 382 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.33 2005-04-28 17:28:14 cg Exp $' |
633 | 383 |
! ! |