EnumerableSet.sol 14 KB

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