EnumerableSet.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.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
  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 of the value in the `values` array, plus 1 because index 0
  52. // means a value is not in the set.
  53. mapping(bytes32 => uint256) _indexes;
  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._indexes[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 read and store the value's index to prevent multiple reads from the same storage slot
  80. uint256 valueIndex = set._indexes[value];
  81. if (valueIndex != 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 toDeleteIndex = valueIndex - 1;
  87. uint256 lastIndex = set._values.length - 1;
  88. if (lastIndex != toDeleteIndex) {
  89. bytes32 lastValue = set._values[lastIndex];
  90. // Move the last value to the index where the value to delete is
  91. set._values[toDeleteIndex] = lastValue;
  92. // Update the index for the moved value
  93. set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
  94. }
  95. // Delete the slot where the moved value was stored
  96. set._values.pop();
  97. // Delete the index for the deleted slot
  98. delete set._indexes[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._indexes[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. /// @solidity memory-safe-assembly
  199. assembly {
  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. /// @solidity memory-safe-assembly
  263. assembly {
  264. result := store
  265. }
  266. return result;
  267. }
  268. // UintSet
  269. struct UintSet {
  270. Set _inner;
  271. }
  272. /**
  273. * @dev Add a value to a set. O(1).
  274. *
  275. * Returns true if the value was added to the set, that is if it was not
  276. * already present.
  277. */
  278. function add(UintSet storage set, uint256 value) internal returns (bool) {
  279. return _add(set._inner, bytes32(value));
  280. }
  281. /**
  282. * @dev Removes a value from a set. O(1).
  283. *
  284. * Returns true if the value was removed from the set, that is if it was
  285. * present.
  286. */
  287. function remove(UintSet storage set, uint256 value) internal returns (bool) {
  288. return _remove(set._inner, bytes32(value));
  289. }
  290. /**
  291. * @dev Returns true if the value is in the set. O(1).
  292. */
  293. function contains(UintSet storage set, uint256 value) internal view returns (bool) {
  294. return _contains(set._inner, bytes32(value));
  295. }
  296. /**
  297. * @dev Returns the number of values in the set. O(1).
  298. */
  299. function length(UintSet storage set) internal view returns (uint256) {
  300. return _length(set._inner);
  301. }
  302. /**
  303. * @dev Returns the value stored at position `index` in the set. O(1).
  304. *
  305. * Note that there are no guarantees on the ordering of values inside the
  306. * array, and it may change when more values are added or removed.
  307. *
  308. * Requirements:
  309. *
  310. * - `index` must be strictly less than {length}.
  311. */
  312. function at(UintSet storage set, uint256 index) internal view returns (uint256) {
  313. return uint256(_at(set._inner, index));
  314. }
  315. /**
  316. * @dev Return the entire set in an array
  317. *
  318. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  319. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  320. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  321. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  322. */
  323. function values(UintSet storage set) internal view returns (uint256[] memory) {
  324. bytes32[] memory store = _values(set._inner);
  325. uint256[] memory result;
  326. /// @solidity memory-safe-assembly
  327. assembly {
  328. result := store
  329. }
  330. return result;
  331. }
  332. }