Update docs
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.6.0) (utils/structs/EnumerableMap.sol)
|
||||
// OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableMap.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
@ -31,6 +31,16 @@ import "./EnumerableSet.sol";
|
||||
* - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
|
||||
* - `address -> uint256` (`AddressToUintMap`) since v4.6.0
|
||||
* - `bytes32 -> bytes32` (`Bytes32ToBytes32`) since v4.6.0
|
||||
* - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
|
||||
* - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
|
||||
*
|
||||
* [WARNING]
|
||||
* ====
|
||||
* Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable.
|
||||
* See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
|
||||
*
|
||||
* In order to clean an EnumerableMap, you can either remove all elements one by one or create a fresh instance using an array of EnumerableMap.
|
||||
* ====
|
||||
*/
|
||||
library EnumerableMap {
|
||||
using EnumerableSet for EnumerableSet.Bytes32Set;
|
||||
@ -147,6 +157,98 @@ library EnumerableMap {
|
||||
return value;
|
||||
}
|
||||
|
||||
// UintToUintMap
|
||||
|
||||
struct UintToUintMap {
|
||||
Bytes32ToBytes32Map _inner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
||||
* key. O(1).
|
||||
*
|
||||
* Returns true if the key was added to the map, that is if it was not
|
||||
* already present.
|
||||
*/
|
||||
function set(
|
||||
UintToUintMap storage map,
|
||||
uint256 key,
|
||||
uint256 value
|
||||
) internal returns (bool) {
|
||||
return set(map._inner, bytes32(key), bytes32(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Removes a value from a set. O(1).
|
||||
*
|
||||
* Returns true if the key was removed from the map, that is if it was present.
|
||||
*/
|
||||
function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
|
||||
return remove(map._inner, bytes32(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns true if the key is in the map. O(1).
|
||||
*/
|
||||
function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
|
||||
return contains(map._inner, bytes32(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the number of elements in the map. O(1).
|
||||
*/
|
||||
function length(UintToUintMap storage map) internal view returns (uint256) {
|
||||
return length(map._inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the element stored at position `index` in the set. O(1).
|
||||
* 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.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `index` must be strictly less than {length}.
|
||||
*/
|
||||
function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
|
||||
(bytes32 key, bytes32 value) = at(map._inner, index);
|
||||
return (uint256(key), uint256(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
(bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
|
||||
return (success, uint256(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value associated with `key`. O(1).
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `key` must be in the map.
|
||||
*/
|
||||
function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
|
||||
return uint256(get(map._inner, bytes32(key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Same as {get}, with a custom error message when `key` is not in the map.
|
||||
*
|
||||
* CAUTION: This function is deprecated because it requires allocating memory for the error
|
||||
* message unnecessarily. For custom revert reasons use {tryGet}.
|
||||
*/
|
||||
function get(
|
||||
UintToUintMap storage map,
|
||||
uint256 key,
|
||||
string memory errorMessage
|
||||
) internal view returns (uint256) {
|
||||
return uint256(get(map._inner, bytes32(key), errorMessage));
|
||||
}
|
||||
|
||||
// UintToAddressMap
|
||||
|
||||
struct UintToAddressMap {
|
||||
@ -302,8 +404,6 @@ library EnumerableMap {
|
||||
/**
|
||||
* @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(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
|
||||
(bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
|
||||
@ -334,4 +434,96 @@ library EnumerableMap {
|
||||
) internal view returns (uint256) {
|
||||
return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
|
||||
}
|
||||
|
||||
// Bytes32ToUintMap
|
||||
|
||||
struct Bytes32ToUintMap {
|
||||
Bytes32ToBytes32Map _inner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
||||
* key. O(1).
|
||||
*
|
||||
* Returns true if the key was added to the map, that is if it was not
|
||||
* already present.
|
||||
*/
|
||||
function set(
|
||||
Bytes32ToUintMap storage map,
|
||||
bytes32 key,
|
||||
uint256 value
|
||||
) internal returns (bool) {
|
||||
return set(map._inner, key, bytes32(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Removes a value from a set. O(1).
|
||||
*
|
||||
* Returns true if the key was removed from the map, that is if it was present.
|
||||
*/
|
||||
function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
|
||||
return remove(map._inner, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns true if the key is in the map. O(1).
|
||||
*/
|
||||
function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
|
||||
return contains(map._inner, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the number of elements in the map. O(1).
|
||||
*/
|
||||
function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
|
||||
return length(map._inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the element stored at position `index` in the set. O(1).
|
||||
* 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.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `index` must be strictly less than {length}.
|
||||
*/
|
||||
function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
|
||||
(bytes32 key, bytes32 value) = at(map._inner, index);
|
||||
return (key, uint256(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
(bool success, bytes32 value) = tryGet(map._inner, key);
|
||||
return (success, uint256(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value associated with `key`. O(1).
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `key` must be in the map.
|
||||
*/
|
||||
function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
|
||||
return uint256(get(map._inner, key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Same as {get}, with a custom error message when `key` is not in the map.
|
||||
*
|
||||
* CAUTION: This function is deprecated because it requires allocating memory for the error
|
||||
* message unnecessarily. For custom revert reasons use {tryGet}.
|
||||
*/
|
||||
function get(
|
||||
Bytes32ToUintMap storage map,
|
||||
bytes32 key,
|
||||
string memory errorMessage
|
||||
) internal view returns (uint256) {
|
||||
return uint256(get(map._inner, key, errorMessage));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.6.0) (utils/structs/EnumerableSet.sol)
|
||||
// OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
@ -26,6 +26,14 @@ pragma solidity ^0.8.0;
|
||||
*
|
||||
* As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
|
||||
* and `uint256` (`UintSet`) are supported.
|
||||
*
|
||||
* [WARNING]
|
||||
* ====
|
||||
* Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable.
|
||||
* See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
|
||||
*
|
||||
* In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet.
|
||||
* ====
|
||||
*/
|
||||
library EnumerableSet {
|
||||
// To implement this library for multiple types with as little code
|
||||
@ -275,6 +283,7 @@ library EnumerableSet {
|
||||
bytes32[] memory store = _values(set._inner);
|
||||
address[] memory result;
|
||||
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
result := store
|
||||
}
|
||||
@ -348,6 +357,7 @@ library EnumerableSet {
|
||||
bytes32[] memory store = _values(set._inner);
|
||||
uint256[] memory result;
|
||||
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
result := store
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user