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.
47 Return the number of elements the ArrayedCollection.
50 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.
52 [4, 5, 6].maxSize; // gosh
56 Return the item at strong::index::.
58 The index can also be an Array of indices to extract specified elements. Example:
62 x[y]; // returns [ 10, 10, 30, 30, 20 ]
66 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
73 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
76 y.wrapAt(3).postln; // this returns the value at index 0
77 y.wrapAt(4).postln; // this returns the value at index 1
78 y.wrapAt([-2, 1]) // index can also be a collection or negative numbers
82 Same as link::#-at::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
85 y.foldAt(3).postln; // this returns the value at index 1
86 y.foldAt(4).postln; // this returns the value at index 0
87 y.foldAt(5).postln; // this returns the value at index 1
91 Plot data in a GUI window. See link::Reference/plot:: for more details.
94 Swap the values at indices i and j.
96 [ 1, 2, 3 ].swap(0, 2).postln;
100 Put strong::item:: at strong::index::, replacing what is there.
103 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be clipped to the last index.
106 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be wrapped around to 0.
109 Same as link::#-put::, but values for strong::index:: greater than the size of the ArrayedCollection will be folded back.
112 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.
114 y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
115 y.putEach([4, 7], [\smelly, \head]);
116 y.putEach([2, 3, 5, 6], \wotsits);
120 Return the first index containing an item which matches strong::item::.
122 y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
127 Return a boolean indicating whether the collection contains anything matching strong::item::.
129 y = [ \the, \symbol, \collection, \contains, \my, \symbol ];
131 y.includes(\solipsism);
134 method::indexOfGreaterThan
135 Return the first index containing an item which is greater than strong::item::.
137 y = [ 10, 5, 77, 55, 12, 123];
138 y.indexOfGreaterThan(70);
142 Remove and return the element at strong::index::, shrinking the size of the ArrayedCollection.
150 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.
152 y = [ 1, 2, 3, 4, 5 ];
158 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::.
161 y.takeThese({ arg item, index; item.odd; }); //remove odd items
166 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.
169 // z and y are the same object
178 // in this case a new object is returned
188 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.
191 // in this case a new object is returned
194 y = z.addAll([7, 8, 9]);
201 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.
206 y = z.extend(10, 9); //fill up with 9 until the size equals 10
213 Inserts the item into the contents of the receiver. note::the difference between this and link::Classes/Collection#fill#Collection's *fill::.::
219 z.fill([1,2,3,4]).postln;
224 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.
227 // in this case a new object is returned
230 y = z.insert(1, 999);
237 Inserts the item before the contents of the receiver, possibly returning a new collection.
240 // in this case a new object is returned
250 Remove and return the last element of the ArrayedCollection.
261 Increase the size of the ArrayedCollection by strong::sizeIncrease:: number of slots, possibly returning a new collection.
264 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.
268 [4,5,6].growClear(5);
272 Return a new ArrayedCollection which is a copy of the indexed slots of the receiver from strong::start:: to strong::end::.
273 code::x.copyRange(a, b):: can also be written as code::x[a..b]::
278 y = z.copyRange(1,3);
285 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::.
286 code::x.copySeries(a, b, c):: can also be written as code::x[a, b..c]::
290 z = [1, 2, 3, 4, 5, 6];
291 y = z.copySeries(0, 2, 5);
297 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:: ...
301 y = Array.newClear(15);
308 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::.
309 code::x.putSeries(a, b, c, val):: can also be written as code::x[a, b..c] = val::
313 z = [1, 2, 3, 4, 5, 6];
314 y = z.putSeries(0, 2, 5, "foo");
320 Concatenate the contents of the two collections into a new ArrayedCollection.
332 Return a new ArrayedCollection whose elements are reversed.
344 Iterate over the elements in order, calling the function for each element. The function is passed two arguments, the element and an index.
346 ['a', 'b', 'c'].do({ arg item, i; [i, item].postln; });
350 Iterate over the elements in reverse order, calling the function for each element. The function is passed two arguments, the element and an index.
352 ['a', 'b', 'c'].reverseDo({ arg item, i; [i, item].postln; });
356 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.
359 The same as link::#-collect::, but can look inside sub-arrays up to the specified strong::depth::.
361 a = [99, [4,6,5], [[32]]];
362 a.deepCollect(1, {|item| item.isArray}).postln;
363 a.deepCollect(2, {|item| item.isArray}).postln;
364 a.deepCollect(3, {|item| item.isArray}).postln;
368 Interprets the array as a list of probabilities which should sum to 1.0 and returns a random index value based on those probabilities.
372 [0.1, 0.6, 0.3].windex;
378 Returns the Array resulting from :
382 so that the array will sum to 1.0.
384 This is useful for using with windex or wchoose.
386 [1, 2, 3].normalizeSum.postln;
390 Returns a new Array with the receiver items normalized between strong::min:: and strong::max::.
392 [1, 2, 3].normalize; //default min=0, max= 1
393 [1, 2, 3].normalize(-20, 10);
396 method::perfectShuffle
397 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.::
401 z = [ 1, 2, 3, 4, 5, 6 ];
402 y = z.perfectShuffle;
408 method::performInPlace
409 Performs a method in place, within a certain region [from..to], returning the same array.
412 a.performInPlace(\normalizeSum, 3, 6);
416 Rank is the number of dimensions in a multidimensional array.
425 For a multidimensional array, returns the number of elements along each dimension.
434 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.
442 Finds the starting index of a number of elements contained in the array.
449 Return a new array in which a number of elements have been replaced by another.
451 a = (0..10) ++ (0..10);
452 a.replace([4, 5, 6], 100);
453 a.replace([4, 5, 6], [1734, 1985, 1860]);
455 this method is inherited by link::Classes/String:: :
458 a.replace("world", "word");
461 method::asRandomTable
462 Return an integral table that can be used to generate random numbers with a specified distribution.
463 (see link::Guides/Randomness:: helpfile for a more detailed example)
466 a = (0..100) ++ (100..50) / 100; // distribution
472 Returns a new random number from a random table.
475 a = (0..100) ++ (100..50) / 100; // distribution
477 20.do { a.tableRand.postln };
482 Return the size of an osc message in bytes
484 a = ["/s_new", "default", -1, "freq", 440];
489 Return the size of an osc bundle in bytes
491 a = [["/s_new", "default", -1, "freq", 440], ["/s_new", "default", -1, "freq", 220]];
496 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).
498 a = (0, pi/10 .. 5pi).collect{|val| val.sin};