Made the code work + Cleaned up Code
This commit is contained in:
@@ -10,7 +10,7 @@ class SortedArrayUtil {
|
||||
* @param getVal Function that returns the position value of T
|
||||
* @return Index
|
||||
*/
|
||||
public static inline function binarySearch<T>(array:Array<T>, val:Float, getVal:T->Float):Int {
|
||||
public static function binarySearch<T>(array:Array<T>, val:Float, getVal:T->Float):Int {
|
||||
if (array.length <= 0) return 0; // if the array is empty, it should be equal to zero (the beginning)
|
||||
if (getVal(array[0]) > val) return 0; // in case its the minimum
|
||||
if (getVal(array[array.length-1]) < val) return array.length; // in case its the maximum
|
||||
@@ -46,4 +46,10 @@ class SortedArrayUtil {
|
||||
if (val != null)
|
||||
array.insert(binarySearch(array, getVal(val), getVal), val);
|
||||
}
|
||||
|
||||
public static function removeSorted<T>(array:Array<T>, val:T, getVal:T->Float) {
|
||||
var index = binarySearch(array, getVal(val), getVal);
|
||||
if (index != -1)
|
||||
array.splice(index, 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user