EnumerableSet.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. /**
  4. * @dev Library for managing
  5. * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
  6. * types.
  7. *
  8. * Sets have the following properties:
  9. *
  10. * - Elements are added, removed, and checked for existence in constant time
  11. * (O(1)).
  12. * - Elements are enumerated in O(n). No guarantees are made on the ordering.
  13. *
  14. * ```
  15. * contract Example {
  16. * // Add the library methods
  17. * using EnumerableSet for EnumerableSet.AddressSet;
  18. *
  19. * // Declare a set state variable
  20. * EnumerableSet.AddressSet private mySet;
  21. * }
  22. * ```
  23. *
  24. * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
  25. * and `uint256` (`UintSet`) are supported.
  26. */
  27. library EnumerableSet {
  28. // To implement this library for multiple types with as little code
  29. // repetition as possible, we write it in terms of a generic Set type with
  30. // bytes32 values.
  31. // The Set implementation uses private functions, and user-facing
  32. // implementations (such as AddressSet) are just wrappers around the
  33. // underlying Set.
  34. // This means that we can only create new EnumerableSets for types that fit
  35. // in bytes32.
  36. struct Set {
  37. // Storage of set values
  38. bytes32[] _values;
  39. // Position of the value in the `values` array, plus 1 because index 0
  40. // means a value is not in the set.
  41. mapping (bytes32 => uint256) _indexes;
  42. }
  43. /**
  44. * @dev Add a value to a set. O(1).
  45. *
  46. * Returns true if the value was added to the set, that is if it was not
  47. * already present.
  48. */
  49. function _add(Set storage set, bytes32 value) private returns (bool) {
  50. if (!_contains(set, value)) {
  51. set._values.push(value);
  52. // The value is stored at length-1, but we add 1 to all indexes
  53. // and use 0 as a sentinel value
  54. set._indexes[value] = set._values.length;
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. }
  60. /**
  61. * @dev Removes a value from a set. O(1).
  62. *
  63. * Returns true if the value was removed from the set, that is if it was
  64. * present.
  65. */
  66. function _remove(Set storage set, bytes32 value) private returns (bool) {
  67. // We read and store the value's index to prevent multiple reads from the same storage slot
  68. uint256 valueIndex = set._indexes[value];
  69. if (valueIndex != 0) { // Equivalent to contains(set, value)
  70. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  71. // the array, and then remove the last element (sometimes called as 'swap and pop').
  72. // This modifies the order of the array, as noted in {at}.
  73. uint256 toDeleteIndex = valueIndex - 1;
  74. uint256 lastIndex = set._values.length - 1;
  75. // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
  76. // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
  77. bytes32 lastvalue = set._values[lastIndex];
  78. // Move the last value to the index where the value to delete is
  79. set._values[toDeleteIndex] = lastvalue;
  80. // Update the index for the moved value
  81. set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based
  82. // Delete the slot where the moved value was stored
  83. set._values.pop();
  84. // Delete the index for the deleted slot
  85. delete set._indexes[value];
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. /**
  92. * @dev Returns true if the value is in the set. O(1).
  93. */
  94. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  95. return set._indexes[value] != 0;
  96. }
  97. /**
  98. * @dev Returns the number of values on the set. O(1).
  99. */
  100. function _length(Set storage set) private view returns (uint256) {
  101. return set._values.length;
  102. }
  103. /**
  104. * @dev Returns the value stored at position `index` in the set. O(1).
  105. *
  106. * Note that there are no guarantees on the ordering of values inside the
  107. * array, and it may change when more values are added or removed.
  108. *
  109. * Requirements:
  110. *
  111. * - `index` must be strictly less than {length}.
  112. */
  113. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  114. require(set._values.length > index, "EnumerableSet: index out of bounds");
  115. return set._values[index];
  116. }
  117. // Bytes32Set
  118. struct Bytes32Set {
  119. Set _inner;
  120. }
  121. /**
  122. * @dev Add a value to a set. O(1).
  123. *
  124. * Returns true if the value was added to the set, that is if it was not
  125. * already present.
  126. */
  127. function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  128. return _add(set._inner, value);
  129. }
  130. /**
  131. * @dev Removes a value from a set. O(1).
  132. *
  133. * Returns true if the value was removed from the set, that is if it was
  134. * present.
  135. */
  136. function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  137. return _remove(set._inner, value);
  138. }
  139. /**
  140. * @dev Returns true if the value is in the set. O(1).
  141. */
  142. function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
  143. return _contains(set._inner, value);
  144. }
  145. /**
  146. * @dev Returns the number of values in the set. O(1).
  147. */
  148. function length(Bytes32Set storage set) internal view returns (uint256) {
  149. return _length(set._inner);
  150. }
  151. /**
  152. * @dev Returns the value stored at position `index` in the set. O(1).
  153. *
  154. * Note that there are no guarantees on the ordering of values inside the
  155. * array, and it may change when more values are added or removed.
  156. *
  157. * Requirements:
  158. *
  159. * - `index` must be strictly less than {length}.
  160. */
  161. function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
  162. return _at(set._inner, index);
  163. }
  164. // AddressSet
  165. struct AddressSet {
  166. Set _inner;
  167. }
  168. /**
  169. * @dev Add a value to a set. O(1).
  170. *
  171. * Returns true if the value was added to the set, that is if it was not
  172. * already present.
  173. */
  174. function add(AddressSet storage set, address value) internal returns (bool) {
  175. return _add(set._inner, bytes32(uint256(value)));
  176. }
  177. /**
  178. * @dev Removes a value from a set. O(1).
  179. *
  180. * Returns true if the value was removed from the set, that is if it was
  181. * present.
  182. */
  183. function remove(AddressSet storage set, address value) internal returns (bool) {
  184. return _remove(set._inner, bytes32(uint256(value)));
  185. }
  186. /**
  187. * @dev Returns true if the value is in the set. O(1).
  188. */
  189. function contains(AddressSet storage set, address value) internal view returns (bool) {
  190. return _contains(set._inner, bytes32(uint256(value)));
  191. }
  192. /**
  193. * @dev Returns the number of values in the set. O(1).
  194. */
  195. function length(AddressSet storage set) internal view returns (uint256) {
  196. return _length(set._inner);
  197. }
  198. /**
  199. * @dev Returns the value stored at position `index` in the set. O(1).
  200. *
  201. * Note that there are no guarantees on the ordering of values inside the
  202. * array, and it may change when more values are added or removed.
  203. *
  204. * Requirements:
  205. *
  206. * - `index` must be strictly less than {length}.
  207. */
  208. function at(AddressSet storage set, uint256 index) internal view returns (address) {
  209. return address(uint256(_at(set._inner, index)));
  210. }
  211. // UintSet
  212. struct UintSet {
  213. Set _inner;
  214. }
  215. /**
  216. * @dev Add a value to a set. O(1).
  217. *
  218. * Returns true if the value was added to the set, that is if it was not
  219. * already present.
  220. */
  221. function add(UintSet storage set, uint256 value) internal returns (bool) {
  222. return _add(set._inner, bytes32(value));
  223. }
  224. /**
  225. * @dev Removes a value from a set. O(1).
  226. *
  227. * Returns true if the value was removed from the set, that is if it was
  228. * present.
  229. */
  230. function remove(UintSet storage set, uint256 value) internal returns (bool) {
  231. return _remove(set._inner, bytes32(value));
  232. }
  233. /**
  234. * @dev Returns true if the value is in the set. O(1).
  235. */
  236. function contains(UintSet storage set, uint256 value) internal view returns (bool) {
  237. return _contains(set._inner, bytes32(value));
  238. }
  239. /**
  240. * @dev Returns the number of values on the set. O(1).
  241. */
  242. function length(UintSet storage set) internal view returns (uint256) {
  243. return _length(set._inner);
  244. }
  245. /**
  246. * @dev Returns the value stored at position `index` in the set. O(1).
  247. *
  248. * Note that there are no guarantees on the ordering of values inside the
  249. * array, and it may change when more values are added or removed.
  250. *
  251. * Requirements:
  252. *
  253. * - `index` must be strictly less than {length}.
  254. */
  255. function at(UintSet storage set, uint256 index) internal view returns (uint256) {
  256. return uint256(_at(set._inner, index));
  257. }
  258. }