rsc3/doc-schelp/HelpSource/Classes/ArrayedCollection.schelp

526 lines
14 KiB
Text

CLASS::ArrayedCollection
categories::Collections>Ordered
summary:: Abstract superclass of Collections of fixed maximum size
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 link::Classes/Array:: (for holding objects), and link::Classes/RawArray::, from which link::Classes/Int8Array::, link::Classes/FloatArray::, link::Classes/Signal:: etc. inherit.
CLASSMETHODS::
method::newClear
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.
code::
Array.newClear(4).postln;
::
method::with
Create a new ArrayedCollection whose slots are filled with the given arguments.
code::
Array.with(7, 'eight', 9).postln;
::
method::series
Fill an ArrayedCollection with an arithmetic series.
code::
Array.series(5, 10, 2).postln;
::
method::geom
Fill an ArrayedCollection with a geometric series.
code::
Array.geom(5, 1, 3).postln;
::
method::iota
Fills an ArrayedCollection with a counter. See link::Guides/J-concepts-in-SC:: for more examples.
code::
Array.iota(2, 3);
Array.iota(2, 3, 4);
::
INSTANCEMETHODS::
method::size
Return the number of elements the ArrayedCollection.
method::maxSize
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.
code::
[4, 5, 6].maxSize; // gosh
::
method::at
Return the item at strong::index::.
The index can also be an Array of indices to extract specified elements. Example:
code::
x = [10,20,30];
y = [0,0,2,2,1];
x[y]; // returns [ 10, 10, 30, 30, 20 ]
::
method::clipAt
Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
code::
y = [ 1, 2, 3 ];
y.clipAt(13).postln;
::
method::wrapAt
Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
code::
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
::
method::foldAt
Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
code::
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
::
method::plot
Plot data in a GUI window. See link::Reference/plot:: for more details.
method::swap
Swap the values at indices i and j.
code::
[ 1, 2, 3 ].swap(0, 2).postln;
::
method::put
Put strong::item:: at strong::index::, replacing what is there.
method::clipPut
Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
method::wrapPut
Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
method::foldPut
Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
method::putEach
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.
code::
y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
y.putEach([4, 7], [\smelly, \head]);
y.putEach([2, 3, 5, 6], \wotsits);
::
method::indexOf
Return the first index containing an item which matches strong::item::.
code::
y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
y.indexOf(\symbol);
::
method::includes
Return a boolean indicating whether the collection contains anything matching strong::item::.
code::
y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
y.includes(\symbol);
y.includes(\solipsism);
::
method::indexOfGreaterThan
Return the first index containing an item which is greater than strong::item::.
code::
y = [ 10, 5, 77, 55, 12, 123];
y.indexOfGreaterThan(70);
::
method::removeAt
Remove and return the element at strong::index::, shrinking the size of the ArrayedCollection.
code::
y = [ 1, 2, 3 ];
y.removeAt(1);
y.postln;
::
method::takeAt
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.
code::
y = [ 1, 2, 3, 4, 5 ];
y.takeAt(1);
y.postln;
::
method::takeThese
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::.
code::
y = [ 1, 2, 3, 4 ];
y.takeThese({ arg item, index; item.odd; }); //remove odd items
y.postln;
::
method::add
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 code::add:: changing the receiver.
code::
(
// z and y are the same object
var y, z;
z = [1, 2, 3];
y = z.add(4);
z.postln;
y.postln;
)
(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
y = z.add(5);
z.postln;
y.postln;
)
::
method::addAll
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 code::addAll:: to a variable - never depend on add changing the receiver.
code::
(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
y = z.addAll([7, 8, 9]);
z.postln;
y.postln;
)
::
method::extend
Extends the object to match strong::size:: by adding a number of strong::item::s. If strong::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 code::extend:: to a variable - never depend on add changing the receiver.
code::
(
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;
)
::
method::fill
Inserts the item into the contents of the receiver. note::the difference between this and link::Classes/Collection#fill#Collection's *fill::.::
code::
(
var z;
z = [1, 2, 3, 4];
z.fill(4).postln;
z.fill([1,2,3,4]).postln;
)
::
method::insert
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 code::insert:: to a variable - never depend on add changing the receiver.
code::
(
// 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;
)
::
method::move
Moves an item from one position to another.
code::
[10, 20, 1000, 40, 50].move(2, 0) // move 1000 to index 0
::
argument::fromIndex
The position in the array from which the element is removed.
argument::toIndex
The position in the array before which the element is inserted again.
method::addFirst
Inserts the item before the contents of the receiver, possibly returning a new collection.
code::
(
// in this case a new object is returned
var y, z;
z = [1, 2, 3, 4];
y = z.addFirst(999);
z.postln;
y.postln;
)
::
method::pop
Remove and return the last element of the ArrayedCollection.
code::
(
var z;
z = [1, 2, 3, 4];
z.pop.postln;
z.postln;
)
::
method::grow
Increase the size of the ArrayedCollection by strong::sizeIncrease:: number of slots, possibly returning a new collection.
method::growClear
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.
code::
// Compare:
[4,5,6].grow(5);
[4,5,6].growClear(5);
::
method::copyRange
Return a new ArrayedCollection which is a copy of the indexed slots of the receiver from strong::start:: to strong::end::. If strong::end:: < strong::start::, an empty ArrayedCollection is returned.
code::
(
var y, z;
z = [1, 2, 3, 4, 5];
y = z.copyRange(1,3);
z.postln;
y.postln;
)
::
warning:: code::x.copyRange(a, b):: is strong::not:: equivalent to code::x[a..b]::. The latter compiles to link::#-copySeries::, which has different behavior when strong::end:: < strong::start::. ::
method::copySeries
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::. If strong::second:: is code::nil::, a step of 1 or -1 is used as appropriate.
code::x.copySeries(a, nil, c):: is equivalent to code::x[a..c]::, and code::x.copySeries(a, b, c):: is equivalent to code::x[a,b..c]::
code::
(
var y, z;
z = [1, 2, 3, 4, 5, 6];
y = z.copySeries(0, 2, 5);
y.postln;
)
::
note::If the intent is to copy emphasis::forward:: in an array, and you are calculating start and end indices such that code::end:: may be less than code::start::, it is not safe to use code::copySeries:: or the shortcut syntax code::x[a..b]:: because it will adapt to use a positive or negative step as needed. In this case, code::copyRange:: is recommended.
code::
a = Array.series(10, 0, 1);
a[2..0]; // [ 2, 1, 0 ]
a.copyRange(2, 0); // [ ]
::
::
method::seriesFill
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:: ...
code::
(
var y;
y = Array.newClear(15);
y.seriesFill(5, 3);
y.postln;
)
::
method::putSeries
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::.
code::x.putSeries(a, b, c, val):: can also be written as code::x[a, b..c] = val::
code::
(
var y, z;
z = [1, 2, 3, 4, 5, 6];
y = z.putSeries(0, 2, 5, "foo");
y.postln;
)
::
method::++
Concatenate the contents of the two collections into a new ArrayedCollection.
code::
(
var y, z;
z = [1, 2, 3, 4];
y = z ++ [7, 8, 9];
z.postln;
y.postln;
)
::
method::reverse
Return a new ArrayedCollection whose elements are reversed.
code::
(
var y, z;
z = [1, 2, 3, 4];
y = z.reverse;
z.postln;
y.postln;
)
::
method::do
Iterate over the elements in order, calling the function for each element. The function is passed two arguments, the element and an index.
code::
['a', 'b', 'c'].do({ arg item, i; [i, item].postln; });
::
method::reverseDo
Iterate over the elements in reverse order, calling the function for each element. The function is passed two arguments, the element and an index.
code::
['a', 'b', 'c'].reverseDo({ arg item, i; [i, item].postln; });
::
method::collect
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.
method::deepCollect
The same as link::#-collect::, but can look inside sub-arrays up to the specified strong::depth::.
code::
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;
::
method::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.
code::
(
Array.fill(10, {
[0.1, 0.6, 0.3].windex;
}).postln;
)
::
method::normalizeSum
Returns the Array resulting from :
code::
(this / this.sum)
::
so that the array will sum to 1.0.
This is useful for using with windex or wchoose.
code::
[1, 2, 3].normalizeSum.postln;
::
method::normalize
Returns a new Array with the receiver items normalized between strong::min:: and strong::max::.
code::
[1, 2, 3].normalize; //default min=0, max= 1
[1, 2, 3].normalize(-20, 10);
::
method::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.::
code::
(
var y, z;
z = [ 1, 2, 3, 4, 5, 6 ];
y = z.perfectShuffle;
z.postln;
y.postln;
)
::
method::performInPlace
Performs a method in place, within a certain region [from..to], returning the same array.
code::
a = (0..10);
a.performInPlace(\normalizeSum, 3, 6);
::
method::rank
Rank is the number of dimensions in a multidimensional array.
code::
a = [4,7,6,8];
a.rank;
a = [[4,7],[6,8]];
a.rank;
::
method::shape
For a multidimensional array, returns the number of elements along each dimension.
code::
a = [4,7,6,8];
a.shape;
a = [[4,7],[6,8]];
a.shape;
::
method::reshape
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.
code::
a = [4,7,6,8];
a.reshape(2,2);
a.reshape(2,3);
::
method::find
Finds the starting index of a number of elements contained in the array.
code::
a = (0..10);
a.find([4, 5, 6]);
::
method::replace
Return a new array in which a number of elements have been replaced by another.
code::
a = (0..10) ++ (0..10);
a.replace([4, 5, 6], 100);
a.replace([4, 5, 6], [1734, 1985, 1860]);
::
this method is inherited by link::Classes/String:: :
code::
a = "hello world";
a.replace("world", "word");
::
method::asRandomTable
Return an integral table that can be used to generate random numbers with a specified distribution.
(see link::Guides/Randomness:: helpfile for a more detailed example)
code::
(
a = (0..100) ++ (100..50) / 100; // distribution
a = a.asRandomTable;
)
::
method::tableRand
Returns a new random number from a random table.
code::
(
a = (0..100) ++ (100..50) / 100; // distribution
a = a.asRandomTable;
20.do { a.tableRand.postln };
)
::
method::msgSize
Return the size of an osc message in bytes
code::
a = ["/s_new", "default", -1, "freq", 440];
a.msgSize;
::
method::bundleSize
Return the size of an osc bundle in bytes
code::
a = [["/s_new", "default", -1, "freq", 440], ["/s_new", "default", -1, "freq", 220]];
a.bundleSize;
::
method::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).
code::
a = (0, pi/10 .. 5pi).collect{|val| val.sin};
a.asciiPlot;
::