Skip to content

Commit

Permalink
Procedurally generate EnumerableSet and EnumerableMap (#3429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx authored Aug 19, 2022
1 parent c797195 commit 17bc2da
Show file tree
Hide file tree
Showing 9 changed files with 708 additions and 15 deletions.
1 change: 1 addition & 0 deletions contracts/mocks/EnumerableMapMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ contract AddressToUintMapMock {
}
}

// Bytes32ToBytes32Map
contract Bytes32ToBytes32MapMock {
using EnumerableMap for EnumerableMap.Bytes32ToBytes32Map;

Expand Down
24 changes: 11 additions & 13 deletions contracts/utils/structs/EnumerableMap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import "./EnumerableSet.sol";
*
* - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
* - `address -> uint256` (`AddressToUintMap`) since v4.6.0
* - `bytes32 -> bytes32` (`Bytes32ToBytes32`) since v4.6.0
* - `bytes32 -> bytes32` (`Bytes32ToBytes32Map`) since v4.6.0
* - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
* - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
*
Expand Down Expand Up @@ -116,7 +116,7 @@ library EnumerableMap {
}

/**
* @dev Tries to returns the value associated with `key`. O(1).
* @dev Tries to returns the value associated with `key`. O(1).
* Does not revert if `key` is not in the map.
*/
function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
Expand All @@ -129,7 +129,7 @@ library EnumerableMap {
}

/**
* @dev Returns the value associated with `key`. O(1).
* @dev Returns the value associated with `key`. O(1).
*
* Requirements:
*
Expand Down Expand Up @@ -216,7 +216,7 @@ library EnumerableMap {
}

/**
* @dev Tries to returns the value associated with `key`. O(1).
* @dev Tries to returns the value associated with `key`. O(1).
* Does not revert if `key` is not in the map.
*/
function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
Expand All @@ -225,7 +225,7 @@ library EnumerableMap {
}

/**
* @dev Returns the value associated with `key`. O(1).
* @dev Returns the value associated with `key`. O(1).
*
* Requirements:
*
Expand Down Expand Up @@ -308,18 +308,16 @@ library EnumerableMap {
}

/**
* @dev Tries to returns the value associated with `key`. O(1).
* @dev Tries to returns the value associated with `key`. O(1).
* Does not revert if `key` is not in the map.
*
* _Available since v3.4._
*/
function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
(bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
return (success, address(uint160(uint256(value))));
}

/**
* @dev Returns the value associated with `key`. O(1).
* @dev Returns the value associated with `key`. O(1).
*
* Requirements:
*
Expand Down Expand Up @@ -402,7 +400,7 @@ library EnumerableMap {
}

/**
* @dev Tries to returns the value associated with `key`. O(1).
* @dev Tries to returns the value associated with `key`. O(1).
* Does not revert if `key` is not in the map.
*/
function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
Expand All @@ -411,7 +409,7 @@ library EnumerableMap {
}

/**
* @dev Returns the value associated with `key`. O(1).
* @dev Returns the value associated with `key`. O(1).
*
* Requirements:
*
Expand Down Expand Up @@ -494,7 +492,7 @@ library EnumerableMap {
}

/**
* @dev Tries to returns the value associated with `key`. O(1).
* @dev Tries to returns the value associated with `key`. O(1).
* Does not revert if `key` is not in the map.
*/
function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
Expand All @@ -503,7 +501,7 @@ library EnumerableMap {
}

/**
* @dev Returns the value associated with `key`. O(1).
* @dev Returns the value associated with `key`. O(1).
*
* Requirements:
*
Expand Down
12 changes: 10 additions & 2 deletions contracts/utils/structs/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,15 @@ library EnumerableSet {
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
return _values(set._inner);
bytes32[] memory store = _values(set._inner);
bytes32[] memory result;

/// @solidity memory-safe-assembly
assembly {
result := store
}

return result;
}

// AddressSet
Expand Down Expand Up @@ -325,7 +333,7 @@ library EnumerableSet {
}

/**
* @dev Returns the number of values on the set. O(1).
* @dev Returns the number of values in the set. O(1).
*/
function length(UintSet storage set) internal view returns (uint256) {
return _length(set._inner);
Expand Down
7 changes: 7 additions & 0 deletions scripts/generate/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ function getVersion (path) {
}

for (const [ file, template ] of Object.entries({
// SafeCast
'utils/math/SafeCast.sol': './templates/SafeCast',
'mocks/SafeCastMock.sol': './templates/SafeCastMock',
// EnumerableSet
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet',
'mocks/EnumerableSetMock.sol': './templates/EnumerableSetMock',
// EnumerableMap
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap',
'mocks/EnumerableMapMock.sol': './templates/EnumerableMapMock',
})) {
const path = `./contracts/${file}`;
const version = getVersion(path);
Expand Down
Loading

0 comments on commit 17bc2da

Please sign in to comment.