From b57fad9f6f7271e1250d656c476f784feefb08dd Mon Sep 17 00:00:00 2001 From: dk1a Date: Wed, 7 Feb 2024 22:06:22 +0300 Subject: [PATCH] replace removeFromArray with custom swapAndPop --- packages/contracts/src/libraries/LibUtils.sol | 38 ------------------- .../systems/machine-network/DestroySystem.sol | 22 ++++++++++- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/packages/contracts/src/libraries/LibUtils.sol b/packages/contracts/src/libraries/LibUtils.sol index 3a4b01f0..bbad15f2 100644 --- a/packages/contracts/src/libraries/LibUtils.sol +++ b/packages/contracts/src/libraries/LibUtils.sol @@ -82,44 +82,6 @@ library LibUtils { return ((a + b) * (a + b + 1)) / 2 + b; } - /** - * @dev Removes an element from an array of `bytes32` if it exists and returns the new array. - * @param array The original array of `bytes32` elements. - * @param element The `bytes32` element to be removed from the array. - * @return newArray The new array containing all elements of the original array except for the removed element. - */ - function removeFromArray(bytes32[] memory array, bytes32 element) internal pure returns (bytes32[] memory) { - // Determine if the element exists and the index of the element to be removed - bool found = false; - uint256 foundIndex = 0; - for (uint256 i = 0; i < array.length; i++) { - if (array[i] == element) { - found = true; - foundIndex = i; - break; - } - } - - // If the element was not found, return the original array - if (!found) { - return array; - } - - // Create a new array of length `array.length - 1` to store the result - bytes32[] memory newArray = new bytes32[](array.length - 1); - - // Copy elements from the original array to the new array, skipping the removed element - uint256 j = 0; - for (uint256 i = 0; i < array.length; i++) { - if (i != foundIndex) { - newArray[j] = array[i]; - j++; - } - } - - return newArray; - } - function stringEq(string memory a, string memory b) internal pure returns (bool) { if (bytes(a).length != bytes(b).length) { return false; diff --git a/packages/contracts/src/systems/machine-network/DestroySystem.sol b/packages/contracts/src/systems/machine-network/DestroySystem.sol index 23992de4..eea5b48a 100644 --- a/packages/contracts/src/systems/machine-network/DestroySystem.sol +++ b/packages/contracts/src/systems/machine-network/DestroySystem.sol @@ -72,6 +72,26 @@ contract DestroySystem is System { LibEntity.destroy(_machineEntity); // Remove it from the list of machines - MachinesInPod.set(podEntity, LibUtils.removeFromArray(MachinesInPod.get(podEntity), _machineEntity)); + _swapAndPopMachinesInPod(podEntity, _machineEntity); + } + + function _swapAndPopMachinesInPod(bytes32 _podEntity, bytes32 _machineEntity) internal { + uint256 length = MachinesInPod.length(_podEntity); + if (length == 0) { + return; + } else { + uint256 lastIndex = length - 1; + + for (uint256 i; i < length; i++) { + if (_machineEntity == MachinesInPod.getItem(_podEntity, i)) { + if (i != lastIndex) { + bytes32 entityToSwap = MachinesInPod.getItem(_podEntity, lastIndex); + MachinesInPod.update(_podEntity, i, entityToSwap); + } + MachinesInPod.pop(_podEntity); + return; + } + } + } } }