Skip to content

Commit

Permalink
ADD FixedArray filter/find
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Oct 30, 2024
1 parent 3f7fff2 commit 07555c2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/dn/struct/FixedArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ class FixedArray<T> {
return exists(idx) ? values[idx] : null;
}


/** Return a filtered standard Array **/
public function filter(filterFunc:T->Bool) : Array<T> {
var out = [];
for(v in this)
if( filterFunc(v) )
out.push(v);
return out;
}

/** Return the first value that matches the filter function **/
public function find(filterFunc:T->Bool) : Null<T> {
for(v in this)
if( filterFunc(v) )
return v;
return null;
}

public function pickRandom(?rndFunc:Int->Int, removeAfterPick=false) : Null<T> {
if( allocated==0 )
return null;
Expand Down Expand Up @@ -407,6 +425,20 @@ class FixedArrayTests {
a.shift(); CiAssert.equals(a.shortString(), "b,c,d,e");
a.removeIndex(1); CiAssert.equals(a.shortString(), "b,d,e");
a.remove("b"); CiAssert.equals(a.shortString(), "d,e");

// Filter
var a = FixedArray.fromArray([0,1,2,3,4,5]);
CiAssert.equals( a.filter( v->v%2==0 ).length, 3);
CiAssert.equals( a.filter( v->v>=0 ).length, 6);
CiAssert.equals( a.filter( v->v>=5 ).length, 1);
CiAssert.equals( a.filter( v->v>=10 ).length, 0);

// Find
var a = FixedArray.fromArray([0,1,2,3,4,5]);
CiAssert.equals( a.find( v->v==3 ), 3);
CiAssert.equals( a.find( v->v==10 ), null);
CiAssert.equals( a.find( v->v%2==0 ), 0);
CiAssert.equals( a.find( v->v%2!=0 ), 1);
}
}
#end
Expand Down

0 comments on commit 07555c2

Please sign in to comment.