EnumerableMap.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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` (`Bytes32ToBytes32Map`) 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. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  284. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  285. return (success, address(uint160(uint256(value))));
  286. }
  287. /**
  288. * @dev Returns the value associated with `key`. O(1).
  289. *
  290. * Requirements:
  291. *
  292. * - `key` must be in the map.
  293. */
  294. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  295. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  296. }
  297. /**
  298. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  299. *
  300. * CAUTION: This function is deprecated because it requires allocating memory for the error
  301. * message unnecessarily. For custom revert reasons use {tryGet}.
  302. */
  303. function get(
  304. UintToAddressMap storage map,
  305. uint256 key,
  306. string memory errorMessage
  307. ) internal view returns (address) {
  308. return address(uint160(uint256(get(map._inner, bytes32(key), errorMessage))));
  309. }
  310. // AddressToUintMap
  311. struct AddressToUintMap {
  312. Bytes32ToBytes32Map _inner;
  313. }
  314. /**
  315. * @dev Adds a key-value pair to a map, or updates the value for an existing
  316. * key. O(1).
  317. *
  318. * Returns true if the key was added to the map, that is if it was not
  319. * already present.
  320. */
  321. function set(
  322. AddressToUintMap storage map,
  323. address key,
  324. uint256 value
  325. ) internal returns (bool) {
  326. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  327. }
  328. /**
  329. * @dev Removes a value from a set. O(1).
  330. *
  331. * Returns true if the key was removed from the map, that is if it was present.
  332. */
  333. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  334. return remove(map._inner, bytes32(uint256(uint160(key))));
  335. }
  336. /**
  337. * @dev Returns true if the key is in the map. O(1).
  338. */
  339. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  340. return contains(map._inner, bytes32(uint256(uint160(key))));
  341. }
  342. /**
  343. * @dev Returns the number of elements in the map. O(1).
  344. */
  345. function length(AddressToUintMap storage map) internal view returns (uint256) {
  346. return length(map._inner);
  347. }
  348. /**
  349. * @dev Returns the element stored at position `index` in the set. O(1).
  350. * Note that there are no guarantees on the ordering of values inside the
  351. * array, and it may change when more values are added or removed.
  352. *
  353. * Requirements:
  354. *
  355. * - `index` must be strictly less than {length}.
  356. */
  357. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  358. (bytes32 key, bytes32 value) = at(map._inner, index);
  359. return (address(uint160(uint256(key))), uint256(value));
  360. }
  361. /**
  362. * @dev Tries to returns the value associated with `key`. O(1).
  363. * Does not revert if `key` is not in the map.
  364. */
  365. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  366. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  367. return (success, uint256(value));
  368. }
  369. /**
  370. * @dev Returns the value associated with `key`. O(1).
  371. *
  372. * Requirements:
  373. *
  374. * - `key` must be in the map.
  375. */
  376. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  377. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  378. }
  379. /**
  380. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  381. *
  382. * CAUTION: This function is deprecated because it requires allocating memory for the error
  383. * message unnecessarily. For custom revert reasons use {tryGet}.
  384. */
  385. function get(
  386. AddressToUintMap storage map,
  387. address key,
  388. string memory errorMessage
  389. ) internal view returns (uint256) {
  390. return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
  391. }
  392. // Bytes32ToUintMap
  393. struct Bytes32ToUintMap {
  394. Bytes32ToBytes32Map _inner;
  395. }
  396. /**
  397. * @dev Adds a key-value pair to a map, or updates the value for an existing
  398. * key. O(1).
  399. *
  400. * Returns true if the key was added to the map, that is if it was not
  401. * already present.
  402. */
  403. function set(
  404. Bytes32ToUintMap storage map,
  405. bytes32 key,
  406. uint256 value
  407. ) internal returns (bool) {
  408. return set(map._inner, key, bytes32(value));
  409. }
  410. /**
  411. * @dev Removes a value from a set. O(1).
  412. *
  413. * Returns true if the key was removed from the map, that is if it was present.
  414. */
  415. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  416. return remove(map._inner, key);
  417. }
  418. /**
  419. * @dev Returns true if the key is in the map. O(1).
  420. */
  421. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  422. return contains(map._inner, key);
  423. }
  424. /**
  425. * @dev Returns the number of elements in the map. O(1).
  426. */
  427. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  428. return length(map._inner);
  429. }
  430. /**
  431. * @dev Returns the element stored at position `index` in the set. O(1).
  432. * Note that there are no guarantees on the ordering of values inside the
  433. * array, and it may change when more values are added or removed.
  434. *
  435. * Requirements:
  436. *
  437. * - `index` must be strictly less than {length}.
  438. */
  439. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  440. (bytes32 key, bytes32 value) = at(map._inner, index);
  441. return (key, uint256(value));
  442. }
  443. /**
  444. * @dev Tries to returns the value associated with `key`. O(1).
  445. * Does not revert if `key` is not in the map.
  446. */
  447. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  448. (bool success, bytes32 value) = tryGet(map._inner, key);
  449. return (success, uint256(value));
  450. }
  451. /**
  452. * @dev Returns the value associated with `key`. O(1).
  453. *
  454. * Requirements:
  455. *
  456. * - `key` must be in the map.
  457. */
  458. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  459. return uint256(get(map._inner, key));
  460. }
  461. /**
  462. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  463. *
  464. * CAUTION: This function is deprecated because it requires allocating memory for the error
  465. * message unnecessarily. For custom revert reasons use {tryGet}.
  466. */
  467. function get(
  468. Bytes32ToUintMap storage map,
  469. bytes32 key,
  470. string memory errorMessage
  471. ) internal view returns (uint256) {
  472. return uint256(get(map._inner, key, errorMessage));
  473. }
  474. }