-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a utility primarily for haxe versions that did not support array resizing
- Loading branch information
1 parent
c9ca80f
commit c03d806
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package starling.utils; | ||
|
||
/** | ||
* Array Utility. | ||
*/ | ||
class ArrayUtil | ||
{ | ||
/** | ||
* Truncates an array to the specified length. | ||
* @param arr The array to truncate. | ||
* @param newSize The new size of the array. | ||
*/ | ||
public static function truncate<T>(arr:Array<T>, newSize:Int):Void { | ||
if (newSize < arr.length) { | ||
arr.splice(newSize, arr.length - newSize); | ||
} | ||
} | ||
|
||
/** | ||
* Extends an array to the specified length, filling new elements with the default value. | ||
* @param arr The array to extend. | ||
* @param newSize The new size of the array. | ||
* @param defaultValue The value to fill new elements with. | ||
*/ | ||
public static function extend<T>(arr:Array<T>, newSize:Int, defaultValue:T):Void { | ||
while (arr.length < newSize) { | ||
arr.push(defaultValue); | ||
} | ||
} | ||
|
||
/** | ||
* Resizes an array to the specified length, truncating or extending as needed. | ||
* @param arr The array to resize. | ||
* @param newSize The new size of the array. | ||
* @param defaultValue The value to fill new elements with when extending. | ||
*/ | ||
public static function resize<T>(arr:Array<T>, newSize:Int, defaultValue:T):Void { | ||
if (newSize < arr.length) { | ||
truncate(arr, newSize); | ||
} else { | ||
extend(arr, newSize, defaultValue); | ||
} | ||
} | ||
} |