polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Collections / Bag.html
blob3dc0cb6b63373ea4dcf0d1d0840efd3b21c42a00
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #001bb9}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ff}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ad140d}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #001bb9}
23 span.s1 {font: 18.0px Helvetica}
24 span.s2 {color: #000000}
25 span.s3 {color: #001bb9}
26 span.s4 {color: #606060}
27 span.s5 {color: #ad140d}
28 span.Apple-tab-span {white-space:pre}
29 </style>
30 </head>
31 <body>
32 <p class="p1"><span class="s1"><b>Bag<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b></span><b>counted set</b></p>
33 <p class="p2"><br></p>
34 <p class="p3"><span class="s2"><b>Inherits from: </b><a href="../Core/Object.html"><b>Object</b></a></span><b> : </b><a href="Collection.html"><b>Collection</b></a></p>
35 <p class="p2"><br></p>
36 <p class="p1">A Bag is an unordered collection of objects.<span class="Apple-converted-space">  </span>In some languages it is referred to as a counted set.<span class="Apple-converted-space">  </span>A Bag keeps track of the number of times objects are inserted and requires that objects be removed the same number of times.<span class="Apple-converted-space">  </span>There is only one instance of an object in a Bag even if the object has been added to the Bag multiple times (test is for <b>equality</b>)</p>
37 <p class="p4"><span class="Apple-converted-space"> </span></p>
38 <p class="p1">Most of Bag's methods are inherited from Collection.</p>
39 <p class="p1">The contents of a Bag are unordered. You must not depend on the order of items in a set.</p>
40 <p class="p2"><br></p>
41 <p class="p5"><span class="s2">See also: <a href="IdentityBag.html">IdentityBag</a>, <a href="Set.html">Set</a></span></p>
42 <p class="p2"><br></p>
43 <p class="p6"><b>Adding and Removing:</b></p>
44 <p class="p2"><br></p>
45 <p class="p1"><b>add(anObject)</b></p>
46 <p class="p2"><br></p>
47 <p class="p1">Add anObject to the Bag. A Bag may contain multiple entries of the same object.</p>
48 <p class="p7"><br></p>
49 <p class="p8"><span class="s3">Bag</span>[1, 2, 3].add(4).postln;</p>
50 <p class="p9"><br></p>
51 <p class="p8"><span class="s3">Bag</span>[1, 2, 3].add(3).postln;</p>
52 <p class="p9"><br></p>
53 <p class="p8"><span class="s3">Bag</span>[<span class="s4">"abc"</span>, <span class="s4">"def"</span>, <span class="s4">"ghi"</span>].add(<span class="s4">"jkl"</span>).postln;</p>
54 <p class="p9"><br></p>
55 <p class="p10"><span class="s3">Bag</span><span class="s2">[</span>"abc"<span class="s2">, </span>"def"<span class="s2">, </span>"ghi"<span class="s2">].add(</span>"def"<span class="s2">).postln;</span></p>
56 <p class="p2"><br></p>
57 <p class="p1"><b>remove(anObject)</b></p>
58 <p class="p2"><br></p>
59 <p class="p1">Remove anObject from the Bag.</p>
60 <p class="p2"><br></p>
61 <p class="p8"><span class="s3">Bag</span>[1, 2, 3].remove(3).postln;</p>
62 <p class="p7"><br></p>
63 <p class="p1"><b>contents</b></p>
64 <p class="p7"><br></p>
65 <p class="p1">Returns the dictionary that stores the objects in pairs (obj -&gt; numberOfObjects)</p>
66 <p class="p2"><br></p>
67 <p class="p8"><span class="s3">Bag</span>[<span class="s4">"a"</span>, <span class="s4">"b"</span>, <span class="s4">"c"</span>, <span class="s4">"c"</span>].contents;</p>
68 <p class="p11"><br></p>
69 <p class="p11"><br></p>
70 <p class="p6"><b>Iteration:</b></p>
71 <p class="p2"><br></p>
72 <p class="p1"><b>do(function)</b></p>
73 <p class="p2"><br></p>
74 <p class="p1">Evaluates function for each item in the Bag.</p>
75 <p class="p1">The function is passed two arguments, the item and an integer index.</p>
76 <p class="p2"><br></p>
77 <p class="p8"><span class="s3">Bag</span>[1, 2, 3, 300].do({ <span class="s3">arg</span> item, i; item.postln });</p>
78 <p class="p2"><br></p>
79 <p class="p8"><span class="s3">Bag</span>[1, 2, 2, 3, 300].do({ <span class="s3">arg</span> item, i; item.postln });</p>
80 <p class="p2"><br></p>
81 <p class="p1"><b>countsDo(function)</b></p>
82 <p class="p1">args to function: item, count, i</p>
83 <p class="p2"><br></p>
84 <p class="p1">Evaluates function for each unique item in the Bag along with that item's count.</p>
85 <p class="p1">The function is passed two arguments, the item, the quantity of that item in the Bag and an integer index.</p>
86 <p class="p2"><br></p>
87 <p class="p8"><span class="s3">Bag</span>[1, 2, 3, 300].countsDo({ <span class="s3">arg</span> item, count, i; [item,count].postln });</p>
88 <p class="p2"><br></p>
89 <p class="p8"><span class="s3">Bag</span>[1, 2, 2, 3, 300].countsDo({ <span class="s3">arg</span> item, count, i; [item,count].postln });</p>
90 <p class="p2"><br></p>
91 <p class="p2"><br></p>
92 <p class="p6"><b>Difference between Bag and IdentityBag:</b></p>
93 <p class="p7"><br></p>
94 <p class="p12">// the two strings are equal, but not identical</p>
95 <p class="p10">"something"<span class="s2"> == </span>"something"<span class="s2">; </span><span class="s5">// true</span></p>
96 <p class="p10">"something"<span class="s2"> === </span>"something"<span class="s2"> </span><span class="s5">// false</span></p>
97 <p class="p9"><br></p>
98 <p class="p8">a = <span class="s3">Bag</span>.new;</p>
99 <p class="p10"><span class="s2">a.add(</span>"something"<span class="s2">);</span></p>
100 <p class="p10"><span class="s2">a.add(</span>"something"<span class="s2">);</span></p>
101 <p class="p12"><span class="s2">a.contents; </span>// only one object in the bag really</p>
102 <p class="p9"><br></p>
103 <p class="p13"><span class="s2">a = </span>IdentityBag<span class="s2">.new;</span></p>
104 <p class="p10"><span class="s2">a.add(</span>"something"<span class="s2">);</span></p>
105 <p class="p10"><span class="s2">a.add(</span>"something"<span class="s2">);</span></p>
106 <p class="p12"><span class="s2">a.contents; </span>// two objects in the bag</p>
107 <p class="p2"><br></p>
108 <p class="p2"><br></p>
109 </body>
110 </html>