EnumerableSet.sol 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.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. * The following types are supported:
  29. *
  30. * - `bytes32` (`Bytes32Set`) since v3.3.0
  31. * - `address` (`AddressSet`) since v3.3.0
  32. * - `uint256` (`UintSet`) since v3.3.0
  33. * - `string` (`StringSet`) since v5.4.0
  34. * - `bytes` (`BytesSet`) since v5.4.0
  35. *
  36. * [WARNING]
  37. * ====
  38. * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
  39. * unusable.
  40. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  41. *
  42. * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
  43. * array of EnumerableSet.
  44. * ====
  45. */
  46. library EnumerableSet {
  47. // To implement this library for multiple types with as little code
  48. // repetition as possible, we write it in terms of a generic Set type with
  49. // bytes32 values.
  50. // The Set implementation uses private functions, and user-facing
  51. // implementations (such as AddressSet) are just wrappers around the
  52. // underlying Set.
  53. // This means that we can only create new EnumerableSets for types that fit
  54. // in bytes32.
  55. struct Set {
  56. // Storage of set values
  57. bytes32[] _values;
  58. // Position is the index of the value in the `values` array plus 1.
  59. // Position 0 is used to mean a value is not in the set.
  60. mapping(bytes32 value => uint256) _positions;
  61. }
  62. /**
  63. * @dev Add a value to a set. O(1).
  64. *
  65. * Returns true if the value was added to the set, that is if it was not
  66. * already present.
  67. */
  68. function _add(Set storage set, bytes32 value) private returns (bool) {
  69. if (!_contains(set, value)) {
  70. set._values.push(value);
  71. // The value is stored at length-1, but we add 1 to all indexes
  72. // and use 0 as a sentinel value
  73. set._positions[value] = set._values.length;
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * @dev Removes a value from a set. O(1).
  81. *
  82. * Returns true if the value was removed from the set, that is if it was
  83. * present.
  84. */
  85. function _remove(Set storage set, bytes32 value) private returns (bool) {
  86. // We cache the value's position to prevent multiple reads from the same storage slot
  87. uint256 position = set._positions[value];
  88. if (position != 0) {
  89. // Equivalent to contains(set, value)
  90. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  91. // the array, and then remove the last element (sometimes called as 'swap and pop').
  92. // This modifies the order of the array, as noted in {at}.
  93. uint256 valueIndex = position - 1;
  94. uint256 lastIndex = set._values.length - 1;
  95. if (valueIndex != lastIndex) {
  96. bytes32 lastValue = set._values[lastIndex];
  97. // Move the lastValue to the index where the value to delete is
  98. set._values[valueIndex] = lastValue;
  99. // Update the tracked position of the lastValue (that was just moved)
  100. set._positions[lastValue] = position;
  101. }
  102. // Delete the slot where the moved value was stored
  103. set._values.pop();
  104. // Delete the tracked position for the deleted slot
  105. delete set._positions[value];
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * @dev Removes all the values from a set. O(n).
  113. *
  114. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  115. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  116. */
  117. function _clear(Set storage set) private {
  118. uint256 len = _length(set);
  119. for (uint256 i = 0; i < len; ++i) {
  120. delete set._positions[set._values[i]];
  121. }
  122. Arrays.unsafeSetLength(set._values, 0);
  123. }
  124. /**
  125. * @dev Returns true if the value is in the set. O(1).
  126. */
  127. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  128. return set._positions[value] != 0;
  129. }
  130. /**
  131. * @dev Returns the number of values on the set. O(1).
  132. */
  133. function _length(Set storage set) private view returns (uint256) {
  134. return set._values.length;
  135. }
  136. /**
  137. * @dev Returns the value stored at position `index` in the set. O(1).
  138. *
  139. * Note that there are no guarantees on the ordering of values inside the
  140. * array, and it may change when more values are added or removed.
  141. *
  142. * Requirements:
  143. *
  144. * - `index` must be strictly less than {length}.
  145. */
  146. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  147. return set._values[index];
  148. }
  149. /**
  150. * @dev Return the entire set in an array
  151. *
  152. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  153. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  154. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  155. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  156. */
  157. function _values(Set storage set) private view returns (bytes32[] memory) {
  158. return set._values;
  159. }
  160. // Bytes32Set
  161. struct Bytes32Set {
  162. Set _inner;
  163. }
  164. /**
  165. * @dev Add a value to a set. O(1).
  166. *
  167. * Returns true if the value was added to the set, that is if it was not
  168. * already present.
  169. */
  170. function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  171. return _add(set._inner, value);
  172. }
  173. /**
  174. * @dev Removes a value from a set. O(1).
  175. *
  176. * Returns true if the value was removed from the set, that is if it was
  177. * present.
  178. */
  179. function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  180. return _remove(set._inner, value);
  181. }
  182. /**
  183. * @dev Removes all the values from a set. O(n).
  184. *
  185. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  186. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  187. */
  188. function clear(Bytes32Set storage set) internal {
  189. _clear(set._inner);
  190. }
  191. /**
  192. * @dev Returns true if the value is in the set. O(1).
  193. */
  194. function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
  195. return _contains(set._inner, value);
  196. }
  197. /**
  198. * @dev Returns the number of values in the set. O(1).
  199. */
  200. function length(Bytes32Set storage set) internal view returns (uint256) {
  201. return _length(set._inner);
  202. }
  203. /**
  204. * @dev Returns the value stored at position `index` in the set. O(1).
  205. *
  206. * Note that there are no guarantees on the ordering of values inside the
  207. * array, and it may change when more values are added or removed.
  208. *
  209. * Requirements:
  210. *
  211. * - `index` must be strictly less than {length}.
  212. */
  213. function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
  214. return _at(set._inner, index);
  215. }
  216. /**
  217. * @dev Return the entire set in an array
  218. *
  219. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  220. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  221. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  222. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  223. */
  224. function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
  225. bytes32[] memory store = _values(set._inner);
  226. bytes32[] memory result;
  227. assembly ("memory-safe") {
  228. result := store
  229. }
  230. return result;
  231. }
  232. // AddressSet
  233. struct AddressSet {
  234. Set _inner;
  235. }
  236. /**
  237. * @dev Add a value to a set. O(1).
  238. *
  239. * Returns true if the value was added to the set, that is if it was not
  240. * already present.
  241. */
  242. function add(AddressSet storage set, address value) internal returns (bool) {
  243. return _add(set._inner, bytes32(uint256(uint160(value))));
  244. }
  245. /**
  246. * @dev Removes a value from a set. O(1).
  247. *
  248. * Returns true if the value was removed from the set, that is if it was
  249. * present.
  250. */
  251. function remove(AddressSet storage set, address value) internal returns (bool) {
  252. return _remove(set._inner, bytes32(uint256(uint160(value))));
  253. }
  254. /**
  255. * @dev Removes all the values from a set. O(n).
  256. *
  257. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  258. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  259. */
  260. function clear(AddressSet storage set) internal {
  261. _clear(set._inner);
  262. }
  263. /**
  264. * @dev Returns true if the value is in the set. O(1).
  265. */
  266. function contains(AddressSet storage set, address value) internal view returns (bool) {
  267. return _contains(set._inner, bytes32(uint256(uint160(value))));
  268. }
  269. /**
  270. * @dev Returns the number of values in the set. O(1).
  271. */
  272. function length(AddressSet storage set) internal view returns (uint256) {
  273. return _length(set._inner);
  274. }
  275. /**
  276. * @dev Returns the value stored at position `index` in the set. O(1).
  277. *
  278. * Note that there are no guarantees on the ordering of values inside the
  279. * array, and it may change when more values are added or removed.
  280. *
  281. * Requirements:
  282. *
  283. * - `index` must be strictly less than {length}.
  284. */
  285. function at(AddressSet storage set, uint256 index) internal view returns (address) {
  286. return address(uint160(uint256(_at(set._inner, index))));
  287. }
  288. /**
  289. * @dev Return the entire set in an array
  290. *
  291. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  292. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  293. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  294. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  295. */
  296. function values(AddressSet storage set) internal view returns (address[] memory) {
  297. bytes32[] memory store = _values(set._inner);
  298. address[] memory result;
  299. assembly ("memory-safe") {
  300. result := store
  301. }
  302. return result;
  303. }
  304. // UintSet
  305. struct UintSet {
  306. Set _inner;
  307. }
  308. /**
  309. * @dev Add a value to a set. O(1).
  310. *
  311. * Returns true if the value was added to the set, that is if it was not
  312. * already present.
  313. */
  314. function add(UintSet storage set, uint256 value) internal returns (bool) {
  315. return _add(set._inner, bytes32(value));
  316. }
  317. /**
  318. * @dev Removes a value from a set. O(1).
  319. *
  320. * Returns true if the value was removed from the set, that is if it was
  321. * present.
  322. */
  323. function remove(UintSet storage set, uint256 value) internal returns (bool) {
  324. return _remove(set._inner, bytes32(value));
  325. }
  326. /**
  327. * @dev Removes all the values from a set. O(n).
  328. *
  329. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  330. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  331. */
  332. function clear(UintSet storage set) internal {
  333. _clear(set._inner);
  334. }
  335. /**
  336. * @dev Returns true if the value is in the set. O(1).
  337. */
  338. function contains(UintSet storage set, uint256 value) internal view returns (bool) {
  339. return _contains(set._inner, bytes32(value));
  340. }
  341. /**
  342. * @dev Returns the number of values in the set. O(1).
  343. */
  344. function length(UintSet storage set) internal view returns (uint256) {
  345. return _length(set._inner);
  346. }
  347. /**
  348. * @dev Returns the value stored at position `index` in the set. O(1).
  349. *
  350. * Note that there are no guarantees on the ordering of values inside the
  351. * array, and it may change when more values are added or removed.
  352. *
  353. * Requirements:
  354. *
  355. * - `index` must be strictly less than {length}.
  356. */
  357. function at(UintSet storage set, uint256 index) internal view returns (uint256) {
  358. return uint256(_at(set._inner, index));
  359. }
  360. /**
  361. * @dev Return the entire set in an array
  362. *
  363. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  364. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  365. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  366. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  367. */
  368. function values(UintSet storage set) internal view returns (uint256[] memory) {
  369. bytes32[] memory store = _values(set._inner);
  370. uint256[] memory result;
  371. assembly ("memory-safe") {
  372. result := store
  373. }
  374. return result;
  375. }
  376. struct StringSet {
  377. // Storage of set values
  378. string[] _values;
  379. // Position is the index of the value in the `values` array plus 1.
  380. // Position 0 is used to mean a value is not in the set.
  381. mapping(string value => uint256) _positions;
  382. }
  383. /**
  384. * @dev Add a value to a set. O(1).
  385. *
  386. * Returns true if the value was added to the set, that is if it was not
  387. * already present.
  388. */
  389. function add(StringSet storage self, string memory value) internal returns (bool) {
  390. if (!contains(self, value)) {
  391. self._values.push(value);
  392. // The value is stored at length-1, but we add 1 to all indexes
  393. // and use 0 as a sentinel value
  394. self._positions[value] = self._values.length;
  395. return true;
  396. } else {
  397. return false;
  398. }
  399. }
  400. /**
  401. * @dev Removes a value from a set. O(1).
  402. *
  403. * Returns true if the value was removed from the set, that is if it was
  404. * present.
  405. */
  406. function remove(StringSet storage self, string memory value) internal returns (bool) {
  407. // We cache the value's position to prevent multiple reads from the same storage slot
  408. uint256 position = self._positions[value];
  409. if (position != 0) {
  410. // Equivalent to contains(self, value)
  411. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  412. // the array, and then remove the last element (sometimes called as 'swap and pop').
  413. // This modifies the order of the array, as noted in {at}.
  414. uint256 valueIndex = position - 1;
  415. uint256 lastIndex = self._values.length - 1;
  416. if (valueIndex != lastIndex) {
  417. string memory lastValue = self._values[lastIndex];
  418. // Move the lastValue to the index where the value to delete is
  419. self._values[valueIndex] = lastValue;
  420. // Update the tracked position of the lastValue (that was just moved)
  421. self._positions[lastValue] = position;
  422. }
  423. // Delete the slot where the moved value was stored
  424. self._values.pop();
  425. // Delete the tracked position for the deleted slot
  426. delete self._positions[value];
  427. return true;
  428. } else {
  429. return false;
  430. }
  431. }
  432. /**
  433. * @dev Removes all the values from a set. O(n).
  434. *
  435. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  436. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  437. */
  438. function clear(StringSet storage set) internal {
  439. uint256 len = length(set);
  440. for (uint256 i = 0; i < len; ++i) {
  441. delete set._positions[set._values[i]];
  442. }
  443. Arrays.unsafeSetLength(set._values, 0);
  444. }
  445. /**
  446. * @dev Returns true if the value is in the set. O(1).
  447. */
  448. function contains(StringSet storage self, string memory value) internal view returns (bool) {
  449. return self._positions[value] != 0;
  450. }
  451. /**
  452. * @dev Returns the number of values on the set. O(1).
  453. */
  454. function length(StringSet storage self) internal view returns (uint256) {
  455. return self._values.length;
  456. }
  457. /**
  458. * @dev Returns the value stored at position `index` in the set. O(1).
  459. *
  460. * Note that there are no guarantees on the ordering of values inside the
  461. * array, and it may change when more values are added or removed.
  462. *
  463. * Requirements:
  464. *
  465. * - `index` must be strictly less than {length}.
  466. */
  467. function at(StringSet storage self, uint256 index) internal view returns (string memory) {
  468. return self._values[index];
  469. }
  470. /**
  471. * @dev Return the entire set in an array
  472. *
  473. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  474. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  475. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  476. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  477. */
  478. function values(StringSet storage self) internal view returns (string[] memory) {
  479. return self._values;
  480. }
  481. struct BytesSet {
  482. // Storage of set values
  483. bytes[] _values;
  484. // Position is the index of the value in the `values` array plus 1.
  485. // Position 0 is used to mean a value is not in the set.
  486. mapping(bytes value => uint256) _positions;
  487. }
  488. /**
  489. * @dev Add a value to a set. O(1).
  490. *
  491. * Returns true if the value was added to the set, that is if it was not
  492. * already present.
  493. */
  494. function add(BytesSet storage self, bytes memory value) internal returns (bool) {
  495. if (!contains(self, value)) {
  496. self._values.push(value);
  497. // The value is stored at length-1, but we add 1 to all indexes
  498. // and use 0 as a sentinel value
  499. self._positions[value] = self._values.length;
  500. return true;
  501. } else {
  502. return false;
  503. }
  504. }
  505. /**
  506. * @dev Removes a value from a set. O(1).
  507. *
  508. * Returns true if the value was removed from the set, that is if it was
  509. * present.
  510. */
  511. function remove(BytesSet storage self, bytes memory value) internal returns (bool) {
  512. // We cache the value's position to prevent multiple reads from the same storage slot
  513. uint256 position = self._positions[value];
  514. if (position != 0) {
  515. // Equivalent to contains(self, value)
  516. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  517. // the array, and then remove the last element (sometimes called as 'swap and pop').
  518. // This modifies the order of the array, as noted in {at}.
  519. uint256 valueIndex = position - 1;
  520. uint256 lastIndex = self._values.length - 1;
  521. if (valueIndex != lastIndex) {
  522. bytes memory lastValue = self._values[lastIndex];
  523. // Move the lastValue to the index where the value to delete is
  524. self._values[valueIndex] = lastValue;
  525. // Update the tracked position of the lastValue (that was just moved)
  526. self._positions[lastValue] = position;
  527. }
  528. // Delete the slot where the moved value was stored
  529. self._values.pop();
  530. // Delete the tracked position for the deleted slot
  531. delete self._positions[value];
  532. return true;
  533. } else {
  534. return false;
  535. }
  536. }
  537. /**
  538. * @dev Removes all the values from a set. O(n).
  539. *
  540. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  541. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  542. */
  543. function clear(BytesSet storage set) internal {
  544. uint256 len = length(set);
  545. for (uint256 i = 0; i < len; ++i) {
  546. delete set._positions[set._values[i]];
  547. }
  548. Arrays.unsafeSetLength(set._values, 0);
  549. }
  550. /**
  551. * @dev Returns true if the value is in the set. O(1).
  552. */
  553. function contains(BytesSet storage self, bytes memory value) internal view returns (bool) {
  554. return self._positions[value] != 0;
  555. }
  556. /**
  557. * @dev Returns the number of values on the set. O(1).
  558. */
  559. function length(BytesSet storage self) internal view returns (uint256) {
  560. return self._values.length;
  561. }
  562. /**
  563. * @dev Returns the value stored at position `index` in the set. O(1).
  564. *
  565. * Note that there are no guarantees on the ordering of values inside the
  566. * array, and it may change when more values are added or removed.
  567. *
  568. * Requirements:
  569. *
  570. * - `index` must be strictly less than {length}.
  571. */
  572. function at(BytesSet storage self, uint256 index) internal view returns (bytes memory) {
  573. return self._values[index];
  574. }
  575. /**
  576. * @dev Return the entire set in an array
  577. *
  578. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  579. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  580. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  581. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  582. */
  583. function values(BytesSet storage self) internal view returns (bytes[] memory) {
  584. return self._values;
  585. }
  586. }