EnumerableSet.sol 13 KB

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