EnumerableSet.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (utils/structs/EnumerableSet.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Library for managing
  6. * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
  7. * types.
  8. *
  9. * Sets have the following properties:
  10. *
  11. * - Elements are added, removed, and checked for existence in constant time
  12. * (O(1)).
  13. * - Elements are enumerated in O(n). No guarantees are made on the ordering.
  14. *
  15. * ```
  16. * contract Example {
  17. * // Add the library methods
  18. * using EnumerableSet for EnumerableSet.AddressSet;
  19. *
  20. * // Declare a set state variable
  21. * EnumerableSet.AddressSet private mySet;
  22. * }
  23. * ```
  24. *
  25. * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
  26. * and `uint256` (`UintSet`) are supported.
  27. */
  28. library EnumerableSet {
  29. // To implement this library for multiple types with as little code
  30. // repetition as possible, we write it in terms of a generic Set type with
  31. // bytes32 values.
  32. // The Set implementation uses private functions, and user-facing
  33. // implementations (such as AddressSet) are just wrappers around the
  34. // underlying Set.
  35. // This means that we can only create new EnumerableSets for types that fit
  36. // in bytes32.
  37. struct Set {
  38. // Storage of set values
  39. bytes32[] _values;
  40. // Position of the value in the `values` array, plus 1 because index 0
  41. // means a value is not in the set.
  42. mapping(bytes32 => uint256) _indexes;
  43. }
  44. /**
  45. * @dev Add a value to a set. O(1).
  46. *
  47. * Returns true if the value was added to the set, that is if it was not
  48. * already present.
  49. */
  50. function _add(Set storage set, bytes32 value) private returns (bool) {
  51. if (!_contains(set, value)) {
  52. set._values.push(value);
  53. // The value is stored at length-1, but we add 1 to all indexes
  54. // and use 0 as a sentinel value
  55. set._indexes[value] = set._values.length;
  56. return true;
  57. } else {
  58. return false;
  59. }
  60. }
  61. /**
  62. * @dev Removes a value from a set. O(1).
  63. *
  64. * Returns true if the value was removed from the set, that is if it was
  65. * present.
  66. */
  67. function _remove(Set storage set, bytes32 value) private returns (bool) {
  68. // We read and store the value's index to prevent multiple reads from the same storage slot
  69. uint256 valueIndex = set._indexes[value];
  70. if (valueIndex != 0) {
  71. // Equivalent to contains(set, value)
  72. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  73. // the array, and then remove the last element (sometimes called as 'swap and pop').
  74. // This modifies the order of the array, as noted in {at}.
  75. uint256 toDeleteIndex = valueIndex - 1;
  76. uint256 lastIndex = set._values.length - 1;
  77. if (lastIndex != toDeleteIndex) {
  78. bytes32 lastValue = set._values[lastIndex];
  79. // Move the last value to the index where the value to delete is
  80. set._values[toDeleteIndex] = lastValue;
  81. // Update the index for the moved value
  82. set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
  83. }
  84. // Delete the slot where the moved value was stored
  85. set._values.pop();
  86. // Delete the index for the deleted slot
  87. delete set._indexes[value];
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. /**
  94. * @dev Returns true if the value is in the set. O(1).
  95. */
  96. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  97. return set._indexes[value] != 0;
  98. }
  99. /**
  100. * @dev Returns the number of values on the set. O(1).
  101. */
  102. function _length(Set storage set) private view returns (uint256) {
  103. return set._values.length;
  104. }
  105. /**
  106. * @dev Returns the value stored at position `index` in the set. O(1).
  107. *
  108. * Note that there are no guarantees on the ordering of values inside the
  109. * array, and it may change when more values are added or removed.
  110. *
  111. * Requirements:
  112. *
  113. * - `index` must be strictly less than {length}.
  114. */
  115. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  116. return set._values[index];
  117. }
  118. /**
  119. * @dev Return the entire set in an array
  120. *
  121. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  122. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  123. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  124. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  125. */
  126. function _values(Set storage set) private view returns (bytes32[] memory) {
  127. return set._values;
  128. }
  129. // Bytes32Set
  130. struct Bytes32Set {
  131. Set _inner;
  132. }
  133. /**
  134. * @dev Add a value to a set. O(1).
  135. *
  136. * Returns true if the value was added to the set, that is if it was not
  137. * already present.
  138. */
  139. function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  140. return _add(set._inner, value);
  141. }
  142. /**
  143. * @dev Removes a value from a set. O(1).
  144. *
  145. * Returns true if the value was removed from the set, that is if it was
  146. * present.
  147. */
  148. function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  149. return _remove(set._inner, value);
  150. }
  151. /**
  152. * @dev Returns true if the value is in the set. O(1).
  153. */
  154. function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
  155. return _contains(set._inner, value);
  156. }
  157. /**
  158. * @dev Returns the number of values in the set. O(1).
  159. */
  160. function length(Bytes32Set storage set) internal view returns (uint256) {
  161. return _length(set._inner);
  162. }
  163. /**
  164. * @dev Returns the value stored at position `index` in the set. O(1).
  165. *
  166. * Note that there are no guarantees on the ordering of values inside the
  167. * array, and it may change when more values are added or removed.
  168. *
  169. * Requirements:
  170. *
  171. * - `index` must be strictly less than {length}.
  172. */
  173. function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
  174. return _at(set._inner, index);
  175. }
  176. /**
  177. * @dev Return the entire set in an array
  178. *
  179. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  180. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  181. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  182. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  183. */
  184. function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
  185. return _values(set._inner);
  186. }
  187. // AddressSet
  188. struct AddressSet {
  189. Set _inner;
  190. }
  191. /**
  192. * @dev Add a value to a set. O(1).
  193. *
  194. * Returns true if the value was added to the set, that is if it was not
  195. * already present.
  196. */
  197. function add(AddressSet storage set, address value) internal returns (bool) {
  198. return _add(set._inner, bytes32(uint256(uint160(value))));
  199. }
  200. /**
  201. * @dev Removes a value from a set. O(1).
  202. *
  203. * Returns true if the value was removed from the set, that is if it was
  204. * present.
  205. */
  206. function remove(AddressSet storage set, address value) internal returns (bool) {
  207. return _remove(set._inner, bytes32(uint256(uint160(value))));
  208. }
  209. /**
  210. * @dev Returns true if the value is in the set. O(1).
  211. */
  212. function contains(AddressSet storage set, address value) internal view returns (bool) {
  213. return _contains(set._inner, bytes32(uint256(uint160(value))));
  214. }
  215. /**
  216. * @dev Returns the number of values in the set. O(1).
  217. */
  218. function length(AddressSet storage set) internal view returns (uint256) {
  219. return _length(set._inner);
  220. }
  221. /**
  222. * @dev Returns the value stored at position `index` in the set. O(1).
  223. *
  224. * Note that there are no guarantees on the ordering of values inside the
  225. * array, and it may change when more values are added or removed.
  226. *
  227. * Requirements:
  228. *
  229. * - `index` must be strictly less than {length}.
  230. */
  231. function at(AddressSet storage set, uint256 index) internal view returns (address) {
  232. return address(uint160(uint256(_at(set._inner, index))));
  233. }
  234. /**
  235. * @dev Return the entire set in an array
  236. *
  237. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  238. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  239. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  240. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  241. */
  242. function values(AddressSet storage set) internal view returns (address[] memory) {
  243. bytes32[] memory store = _values(set._inner);
  244. address[] memory result;
  245. assembly {
  246. result := store
  247. }
  248. return result;
  249. }
  250. // UintSet
  251. struct UintSet {
  252. Set _inner;
  253. }
  254. /**
  255. * @dev Add a value to a set. O(1).
  256. *
  257. * Returns true if the value was added to the set, that is if it was not
  258. * already present.
  259. */
  260. function add(UintSet storage set, uint256 value) internal returns (bool) {
  261. return _add(set._inner, bytes32(value));
  262. }
  263. /**
  264. * @dev Removes a value from a set. O(1).
  265. *
  266. * Returns true if the value was removed from the set, that is if it was
  267. * present.
  268. */
  269. function remove(UintSet storage set, uint256 value) internal returns (bool) {
  270. return _remove(set._inner, bytes32(value));
  271. }
  272. /**
  273. * @dev Returns true if the value is in the set. O(1).
  274. */
  275. function contains(UintSet storage set, uint256 value) internal view returns (bool) {
  276. return _contains(set._inner, bytes32(value));
  277. }
  278. /**
  279. * @dev Returns the number of values on the set. O(1).
  280. */
  281. function length(UintSet storage set) internal view returns (uint256) {
  282. return _length(set._inner);
  283. }
  284. /**
  285. * @dev Returns the value stored at position `index` in the set. O(1).
  286. *
  287. * Note that there are no guarantees on the ordering of values inside the
  288. * array, and it may change when more values are added or removed.
  289. *
  290. * Requirements:
  291. *
  292. * - `index` must be strictly less than {length}.
  293. */
  294. function at(UintSet storage set, uint256 index) internal view returns (uint256) {
  295. return uint256(_at(set._inner, index));
  296. }
  297. /**
  298. * @dev Return the entire set in an array
  299. *
  300. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  301. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  302. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  303. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  304. */
  305. function values(UintSet storage set) internal view returns (uint256[] memory) {
  306. bytes32[] memory store = _values(set._inner);
  307. uint256[] memory result;
  308. assembly {
  309. result := store
  310. }
  311. return result;
  312. }
  313. }