1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
Interval : Collection {
var <>start, <>end, <>step;
*new { arg start, end, step=1;
^super.newCopyArgs(start, end, step)
}
size { ^end - start div: step + 1 }
at { arg index;
if (index < 0 or: { index >= this.size }, { ^nil });
^step * index + start;
}
wrapAt { arg index; ^step * (index % this.size) + start }
clipAt { arg index;
if (index < 0) { ^start };
if (index >= this.size) { ^end };
^step * index + start;
}
do { arg function;
forBy(start, end, step, function);
}
add { ^this.shouldNotImplement(thisMethod) }
put { ^this.shouldNotImplement(thisMethod) }
storeArgs { ^[start, end, step] }
storeOn { arg stream;
stream << this.class.name;
this.storeParamsOn(stream);
}
printOn { arg stream; this.storeOn(stream) }
}
Range : Collection {
var <>start, <>size;
*new { arg start, size;
^super.newCopyArgs(start, size);
}
end { ^start + size }
do { arg function;
for(start, start+size-1, function);
}
at { arg index;
var val;
if (index < 0 or: { index >= size }, { ^nil });
^start + index;
}
includes { arg val;
^(val >= start) and: { (val < this.end) and: { val.frac == 0 }}
}
add { ^this.shouldNotImplement(thisMethod) }
put { ^this.shouldNotImplement(thisMethod) }
split { arg num;
// assert: size > num
var newRange = this.class.new(start, num);
start = start + num;
size = size - num;
^newRange
}
storeArgs { ^[start, size] }
storeOn { arg stream;
stream << this.class.name;
this.storeParamsOn(stream);
}
printOn { arg stream; this.storeOn(stream) }
}
|