1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-10 08:44:47 +00:00
Funkin/source/funkin/audiovis/dsp/OffsetArray.hx

88 lines
2.1 KiB
Haxe
Raw Normal View History

package funkin.audiovis.dsp;
2021-09-28 02:30:38 +00:00
/**
A view into an Array with an indexing offset.
Usages include 1-indexed sequences or zero-centered buffers with negative indexing.
**/
@:forward(array, offset)
abstract OffsetArray<T>({
final array:Array<T>;
final offset:Int;
})
{
2021-09-28 02:30:38 +00:00
public inline function new(array:Array<T>, offset:Int)
this = {array: array, offset: offset};
public var length(get, never):Int;
2021-09-28 02:30:38 +00:00
inline function get_length()
return this.array.length;
@:arrayAccess
public inline function get(index:Int):T
2021-09-28 02:30:38 +00:00
return this.array[index - this.offset];
@:arrayAccess
public inline function set(index:Int, value:T):Void
2021-09-28 02:30:38 +00:00
this.array[index - this.offset] = value;
/**
Iterates through items in their original order while providing the altered indexes as keys.
**/
public inline function keyValueIterator():KeyValueIterator<Int, T>
2021-09-28 02:30:38 +00:00
return new OffsetArrayIterator(this.array, this.offset);
@:from
static inline function fromArray<T>(array:Array<T>)
return new OffsetArray(array, 0);
@:to
inline function toArray()
return this.array;
/**
Makes a shifted version of the given `array`, where elements are in the
same order but shifted by `n` positions (to the right if positive and to
the left if negative) in **circular** fashion (no elements discarded).
**/
public static function circShift<T>(array:Array<T>, n:Int):Array<T>
{
if (n < 0)
return circShift(array, array.length + n);
2021-09-28 02:30:38 +00:00
var shifted = new Array<T>();
n = n % array.length;
for (i in array.length - n...array.length)
shifted.push(array[i]);
for (i in 0...array.length - n)
shifted.push(array[i]);
2021-09-28 02:30:38 +00:00
return shifted;
}
}
private class OffsetArrayIterator<T>
{
private final array:Array<T>;
private final offset:Int;
private var enumeration:Int;
2021-09-28 02:30:38 +00:00
public inline function new(array:Array<T>, offset:Int)
{
2021-09-28 02:30:38 +00:00
this.array = array;
this.offset = offset;
this.enumeration = 0;
}
public inline function next():{key:Int, value:T}
{
2021-09-28 02:30:38 +00:00
final i = this.enumeration++;
return {key: i + this.offset, value: this.array[i]};
2021-09-28 02:30:38 +00:00
}
public inline function hasNext():Bool
2021-09-28 02:30:38 +00:00
return this.enumeration < this.array.length;
}