author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 20 Dec 2010 07:13:27 +0000 | |
branch | jv |
changeset 17814 | b75a7f0c346b |
parent 17807 | 06cc6c49e291 |
child 17834 | 04ff72c5039a |
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 |
" |
|
5375 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#Bag |
1057 | 15 |
instanceVariableNames:'contents' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
1 | 19 |
! |
20 |
||
54 | 21 |
!Bag class methodsFor:'documentation'! |
22 |
||
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
54 | 37 |
documentation |
38 |
" |
|
88 | 39 |
Bag implements collections whose elements are unordered and have no |
154 | 40 |
external key. Elements may occur more than once in a bag. There is no defined |
8873 | 41 |
order within a bag. |
42 |
The default implementation uses a dictionary to store each objects occurrence |
|
43 |
count, using the object itself as key (i.e. using = and hash for inclusion |
|
345 | 44 |
tests). |
399 | 45 |
|
345 | 46 |
There is also an instance creation variant (#identityNew:) creating a |
47 |
bag which compares using #== and hashes using #identityHash. |
|
8873 | 48 |
(I'd say that an IdentityBag was a better thing to implement ... |
399 | 49 |
... but for compatibility ... we do it here as well) |
1 | 50 |
|
1283 | 51 |
[Instance variables:] |
52 |
||
8873 | 53 |
contents <Dictionary> for each element, the number of occurrences |
1283 | 54 |
|
1 | 55 |
|
1290 | 56 |
[author:] |
8873 | 57 |
Claus Gittinger |
1290 | 58 |
|
1283 | 59 |
[See also:] |
8873 | 60 |
Set IdentitySet |
61 |
Dictionary IdentityDictionary |
|
62 |
OrderedCollection Array |
|
54 | 63 |
" |
64 |
! ! |
|
1 | 65 |
|
66 |
!Bag class methodsFor:'instance creation'! |
|
67 |
||
345 | 68 |
equalityNew:size |
69 |
"return a new empty Bag with initial space for size elements. |
|
70 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 71 |
|
5558 | 72 |
^ self basicNew initContents:size |
345 | 73 |
! |
74 |
||
5375 | 75 |
identityNew |
76 |
"return a new empty Bag which compares for identity |
|
77 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
78 |
||
79 |
^ super new initContentsForIdentity |
|
80 |
! |
|
81 |
||
345 | 82 |
identityNew:size |
83 |
"return a new empty Bag with initial space for size elements. |
|
84 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
85 |
||
5558 | 86 |
^ self basicNew initContentsForIdentity:size |
1 | 87 |
! |
88 |
||
609 | 89 |
new |
90 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 91 |
|
5558 | 92 |
^ self basicNew initContents |
345 | 93 |
! |
94 |
||
609 | 95 |
new:size |
96 |
"return a new empty Bag with initial space for size elements. |
|
97 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 98 |
|
609 | 99 |
^ self equalityNew:size |
1 | 100 |
! ! |
101 |
||
7261 | 102 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 103 |
|
104 |
asAssociations |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
105 |
"return the dictionary which associates occurrence-counts |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
106 |
to the bags elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
107 |
Same as #contents for dolphin compatibility." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
108 |
|
6411 | 109 |
^ contents |
110 |
! ! |
|
111 |
||
1 | 112 |
!Bag methodsFor:'accessing'! |
113 |
||
114 |
at:index |
|
115 |
"report an error: at: is not allowed for Bags" |
|
116 |
||
117 |
^ self errorNotKeyed |
|
118 |
! |
|
119 |
||
120 |
at:index put:anObject |
|
121 |
"report an error: at:put: is not allowed for Bags" |
|
122 |
||
123 |
^ self errorNotKeyed |
|
345 | 124 |
! |
125 |
||
126 |
contents |
|
127 |
"return the dictionary which associates occurrence-counts |
|
128 |
to the bags elements." |
|
129 |
||
130 |
^ contents |
|
1 | 131 |
! ! |
132 |
||
133 |
!Bag methodsFor:'adding & removing'! |
|
134 |
||
54 | 135 |
add:newObject |
345 | 136 |
"add the argument, anObject to the receiver. |
1057 | 137 |
Returns the object. |
138 |
||
139 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 140 |
Iterate over a copy to do this." |
1 | 141 |
|
54 | 142 |
|n| |
143 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
144 |
n := contents at:newObject ifAbsent:0. |
54 | 145 |
contents at:newObject put:(n + 1). |
146 |
^ newObject |
|
1057 | 147 |
|
148 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 149 |
! |
150 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
151 |
add:newObject withOccurrences:anInteger |
345 | 152 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 153 |
Returns the object. |
154 |
||
155 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 156 |
Iterate over a copy to do this." |
1 | 157 |
|
54 | 158 |
|n| |
159 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
160 |
n := contents at:newObject ifAbsent:0. |
54 | 161 |
contents at:newObject put:(n + anInteger). |
1 | 162 |
^ newObject |
1057 | 163 |
|
164 |
"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
|
165 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 166 |
! |
167 |
||
168 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 169 |
"Remove oldObject from the collection. |
170 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 171 |
otherwise return the removed object. |
172 |
||
173 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 174 |
Iterate over a copy to do this." |
1 | 175 |
|
176 |
|count| |
|
177 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
178 |
count := contents at:oldObject ifAbsent:0. |
1 | 179 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
180 |
(count == 1) ifTrue:[ |
|
8873 | 181 |
contents removeKey:oldObject |
182 |
] ifFalse:[ |
|
183 |
contents at:oldObject put:(count - 1) |
|
1 | 184 |
]. |
185 |
^ oldObject |
|
1057 | 186 |
|
187 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 188 |
! |
189 |
||
1110 | 190 |
removeAll |
1164 | 191 |
"remove all objects from the receiver collection (i.e. make it empty). |
192 |
Returns the receiver." |
|
1110 | 193 |
|
194 |
contents := contents species new. |
|
195 |
||
196 |
" |
|
197 |
|b| |
|
198 |
||
199 |
b := Bag new. |
|
200 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 201 |
Transcript showCR:b. |
1110 | 202 |
|
203 |
b removeAll. |
|
204 |
||
1422 | 205 |
Transcript showCR:b |
1110 | 206 |
" |
207 |
||
1164 | 208 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 209 |
! |
210 |
||
345 | 211 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
212 |
"Remove all occurrences of oldObject from the collection. |
|
213 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 214 |
otherwise return the number of removes. |
215 |
||
216 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 217 |
Iterate over a copy to do this." |
345 | 218 |
|
219 |
|count| |
|
220 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
221 |
count := contents at:oldObject ifAbsent:0. |
345 | 222 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
223 |
contents removeKey:oldObject. |
|
224 |
^ oldObject |
|
1057 | 225 |
|
226 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 227 |
! ! |
228 |
||
8860 | 229 |
!Bag methodsFor:'bulk operations'! |
230 |
||
231 |
sum |
|
11522 | 232 |
"sum up all elements; return 0 for an empty collection. |
233 |
can be done easier, using bags knowledge." |
|
8860 | 234 |
|
235 |
|accu| |
|
236 |
||
11522 | 237 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 238 |
|
239 |
accu := nil. |
|
240 |
self |
|
241 |
valuesAndCountsDo:[:n :count | |
|
242 |
|thisAmount| |
|
243 |
||
244 |
thisAmount := count * n. |
|
245 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 246 |
^ accu |
247 |
||
248 |
" |
|
11474 | 249 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 250 |
" |
251 |
! ! |
|
252 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
253 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
254 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
255 |
= aBag |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
256 |
"Compare the receiver with the argument and return true if the |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
257 |
receiver is equal to the argument (i.e. has the same size and elements). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
258 |
Otherwise return false." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
259 |
|
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
260 |
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
|
261 |
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
|
262 |
self valuesAndCountsDo:[:val :cnt | |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
263 |
(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
|
264 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
265 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
266 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
267 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
268 |
hash |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
269 |
"return an integer useful for hashing on the receiver; |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
270 |
redefined since = is redefined here." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
271 |
|
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
272 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
273 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
274 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
275 |
self valuesAndCountsDo:[:val :cnt | |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
276 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
277 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
278 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
279 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
280 |
|
609 | 281 |
!Bag methodsFor:'converting'! |
282 |
||
283 |
asBag |
|
284 |
"return the receiver as a bag" |
|
285 |
||
286 |
"could be an instance of a subclass..." |
|
287 |
self class == Bag ifTrue:[ |
|
288 |
^ self |
|
289 |
]. |
|
290 |
^ super asBag |
|
7161 | 291 |
! |
292 |
||
293 |
asSet |
|
294 |
"return the receiver as a set" |
|
295 |
||
296 |
^ contents keys copy |
|
297 |
||
298 |
" |
|
299 |
|b| |
|
300 |
||
301 |
b := Bag new. |
|
302 |
b add:1; add:2; add:3; add:1; add:1. |
|
303 |
b asSet. |
|
304 |
" |
|
609 | 305 |
! ! |
306 |
||
307 |
!Bag methodsFor:'copying'! |
|
308 |
||
309 |
postCopy |
|
310 |
"must copy the contents as well" |
|
311 |
||
312 |
contents := contents copy |
|
313 |
! ! |
|
314 |
||
1 | 315 |
!Bag methodsFor:'enumerating'! |
316 |
||
317 |
do:aBlock |
|
1057 | 318 |
"evaluate the block for all elements in the collection. |
319 |
||
320 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 321 |
Iterate over a copy to do this." |
1 | 322 |
|
54 | 323 |
contents keysAndValuesDo:[:key :value| |
8873 | 324 |
value timesRepeat:[ |
325 |
aBlock value:key |
|
326 |
] |
|
1 | 327 |
] |
1057 | 328 |
|
329 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 330 |
! |
331 |
||
10835 | 332 |
valuesAndCounts |
11481 | 333 |
"return an orderedCollection containing value->count associations" |
10835 | 334 |
|
335 |
|coll| |
|
336 |
||
337 |
coll := OrderedCollection new. |
|
338 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
339 |
^ coll |
|
340 |
||
341 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
342 |
! |
|
343 |
||
345 | 344 |
valuesAndCountsDo:aTwoArgBlock |
345 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 346 |
passing both the element and the occurrence count as arguments. |
1057 | 347 |
|
348 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 349 |
Iterate over a copy to do this." |
345 | 350 |
|
351 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 352 |
|
353 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 354 |
! ! |
609 | 355 |
|
17757 | 356 |
!Bag methodsFor:'printing & storing'! |
357 |
||
358 |
printElementsDo:aBlock |
|
359 |
contents keysAndValuesDo:[:key :value| |
|
360 |
aBlock value:('%1(*%2)' bindWith:key with:value). |
|
361 |
] |
|
362 |
! ! |
|
363 |
||
609 | 364 |
!Bag methodsFor:'private'! |
365 |
||
366 |
initContents |
|
367 |
"set the contents to be an empty Dictionary" |
|
368 |
||
369 |
contents := Dictionary new |
|
370 |
! |
|
371 |
||
372 |
initContents:size |
|
373 |
"set the contents to be an empty Dictionary with initial size" |
|
374 |
||
375 |
contents := Dictionary new:size |
|
376 |
! |
|
377 |
||
5375 | 378 |
initContentsForIdentity |
379 |
"set the contents to be an empty IdentityDictionary" |
|
380 |
||
381 |
contents := IdentityDictionary new |
|
382 |
! |
|
383 |
||
609 | 384 |
initContentsForIdentity:size |
385 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
386 |
||
387 |
contents := IdentityDictionary new:size |
|
388 |
! ! |
|
389 |
||
5557 | 390 |
!Bag methodsFor:'queries'! |
391 |
||
392 |
size |
|
393 |
"return the number of bag elements" |
|
394 |
||
395 |
|count| |
|
396 |
||
397 |
count := 0. |
|
398 |
contents do:[:element | count := count + element]. |
|
399 |
^ count |
|
400 |
! ! |
|
401 |
||
609 | 402 |
!Bag methodsFor:'testing'! |
403 |
||
404 |
includes:anObject |
|
405 |
"return true, if anObject is in the receiver" |
|
406 |
||
407 |
^ contents includesKey:anObject |
|
408 |
! |
|
409 |
||
410 |
occurrencesOf:anObject |
|
411 |
"return how many times anObject is in the receiver" |
|
412 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
413 |
^ contents at:anObject ifAbsent:0 |
609 | 414 |
! ! |
415 |
||
633 | 416 |
!Bag class methodsFor:'documentation'! |
417 |
||
418 |
version |
|
17814 | 419 |
^ '$Id: Bag.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
17757 | 420 |
! |
421 |
||
422 |
version_CVS |
|
17814 | 423 |
^ 'Header: /cvs/stx/stx/libbasic/Bag.st,v 1.41 2010/03/30 13:38:23 stefan Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
424 |
! |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
425 |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
426 |
version_SVN |
17814 | 427 |
^ '$Id: Bag.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
428 |
! ! |
17767 | 429 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
430 |
|
17780 | 431 |
|
17807 | 432 |
|
17814 | 433 |