EnumerableSet.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol)
  3. // This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.
  4. pragma solidity ^0.8.0;
  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. * ```
  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 unusable.
  32. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  33. *
  34. * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet.
  35. * ====
  36. */
  37. library EnumerableSet {
  38. // To implement this library for multiple types with as little code
  39. // repetition as possible, we write it in terms of a generic Set type with
  40. // bytes32 values.
  41. // The Set implementation uses private functions, and user-facing
  42. // implementations (such as AddressSet) are just wrappers around the
  43. // underlying Set.
  44. // This means that we can only create new EnumerableSets for types that fit
  45. // in bytes32.
  46. struct Set {
  47. // Storage of set values
  48. bytes32[] _values;
  49. // Position of the value in the `values` array, plus 1 because index 0
  50. // means a value is not in the set.
  51. mapping(bytes32 => uint256) _indexes;
  52. }
  53. /**
  54. * @dev Add a value to a set. O(1).
  55. *
  56. * Returns true if the value was added to the set, that is if it was not
  57. * already present.
  58. */
  59. function _add(Set storage set, bytes32 value) private returns (bool) {
  60. if (!_contains(set, value)) {
  61. set._values.push(value);
  62. // The value is stored at length-1, but we add 1 to all indexes
  63. // and use 0 as a sentinel value
  64. set._indexes[value] = set._values.length;
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. /**
  71. * @dev Removes a value from a set. O(1).
  72. *
  73. * Returns true if the value was removed from the set, that is if it was
  74. * present.
  75. */
  76. function _remove(Set storage set, bytes32 value) private returns (bool) {
  77. // We read and store the value's index to prevent multiple reads from the same storage slot
  78. uint256 valueIndex = set._indexes[value];
  79. if (valueIndex != 0) {
  80. // Equivalent to contains(set, value)
  81. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  82. // the array, and then remove the last element (sometimes called as 'swap and pop').
  83. // This modifies the order of the array, as noted in {at}.
  84. uint256 toDeleteIndex = valueIndex - 1;
  85. uint256 lastIndex = set._values.length - 1;
  86. if (lastIndex != toDeleteIndex) {
  87. bytes32 lastValue = set._values[lastIndex];
  88. // Move the last value to the index where the value to delete is
  89. set._values[toDeleteIndex] = lastValue;
  90. // Update the index for the moved value
  91. set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
  92. }
  93. // Delete the slot where the moved value was stored
  94. set._values.pop();
  95. // Delete the index for the deleted slot
  96. delete set._indexes[value];
  97. return true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. /**
  103. * @dev Returns true if the value is in the set. O(1).
  104. */
  105. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  106. return set._indexes[value] != 0;
  107. }
  108. /**
  109. * @dev Returns the number of values on the set. O(1).
  110. */
  111. function _length(Set storage set) private view returns (uint256) {
  112. return set._values.length;
  113. }
  114. /**
  115. * @dev Returns the value stored at position `index` in the set. O(1).
  116. *
  117. * Note that there are no guarantees on the ordering of values inside the
  118. * array, and it may change when more values are added or removed.
  119. *
  120. * Requirements:
  121. *
  122. * - `index` must be strictly less than {length}.
  123. */
  124. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  125. return set._values[index];
  126. }
  127. /**
  128. * @dev Return the entire set in an array
  129. *
  130. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  131. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  132. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  133. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  134. */
  135. function _values(Set storage set) private view returns (bytes32[] memory) {
  136. return set._values;
  137. }
  138. // Bytes32Set
  139. struct Bytes32Set {
  140. Set _inner;
  141. }
  142. /**
  143. * @dev Add a value to a set. O(1).
  144. *
  145. * Returns true if the value was added to the set, that is if it was not
  146. * already present.
  147. */
  148. function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  149. return _add(set._inner, value);
  150. }
  151. /**
  152. * @dev Removes a value from a set. O(1).
  153. *
  154. * Returns true if the value was removed from the set, that is if it was
  155. * present.
  156. */
  157. function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  158. return _remove(set._inner, value);
  159. }
  160. /**
  161. * @dev Returns true if the value is in the set. O(1).
  162. */
  163. function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
  164. return _contains(set._inner, value);
  165. }
  166. /**
  167. * @dev Returns the number of values in the set. O(1).
  168. */
  169. function length(Bytes32Set storage set) internal view returns (uint256) {
  170. return _length(set._inner);
  171. }
  172. /**
  173. * @dev Returns the value stored at position `index` in the set. O(1).
  174. *
  175. * Note that there are no guarantees on the ordering of values inside the
  176. * array, and it may change when more values are added or removed.
  177. *
  178. * Requirements:
  179. *
  180. * - `index` must be strictly less than {length}.
  181. */
  182. function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
  183. return _at(set._inner, index);
  184. }
  185. /**
  186. * @dev Return the entire set in an array
  187. *
  188. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  189. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  190. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  191. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  192. */
  193. function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
  194. bytes32[] memory store = _values(set._inner);
  195. bytes32[] memory result;
  196. /// @solidity memory-safe-assembly
  197. assembly {
  198. result := store
  199. }
  200. return result;
  201. }
  202. // AddressSet
  203. struct AddressSet {
  204. Set _inner;
  205. }
  206. /**
  207. * @dev Add a value to a set. O(1).
  208. *
  209. * Returns true if the value was added to the set, that is if it was not
  210. * already present.
  211. */
  212. function add(AddressSet storage set, address value) internal returns (bool) {
  213. return _add(set._inner, bytes32(uint256(uint160(value))));
  214. }
  215. /**
  216. * @dev Removes a value from a set. O(1).
  217. *
  218. * Returns true if the value was removed from the set, that is if it was
  219. * present.
  220. */
  221. function remove(AddressSet storage set, address value) internal returns (bool) {
  222. return _remove(set._inner, bytes32(uint256(uint160(value))));
  223. }
  224. /**
  225. * @dev Returns true if the value is in the set. O(1).
  226. */
  227. function contains(AddressSet storage set, address value) internal view returns (bool) {
  228. return _contains(set._inner, bytes32(uint256(uint160(value))));
  229. }
  230. /**
  231. * @dev Returns the number of values in the set. O(1).
  232. */
  233. function length(AddressSet storage set) internal view returns (uint256) {
  234. return _length(set._inner);
  235. }
  236. /**
  237. * @dev Returns the value stored at position `index` in the set. O(1).
  238. *
  239. * Note that there are no guarantees on the ordering of values inside the
  240. * array, and it may change when more values are added or removed.
  241. *
  242. * Requirements:
  243. *
  244. * - `index` must be strictly less than {length}.
  245. */
  246. function at(AddressSet storage set, uint256 index) internal view returns (address) {
  247. return address(uint160(uint256(_at(set._inner, index))));
  248. }
  249. /**
  250. * @dev Return the entire set in an array
  251. *
  252. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  253. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  254. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  255. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  256. */
  257. function values(AddressSet storage set) internal view returns (address[] memory) {
  258. bytes32[] memory store = _values(set._inner);
  259. address[] memory result;
  260. /// @solidity memory-safe-assembly
  261. assembly {
  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. /// @solidity memory-safe-assembly
  325. assembly {
  326. result := store
  327. }
  328. return result;
  329. }
  330. }