Merge branch 'main' into translations-system
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package funkin.backend.utils;
|
||||
|
||||
/**
|
||||
* All types of utils only usable in a sorted array.
|
||||
* Tools to work with sorted arrays, in a more efficient way.
|
||||
*
|
||||
* DISCLAIMER: All types of utils only usable in a sorted array.
|
||||
*/
|
||||
class SortedArrayUtil {
|
||||
/**
|
||||
* Gets the index of a possible new element of an Array of T using an efficient algorithm.
|
||||
* @param array Array of T to check in
|
||||
* @param getVal Function that returns the position value of T
|
||||
* @return Index
|
||||
* @return The index of the element
|
||||
*/
|
||||
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)
|
||||
@@ -47,7 +49,13 @@ class SortedArrayUtil {
|
||||
array.insert(binarySearch(array, getVal(val), getVal), val);
|
||||
}
|
||||
|
||||
public static function removeSorted<T>(array:Array<T>, val:T, getVal:T->Float) {
|
||||
/**
|
||||
* Removes from a sorted array, using binary search.
|
||||
* @param array Array to remove from
|
||||
* @param val Value to remove
|
||||
* @param getVal Function that returns the value that needs to be sorted
|
||||
*/
|
||||
public static inline 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