Skip to content

Add Array.uniquifySorted to the Arrays library #5534

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions contracts/mocks/ArraysMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ contract Uint256ArraysMock {
function length() external view returns (uint256) {
return _array.length;
}

function uniquifySorted(uint256[] memory array) external pure returns (uint256[] memory) {
return array.uniquifySorted();
}
}

contract AddressArraysMock {
Expand Down Expand Up @@ -90,6 +94,10 @@ contract AddressArraysMock {
function length() external view returns (uint256) {
return _array.length;
}

function uniquifySorted(address[] memory array) external pure returns (address[] memory) {
return array.uniquifySorted();
}
}

contract Bytes32ArraysMock {
Expand Down Expand Up @@ -124,4 +132,8 @@ contract Bytes32ArraysMock {
function length() external view returns (uint256) {
return _array.length;
}

function uniquifySorted(bytes32[] memory array) external pure returns (bytes32[] memory) {
return array.uniquifySorted();
}
}
65 changes: 65 additions & 0 deletions contracts/utils/Arrays.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,69 @@ library Arrays {
sstore(array.slot, len)
}
}

/**
* @dev Removes duplicate values from a sorted array. This function does not check that the array is sorted,
* behavior is undefined if the array is not sorted. The resulting array will have no duplicates and will
* be shorter or the same length as the input array.
*
* This operation is performed in-place by moving elements and modifying the length of the array.
* Time complexity O(n).
*
* WARNING: This function is destructive. It will modify the array passed by reference.
*/
function uniquifySorted(uint256[] memory array) internal pure returns (uint256[] memory) {
if (array.length <= 1) {
return array;
}

uint256 resultSize = 1;
for (uint256 i = 1; i < array.length; ++i) {
if (array[i] != array[i - 1]) {
if (i != resultSize) {
array[resultSize] = array[i];
}
++resultSize;
}
}

// Resize the array by creating a new one (can't modify length of memory arrays directly)
uint256[] memory result = new uint256[](resultSize);
for (uint256 i = 0; i < resultSize; ++i) {
result[i] = array[i];
}
return result;
Comment on lines +508 to +513
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is agreed that modifying the array passed by reference is the right approach, then we should not do that copy. Instead, we should just resize (down) the array. unsafeSetLength does that.

}

/**
* @dev Removes duplicate values from a sorted address array. This function does not check that the array
* is sorted, behavior is undefined if the array is not sorted. The resulting array will have no duplicates
* and will be shorter or the same length as the input array.
*
* This operation is performed in-place by moving elements and modifying the length of the array.
* Time complexity O(n).
*
* WARNING: This function is destructive. It will modify the array passed by reference.
*/
function uniquifySorted(address[] memory array) internal pure returns (address[] memory) {
uint256[] memory castedArray = _castToUint256Array(array);
uniquifySorted(castedArray);
return array;
}

/**
* @dev Removes duplicate values from a sorted bytes32 array. This function does not check that the array
* is sorted, behavior is undefined if the array is not sorted. The resulting array will have no duplicates
* and will be shorter or the same length as the input array.
*
* This operation is performed in-place by moving elements and modifying the length of the array.
* Time complexity O(n).
*
* WARNING: This function is destructive. It will modify the array passed by reference.
*/
function uniquifySorted(bytes32[] memory array) internal pure returns (bytes32[] memory) {
uint256[] memory castedArray = _castToUint256Array(array);
uniquifySorted(castedArray);
return array;
}
}
Loading
Loading