Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a swap function to FlxArrayUtil #2685

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Geokureli marked this conversation as resolved.
Show resolved Hide resolved
{
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]));
}
}