pragma solidity ^0.5.0; /** * @dev 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). The ordering in this list may change. * * As of v2.5.0, only `address` sets are supported. * * Include with `using EnumerableSet for EnumerableSet.AddressSet;`, and use * {newAddressSet} to create a new `AddressSet`. * * _Available since v2.5.0._ * * @author Alberto Cuesta CaƱada */ library EnumerableSet { struct AddressSet { // Position of the value in the `values` array, plus 1 because index 0 // means a value is not in the set. mapping (address => uint256) index; address[] values; } /** * @dev Add a value. O(1). */ function newAddressSet() internal pure returns (AddressSet memory) { return AddressSet({values: new address[](0)}); } /** * @dev Add a value. O(1). Returns false if the value was already in the set. */ function add(AddressSet storage set, address value) internal returns (bool) { if (!contains(set, value)){ set.index[value] = set.values.push(value); return true; } else { return false; } } /** * @dev Remove a value. O(1). Returns false if the calue was not present in the set. */ function remove(AddressSet storage set, address value) internal returns (bool) { if (contains(set, value)){ uint256 toDeleteIndex = set.index[value] - 1; uint256 lastIndex = set.values.length - 1; address lastValue = set.values[lastIndex]; // Move the last value to the index where the deleted value is set.values[toDeleteIndex] = lastValue; // Update the index for the moved value set.index[lastValue] = toDeleteIndex + 1; // All indexes are 1-based // Delete the index entry for the deleted value delete set.index[value]; // Delete the old entry for the moved value set.values.pop(); return true; } else { return false; } } /** * @dev Returns true if the value is in the set. O(1). */ function contains(AddressSet storage set, address value) internal view returns (bool) { return set.index[value] != 0; } /** * @dev Return an array with all values in the set. O(N). */ function enumerate(AddressSet storage set) internal view returns (address[] memory) { address[] memory output = new address[](set.values.length); for (uint256 i; i < set.values.length; i++){ output[i] = set.values[i]; } return output; } }