5 ^super.new.setDictionary(n)
9 ^contents.includesKey(item)
12 add { arg item, count=1;
13 if ( this.includes(item), {
14 contents.put(item, contents.at(item) + count);
16 contents.put(item, count);
19 remove { arg item, count=1;
21 if ( this.includes(item), {
22 newCount = contents.at(item) - count;
24 contents.removeAt(item);
26 contents.put(item, newCount);
32 at { ^this.shouldNotImplement(thisMethod) }
33 atFail { ^this.shouldNotImplement(thisMethod) }
34 put { ^this.shouldNotImplement(thisMethod) }
36 // get something from the bag without removing it
37 // the item is the dictionary's key
39 var index, key, array;
40 if( this.isEmpty, { ^nil }); // empty dictionary
41 array = contents.array;
43 index = (array.size >> 1).rand << 1; // generate an even index.
44 array.at(index).isNil; // key is at even index.
46 // return the first non-nil key
51 var items = Array(contents.size), counts = Array(contents.size);
52 contents.keysValuesDo({ |item, count|
56 ^items[counts.normalizeSum.windex]
60 var result = this.choose;
68 contents.associationsDo({ arg assn;
70 function.value(assn.key, j);
75 countsDo { arg function;
77 contents.associationsDo({ arg assn;
78 function.value(assn.key,assn.value,j);
83 ^(contents.at(item) ? 0)
86 // PRIVATE IMPLEMENTATION
87 setDictionary { arg n;
88 contents = Dictionary.new(n)
96 // PRIVATE IMPLEMENTATION
97 setDictionary { arg n;
98 contents = IdentityDictionary.new(n)