EnumerableSet.sol 12 KB

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