EnumerableSet.sol 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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) { // Equivalent to contains(set, value)
  70. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  71. // the array, and then remove the last element (sometimes called as 'swap and pop').
  72. // This modifies the order of the array, as noted in {at}.
  73. uint256 toDeleteIndex = valueIndex - 1;
  74. uint256 lastIndex = set._values.length - 1;
  75. if (lastIndex != toDeleteIndex) {
  76. bytes32 lastvalue = set._values[lastIndex];
  77. // Move the last value to the index where the value to delete is
  78. set._values[toDeleteIndex] = lastvalue;
  79. // Update the index for the moved value
  80. set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
  81. }
  82. // Delete the slot where the moved value was stored
  83. set._values.pop();
  84. // Delete the index for the deleted slot
  85. delete set._indexes[value];
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. /**
  92. * @dev Returns true if the value is in the set. O(1).
  93. */
  94. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  95. return set._indexes[value] != 0;
  96. }
  97. /**
  98. * @dev Returns the number of values on the set. O(1).
  99. */
  100. function _length(Set storage set) private view returns (uint256) {
  101. return set._values.length;
  102. }
  103. /**
  104. * @dev Returns the value stored at position `index` in the set. O(1).
  105. *
  106. * Note that there are no guarantees on the ordering of values inside the
  107. * array, and it may change when more values are added or removed.
  108. *
  109. * Requirements:
  110. *
  111. * - `index` must be strictly less than {length}.
  112. */
  113. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  114. return set._values[index];
  115. }
  116. // Bytes32Set
  117. struct Bytes32Set {
  118. Set _inner;
  119. }
  120. /**
  121. * @dev Add a value to a set. O(1).
  122. *
  123. * Returns true if the value was added to the set, that is if it was not
  124. * already present.
  125. */
  126. function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  127. return _add(set._inner, value);
  128. }
  129. /**
  130. * @dev Removes a value from a set. O(1).
  131. *
  132. * Returns true if the value was removed from the set, that is if it was
  133. * present.
  134. */
  135. function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
  136. return _remove(set._inner, value);
  137. }
  138. /**
  139. * @dev Returns true if the value is in the set. O(1).
  140. */
  141. function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
  142. return _contains(set._inner, value);
  143. }
  144. /**
  145. * @dev Returns the number of values in the set. O(1).
  146. */
  147. function length(Bytes32Set storage set) internal view returns (uint256) {
  148. return _length(set._inner);
  149. }
  150. /**
  151. * @dev Returns the value stored at position `index` in the set. O(1).
  152. *
  153. * Note that there are no guarantees on the ordering of values inside the
  154. * array, and it may change when more values are added or removed.
  155. *
  156. * Requirements:
  157. *
  158. * - `index` must be strictly less than {length}.
  159. */
  160. function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
  161. return _at(set._inner, index);
  162. }
  163. // AddressSet
  164. struct AddressSet {
  165. Set _inner;
  166. }
  167. /**
  168. * @dev Add a value to a set. O(1).
  169. *
  170. * Returns true if the value was added to the set, that is if it was not
  171. * already present.
  172. */
  173. function add(AddressSet storage set, address value) internal returns (bool) {
  174. return _add(set._inner, bytes32(uint256(uint160(value))));
  175. }
  176. /**
  177. * @dev Removes a value from a set. O(1).
  178. *
  179. * Returns true if the value was removed from the set, that is if it was
  180. * present.
  181. */
  182. function remove(AddressSet storage set, address value) internal returns (bool) {
  183. return _remove(set._inner, bytes32(uint256(uint160(value))));
  184. }
  185. /**
  186. * @dev Returns true if the value is in the set. O(1).
  187. */
  188. function contains(AddressSet storage set, address value) internal view returns (bool) {
  189. return _contains(set._inner, bytes32(uint256(uint160(value))));
  190. }
  191. /**
  192. * @dev Returns the number of values in the set. O(1).
  193. */
  194. function length(AddressSet storage set) internal view returns (uint256) {
  195. return _length(set._inner);
  196. }
  197. /**
  198. * @dev Returns the value stored at position `index` in the set. O(1).
  199. *
  200. * Note that there are no guarantees on the ordering of values inside the
  201. * array, and it may change when more values are added or removed.
  202. *
  203. * Requirements:
  204. *
  205. * - `index` must be strictly less than {length}.
  206. */
  207. function at(AddressSet storage set, uint256 index) internal view returns (address) {
  208. return address(uint160(uint256(_at(set._inner, index))));
  209. }
  210. // UintSet
  211. struct UintSet {
  212. Set _inner;
  213. }
  214. /**
  215. * @dev Add a value to a set. O(1).
  216. *
  217. * Returns true if the value was added to the set, that is if it was not
  218. * already present.
  219. */
  220. function add(UintSet storage set, uint256 value) internal returns (bool) {
  221. return _add(set._inner, bytes32(value));
  222. }
  223. /**
  224. * @dev Removes a value from a set. O(1).
  225. *
  226. * Returns true if the value was removed from the set, that is if it was
  227. * present.
  228. */
  229. function remove(UintSet storage set, uint256 value) internal returns (bool) {
  230. return _remove(set._inner, bytes32(value));
  231. }
  232. /**
  233. * @dev Returns true if the value is in the set. O(1).
  234. */
  235. function contains(UintSet storage set, uint256 value) internal view returns (bool) {
  236. return _contains(set._inner, bytes32(value));
  237. }
  238. /**
  239. * @dev Returns the number of values on the set. O(1).
  240. */
  241. function length(UintSet storage set) internal view returns (uint256) {
  242. return _length(set._inner);
  243. }
  244. /**
  245. * @dev Returns the value stored at position `index` in the set. O(1).
  246. *
  247. * Note that there are no guarantees on the ordering of values inside the
  248. * array, and it may change when more values are added or removed.
  249. *
  250. * Requirements:
  251. *
  252. * - `index` must be strictly less than {length}.
  253. */
  254. function at(UintSet storage set, uint256 index) internal view returns (uint256) {
  255. return uint256(_at(set._inner, index));
  256. }
  257. }