diff --git a/flixel/util/FlxArrayUtil.hx b/flixel/util/FlxArrayUtil.hx index f9940508d2..d9b599cf7d 100644 --- a/flixel/util/FlxArrayUtil.hx +++ b/flixel/util/FlxArrayUtil.hx @@ -82,6 +82,61 @@ 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 + */ + 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) + { + 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. 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])); + } }