Funkin/source/funkin/util/tools/MapTools.hx

64 lines
1.5 KiB
Haxe
Raw Normal View History

2023-01-22 19:55:30 -05:00
package funkin.util.tools;
/**
* A static extension which provides utility functions for Maps.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* For example, add `using MapTools` then call `map.values()`.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* @see https://haxe.org/manual/lf-static-extension.html
*/
class MapTools
{
/**
* Return the quantity of keys in the map.
*/
public static function size<K, T>(map:Map<K, T>):Int
{
return map.keys().array().length;
}
/**
* Return a list of values from the map, as an array.
*/
2023-01-22 19:55:30 -05:00
public static function values<K, T>(map:Map<K, T>):Array<T>
{
return [for (i in map.iterator()) i];
}
2023-04-10 16:05:37 -04:00
/**
* Create a new array with all elements of the given array, to prevent modifying the original.
*/
public static function clone<K, T>(map:Map<K, T>):Map<K, T>
{
return map.copy();
}
/**
* Create a new array with clones of all elements of the given array, to prevent modifying the original.
*/
public static function deepClone<K, T, U:ICloneable<T>>(map:Map<K, U>):Map<K, T>
{
// TODO: This function does NOT work.
throw "Not implemented";
/*
var newMap:Map<K, T> = [];
// Replace each value with a clone of itself.
for (key in newMap.keys())
{
newMap.set(key, newMap.get(key).clone());
}
return newMap;
*/
}
/**
* Return a list of keys from the map (as an array, rather than an iterator).
* TODO: Rename this?
*/
2023-04-10 16:05:37 -04:00
public static function keyValues<K, T>(map:Map<K, T>):Array<K>
{
return map.keys().array();
2023-04-10 16:05:37 -04:00
}
2023-01-22 19:55:30 -05:00
}