EnumerableSet.sol 12 KB

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