1 CLASS::ArrayedCollection
2 categories::Collections>Ordered
3 summary:: Abstract superclass of Collections of fixed maximum size
6 ArrayedCollection is an abstract class, a subclass of SequenceableCollections whose elements are held in a vector of slots. Instances of ArrayedCollection have a fixed maximum size beyond which they may not grow.
8 Its principal subclasses are link::Classes/Array:: (for holding objects), and link::Classes/RawArray::, from which link::Classes/Int8Array::, link::Classes/FloatArray::, link::Classes/Signal:: etc. inherit.
13 Creates a new instance with strong::indexedSize:: indexable slots. The slots are filled with link::Classes/Nil::, zero or something else appropriate to the type of indexable slots in the object.
15 Array.newClear(4).postln;
19 Create a new ArrayedCollection whose slots are filled with the given arguments.
21 Array.with(7, 'eight', 9).postln;
25 Fill an ArrayedCollection with an arithmetic series.
27 Array.series(5, 10, 2).postln;
31 Fill an ArrayedCollection with a geometric series.
33 Array.geom(5, 1, 3).postln;
37 Fills an ArrayedCollection with a counter. See link::Guides/J-concepts-in-SC:: for more examples.
44 Creates a 2 dimensional ArrayedCollection of the given sizes. The items are determined by evaluation of the supplied function. The function is passed row and column indexes as arguments. See link::Guides/J-concepts-in-SC::
46 Array.fill2D(2, 4, 0);
47 Array.fill2D(3, 4, { arg r, c; r*c+c; });
51 Creates a 3 dimensional ArrayedCollection of the given sizes. The items are determined by evaluation of the supplied function. The function is passed plane, row and column indexes as arguments. See link::Guides/J-concepts-in-SC::
53 Array.fill3D(2, 3, 4, { arg p, r, c; p; });
57 Creates a N dimensional ArrayedCollection where N is the size of the array strong::dimensions::. The items are determined by evaluation of the supplied function. The function is passed N number of indexes as arguments.
59 Array.fillND([4, 4], { arg a, b; a+b; }); //2d
60 Array.fillND([4, 4, 4], { arg a, b, c; a+b*c; }); //3d
61 Array.fillND([1, 2, 3, 4], { arg a, b, c, d; b+d; }); //4d
67 Return the number of elements the ArrayedCollection.
70 Return the maximum number of elements the ArrayedCollection can hold. For example, link::Classes/Array::s may initialise themselves with a larger capacity than the number of elements added.
72 [4, 5, 6].maxSize; // gosh
76 Return the item at strong::index::.
79 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
86 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
89 y.wrapAt(3).postln; // this returns the value at index 0
90 y.wrapAt(4).postln; // this returns the value at index 1
94 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
97 y.foldAt(3).postln; // this returns the value at index 1
98 y.foldAt(4).postln; // this returns the value at index 0
99 y.foldAt(5).postln; // this returns the value at index 1
103 Plot data in a GUI window. See link::Reference/plot:: for more details.
106 Swap the values at indices i and j.
108 [ 1, 2, 3 ].swap(0, 2).postln;
112 Put strong::item:: at strong::index::, replacing what is there.
115 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
118 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
121 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
124 Put the strong::values:: in the corresponding indices given by strong::keys::. If one of the two argument arrays is longer then it will wrap.
126 y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
127 y.putEach([4, 7], [\smelly, \head]);
128 y.putEach([2, 3, 5, 6], \wotsits);
132 Return the first index containing an item which matches strong::item::.
134 y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
139 Return a boolean indicating whether the collection contains anything matching strong::item::.
141 y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
143 y.includes(\solipsism);
146 method::indexOfGreaterThan
147 Return the first index containing an item which is greater than strong::item::.
149 y = [ 10, 5, 77, 55, 12, 123];
150 y.indexOfGreaterThan(70);
154 Remove and return the element at strong::index::, shrinking the size of the ArrayedCollection.
162 Similar to link::#-removeAt::, but does not maintain the order of the items following the one that was removed. Instead, the last item is placed into the position of the removed item and the array's size decreases by one.
164 y = [ 1, 2, 3, 4, 5 ];
170 Removes all items in the receiver for which the strong::func:: answers true. The function is passed two arguments, the item and an integer index. note::that order is not preserved. See link::#-takeAt::.
173 y.takeThese({ arg item, index; item.odd; }); //remove odd items
178 Adds an item to an ArrayedCollection if there is space. If there is not any space left in the object then this method returns a new ArrayedCollection. For this reason, you should always assign the result of add to a variable - never depend on add changing the receiver.
181 // z and y are the same object
190 // in this case a new object is returned
200 Adds all the elements of aCollection to the contents of the receiver, possibly returning a new collection.
203 // in this case a new object is returned
206 y = z.addAll([7, 8, 9]);
213 Extends the receiver to match strong::size:: by adding a number of strong::item::s. If strong::size:: is less than receiver size then truncate.
218 y = z.extend(10, 9); //fill up with 9 until the size equals 10
225 Inserts the item into the contents of the receiver, possibly returning a new collection. note::the difference between this and link::Classes/Collection#fill#Collection's *fill::.::
231 z.fill([1,2,3,4]).postln;
236 Inserts the item into the contents of the receiver, possibly returning a new collection.
239 // in this case a new object is returned
242 y = z.insert(1, 999);
249 Inserts the item before the contents of the receiver, possibly returning a new collection.
252 // in this case a new object is returned
262 Remove and return the last element of the ArrayedCollection.
273 Increase the size of the ArrayedCollection by strong::sizeIncrease:: number of slots, possibly returning a new collection.
276 Increase the size of the ArrayedCollection by strong::sizeIncrease:: number of slots, returning a new collection with link::Classes/Nil::s in the added slots.
280 [4,5,6].growClear(5);
284 Return a new ArrayedCollection which is a copy of the indexed slots of the receiver from strong::start:: to strong::end::.
285 code::x.copyRange(a, b):: can also be written as code::x[a..b]::
290 y = z.copyRange(1,3);
297 Return a new ArrayedCollection consisting of the values starting at strong::first::, then every step of the distance between strong::first:: and strong::second::, up until strong::last::.
298 code::x.copySeries(a, b, c):: can also be written as code::x[a, b..c]::
302 z = [1, 2, 3, 4, 5, 6];
303 y = z.copySeries(0, 2, 5);
309 Fill the receiver with an arithmetic progression. The first element will be strong::start::, the second strong::start + step::, the third strong::start + step + step:: ...
313 y = Array.newClear(15);
320 Put strong::value:: at every index starting at strong::first::, then every step of the distance between strong::first:: and strong::second::, up until strong::last::.
321 code::x.putSeries(a, b, c, val):: can also be written as code::x[a, b..c] = val::
325 z = [1, 2, 3, 4, 5, 6];
326 y = z.putSeries(0, 2, 5, "foo");
332 Concatenate the contents of the two collections into a new ArrayedCollection.
344 Return a new ArrayedCollection whose elements are reversed.
356 Iterate over the elements in order, calling the function for each element. The function is passed two arguments, the element and an index.
358 ['a', 'b', 'c'].do({ arg item, i; [i, item].postln; });
362 Iterate over the elements in reverse order, calling the function for each element. The function is passed two arguments, the element and an index.
364 ['a', 'b', 'c'].reverseDo({ arg item, i; [i, item].postln; });
368 Answer a new collection which consists of the results of function evaluated for each item in the collection. The function is passed two arguments, the item and an integer index. See link::Classes/Collection:: helpfile for examples.
371 The same as link::#-collect::, but can look inside sub-arrays up to the specified strong::depth::.
373 a = [99, [4,6,5], [[32]]];
374 a.deepCollect(1, {|item| item.isArray}).postln;
375 a.deepCollect(2, {|item| item.isArray}).postln;
376 a.deepCollect(3, {|item| item.isArray}).postln;
380 Interprets the array as a list of probabilities which should sum to 1.0 and returns a random index value based on those probabilities.
384 [0.1, 0.6, 0.3].windex;
390 Returns the Array resulting from :
394 so that the array will sum to 1.0.
396 This is useful for using with windex or wchoose.
398 [1, 2, 3].normalizeSum.postln;
402 Returns a new Array with the receiver items normalized between strong::min:: and strong::max::.
404 [1, 2, 3].normalize; //default min=0, max= 1
405 [1, 2, 3].normalize(-20, 10);
408 method::perfectShuffle
409 Returns a copy of the receiver with its items split into two equal halves, then reconstructed by interleaving the two halves. note::use an even number of item pairs in order to not loose any items in the shuffle.::
413 z = [ 1, 2, 3, 4, 5, 6 ];
414 y = z.perfectShuffle;
420 method::performInPlace
421 Performs a method in place, within a certain region [from..to], returning the same array.
424 a.performInPlace(\normalizeSum, 3, 6);
428 Rank is the number of dimensions in a multidimensional array.
437 For a multidimensional array, returns the number of elements along each dimension.
446 For a multidimensional array, rearranges the data using the desired number of elements along each dimension. The data may be extended using wrapExtend if needed.
454 Finds the starting index of a number of elements contained in the array.
461 Return a new array in which a number of elements have been replaced by another.
463 a = (0..10) ++ (0..10);
464 a.replace([4, 5, 6], 100);
465 a.replace([4, 5, 6], [1734, 1985, 1860]);
467 this method is inherited by link::Classes/String:: :
470 a.replace("world", "word");
473 method::asRandomTable
474 Return an integral table that can be used to generate random numbers with a specified distribution.
475 (see link::Guides/Randomness:: helpfile for a more detailed example)
478 a = (0..100) ++ (100..50) / 100; // distribution
484 Returns a new random number from a random table.
487 a = (0..100) ++ (100..50) / 100; // distribution
489 20.do { a.tableRand.postln };
494 Return the size of an osc message in bytes
496 a = ["/s_new", "default", -1, "freq", 440];
501 Return the size of an osc bundle in bytes
503 a = [["/s_new", "default", -1, "freq", 440], ["/s_new", "default", -1, "freq", 220]];
508 For an ArrayedCollection containing numbers (e.g. audio data) this renders a plot in the post window using asterisks and spaces (works best if you use a monospace font in your post window).
510 a = (0, pi/10 .. 5pi).collect{|val| val.sin};