EnumerableSet.sol 17 KB

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