EnumerableSet improvements (#2077)

* Remove newAddressSet

* Add count and get functions.

* Fix lint

(cherry picked from commit 7988c044e0)
This commit is contained in:
Nicolás Venturo
2020-02-04 19:15:32 -03:00
parent 0ac83ce289
commit 1b938e39a8
3 changed files with 62 additions and 29 deletions

View File

@ -29,17 +29,6 @@ library EnumerableSet {
address[] values;
}
/**
* @dev Creates a new empty address set.
*/
function newAddressSet()
internal
pure
returns (AddressSet memory)
{
return AddressSet({values: new address[](0)});
}
/**
* @dev Add a value to a set. O(1).
* Returns false if the value was already in the set.
@ -105,6 +94,9 @@ library EnumerableSet {
* @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
* {get} instead in these cases.
*/
function enumerate(AddressSet storage set)
internal
@ -117,4 +109,33 @@ library EnumerableSet {
}
return output;
}
/**
* @dev Returns the number of elements on 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.
*/
function length(AddressSet storage set)
internal
view
returns (uint256)
{
return set.values.length;
}
/** @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 get(AddressSet storage set, uint256 index)
internal
view
returns (address)
{
return set.values[index];
}
}