Made the code work + Cleaned up Code

This commit is contained in:
NeeEoo
2024-04-10 18:57:18 +02:00
parent d955e00ece
commit 244fddc3c7
13 changed files with 240 additions and 117 deletions
@@ -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);
}
}