EnumerableSet
*Library for managing https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive types. Sets have the following properties:
- Elements are added, removed, and checked for existence in constant time (O(1)).
- Elements are enumerated in O(n). No guarantees are made on the ordering.
- Set can be cleared (all elements removed) in O(n).
contract Example {
// Add the library methods
using EnumerableSet for EnumerableSet.AddressSet;
// Declare a set state variable
EnumerableSet.AddressSet private mySet;
}
The following types are supported:
bytes32(Bytes32Set) since v3.3.0address(AddressSet) since v3.3.0uint256(UintSet) since v3.3.0string(StringSet) since v5.4.0bytes(BytesSet) since v5.4.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 EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet. ====*
Functions
_add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function _add(Set storage set, bytes32 value) private returns (bool);
_remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function _remove(Set storage set, bytes32 value) private returns (bool);
_clear
Removes all the values from a set. O(n). WARNING: This function has an unbounded cost that scales with set size. Developers should keep in mind that 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(Set storage set) private;
_contains
Returns true if the value is in the set. O(1).
function _contains(Set storage set, bytes32 value) private view returns (bool);
_length
Returns the number of values on the set. O(1).
function _length(Set storage set) private view returns (uint256);
_at
*Returns the value 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:
indexmust be strictly less than length.*
function _at(Set storage set, uint256 index) private view returns (bytes32);
_values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function _values(Set storage set) private view returns (bytes32[] memory);
_values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function _values(Set storage set, uint256 start, uint256 end) private view returns (bytes32[] memory);
add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function add(Bytes32Set storage set, bytes32 value) internal returns (bool);
remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool);
clear
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(Bytes32Set storage set) internal;
contains
Returns true if the value is in the set. O(1).
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool);
length
Returns the number of values in the set. O(1).
function length(Bytes32Set storage set) internal view returns (uint256);
at
*Returns the value 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:
indexmust be strictly less than length.*
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32);
values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function 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);
values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function 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, uint256 start, uint256 end) internal view returns (bytes32[] memory);
add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function add(AddressSet storage set, address value) internal returns (bool);
remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function remove(AddressSet storage set, address value) internal returns (bool);
clear
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(AddressSet storage set) internal;
contains
Returns true if the value is in the set. O(1).
function contains(AddressSet storage set, address value) internal view returns (bool);
length
Returns the number of values in the set. O(1).
function length(AddressSet storage set) internal view returns (uint256);
at
*Returns the value 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:
indexmust be strictly less than length.*
function at(AddressSet storage set, uint256 index) internal view returns (address);
values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(AddressSet storage set) internal view returns (address[] memory);
values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(AddressSet storage set, uint256 start, uint256 end) internal view returns (address[] memory);
add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function add(UintSet storage set, uint256 value) internal returns (bool);
remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function remove(UintSet storage set, uint256 value) internal returns (bool);
clear
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(UintSet storage set) internal;
contains
Returns true if the value is in the set. O(1).
function contains(UintSet storage set, uint256 value) internal view returns (bool);
length
Returns the number of values in the set. O(1).
function length(UintSet storage set) internal view returns (uint256);
at
*Returns the value 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:
indexmust be strictly less than length.*
function at(UintSet storage set, uint256 index) internal view returns (uint256);
values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(UintSet storage set) internal view returns (uint256[] memory);
values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(UintSet storage set, uint256 start, uint256 end) internal view returns (uint256[] memory);
add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function add(StringSet storage set, string memory value) internal returns (bool);
remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function remove(StringSet storage set, string memory value) internal returns (bool);
clear
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(StringSet storage set) internal;
contains
Returns true if the value is in the set. O(1).
function contains(StringSet storage set, string memory value) internal view returns (bool);
length
Returns the number of values on the set. O(1).
function length(StringSet storage set) internal view returns (uint256);
at
*Returns the value 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:
indexmust be strictly less than length.*
function at(StringSet storage set, uint256 index) internal view returns (string memory);
values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(StringSet storage set) internal view returns (string[] memory);
values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(StringSet storage set, uint256 start, uint256 end) internal view returns (string[] memory);
add
Add a value to a set. O(1). Returns true if the value was added to the set, that is if it was not already present.
function add(BytesSet storage set, bytes memory value) internal returns (bool);
remove
Removes a value from a set. O(1). Returns true if the value was removed from the set, that is if it was present.
function remove(BytesSet storage set, bytes memory value) internal returns (bool);
clear
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(BytesSet storage set) internal;
contains
Returns true if the value is in the set. O(1).
function contains(BytesSet storage set, bytes memory value) internal view returns (bool);
length
Returns the number of values on the set. O(1).
function length(BytesSet storage set) internal view returns (uint256);
at
*Returns the value 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:
indexmust be strictly less than length.*
function at(BytesSet storage set, uint256 index) internal view returns (bytes memory);
values
Return the entire set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(BytesSet storage set) internal view returns (bytes[] memory);
values
Return a slice of the set in an array WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that this function has an unbounded cost, and using it as part of a state-changing function may render the function uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
function values(BytesSet storage set, uint256 start, uint256 end) internal view returns (bytes[] memory);
Structs
Set
struct Set {
bytes32[] _values;
mapping(bytes32 value => uint256) _positions;
}
Bytes32Set
struct Bytes32Set {
Set _inner;
}
AddressSet
struct AddressSet {
Set _inner;
}
UintSet
struct UintSet {
Set _inner;
}
StringSet
struct StringSet {
string[] _values;
mapping(string value => uint256) _positions;
}
BytesSet
struct BytesSet {
bytes[] _values;
mapping(bytes value => uint256) _positions;
}