EnumerableMap.sol 11 KB

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