SuperCollider CLASSES

# ArrayedCollection

Abstract superclass of Collections of fixed maximum size
Source: /usr/local/share/SuperCollider/SCClassLibrary/Common/Collections/ArrayedCollection.sc
Subclasses: Array, RawArray

## Description

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.

Its principal subclasses are Array (for holding objects), and RawArray, from which Int8Array, FloatArray, Signal etc. inherit.

## Class Methods

### *newClear (indexedSize: 0)

Creates a new instance with indexedSize indexable slots. The slots are filled with Nil, zero or something else appropriate to the type of indexable slots in the object.

Array.newClear(4).postln;

### *with ( ... args)

From superclass: Collection

Create a new ArrayedCollection whose slots are filled with the given arguments.

Array.with(7, 'eight',  9).postln;

### *series (size, start: 0, step: 1)

From superclass: SequenceableCollection

Fill an ArrayedCollection with an arithmetic series.

Array.series(5, 10, 2).postln;

### *geom (size, start, grow)

From superclass: SequenceableCollection

Fill an ArrayedCollection with a geometric series.

Array.geom(5, 1, 3).postln;

### *iota ( ... sizes)

Fills an ArrayedCollection with a counter. See J concepts in SC for more examples.

Array.iota(2, 3);
Array.iota(2, 3, 4);

## Instance Methods

### -size

Return the number of elements the ArrayedCollection.

### -maxSize

Return the maximum number of elements the ArrayedCollection can hold. For example, Arrays may initialise themselves with a larger capacity than the number of elements added.

[4, 5, 6].maxSize; // gosh

### -at (index)

Return the item at index.

The index can also be an Array of indices to extract specified elements. Example:

x = [10,20,30];
y = [0,0,2,2,1];
x[y]; // returns [ 10, 10, 30, 30, 20 ]

### -clipAt (index)

Same as -at, but values for index greater than the size of the ArrayedCollection will be clipped to the last index.

y = [ 1, 2, 3 ];
y.clipAt(13).postln;

### -wrapAt (index)

Same as -at, but values for index greater than the size of the ArrayedCollection will be wrapped around to 0.

y = [ 1, 2, 3 ];
y.wrapAt(3).postln; // this returns the value at index 0
y.wrapAt(4).postln; // this returns the value at index 1
y.wrapAt([-2, 1])   // index can also be a collection or negative numbers

### -foldAt (index)

Same as -at, but values for index greater than the size of the ArrayedCollection will be folded back.

y = [ 1, 2, 3 ];
y.foldAt(3).postln; // this returns the value at index 1
y.foldAt(4).postln; // this returns the value at index 0
y.foldAt(5).postln; // this returns the value at index 1

### -plot (name, bounds, discrete: false, numChannels, minval, maxval)

Plot data in a GUI window. See plot for more details.

### -swap (i, j)

Swap the values at indices i and j.

[ 1, 2, 3 ].swap(0, 2).postln;

### -put (index, item)

Put item at index, replacing what is there.

### -clipPut (index, item)

Same as -put, but values for index greater than the size of the ArrayedCollection will be clipped to the last index.

### -wrapPut (index, item)

Same as -put, but values for index greater than the size of the ArrayedCollection will be wrapped around to 0.

### -foldPut (index, item)

Same as -put, but values for index greater than the size of the ArrayedCollection will be folded back.

### -putEach (keys, values)

Put the values in the corresponding indices given by keys. If one of the two argument arrays is longer then it will wrap.

y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
y.putEach([2, 3, 5, 6], \wotsits);

### -indexOf (item)

Return the first index containing an item which matches item.

y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
y.indexOf(\symbol);

### -includes (item)

Return a boolean indicating whether the collection contains anything matching item.

y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
y.includes(\symbol);
y.includes(\solipsism);

### -indexOfGreaterThan (val)

Return the first index containing an item which is greater than item.

y = [ 10, 5, 77, 55, 12, 123];
y.indexOfGreaterThan(70);

### -removeAt (index)

Remove and return the element at index, shrinking the size of the ArrayedCollection.

y = [ 1, 2, 3 ];
y.removeAt(1);
y.postln;

### -takeAt (index)

Similar to -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.

y = [ 1, 2, 3, 4, 5 ];
y.takeAt(1);
y.postln;

### -takeThese (func)

Removes all items in the receiver for which the func answers true. The function is passed two arguments, the item and an integer index. Note that order is not preserved. See -takeAt.

y = [ 1, 2, 3, 4 ];
y.takeThese({ arg item, index; item.odd; });    //remove odd items
y.postln;

Adds an item to an ArrayedCollection if there is space. This method may return a new ArrayedCollection. For this reason, you should always assign the result of add to a variable - never depend on add changing the receiver.

(
// z and y are the same object
var y, z;
z = [1, 2, 3];
z.postln;
y.postln;
)

(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
z.postln;
y.postln;
)

Adds all the elements of aCollection to the contents of the receiver. This method may return a new ArrayedCollection. For this reason, you should always assign the result of addAll to a variable - never depend on add changing the receiver.

(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
z.postln;
y.postln;
)

### -extend (size, item)

Extends the object to match size by adding a number of items. If size is less than receiver size then truncate. This method may return a new ArrayedCollection. For this reason, you should always assign the result of extend to a variable - never depend on add changing the receiver.

(
var y, z;
z = [1, 2, 3, 4];
y = z.extend(10, 9);        //fill up with 9 until the size equals 10
z.postln;
y.postln;
)

### -fill (value)

Inserts the item into the contents of the receiver.

NOTE: the difference between this and Collection's *fill.

(
var z;
z = [1, 2, 3, 4];
z.fill(4).postln;
z.fill([1,2,3,4]).postln;
)

### -insert (index, item)

Inserts the item into the contents of the receiver. This method may return a new ArrayedCollection. For this reason, you should always assign the result of insert to a variable - never depend on add changing the receiver.

(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
y = z.insert(1, 999);
z.postln;
y.postln;
)

Inserts the item before the contents of the receiver, possibly returning a new collection.

(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
z.postln;
y.postln;
)

### -pop

Remove and return the last element of the ArrayedCollection.

(
var z;
z = [1, 2, 3, 4];
z.pop.postln;
z.postln;
)

### -grow (sizeIncrease)

Increase the size of the ArrayedCollection by sizeIncrease number of slots, possibly returning a new collection.

### -growClear (sizeIncrease)

Increase the size of the ArrayedCollection by sizeIncrease number of slots, returning a new collection with Nils in the added slots.

// Compare:
[4,5,6].grow(5);
[4,5,6].growClear(5);

### -copyRange (start, end)

Return a new ArrayedCollection which is a copy of the indexed slots of the receiver from start to end. x.copyRange(a, b) can also be written as x[a..b]

(
var y, z;
z = [1, 2, 3, 4, 5];
y = z.copyRange(1,3);
z.postln;
y.postln;
)

### -copySeries (first, second, last)

Return a new ArrayedCollection consisting of the values starting at first, then every step of the distance between first and second, up until last. x.copySeries(a, b, c) can also be written as x[a, b..c]

(
var y, z;
z = [1, 2, 3, 4, 5, 6];
y = z.copySeries(0, 2, 5);
y.postln;
)

### -seriesFill (start, step)

Fill the receiver with an arithmetic progression. The first element will be start, the second start + step, the third start + step + step ...

(
var y;
y = Array.newClear(15);
y.seriesFill(5, 3);
y.postln;
)

### -putSeries (first, second, last, value)

Put value at every index starting at first, then every step of the distance between first and second, up until last. x.putSeries(a, b, c, val) can also be written as x[a, b..c] = val

(
var y, z;
z = [1, 2, 3, 4, 5, 6];
y = z.putSeries(0, 2, 5, "foo");
y.postln;
)

### -++ (anArray)

Concatenate the contents of the two collections into a new ArrayedCollection.

(
var y, z;
z = [1, 2, 3, 4];
y = z ++ [7, 8, 9];
z.postln;
y.postln;
)

### -reverse

Return a new ArrayedCollection whose elements are reversed.

(
var y, z;
z = [1, 2, 3, 4];
y = z.reverse;
z.postln;
y.postln;
)

### -do (function)

Iterate over the elements in order, calling the function for each element. The function is passed two arguments, the element and an index.

['a', 'b', 'c'].do({ arg item, i; [i, item].postln; });

### -reverseDo (function)

Iterate over the elements in reverse order, calling the function for each element. The function is passed two arguments, the element and an index.

['a', 'b', 'c'].reverseDo({ arg item, i; [i, item].postln; });

### -collect (function)

From superclass: Collection

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 Collection helpfile for examples.

### -deepCollect (depth: 1, function, index: 0, rank: 0)

The same as -collect, but can look inside sub-arrays up to the specified depth.

a = [99, [4,6,5], [[32]]];
a.deepCollect(1, {|item| item.isArray}).postln;
a.deepCollect(2, {|item| item.isArray}).postln;
a.deepCollect(3, {|item| item.isArray}).postln;

### -windex

Interprets the array as a list of probabilities which should sum to 1.0 and returns a random index value based on those probabilities.

(
Array.fill(10, {
[0.1, 0.6, 0.3].windex;
}).postln;
)

### -normalizeSum

Returns the Array resulting from :

(this / this.sum)

so that the array will sum to 1.0.

This is useful for using with windex or wchoose.

[1, 2, 3].normalizeSum.postln;

### -normalize (min: 0, max: 1)

Returns a new Array with the receiver items normalized between min and max.

[1, 2, 3].normalize;            //default min=0, max= 1
[1, 2, 3].normalize(-20, 10);

### -perfectShuffle

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.

(
var y, z;
z = [ 1, 2, 3, 4, 5, 6 ];
y = z.perfectShuffle;
z.postln;
y.postln;
)

### -performInPlace (selector, from, to, argList)

Performs a method in place, within a certain region [from..to], returning the same array.

a = (0..10);
a.performInPlace(\normalizeSum, 3, 6);

### -rank

Rank is the number of dimensions in a multidimensional array.

a = [4,7,6,8];
a.rank;
a = [[4,7],[6,8]];
a.rank;

### -shape

For a multidimensional array, returns the number of elements along each dimension.

a = [4,7,6,8];
a.shape;
a = [[4,7],[6,8]];
a.shape;

### -reshape ( ... shape)

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.

a = [4,7,6,8];
a.reshape(2,2);
a.reshape(2,3);

### -find (sublist, offset: 0)

From superclass: SequenceableCollection

Finds the starting index of a number of elements contained in the array.

a = (0..10);
a.find([4, 5, 6]);

### -replace (find, replace)

Return a new array in which a number of elements have been replaced by another.

a = (0..10) ++ (0..10);
a.replace([4, 5, 6], 100);
a.replace([4, 5, 6], [1734, 1985, 1860]);

this method is inherited by String :

a = "hello world";
a.replace("world", "word");

### -asRandomTable (size)

Return an integral table that can be used to generate random numbers with a specified distribution. (see Randomness helpfile for a more detailed example)

(
a = (0..100) ++ (100..50) / 100; // distribution
a = a.asRandomTable;
)

### -tableRand

Returns a new random number from a random table.

(
a = (0..100) ++ (100..50) / 100; // distribution
a = a.asRandomTable;
20.do { a.tableRand.postln };
)

### -msgSize

Return the size of an osc message in bytes

a = ["/s_new", "default", -1, "freq", 440];
a.msgSize;

### -bundleSize

Return the size of an osc bundle in bytes

a = [["/s_new", "default", -1, "freq", 440], ["/s_new", "default", -1, "freq", 220]];
a.bundleSize;

### -asciiPlot

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).

a = (0, pi/10 .. 5pi).collect{|val| val.sin};
a.asciiPlot;