Skip to content

Commit

Permalink
add clear for Bytes32x2Set
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Feb 4, 2025
1 parent 6941ac4 commit a1b7066
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions contracts/utils/structs/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ library EnumerableSet {
}
}

/**
* @dev Removes all the values from a set. O(n).
*
* WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
* function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
*/
function clear(Bytes32x2Set storage self) internal {
bytes32[2][] storage v = self._values;

uint256 len = length(self);
for (uint256 i = 0; i < len; ++i) {
delete self._positions[_hash(v[i])];
}
assembly ("memory-safe") {
sstore(v.slot, 0)
}
}

/**
* @dev Returns true if the value is in the self. O(1).
*/
Expand Down
18 changes: 18 additions & 0 deletions scripts/generate/templates/EnumerableSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ function remove(${name} storage self, ${type} memory value) internal returns (bo
}
}
/**
* @dev Removes all the values from a set. O(n).
*
* WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
* function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
*/
function clear(${name} storage self) internal {
${type}[] storage v = self._values;
uint256 len = length(self);
for (uint256 i = 0; i < len; ++i) {
delete self._positions[_hash(v[i])];
}
assembly ("memory-safe") {
sstore(v.slot, 0)
}
}
/**
* @dev Returns true if the value is in the self. O(1).
*/
Expand Down

0 comments on commit a1b7066

Please sign in to comment.