File: Bag.schelp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (103 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
CLASS::Bag
summary::Unordered collection of objects
related::Classes/IdentityBag, Classes/Set
categories::Collections>Unordered

DESCRIPTION::
A Bag is an unordered collection of objects. In some languages it is referred to as a counted set. A Bag keeps track of the number of times objects are inserted and requires that objects be removed the same number of times. 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 strong::equality::)

Most of Bag's methods are inherited from Collection.
The contents of a Bag are unordered. You must not depend on the order of items in a set.

CLASSMETHODS::

method::new
Creates a Bag with an initial capacity for strong::n:: objects.

INSTANCEMETHODS::

private::setDictionary

method::contents
Returns the dictionary that stores the objects in pairs (obj -> numberOfObjects)
code::
Bag["a", "b", "c", "c"].contents;
::
returns:: link::Classes/Dictionary::

method::itemCount
Count the number of strong::item::s.
code::
Bag[1, 2, 2, 3, 300, 2].itemCount(2);
::

subsection::Adding and Removing

method::add
Add an object to the Bag. A Bag may contain multiple entries of the same object.
code::
Bag[1, 2, 3].add(4).postln;

Bag[1, 2, 3].add(3).postln;

Bag["abc", "def", "ghi"].add("jkl").postln;

Bag["abc", "def", "ghi"].add("def").postln;
::

method::remove
Remove an object from the Bag.
code::
Bag[1, 2, 3].remove(3).postln;
::

subsection::Iteration

method::do
Evaluates strong::function:: for each item in the Bag.
The function is passed two arguments, the item and an integer index.
code::
Bag[1, 2, 3, 300].do({ arg item, i; item.postln });

Bag[1, 2, 2, 3, 300].do({ arg item, i; item.postln });
::
argument::function
args to function: item, i

method::countsDo
Evaluates strong::function:: for each unique item in the Bag along with that item's count.
The function is passed two arguments, the item, the quantity of that item in the Bag and an integer index.
code::
Bag[1, 2, 3, 300].countsDo({ arg item, count, i; [item,count].postln });

Bag[1, 2, 2, 3, 300].countsDo({ arg item, count, i; [item,count].postln });
::

subsection::Testing

method::includes
Answer whether an object is contained in the Bag.
code::
Bag[1, 2, 3, 4].includes(3);
::
returns:: link::Classes/Boolean::

EXAMPLES::

subsection::Difference between Bag and IdentityBag:
code::
// the two strings are equal, but not identical
"something" == "something"; // true
"something" === "something" // false

a = Bag.new;
a.add("something");
a.add("something");
a.contents; // only one object in the bag really

a = IdentityBag.new;
a.add("something");
a.add("something");
a.contents; // two objects in the bag
::