EnumerableMap.sol 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./EnumerableSet.sol";
  4. /**
  5. * @dev Library for managing an enumerable variant of Solidity's
  6. * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
  7. * type.
  8. *
  9. * Maps have the following properties:
  10. *
  11. * - Entries are added, removed, and checked for existence in constant time
  12. * (O(1)).
  13. * - Entries are enumerated in O(n). No guarantees are made on the ordering.
  14. *
  15. * ```
  16. * contract Example {
  17. * // Add the library methods
  18. * using EnumerableMap for EnumerableMap.UintToAddressMap;
  19. *
  20. * // Declare a set state variable
  21. * EnumerableMap.UintToAddressMap private myMap;
  22. * }
  23. * ```
  24. *
  25. * As of v3.0.0, only maps of type `uint256 -> address` (`UintToAddressMap`) are
  26. * supported.
  27. */
  28. library EnumerableMap {
  29. using EnumerableSet for EnumerableSet.Bytes32Set;
  30. // To implement this library for multiple types with as little code
  31. // repetition as possible, we write it in terms of a generic Map type with
  32. // bytes32 keys and values.
  33. // The Map implementation uses private functions, and user-facing
  34. // implementations (such as Uint256ToAddressMap) are just wrappers around
  35. // the underlying Map.
  36. // This means that we can only create new EnumerableMaps for types that fit
  37. // in bytes32.
  38. struct Map {
  39. // Storage of keys
  40. EnumerableSet.Bytes32Set _keys;
  41. mapping(bytes32 => bytes32) _values;
  42. }
  43. /**
  44. * @dev Adds a key-value pair to a map, or updates the value for an existing
  45. * key. O(1).
  46. *
  47. * Returns true if the key was added to the map, that is if it was not
  48. * already present.
  49. */
  50. function _set(
  51. Map storage map,
  52. bytes32 key,
  53. bytes32 value
  54. ) private returns (bool) {
  55. map._values[key] = value;
  56. return map._keys.add(key);
  57. }
  58. /**
  59. * @dev Removes a key-value pair from a map. O(1).
  60. *
  61. * Returns true if the key was removed from the map, that is if it was present.
  62. */
  63. function _remove(Map storage map, bytes32 key) private returns (bool) {
  64. delete map._values[key];
  65. return map._keys.remove(key);
  66. }
  67. /**
  68. * @dev Returns true if the key is in the map. O(1).
  69. */
  70. function _contains(Map storage map, bytes32 key) private view returns (bool) {
  71. return map._keys.contains(key);
  72. }
  73. /**
  74. * @dev Returns the number of key-value pairs in the map. O(1).
  75. */
  76. function _length(Map storage map) private view returns (uint256) {
  77. return map._keys.length();
  78. }
  79. /**
  80. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  81. *
  82. * Note that there are no guarantees on the ordering of entries inside the
  83. * array, and it may change when more entries are added or removed.
  84. *
  85. * Requirements:
  86. *
  87. * - `index` must be strictly less than {length}.
  88. */
  89. function _at(Map storage map, uint256 index) private view returns (bytes32, bytes32) {
  90. bytes32 key = map._keys.at(index);
  91. return (key, map._values[key]);
  92. }
  93. /**
  94. * @dev Tries to returns the value associated with `key`. O(1).
  95. * Does not revert if `key` is not in the map.
  96. */
  97. function _tryGet(Map storage map, bytes32 key) private view returns (bool, bytes32) {
  98. bytes32 value = map._values[key];
  99. if (value == bytes32(0)) {
  100. return (_contains(map, key), bytes32(0));
  101. } else {
  102. return (true, value);
  103. }
  104. }
  105. /**
  106. * @dev Returns the value associated with `key`. O(1).
  107. *
  108. * Requirements:
  109. *
  110. * - `key` must be in the map.
  111. */
  112. function _get(Map storage map, bytes32 key) private view returns (bytes32) {
  113. bytes32 value = map._values[key];
  114. require(value != 0 || _contains(map, key), "EnumerableMap: nonexistent key");
  115. return value;
  116. }
  117. /**
  118. * @dev Same as {_get}, with a custom error message when `key` is not in the map.
  119. *
  120. * CAUTION: This function is deprecated because it requires allocating memory for the error
  121. * message unnecessarily. For custom revert reasons use {_tryGet}.
  122. */
  123. function _get(
  124. Map storage map,
  125. bytes32 key,
  126. string memory errorMessage
  127. ) private view returns (bytes32) {
  128. bytes32 value = map._values[key];
  129. require(value != 0 || _contains(map, key), errorMessage);
  130. return value;
  131. }
  132. // UintToAddressMap
  133. struct UintToAddressMap {
  134. Map _inner;
  135. }
  136. /**
  137. * @dev Adds a key-value pair to a map, or updates the value for an existing
  138. * key. O(1).
  139. *
  140. * Returns true if the key was added to the map, that is if it was not
  141. * already present.
  142. */
  143. function set(
  144. UintToAddressMap storage map,
  145. uint256 key,
  146. address value
  147. ) internal returns (bool) {
  148. return _set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  149. }
  150. /**
  151. * @dev Removes a value from a set. O(1).
  152. *
  153. * Returns true if the key was removed from the map, that is if it was present.
  154. */
  155. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  156. return _remove(map._inner, bytes32(key));
  157. }
  158. /**
  159. * @dev Returns true if the key is in the map. O(1).
  160. */
  161. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  162. return _contains(map._inner, bytes32(key));
  163. }
  164. /**
  165. * @dev Returns the number of elements in the map. O(1).
  166. */
  167. function length(UintToAddressMap storage map) internal view returns (uint256) {
  168. return _length(map._inner);
  169. }
  170. /**
  171. * @dev Returns the element stored at position `index` in the set. O(1).
  172. * Note that there are no guarantees on the ordering of values inside the
  173. * array, and it may change when more values are added or removed.
  174. *
  175. * Requirements:
  176. *
  177. * - `index` must be strictly less than {length}.
  178. */
  179. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  180. (bytes32 key, bytes32 value) = _at(map._inner, index);
  181. return (uint256(key), address(uint160(uint256(value))));
  182. }
  183. /**
  184. * @dev Tries to returns the value associated with `key`. O(1).
  185. * Does not revert if `key` is not in the map.
  186. *
  187. * _Available since v3.4._
  188. */
  189. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  190. (bool success, bytes32 value) = _tryGet(map._inner, bytes32(key));
  191. return (success, address(uint160(uint256(value))));
  192. }
  193. /**
  194. * @dev Returns the value associated with `key`. O(1).
  195. *
  196. * Requirements:
  197. *
  198. * - `key` must be in the map.
  199. */
  200. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  201. return address(uint160(uint256(_get(map._inner, bytes32(key)))));
  202. }
  203. /**
  204. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  205. *
  206. * CAUTION: This function is deprecated because it requires allocating memory for the error
  207. * message unnecessarily. For custom revert reasons use {tryGet}.
  208. */
  209. function get(
  210. UintToAddressMap storage map,
  211. uint256 key,
  212. string memory errorMessage
  213. ) internal view returns (address) {
  214. return address(uint160(uint256(_get(map._inner, bytes32(key), errorMessage))));
  215. }
  216. }