polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Collections / Array2D.html
blobc0b5e6a03a09c3ed2c542531c3062d65e632e5a5
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: 13.0px Arial}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; min-height: 15.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 13.0px Arial}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
17 p.p8 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 14.0px Helvetica}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ad140d}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
20 span.s1 {font: 18.0px Helvetica}
21 span.s2 {color: #0024f8}
22 span.s3 {font: 9.0px Monaco}
23 span.s4 {color: #001bb9}
24 span.Apple-tab-span {white-space:pre}
25 </style>
26 </head>
27 <body>
28 <p class="p1"><span class="s1"><b>Array2D</b></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>two-dimensional array</b></p>
29 <p class="p2"><br></p>
30 <p class="p3"><b>Inherits from: </b><a href="Collection.html"><span class="s2"><b>Collection</b></span></a></p>
31 <p class="p2"><br></p>
32 <p class="p1">Represents a two-dimensional array of data. The number of rows and columns is fixed.</p>
33 <p class="p2"><br></p>
34 <p class="p1">See also: <a href="Array.html"><span class="s2">Array</span></a></p>
35 <p class="p2"><br></p>
36 <p class="p1"><b>Note:</b> It is possible to implement a similar behaviour using an "array-of-arrays" - see the examples towards the bottom of this page for comparison.</p>
37 <p class="p2"><br></p>
38 <p class="p2"><br></p>
39 <p class="p4"><b>Creation / Class Methods</b></p>
40 <p class="p5"><span class="Apple-tab-span"> </span></p>
41 <p class="p6"><span class="s3"><span class="Apple-tab-span"> </span></span><b>*new(rows, cols)</b></p>
42 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
43 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Create an array of the specified size.</p>
44 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
45 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a = <span class="s4">Array2D</span>.new(3,4);</p>
46 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a[2,2] = 1;</p>
47 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.postln</p>
48 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
49 <p class="p1"><span class="Apple-tab-span"> </span><b>*fromArray(rows, cols, array)</b></p>
50 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
51 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Build an Array2D from the supplied array.</p>
52 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
53 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a = <span class="s4">Array2D</span>.fromArray(3,4, [9,8,7,6,5,4,3,2,1,2,3,4]);</p>
54 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a[2,2] = 1;</p>
55 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.postln</p>
56 <p class="p2"><span class="Apple-tab-span"> </span></p>
57 <p class="p2"><span class="Apple-tab-span"> </span></p>
58 <p class="p8"><b>Instance Methods</b></p>
59 <p class="p2"><span class="Apple-tab-span"> </span></p>
60 <p class="p1"><span class="Apple-tab-span"> </span><b>at(row, col)</b></p>
61 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
62 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Get a value from the array.</p>
63 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
64 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.at(2,3);</p>
65 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a[2,3];</p>
66 <p class="p2"><br></p>
67 <p class="p1"><span class="Apple-tab-span"> </span><b>put(row, col, val)</b></p>
68 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
69 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Put a value into the array.</p>
70 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
71 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.put(2,3, 72);</p>
72 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a[2,3];</p>
73 <p class="p2"><br></p>
74 <p class="p1"><span class="Apple-tab-span"> </span><b>colsDo(function)</b></p>
75 <p class="p1"><b><span class="Apple-tab-span"> </span>rowsDo(function)</b></p>
76 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
77 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Iterate over the columns, or the rows. Each row or column will be passed to <b>function</b> in turn.</p>
78 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
79 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.colsDo(<span class="s4">_</span>.postln);</p>
80 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.rowsDo(<span class="s4">_</span>.postln);</p>
81 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
82 <p class="p2"><span class="Apple-tab-span"> </span></p>
83 <p class="p1"><span class="Apple-tab-span"> </span><b>colAt(index)</b></p>
84 <p class="p1"><b><span class="Apple-tab-span"> </span>rowAt(index)</b></p>
85 <p class="p2"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b></p>
86 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Retrieve a single row, or column.</p>
87 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
88 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.colAt(2)</p>
89 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.rowAt(2)</p>
90 <p class="p2"><span class="Apple-tab-span"> </span></p>
91 <p class="p1"><b><span class="Apple-tab-span"> </span>asArray</b></p>
92 <p class="p2"><span class="Apple-tab-span"> </span></p>
93 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Return a flat array containing the elements.</p>
94 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
95 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.postln</p>
96 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a.asArray.postln;</p>
97 <p class="p2"><span class="Apple-tab-span"> </span></p>
98 <p class="p1"><b>Examples</b></p>
99 <p class="p2"><br></p>
100 <p class="p9">// "a" is an array-of-arrays</p>
101 <p class="p7">a = { { 100.0.rand }.dup(100) }.dup(100);</p>
102 <p class="p9">// "b" is an equivalent Array2D, made using the "fromArray" class method</p>
103 <p class="p7">b = <span class="s4">Array2D</span>.fromArray(100,100, a.flat);</p>
104 <p class="p10"><br></p>
105 <p class="p9">// Accessing</p>
106 <p class="p7">a[15][22]</p>
107 <p class="p7">b[15, 22]</p>
108 <p class="p10"><br></p>
109 <p class="p9">// Speed comparison 1: random access</p>
110 <p class="p7">bench { 100.do(a[100.rand][100.rand]) }</p>
111 <p class="p7">bench { 100.do(b[100.rand, 100.rand]) }</p>
112 <p class="p10"><br></p>
113 <p class="p9">// Speed comparison 2: iteration</p>
114 <p class="p7">bench { 100.do(a.do { <span class="s4">|row|</span> row.do { <span class="s4">|item|</span> item * 2 } }) }</p>
115 <p class="p7">bench { 100.do(b.do { <span class="s4">|item|</span> item * 2 }) }</p>
116 <p class="p10"><br></p>
117 </body>
118 </html>