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

Add enumerate() back into EnumerableSet #2296

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 30 additions & 0 deletions contracts/utils/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ library EnumerableSet {
return _remove(set._inner, bytes32(uint256(value)));
}

/**
* @dev Returns an array with all values in the set. O(N).
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
* WARNING: This function may run out of gas on large sets: use {length} and
* {at} instead in these cases.
*/
function enumerate(AddressSet storage set) internal view returns (address[] memory) {
address[] memory output = new address[](set._inner._values.length);
for (uint256 i; i < set._inner._values.length; i++) {
output[i] = address(uint256(set._inner._values[i]));
}
return output;
}

/**
* @dev Returns true if the value is in the set. O(1).
*/
Expand Down Expand Up @@ -213,6 +228,21 @@ library EnumerableSet {
return _remove(set._inner, bytes32(value));
}

/**
* @dev Returns an array with all values in the set. O(N).
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
* WARNING: This function may run out of gas on large sets: use {length} and
* {at} instead in these cases.
*/
function enumerate(UintSet storage set) internal view returns (uint256[] memory) {
uint256[] memory output = new uint256[](set._inner._values.length);
for (uint256 i; i < set._inner._values.length; i++) {
output[i] = uint256(set._inner._values[i]);
}
return output;
}

/**
* @dev Returns true if the value is in the set. O(1).
*/
Expand Down