EnumerableSet.sol 19 KB

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