ArrayUtil.removeFirstIf(), removeLastIf(), removeAllIf() predicate-based removal functions
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5533 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -208,6 +208,45 @@ public class ArrayUtil
|
|||||||
return removeImpl(arr, element, false);
|
return removeImpl(arr, element, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the first element in the array for which the specified predicate returns true.
|
||||||
|
*
|
||||||
|
* @param pred a Function of the form: function (element :*) :Boolean
|
||||||
|
*
|
||||||
|
* @return true if an element was removed, false otherwise.
|
||||||
|
*/
|
||||||
|
public static function removeFirstIf (arr :Array, pred :Function) :Boolean
|
||||||
|
{
|
||||||
|
return removeIfImpl(arr, pred, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the last element in the array for which the specified predicate returns true.
|
||||||
|
*
|
||||||
|
* @param pred a Function of the form: function (element :*) :Boolean
|
||||||
|
*
|
||||||
|
* @return true if an element was removed, false otherwise.
|
||||||
|
*/
|
||||||
|
public static function removeLastIf (arr :Array, pred :Function) :Boolean
|
||||||
|
{
|
||||||
|
arr.reverse();
|
||||||
|
var removed :Boolean = removeFirstIf(arr, pred);
|
||||||
|
arr.reverse();
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all elements in the array for which the specified predicate returns true.
|
||||||
|
*
|
||||||
|
* @param pred a Function of the form: function (element :*) :Boolean
|
||||||
|
*
|
||||||
|
* @return true if an element was removed, false otherwise.
|
||||||
|
*/
|
||||||
|
public static function removeAllIf (arr :Array, pred :Function) :Boolean
|
||||||
|
{
|
||||||
|
return removeIfImpl(arr, pred, false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do the two arrays contain elements that are all equals()?
|
* Do the two arrays contain elements that are all equals()?
|
||||||
*/
|
*/
|
||||||
@@ -249,10 +288,18 @@ public class ArrayUtil
|
|||||||
*/
|
*/
|
||||||
private static function removeImpl (
|
private static function removeImpl (
|
||||||
arr :Array, element :Object, firstOnly :Boolean) :Boolean
|
arr :Array, element :Object, firstOnly :Boolean) :Boolean
|
||||||
|
{
|
||||||
|
return removeIfImpl(arr, createEqualsPred(element), firstOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of removeIf methods.
|
||||||
|
*/
|
||||||
|
private static function removeIfImpl (arr :Array, pred :Function, firstOnly :Boolean) :Boolean
|
||||||
{
|
{
|
||||||
var removed :Boolean = false;
|
var removed :Boolean = false;
|
||||||
for (var ii :int = 0; ii < arr.length; ii++) {
|
for (var ii :int = 0; ii < arr.length; ii++) {
|
||||||
if (Util.equals(arr[ii], element)) {
|
if (pred(arr[ii])) {
|
||||||
arr.splice(ii--, 1);
|
arr.splice(ii--, 1);
|
||||||
if (firstOnly) {
|
if (firstOnly) {
|
||||||
return true;
|
return true;
|
||||||
@@ -262,5 +309,12 @@ public class ArrayUtil
|
|||||||
}
|
}
|
||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function createEqualsPred (element :Object) :Function
|
||||||
|
{
|
||||||
|
return function (other :Object) :Boolean {
|
||||||
|
return Util.equals(other, element);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user