EnumerableMap.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.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. * - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
  33. *
  34. * [WARNING]
  35. * ====
  36. * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable.
  37. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  38. *
  39. * 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.
  40. * ====
  41. */
  42. library EnumerableMap {
  43. using EnumerableSet for EnumerableSet.Bytes32Set;
  44. // To implement this library for multiple types with as little code
  45. // repetition as possible, we write it in terms of a generic Map type with
  46. // bytes32 keys and values.
  47. // The Map implementation uses private functions, and user-facing
  48. // implementations (such as Uint256ToAddressMap) are just wrappers around
  49. // the underlying Map.
  50. // This means that we can only create new EnumerableMaps for types that fit
  51. // in bytes32.
  52. struct Bytes32ToBytes32Map {
  53. // Storage of keys
  54. EnumerableSet.Bytes32Set _keys;
  55. mapping(bytes32 => bytes32) _values;
  56. }
  57. /**
  58. * @dev Adds a key-value pair to a map, or updates the value for an existing
  59. * key. O(1).
  60. *
  61. * Returns true if the key was added to the map, that is if it was not
  62. * already present.
  63. */
  64. function set(
  65. Bytes32ToBytes32Map storage map,
  66. bytes32 key,
  67. bytes32 value
  68. ) internal returns (bool) {
  69. map._values[key] = value;
  70. return map._keys.add(key);
  71. }
  72. /**
  73. * @dev Removes a key-value pair from a map. O(1).
  74. *
  75. * Returns true if the key was removed from the map, that is if it was present.
  76. */
  77. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
  78. delete map._values[key];
  79. return map._keys.remove(key);
  80. }
  81. /**
  82. * @dev Returns true if the key is in the map. O(1).
  83. */
  84. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
  85. return map._keys.contains(key);
  86. }
  87. /**
  88. * @dev Returns the number of key-value pairs in the map. O(1).
  89. */
  90. function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
  91. return map._keys.length();
  92. }
  93. /**
  94. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  95. *
  96. * Note that there are no guarantees on the ordering of entries inside the
  97. * array, and it may change when more entries are added or removed.
  98. *
  99. * Requirements:
  100. *
  101. * - `index` must be strictly less than {length}.
  102. */
  103. function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32, bytes32) {
  104. bytes32 key = map._keys.at(index);
  105. return (key, map._values[key]);
  106. }
  107. /**
  108. * @dev Tries to returns the value associated with `key`. O(1).
  109. * Does not revert if `key` is not in the map.
  110. */
  111. function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
  112. bytes32 value = map._values[key];
  113. if (value == bytes32(0)) {
  114. return (contains(map, key), bytes32(0));
  115. } else {
  116. return (true, value);
  117. }
  118. }
  119. /**
  120. * @dev Returns the value associated with `key`. O(1).
  121. *
  122. * Requirements:
  123. *
  124. * - `key` must be in the map.
  125. */
  126. function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
  127. bytes32 value = map._values[key];
  128. require(value != 0 || contains(map, key), "EnumerableMap: nonexistent key");
  129. return value;
  130. }
  131. /**
  132. * @dev Same as {_get}, with a custom error message when `key` is not in the map.
  133. *
  134. * CAUTION: This function is deprecated because it requires allocating memory for the error
  135. * message unnecessarily. For custom revert reasons use {_tryGet}.
  136. */
  137. function get(
  138. Bytes32ToBytes32Map storage map,
  139. bytes32 key,
  140. string memory errorMessage
  141. ) internal view returns (bytes32) {
  142. bytes32 value = map._values[key];
  143. require(value != 0 || contains(map, key), errorMessage);
  144. return value;
  145. }
  146. // UintToUintMap
  147. struct UintToUintMap {
  148. Bytes32ToBytes32Map _inner;
  149. }
  150. /**
  151. * @dev Adds a key-value pair to a map, or updates the value for an existing
  152. * key. O(1).
  153. *
  154. * Returns true if the key was added to the map, that is if it was not
  155. * already present.
  156. */
  157. function set(
  158. UintToUintMap storage map,
  159. uint256 key,
  160. uint256 value
  161. ) internal returns (bool) {
  162. return set(map._inner, bytes32(key), bytes32(value));
  163. }
  164. /**
  165. * @dev Removes a value from a set. O(1).
  166. *
  167. * Returns true if the key was removed from the map, that is if it was present.
  168. */
  169. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  170. return remove(map._inner, bytes32(key));
  171. }
  172. /**
  173. * @dev Returns true if the key is in the map. O(1).
  174. */
  175. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  176. return contains(map._inner, bytes32(key));
  177. }
  178. /**
  179. * @dev Returns the number of elements in the map. O(1).
  180. */
  181. function length(UintToUintMap storage map) internal view returns (uint256) {
  182. return length(map._inner);
  183. }
  184. /**
  185. * @dev Returns the element stored at position `index` in the set. O(1).
  186. * Note that there are no guarantees on the ordering of values inside the
  187. * array, and it may change when more values are added or removed.
  188. *
  189. * Requirements:
  190. *
  191. * - `index` must be strictly less than {length}.
  192. */
  193. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
  194. (bytes32 key, bytes32 value) = at(map._inner, index);
  195. return (uint256(key), uint256(value));
  196. }
  197. /**
  198. * @dev Tries to returns the value associated with `key`. O(1).
  199. * Does not revert if `key` is not in the map.
  200. */
  201. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
  202. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  203. return (success, 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(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  213. return 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. UintToUintMap storage map,
  223. uint256 key,
  224. string memory errorMessage
  225. ) internal view returns (uint256) {
  226. return uint256(get(map._inner, bytes32(key), errorMessage));
  227. }
  228. // UintToAddressMap
  229. struct UintToAddressMap {
  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. UintToAddressMap storage map,
  241. uint256 key,
  242. address value
  243. ) internal returns (bool) {
  244. return set(map._inner, bytes32(key), bytes32(uint256(uint160(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(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  252. return remove(map._inner, bytes32(key));
  253. }
  254. /**
  255. * @dev Returns true if the key is in the map. O(1).
  256. */
  257. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  258. return contains(map._inner, bytes32(key));
  259. }
  260. /**
  261. * @dev Returns the number of elements in the map. O(1).
  262. */
  263. function length(UintToAddressMap 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(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  276. (bytes32 key, bytes32 value) = at(map._inner, index);
  277. return (uint256(key), address(uint160(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(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  286. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  287. return (success, address(uint160(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(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  297. return address(uint160(uint256(get(map._inner, bytes32(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. UintToAddressMap storage map,
  307. uint256 key,
  308. string memory errorMessage
  309. ) internal view returns (address) {
  310. return address(uint160(uint256(get(map._inner, bytes32(key), errorMessage))));
  311. }
  312. // AddressToUintMap
  313. struct AddressToUintMap {
  314. Bytes32ToBytes32Map _inner;
  315. }
  316. /**
  317. * @dev Adds a key-value pair to a map, or updates the value for an existing
  318. * key. O(1).
  319. *
  320. * Returns true if the key was added to the map, that is if it was not
  321. * already present.
  322. */
  323. function set(
  324. AddressToUintMap storage map,
  325. address key,
  326. uint256 value
  327. ) internal returns (bool) {
  328. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  329. }
  330. /**
  331. * @dev Removes a value from a set. O(1).
  332. *
  333. * Returns true if the key was removed from the map, that is if it was present.
  334. */
  335. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  336. return remove(map._inner, bytes32(uint256(uint160(key))));
  337. }
  338. /**
  339. * @dev Returns true if the key is in the map. O(1).
  340. */
  341. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  342. return contains(map._inner, bytes32(uint256(uint160(key))));
  343. }
  344. /**
  345. * @dev Returns the number of elements in the map. O(1).
  346. */
  347. function length(AddressToUintMap storage map) internal view returns (uint256) {
  348. return length(map._inner);
  349. }
  350. /**
  351. * @dev Returns the element stored at position `index` in the set. O(1).
  352. * Note that there are no guarantees on the ordering of values inside the
  353. * array, and it may change when more values are added or removed.
  354. *
  355. * Requirements:
  356. *
  357. * - `index` must be strictly less than {length}.
  358. */
  359. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  360. (bytes32 key, bytes32 value) = at(map._inner, index);
  361. return (address(uint160(uint256(key))), uint256(value));
  362. }
  363. /**
  364. * @dev Tries to returns the value associated with `key`. O(1).
  365. * Does not revert if `key` is not in the map.
  366. */
  367. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  368. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  369. return (success, uint256(value));
  370. }
  371. /**
  372. * @dev Returns the value associated with `key`. O(1).
  373. *
  374. * Requirements:
  375. *
  376. * - `key` must be in the map.
  377. */
  378. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  379. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  380. }
  381. /**
  382. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  383. *
  384. * CAUTION: This function is deprecated because it requires allocating memory for the error
  385. * message unnecessarily. For custom revert reasons use {tryGet}.
  386. */
  387. function get(
  388. AddressToUintMap storage map,
  389. address key,
  390. string memory errorMessage
  391. ) internal view returns (uint256) {
  392. return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
  393. }
  394. // Bytes32ToUintMap
  395. struct Bytes32ToUintMap {
  396. Bytes32ToBytes32Map _inner;
  397. }
  398. /**
  399. * @dev Adds a key-value pair to a map, or updates the value for an existing
  400. * key. O(1).
  401. *
  402. * Returns true if the key was added to the map, that is if it was not
  403. * already present.
  404. */
  405. function set(
  406. Bytes32ToUintMap storage map,
  407. bytes32 key,
  408. uint256 value
  409. ) internal returns (bool) {
  410. return set(map._inner, key, bytes32(value));
  411. }
  412. /**
  413. * @dev Removes a value from a set. O(1).
  414. *
  415. * Returns true if the key was removed from the map, that is if it was present.
  416. */
  417. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  418. return remove(map._inner, key);
  419. }
  420. /**
  421. * @dev Returns true if the key is in the map. O(1).
  422. */
  423. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  424. return contains(map._inner, key);
  425. }
  426. /**
  427. * @dev Returns the number of elements in the map. O(1).
  428. */
  429. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  430. return length(map._inner);
  431. }
  432. /**
  433. * @dev Returns the element stored at position `index` in the set. O(1).
  434. * Note that there are no guarantees on the ordering of values inside the
  435. * array, and it may change when more values are added or removed.
  436. *
  437. * Requirements:
  438. *
  439. * - `index` must be strictly less than {length}.
  440. */
  441. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  442. (bytes32 key, bytes32 value) = at(map._inner, index);
  443. return (key, uint256(value));
  444. }
  445. /**
  446. * @dev Tries to returns the value associated with `key`. O(1).
  447. * Does not revert if `key` is not in the map.
  448. */
  449. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  450. (bool success, bytes32 value) = tryGet(map._inner, key);
  451. return (success, uint256(value));
  452. }
  453. /**
  454. * @dev Returns the value associated with `key`. O(1).
  455. *
  456. * Requirements:
  457. *
  458. * - `key` must be in the map.
  459. */
  460. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  461. return uint256(get(map._inner, key));
  462. }
  463. /**
  464. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  465. *
  466. * CAUTION: This function is deprecated because it requires allocating memory for the error
  467. * message unnecessarily. For custom revert reasons use {tryGet}.
  468. */
  469. function get(
  470. Bytes32ToUintMap storage map,
  471. bytes32 key,
  472. string memory errorMessage
  473. ) internal view returns (uint256) {
  474. return uint256(get(map._inner, key, errorMessage));
  475. }
  476. }