EnumerableMap.sol 11 KB

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