author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 28 Aug 2013 10:47:51 +0100 | |
branch | jv |
changeset 18091 | abbcac10730e |
parent 18071 | 009cf668b0ed |
parent 15686 | 2efc686ef383 |
child 18093 | 2b786a9af1d0 |
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 |
||
14956 | 68 |
contentsClass |
69 |
"the default class to use for the underlying contents array, |
|
70 |
used when instantiated with new/new:" |
|
71 |
||
72 |
^ Dictionary |
|
73 |
! |
|
74 |
||
75 |
equalityNew |
|
76 |
"return a new empty Bag. |
|
77 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
78 |
||
79 |
^ self basicNew initContentsForEquality |
|
80 |
! |
|
81 |
||
345 | 82 |
equalityNew: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)." |
|
1 | 85 |
|
14956 | 86 |
^ self basicNew initContentsForEquality:size |
345 | 87 |
! |
88 |
||
5375 | 89 |
identityNew |
14956 | 90 |
"return a new empty Identity-Bag. |
5375 | 91 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
92 |
||
14956 | 93 |
^ self basicNew initContentsForIdentity |
5375 | 94 |
! |
95 |
||
345 | 96 |
identityNew:size |
97 |
"return a new empty Bag with initial space for size elements. |
|
98 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
99 |
||
5558 | 100 |
^ self basicNew initContentsForIdentity:size |
1 | 101 |
! |
102 |
||
609 | 103 |
new |
104 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 105 |
|
5558 | 106 |
^ self basicNew initContents |
345 | 107 |
! |
108 |
||
609 | 109 |
new:size |
110 |
"return a new empty Bag with initial space for size elements. |
|
111 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 112 |
|
609 | 113 |
^ self equalityNew:size |
1 | 114 |
! ! |
115 |
||
7261 | 116 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 117 |
|
118 |
asAssociations |
|
11935 | 119 |
"return the dictionary which associates occurrence-counts |
120 |
to the bags elements. |
|
121 |
Same as #contents for dolphin compatibility." |
|
122 |
||
14685 | 123 |
^ self contents |
6411 | 124 |
! ! |
125 |
||
1 | 126 |
!Bag methodsFor:'accessing'! |
127 |
||
128 |
at:index |
|
129 |
"report an error: at: is not allowed for Bags" |
|
130 |
||
131 |
^ self errorNotKeyed |
|
132 |
! |
|
133 |
||
134 |
at:index put:anObject |
|
135 |
"report an error: at:put: is not allowed for Bags" |
|
136 |
||
137 |
^ self errorNotKeyed |
|
345 | 138 |
! |
139 |
||
140 |
contents |
|
141 |
"return the dictionary which associates occurrence-counts |
|
142 |
to the bags elements." |
|
143 |
||
144 |
^ contents |
|
15686 | 145 |
! |
146 |
||
147 |
sortedCounts |
|
148 |
"Answer with a collection of counts with elements, sorted by decreasing count." |
|
149 |
"Suggested by l. Uzonyi" |
|
150 |
||
151 |
^ (Array new:contents size |
|
152 |
streamContents:[:stream | |
|
153 |
contents associationsDo:[:each | stream nextPut:each value -> each key ] |
|
154 |
]) |
|
155 |
sort:[:x :y | x >= y ]; |
|
156 |
yourself |
|
1 | 157 |
! ! |
158 |
||
159 |
!Bag methodsFor:'adding & removing'! |
|
160 |
||
54 | 161 |
add:newObject |
345 | 162 |
"add the argument, anObject to the receiver. |
14896 | 163 |
Returns the object (sigh). |
1057 | 164 |
|
165 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14896 | 166 |
Iterate over a copy to do this." |
1 | 167 |
|
54 | 168 |
|n| |
169 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
170 |
n := contents at:newObject ifAbsent:0. |
54 | 171 |
contents at:newObject put:(n + 1). |
172 |
^ newObject |
|
1057 | 173 |
|
174 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 175 |
! |
176 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
177 |
add:newObject withOccurrences:anInteger |
345 | 178 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 179 |
Returns the object. |
180 |
||
181 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 182 |
Iterate over a copy to do this." |
1 | 183 |
|
54 | 184 |
|n| |
185 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
186 |
n := contents at:newObject ifAbsent:0. |
54 | 187 |
contents at:newObject put:(n + anInteger). |
1 | 188 |
^ newObject |
1057 | 189 |
|
190 |
"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
|
191 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 192 |
! |
193 |
||
194 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 195 |
"Remove oldObject from the collection. |
196 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 197 |
otherwise return the removed object. |
198 |
||
199 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 200 |
Iterate over a copy to do this." |
1 | 201 |
|
202 |
|count| |
|
203 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
204 |
count := contents at:oldObject ifAbsent:0. |
1 | 205 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
206 |
(count == 1) ifTrue:[ |
|
8873 | 207 |
contents removeKey:oldObject |
208 |
] ifFalse:[ |
|
209 |
contents at:oldObject put:(count - 1) |
|
1 | 210 |
]. |
211 |
^ oldObject |
|
1057 | 212 |
|
213 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 214 |
! |
215 |
||
1110 | 216 |
removeAll |
1164 | 217 |
"remove all objects from the receiver collection (i.e. make it empty). |
218 |
Returns the receiver." |
|
1110 | 219 |
|
220 |
contents := contents species new. |
|
221 |
||
222 |
" |
|
223 |
|b| |
|
224 |
||
225 |
b := Bag new. |
|
226 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 227 |
Transcript showCR:b. |
1110 | 228 |
|
229 |
b removeAll. |
|
230 |
||
1422 | 231 |
Transcript showCR:b |
1110 | 232 |
" |
233 |
||
1164 | 234 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 235 |
! |
236 |
||
345 | 237 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
238 |
"Remove all occurrences of oldObject from the collection. |
|
239 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 240 |
otherwise return the number of removes. |
241 |
||
242 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 243 |
Iterate over a copy to do this." |
345 | 244 |
|
245 |
|count| |
|
246 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
247 |
count := contents at:oldObject ifAbsent:0. |
345 | 248 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
249 |
contents removeKey:oldObject. |
|
250 |
^ oldObject |
|
1057 | 251 |
|
252 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 253 |
! ! |
254 |
||
8860 | 255 |
!Bag methodsFor:'bulk operations'! |
256 |
||
257 |
sum |
|
11522 | 258 |
"sum up all elements; return 0 for an empty collection. |
259 |
can be done easier, using bags knowledge." |
|
8860 | 260 |
|
261 |
|accu| |
|
262 |
||
11522 | 263 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 264 |
|
265 |
accu := nil. |
|
266 |
self |
|
267 |
valuesAndCountsDo:[:n :count | |
|
268 |
|thisAmount| |
|
269 |
||
270 |
thisAmount := count * n. |
|
271 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 272 |
^ accu |
273 |
||
274 |
" |
|
11474 | 275 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 276 |
" |
277 |
! ! |
|
278 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
279 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
280 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
281 |
= aBag |
11935 | 282 |
"Compare the receiver with the argument and return true if the |
283 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
284 |
Otherwise return false." |
|
285 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
286 |
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
|
287 |
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
|
288 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 289 |
(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
|
290 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
291 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
292 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
293 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
294 |
hash |
11935 | 295 |
"return an integer useful for hashing on the receiver; |
296 |
redefined since = is redefined here." |
|
297 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
298 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
299 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
300 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
301 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 302 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
303 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
304 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
305 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
306 |
|
609 | 307 |
!Bag methodsFor:'converting'! |
308 |
||
309 |
asBag |
|
310 |
"return the receiver as a bag" |
|
311 |
||
312 |
"could be an instance of a subclass..." |
|
313 |
self class == Bag ifTrue:[ |
|
314 |
^ self |
|
315 |
]. |
|
316 |
^ super asBag |
|
7161 | 317 |
! |
318 |
||
319 |
asSet |
|
320 |
"return the receiver as a set" |
|
321 |
||
322 |
^ contents keys copy |
|
323 |
||
324 |
" |
|
325 |
|b| |
|
326 |
||
327 |
b := Bag new. |
|
328 |
b add:1; add:2; add:3; add:1; add:1. |
|
329 |
b asSet. |
|
330 |
" |
|
609 | 331 |
! ! |
332 |
||
333 |
!Bag methodsFor:'copying'! |
|
334 |
||
335 |
postCopy |
|
336 |
"must copy the contents as well" |
|
337 |
||
338 |
contents := contents copy |
|
339 |
! ! |
|
340 |
||
1 | 341 |
!Bag methodsFor:'enumerating'! |
342 |
||
343 |
do:aBlock |
|
1057 | 344 |
"evaluate the block for all elements in the collection. |
345 |
||
346 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 347 |
Iterate over a copy to do this." |
1 | 348 |
|
54 | 349 |
contents keysAndValuesDo:[:key :value| |
8873 | 350 |
value timesRepeat:[ |
351 |
aBlock value:key |
|
352 |
] |
|
1 | 353 |
] |
1057 | 354 |
|
355 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 356 |
! |
357 |
||
10835 | 358 |
valuesAndCounts |
11481 | 359 |
"return an orderedCollection containing value->count associations" |
10835 | 360 |
|
361 |
|coll| |
|
362 |
||
363 |
coll := OrderedCollection new. |
|
364 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
365 |
^ coll |
|
366 |
||
367 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
368 |
! |
|
369 |
||
345 | 370 |
valuesAndCountsDo:aTwoArgBlock |
371 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 372 |
passing both the element and the occurrence count as arguments. |
1057 | 373 |
|
374 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 375 |
Iterate over a copy to do this." |
345 | 376 |
|
377 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 378 |
|
379 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 380 |
! ! |
609 | 381 |
|
12823 | 382 |
!Bag methodsFor:'printing & storing'! |
383 |
||
384 |
printElementsDo:aBlock |
|
14685 | 385 |
self valuesAndCountsDo:[:value :count| |
386 |
aBlock value:('%1(*%2)' bindWith:value with:count). |
|
12823 | 387 |
] |
388 |
! ! |
|
389 |
||
609 | 390 |
!Bag methodsFor:'private'! |
391 |
||
392 |
initContents |
|
14956 | 393 |
"set the contents to be an empty Dictionary. |
394 |
This is the default method for initialization, which can be redefined in subclasses." |
|
395 |
||
396 |
contents := self class contentsClass new |
|
397 |
! |
|
398 |
||
399 |
initContents:size |
|
400 |
"set the contents to be an empty Dictionary with initial size. |
|
401 |
This is the default method for initialization, which can be redefined in subclasses." |
|
402 |
||
403 |
contents := self class contentsClass new: size |
|
404 |
! |
|
405 |
||
406 |
initContentsForEquality |
|
609 | 407 |
"set the contents to be an empty Dictionary" |
408 |
||
409 |
contents := Dictionary new |
|
410 |
! |
|
411 |
||
14956 | 412 |
initContentsForEquality:size |
609 | 413 |
"set the contents to be an empty Dictionary with initial size" |
414 |
||
415 |
contents := Dictionary new:size |
|
416 |
! |
|
417 |
||
5375 | 418 |
initContentsForIdentity |
419 |
"set the contents to be an empty IdentityDictionary" |
|
420 |
||
421 |
contents := IdentityDictionary new |
|
422 |
! |
|
423 |
||
609 | 424 |
initContentsForIdentity:size |
425 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
426 |
||
427 |
contents := IdentityDictionary new:size |
|
428 |
! ! |
|
429 |
||
5557 | 430 |
!Bag methodsFor:'queries'! |
431 |
||
432 |
size |
|
433 |
"return the number of bag elements" |
|
434 |
||
435 |
|count| |
|
436 |
||
437 |
count := 0. |
|
438 |
contents do:[:element | count := count + element]. |
|
439 |
^ count |
|
440 |
! ! |
|
441 |
||
15457 | 442 |
!Bag methodsFor:'statistical functions'! |
443 |
||
444 |
variance |
|
445 |
"compute the variance over a complete data set (and not of a sample)" |
|
446 |
||
447 |
|m sz sumDeltaSquares| |
|
448 |
||
449 |
m := self arithmeticMean. |
|
450 |
sumDeltaSquares := 0. |
|
451 |
sz := 0. |
|
452 |
self |
|
453 |
valuesAndCountsDo:[:val :count | |
|
454 |
sumDeltaSquares := sumDeltaSquares + ((val - m) squared). |
|
455 |
sz := sz + 1. |
|
456 |
]. |
|
457 |
||
458 |
^ sumDeltaSquares / sz |
|
459 |
||
460 |
" |
|
461 |
TestCase assert:( #(1 1 1 2 2 2 1 1 1 2 2 2) asBag variance = #(1 1 1 2 2 2 1 1 1 2 2 2) variance). |
|
462 |
TestCase assert:( #(1 1 1 1 1 0 0 0 0 0) asBag variance = #(1 1 1 1 1 0 0 0 0 0) variance). |
|
463 |
" |
|
464 |
! ! |
|
465 |
||
609 | 466 |
!Bag methodsFor:'testing'! |
467 |
||
468 |
includes:anObject |
|
469 |
"return true, if anObject is in the receiver" |
|
470 |
||
14685 | 471 |
^ (self occurrencesOf:anObject) > 0 |
609 | 472 |
! |
473 |
||
15429 | 474 |
isFixedSize |
475 |
"return true if the receiver cannot grow" |
|
476 |
||
477 |
^ false |
|
478 |
! |
|
479 |
||
609 | 480 |
occurrencesOf:anObject |
481 |
"return how many times anObject is in the receiver" |
|
482 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
483 |
^ contents at:anObject ifAbsent:0 |
609 | 484 |
! ! |
485 |
||
633 | 486 |
!Bag class methodsFor:'documentation'! |
487 |
||
488 |
version |
|
15686 | 489 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.47 2013-08-23 00:21:19 cg Exp $' |
12823 | 490 |
! |
491 |
||
492 |
version_CVS |
|
15686 | 493 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.47 2013-08-23 00:21:19 cg Exp $' |
633 | 494 |
! ! |
14685 | 495 |