1 ArrayedCollection : SequenceableCollection {
2 *newClear { arg indexedSize = 0;
5 // creates a new instance with indexedSize indexable slots.
6 // the slots are filled with nil, zero or something else
7 // appropriate to the type of indexable slots in the
11 // ArrayedCollections are vectors which have a
12 // fixed maximum capacity.
14 indexedSize { _BasicSize }
16 maxSize { _BasicMaxSize }
18 swap { arg i, j; var temp;
20 ^this.primitiveFailed;
25 ^this.primitiveFailed;
29 ^this.primitiveFailed;
33 ^this.primitiveFailed;
37 ^this.primitiveFailed;
39 put { arg index, item;
41 ^this.primitiveFailed;
43 clipPut { arg index, item;
45 ^this.primitiveFailed;
47 wrapPut { arg index, item;
49 ^this.primitiveFailed;
51 foldPut { arg index, item;
53 ^this.primitiveFailed;
57 ^this.primitiveFailed;
61 ^this.primitiveFailed;
66 ^this.primitiveFailed;
68 indexOfGreaterThan { arg val;
69 _ArrayIndexOfGreaterThan
70 ^super.indexOfGreaterThan(val)
75 while { i < this.size }
77 if (func.value(this[i], i)) {
85 replace { arg find, replace;
86 var index, out = [], array = this;
88 replace = replace.asArray;
90 (index = array.find(find)).notNil
92 out = out ++ array.keep(index) ++ replace;
93 array = array.drop(index + find.size);
98 // see counterparts to these in Object
105 slotPut { arg index, value;
106 ^this.put(index, value);
117 setSlots { arg array;
118 this.overWrite(array)
121 atModify { arg index, function; this.put(index, function.value(this.at(index), index)) }
122 atInc { arg index, inc=1; this.put(index, this.at(index)+inc); }
123 atDec { arg index, dec=1; this.put(index, this.at(index)-dec); }
128 copyRange { arg start, end;
129 // copies the fixed part of an object and the indexed slots
130 // from start to end.
132 ^this.primitiveFailed
134 copySeries { arg first, second, last;
135 // copies elements from first to last, stepping by (second-first)
136 // copySeries(3,5,11) copies elements 3,5,7,9,11
137 // if second is nil then the step is 1.
138 // copySeries(3,nil,11) copies elements 3,4,5,6,7,8,9,10,11
140 ^this.primitiveFailed
142 putSeries { arg first, second, last, value;
143 // puts value into array at indices defined by the series. see copySeries.
145 ^this.primitiveFailed
149 // add item to end of array.
150 // if the capacity is exceeded, this returns a new
151 // ArrayedCollection.
153 ^this.primitiveFailed;
155 addAll { arg aCollection;
159 aCollection.asCollection.do({ arg item; array = array.add(item) }) ;
162 putEach { arg keys, values;
164 ^super.putEach(keys, values)
166 extend { arg size, item;
168 ^this.primitiveFailed
170 insert { arg index, item;
171 // add item at specified index.
172 // if the capacity is exceeded, this returns a new
173 // ArrayedCollection.
175 ^this.primitiveFailed;
177 addFirst { arg item; ^this.insert(0, item) }
178 addIfNotNil { arg item; if(item.notNil,{ ^this.add(item) }) }
180 // remove and return last item in array
185 // concatenate two arrays of the same type
186 // this primitive will handle all array element types
188 // primitive fails if arrays are different types
191 overWrite { arg aCollection, pos=0;
194 ^this.primitiveFailed
195 //array = this.growClear(pos + aCollection.size - this.size);
196 //grow = pos + aCollection.size - this.size;
197 //if (grow > 0, { array = this ++ this.class.newClear(grow); },{ array = this });
198 //aCollection.do({ arg item, i; array.put(pos + i, item); });
201 grow { arg sizeIncrease;
202 // returns an array of sufficient capacity.
203 // may return same object if it still has enough space or if sizeIncrease <= 0.
205 ^this.primitiveFailed
207 growClear { arg sizeIncrease;
208 // returns an array of sufficient capacity.
209 // may return same object if it still has enough space or if sizeIncrease <= 0.
212 ^this.primitiveFailed
214 seriesFill { arg start, step;
215 this.size.do({ arg i;
217 start = start + step;
223 ^this.primitiveFailed
224 /* replaced by primitive
227 while ({ i < size }, {
234 // special byte codes inserted by compiler for this method
236 while ({ i < this.size }, {
237 function.value(this.at(i), i);
241 reverseDo { arg function;
242 // special byte codes inserted by compiler for this method
246 function.value(this.at(i), j);
254 var size1 = res.size - 1;
255 var halfsize = res.size div: 2;
257 res.swap(i, size1 - i);
264 ^this.primitiveFailed
265 // var r, sum = 0.0, index;
267 // this.detect({ arg weight, i;
268 // sum = sum + weight;
280 normalize { arg min=0.0, max=1.0;
281 var minItem = this.minItem;
282 var maxItem = this.maxItem;
283 ^this.collect { |el| el.linlin(minItem, maxItem, min, max) };
287 // draw the waveform down the page as asterisks
288 var lo = this.minItem;
289 var hi = this.maxItem;
290 var scale = 80 / (hi - lo);
292 var pt = ((this[i] - lo) * scale).asInteger;
293 pt.do({ " ".post; });
299 if(this.size < 2) { ^this.copy };
300 ^this[(0 .. this.size div: 2 - 1).stutter + [0, this.size + 1 div: 2]]
303 performInPlace { arg selector, from, to, argList;
304 ^this.overWrite(this.copyRange(from, to).performList(selector, argList), from)
308 // concepts borrowed from J programming language
310 // rank is the number of dimensions in a multidimensional array.
311 // see also Object-rank
312 // this assumes every element has the same rank
313 ^ 1 + this.first.rank
316 // this assumes every element has the same shape
317 ^[this.size] ++ this[0].shape
319 reshape { arg ... shape;
320 var size = shape.product;
321 var result = this.flat.wrapExtend(size);
322 shape[1..].reverseDo {|n| result = result.clump(n) };
325 deepCollect { arg depth = 1, function;
327 ^function.value(this, 0)
330 ^this.collect {|item| item.deepCollect(depth, function) }
332 reshapeLike { arg another, indexing=\wrapAt;
334 var flat = this.flat;
335 ^another.deepCollect(0x7FFFFFFF) {
336 var item = flat.perform(indexing, index);
342 unbubble { arg depth=0, levels=1;
344 // converts a size 1 array to the item.
345 if (this.size > 1) { ^this };
346 if (levels <= 1) { ^this[0] }
347 ^this[0].unbubble(depth, levels-1)
349 ^this.collect {|item| item.unbubble(depth-1) }
351 bubble { arg depth=0, levels=1;
353 if (levels <= 1) { ^[this] }
354 ^[this.bubble(depth,levels-1)]
356 ^this.collect {|item| item.bubble(depth-1, levels) }
360 slice { arg ... cuts;
361 var firstCut, index, list;
362 if (cuts.size == 0) { ^this.copy };
365 if (firstCut.isNil) { list = this.copy } { list = this[firstCut.asArray] };
366 if (cuts.size == 1) {
370 ^list.collect {|item| item.slice(*cuts) }.unbubble
373 *iota { arg ... sizes;
374 ^(0..sizes.product-1).reshape(*sizes)
376 *fill2D { arg rows, cols, function;
377 var array = this.new(rows);
379 var array2 = this.new(cols);
381 array2 = array2.add(function.(row, col))
383 array = array.add(array2);
387 *fill3D { arg planes, rows, cols, function;
388 var array = this.new(planes);
390 var array2 = this.new(rows);
392 var array3 = this.new(cols);
394 array3 = array3.add(function.(plane, row, col))
396 array2 = array2.add(array3);
398 array = array.add(array2);
402 *fillND { arg dimensions, function, args=[]; // args are private
403 var n = dimensions.first;
404 var array = this.new(n);
405 var argIndex = args.size;
407 if(dimensions.size <= 1) {
408 n.do { |i| array.add(function.valueArray(args.put(argIndex, i))) };
410 dimensions = dimensions.drop(1);
412 var array2 = this.fillND(dimensions, function, args.put(argIndex, i));
413 array = array.add(array2);
420 // random distribution table
422 asRandomTable { arg size;
424 if(size.isNil) { size = this.size } { a = a.resamp1(size) };
425 a = a.integrate; // incrementally integrate
426 a = a.normalize(0, size-1); // normalize and scale by max index
427 b = Array.fill(size, { arg i; a.indexInBetween(i) }); // flip array
428 b = b / size // rescale to 0..1
433 ^this.blendAt((this.size - 1).asFloat.rand)
436 // osc bundle support
440 ^this.primitiveFailed
444 ^([nil] ++ this).prBundleSize;
447 var size=0, res, clumps, count=0, bundleSizes;
448 bundleSizes = this.collect {|item| [item].bundleSize };
449 bundleSizes.do { |a, i|
451 if(size >= 8192) { clumps = clumps.add(count); count = 0; size = a };
454 ^this.clumps(clumps);
459 ^this.primitiveFailed
461 includes { |item| ^this.indexOf(item).notNil }
464 RawArray : ArrayedCollection {
465 // species { ^this.class }
466 archiveAsCompileString { ^true }
467 archiveAsObject { ^true }
471 readFromStream { |stream, method|
474 this.put(i, stream.perform(method));
477 this.shouldNotImplement(thisMethod);
482 Int8Array[int8] : RawArray {
485 ^this.primitiveFailed
488 readFromStream { |stream|
489 super.readFromStream(stream, \getInt8);
493 Int16Array[int16] : RawArray {
494 readFromStream { |stream|
495 super.readFromStream(stream, \getInt16);
499 Int32Array[int32] : RawArray {
500 readFromStream { |stream|
501 super.readFromStream(stream, \getInt32);
505 FloatArray[float] : RawArray {
506 readFromStream { |stream|
507 super.readFromStream(stream, \getFloat);
511 DoubleArray[double] : RawArray {
512 readFromStream { |stream|
513 super.readFromStream(stream, \getDouble);
517 // readFromStream not implemented yet
518 SymbolArray[symbol] : RawArray {