EnumerableSet.sol 12 KB

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