From e6c4a61c3dba1a5fa79ea9562f1bf302e8fdf1ab Mon Sep 17 00:00:00 2001 From: UncertainProd <83609901+UncertainProd@users.noreply.github.com> Date: Fri, 25 Nov 2022 23:32:23 +0530 Subject: [PATCH 1/3] Added a swap function to FlxArrayUtil --- flixel/util/FlxArrayUtil.hx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/flixel/util/FlxArrayUtil.hx b/flixel/util/FlxArrayUtil.hx index f9940508d2..9881fcfa6c 100644 --- a/flixel/util/FlxArrayUtil.hx +++ b/flixel/util/FlxArrayUtil.hx @@ -82,6 +82,25 @@ class FlxArrayUtil return array; } + /** + * Swaps two elements of an array, which are located at indices `index1` and `index2` + * @param array The array whose elements need to be swapped + * @param index1 The index of one of the elements to be swapped + * @param index2 The index of the other element to be swapped + * @return The array + */ + @:generic + public static inline function swap(array:Array, index1:Int, index2:Int):Array + { + if(index1 >= 0 && index1 < array.length && index2 >= 0 && index2 < array.length) + { + var temp = array[index1]; + array[index1] = array[index2]; + array[index2] = temp; + } + return array; + } + /** * Clears an array structure, but leaves the object data untouched * Useful for cleaning up temporary references to data you want to preserve. From 5874adb60d109636ce40fe22f624cf66f1a6d76f Mon Sep 17 00:00:00 2001 From: UncertainProd <83609901+UncertainProd@users.noreply.github.com> Date: Thu, 1 Dec 2022 21:05:48 +0530 Subject: [PATCH 2/3] added swap functions for both elements and indices --- flixel/util/FlxArrayUtil.hx | 46 +++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/flixel/util/FlxArrayUtil.hx b/flixel/util/FlxArrayUtil.hx index 9881fcfa6c..d9b599cf7d 100644 --- a/flixel/util/FlxArrayUtil.hx +++ b/flixel/util/FlxArrayUtil.hx @@ -89,18 +89,54 @@ class FlxArrayUtil * @param index2 The index of the other element to be swapped * @return The array */ - @:generic - public static inline function swap(array:Array, index1:Int, index2:Int):Array + public static inline function swapByIndex(array:Array, index1:Int, index2:Int):Array + { + var temp = array[index1]; + array[index1] = array[index2]; + array[index2] = temp; + return array; + } + + /** + * Swaps two elements of an array, which are located at indices `index1` and `index2` and also checks if the indices are valid before swapping + * @param array The array whose elements need to be swapped + * @param index1 The index of one of the elements to be swapped + * @param index2 The index of the other element to be swapped + * @return The array + */ + public static inline function safeSwapByIndex(array:Array, index1:Int, index2:Int):Array { if(index1 >= 0 && index1 < array.length && index2 >= 0 && index2 < array.length) { - var temp = array[index1]; - array[index1] = array[index2]; - array[index2] = temp; + swapByIndex(array, index1, index2); } return array; } + /** + * Swaps two items, `item1` and `item2` which are in the array + * @param array The array whose elements need to be swapped + * @param item1 One of the elements of the array which needs to be swapped + * @param item2 The other element of the array which needs to be swapped + * @return The array + */ + public static inline function swap(array:Array, item1:T, item2:T):Array + { + return swapByIndex(array, array.indexOf(item1), array.indexOf(item2)); + } + + /** + * Swaps two items, `item1` and `item2` which are in the array, but only if both elements are present in the array + * @param array The array whose elements need to be swapped + * @param item1 One of the elements of the array which needs to be swapped + * @param item2 The other element of the array which needs to be swapped + * @return The array + */ + public static inline function safeSwap(array:Array, item1:T, item2:T):Array + { + return safeSwapByIndex(array, array.indexOf(item1), array.indexOf(item2)); + } + /** * Clears an array structure, but leaves the object data untouched * Useful for cleaning up temporary references to data you want to preserve. From 71e531456ffdf7464ae3f3b26bb14f1c2386769e Mon Sep 17 00:00:00 2001 From: UncertainProd <83609901+UncertainProd@users.noreply.github.com> Date: Thu, 1 Dec 2022 22:42:43 +0530 Subject: [PATCH 3/3] Added tests for the different swap functions --- .../unit/src/flixel/util/FlxArrayUtilTest.hx | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/unit/src/flixel/util/FlxArrayUtilTest.hx b/tests/unit/src/flixel/util/FlxArrayUtilTest.hx index 2cf2ef907a..3338ae694d 100644 --- a/tests/unit/src/flixel/util/FlxArrayUtilTest.hx +++ b/tests/unit/src/flixel/util/FlxArrayUtilTest.hx @@ -75,4 +75,31 @@ class FlxArrayUtilTest Assert.areEqual(1, [1].last()); Assert.areEqual(3, [1, 2, 3].last()); } + + @Test + function testSwapByIndex() + { + Assert.isTrue([0, 1, 2, 3, 4, 5].swapByIndex(2, 4).equals([0, 1, 4, 3, 2, 5])); + } + + @Test + function testSafeSwapByIndex() + { + Assert.isTrue([0, 1, 2, 3].safeSwapByIndex(0, 3).equals([3, 1, 2, 0])); + Assert.isTrue([0, 1, 2, 3].safeSwapByIndex(2, 4).equals([0, 1, 2, 3])); + Assert.isTrue([0, 1, 2, 3].safeSwapByIndex(-1, 2).equals([0, 1, 2, 3])); + } + + @Test + function testSwap() + { + Assert.isTrue([0, 1, 2, 3, 4, 5].swap(1, 3).equals([0, 3, 2, 1, 4, 5])); + } + + @Test + function testSafeSwap() + { + Assert.isTrue([0, 1, 2, 3, 4, 5].safeSwap(0, 2).equals([2, 1, 0, 3, 4, 5])); + Assert.isTrue([0, 1, 2, 3, 4, 5].safeSwap(1, 6).equals([0, 1, 2, 3, 4, 5])); + } }