Added some documentation

This commit is contained in:
Ne_Eo
2024-12-16 03:53:42 +01:00
parent fd0c188577
commit a6c99754fd
7 changed files with 187 additions and 23 deletions
+15 -2
View File
@@ -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 inline 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)
@@ -46,4 +48,15 @@ class SortedArrayUtil {
if (val != null)
array.insert(binarySearch(array, getVal(val), getVal), val);
}
/**
* 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) {
if (val != null)
array.splice(binarySearch(array, getVal(val), getVal), 1);
}
}