EnumerableMap.sol 14 KB

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