Skip to content

Instantly share code, notes, and snippets.

@nanjizal
Created November 5, 2023 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanjizal/f699b4299c205ddca8d10646eaeb1153 to your computer and use it in GitHub Desktop.
Save nanjizal/f699b4299c205ddca8d10646eaeb1153 to your computer and use it in GitHub Desktop.
multitype A
import haxe.ds.Vector;
function main() {
var a: A<ArrInt> = A.arrInt( 10 );
var v: A<VecInt> = A.vecInt( 10 );
a[3]=1;
v[2]=2;
trace( a[3] );
trace( v[2] );
}
typedef ArrInt = Array<Int>;
typedef VecInt = Vector<Int>;
interface IA<T>
{
public function set( index: Int, value:Int ): Int;
public function get( index: Int): Int;
public function zero( len: Int ): T;
public function size( len: Int ): T;
}
class ArrIntA implements IA<ArrInt>
{
var val: ArrInt;
public function new() {
}
public inline function set( index: Int, value: Int ): Int {
val[ index ] = value;
return value;
}
public inline function get( index: Int ): Int {
return val[ index ];
}
public inline function zero( len :Int ): ArrInt {
for( i in 0...len ){
val[i] = 0;
}
return this.val;
}
public inline function size( len: Int ): ArrInt {
val = [];
return zero(len);
}
}
class VecIntA implements IA<VecInt>
{
var val:VecInt;
public function new() {
//this.val = val;
}
public inline function set( index: Int, value: Int ): Int {
val[ index ] = value;
return value;
}
public inline function get( index: Int ): Int {
return val[ index ];
}
public inline function zero( len :Int ): VecInt
{
for( i in 0...len ){
val[i] = 0;
}
return this.val;
}
public inline function size( len: Int ): VecInt {
val = new Vector( len );
return zero(len);
}
}
@:forward
@:multiType
abstract A<T>(IA<T>)
{
public function new(a:T);
@:to static inline function toArrIntA(t:IA<ArrInt>, s: Null<ArrInt> = null ):ArrIntA
{
var arrI = new ArrIntA();
return arrI;
}
@:to static inline function toVecIntA(t:IA<VecInt>, s: Null<VecInt> = null ):VecIntA
{
var vec = new VecIntA( );
return vec;
}
public inline static
function arrInt( len: Int ){
var a = new A<ArrInt>(null);
a.size(len);
return a;
}
public inline static
function vecInt( len: Int ){
var v = new A<VecInt>(null);
v.size(len);
return v;
}
@:arrayAccess
public inline function set( index: Int, value: Int ): Int {
return this.set( index, value );
}
@:arrayAccess
public inline function get( index: Int ): Int {
return this.get( index );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment