cmake build system: visiblity support for clang
[supercollider.git] / SCClassLibrary / Common / Collections / Bag.sc
blobec7297f6c657f58984e5b9d0f4112c4511a28bd2
1 Bag : Collection {
2         var <contents;
4         *new { arg n=4;
5                 ^super.new.setDictionary(n)
6         }
7         // testing
8         includes { arg item;
9                 ^contents.includesKey(item)
10         }
11         // adding
12         add { arg item, count=1;
13                 if ( this.includes(item), {
14                         contents.put(item, contents.at(item) + count);
15                 },{
16                         contents.put(item, count);
17                 });
18         }
19         remove { arg item, count=1;
20                 var newCount;
21                 if ( this.includes(item), {
22                         newCount = contents.at(item) - count;
23                         if (newCount <= 0, {
24                                 contents.removeAt(item);
25                         },{
26                                 contents.put(item, newCount);
27                         });
28                 });
29         }
31         // accessing
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
38         choose {
39                 var index, key, array;
40                 if( this.isEmpty, { ^nil }); // empty dictionary
41                 array = contents.array;
42                 while({
43                         index = (array.size >> 1).rand << 1; // generate an even index.
44                         array.at(index).isNil;                    // key is at even index.
45                 });
46                 // return the first non-nil key
47                 ^array.at(index);
48         }
50         wchoose {
51                 var     items = Array(contents.size), counts = Array(contents.size);
52                 contents.keysValuesDo({ |item, count|
53                         items.add(item);
54                         counts.add(count);
55                 });
56                 ^items[counts.normalizeSum.windex]
57         }
59         take {
60                 var     result = this.choose;
61                 this.remove(result);
62                 ^result
63         }
65         // enumerating
66         do { arg function;
67                 var j = 0;
68                 contents.associationsDo({ arg assn;
69                          assn.value.do({
70                                 function.value(assn.key, j);
71                                 j = j + 1;
72                          })
73                 });
74         }
75         countsDo { arg function;
76                 var j = 0;
77                 contents.associationsDo({ arg assn;
78                         function.value(assn.key,assn.value,j);
79                          j = j + 1;
80                 });
81         }
82         itemCount { arg item;
83                 ^(contents.at(item) ? 0)
84         }
86         // PRIVATE IMPLEMENTATION
87         setDictionary { arg n;
88                 contents = Dictionary.new(n)
89         }
91         asBag { ^this }
94 IdentityBag : Bag
96         // PRIVATE IMPLEMENTATION
97         setDictionary { arg n;
98                 contents = IdentityDictionary.new(n)
99         }