Skip to content

Commit

Permalink
Added a swap function to FlxArrayUtil (#2685)
Browse files Browse the repository at this point in the history
* Added a swap function to FlxArrayUtil

* added swap functions for both elements and indices

* Added tests for the different swap functions
  • Loading branch information
UncertainProd authored Dec 2, 2022
1 parent 83fab90 commit 02c0831
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
55 changes: 55 additions & 0 deletions flixel/util/FlxArrayUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(array:Array<T>, index1:Int, index2:Int):Array<T>
{
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<T>(array:Array<T>, index1:Int, index2:Int):Array<T>
{
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<T>(array:Array<T>, item1:T, item2:T):Array<T>
{
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<T>(array:Array<T>, item1:T, item2:T):Array<T>
{
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.
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/src/flixel/util/FlxArrayUtilTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}

0 comments on commit 02c0831

Please sign in to comment.