EnumerableMap.sol 10 KB

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