EnumerableMap.sol 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (utils/structs/EnumerableMap.sol)
  3. // This file was procedurally generated from scripts/generate/templates/EnumerableMap.js.
  4. pragma solidity ^0.8.20;
  5. import {EnumerableSet} from "./EnumerableSet.sol";
  6. /**
  7. * @dev Library for managing an enumerable variant of Solidity's
  8. * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
  9. * type.
  10. *
  11. * Maps have the following properties:
  12. *
  13. * - Entries are added, removed, and checked for existence in constant time
  14. * (O(1)).
  15. * - Entries are enumerated in O(n). No guarantees are made on the ordering.
  16. * - Map can be cleared (all entries removed) in O(n).
  17. *
  18. * ```solidity
  19. * contract Example {
  20. * // Add the library methods
  21. * using EnumerableMap for EnumerableMap.UintToAddressMap;
  22. *
  23. * // Declare a set state variable
  24. * EnumerableMap.UintToAddressMap private myMap;
  25. * }
  26. * ```
  27. *
  28. * The following map types are supported:
  29. *
  30. * - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
  31. * - `address -> uint256` (`AddressToUintMap`) since v4.6.0
  32. * - `bytes32 -> bytes32` (`Bytes32ToBytes32Map`) since v4.6.0
  33. * - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
  34. * - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
  35. * - `uint256 -> bytes32` (`UintToBytes32Map`) since v5.1.0
  36. * - `address -> address` (`AddressToAddressMap`) since v5.1.0
  37. * - `address -> bytes32` (`AddressToBytes32Map`) since v5.1.0
  38. * - `bytes32 -> address` (`Bytes32ToAddressMap`) since v5.1.0
  39. *
  40. * [WARNING]
  41. * ====
  42. * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
  43. * unusable.
  44. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  45. *
  46. * In order to clean an EnumerableMap, you can either remove all elements one by one or create a fresh instance using an
  47. * array of EnumerableMap.
  48. * ====
  49. */
  50. library EnumerableMap {
  51. using EnumerableSet for EnumerableSet.Bytes32Set;
  52. // To implement this library for multiple types with as little code repetition as possible, we write it in
  53. // terms of a generic Map type with bytes32 keys and values. The Map implementation uses private functions,
  54. // and user-facing implementations such as `UintToAddressMap` are just wrappers around the underlying Map.
  55. // This means that we can only create new EnumerableMaps for types that fit in bytes32.
  56. /**
  57. * @dev Query for a nonexistent map key.
  58. */
  59. error EnumerableMapNonexistentKey(bytes32 key);
  60. struct Bytes32ToBytes32Map {
  61. // Storage of keys
  62. EnumerableSet.Bytes32Set _keys;
  63. mapping(bytes32 key => bytes32) _values;
  64. }
  65. /**
  66. * @dev Adds a key-value pair to a map, or updates the value for an existing
  67. * key. O(1).
  68. *
  69. * Returns true if the key was added to the map, that is if it was not
  70. * already present.
  71. */
  72. function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value) internal returns (bool) {
  73. map._values[key] = value;
  74. return map._keys.add(key);
  75. }
  76. /**
  77. * @dev Removes a key-value pair from a map. O(1).
  78. *
  79. * Returns true if the key was removed from the map, that is if it was present.
  80. */
  81. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
  82. delete map._values[key];
  83. return map._keys.remove(key);
  84. }
  85. /**
  86. * @dev Removes all the entries from a map. O(n).
  87. *
  88. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  89. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  90. */
  91. function clear(Bytes32ToBytes32Map storage map) internal {
  92. uint256 len = length(map);
  93. for (uint256 i = 0; i < len; ++i) {
  94. delete map._values[map._keys.at(i)];
  95. }
  96. map._keys.clear();
  97. }
  98. /**
  99. * @dev Returns true if the key is in the map. O(1).
  100. */
  101. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
  102. return map._keys.contains(key);
  103. }
  104. /**
  105. * @dev Returns the number of key-value pairs in the map. O(1).
  106. */
  107. function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
  108. return map._keys.length();
  109. }
  110. /**
  111. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  112. *
  113. * Note that there are no guarantees on the ordering of entries inside the
  114. * array, and it may change when more entries are added or removed.
  115. *
  116. * Requirements:
  117. *
  118. * - `index` must be strictly less than {length}.
  119. */
  120. function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32 key, bytes32 value) {
  121. bytes32 atKey = map._keys.at(index);
  122. return (atKey, map._values[atKey]);
  123. }
  124. /**
  125. * @dev Tries to returns the value associated with `key`. O(1).
  126. * Does not revert if `key` is not in the map.
  127. */
  128. function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool exists, bytes32 value) {
  129. bytes32 val = map._values[key];
  130. if (val == bytes32(0)) {
  131. return (contains(map, key), bytes32(0));
  132. } else {
  133. return (true, val);
  134. }
  135. }
  136. /**
  137. * @dev Returns the value associated with `key`. O(1).
  138. *
  139. * Requirements:
  140. *
  141. * - `key` must be in the map.
  142. */
  143. function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
  144. bytes32 value = map._values[key];
  145. if (value == 0 && !contains(map, key)) {
  146. revert EnumerableMapNonexistentKey(key);
  147. }
  148. return value;
  149. }
  150. /**
  151. * @dev Return the an array containing all the keys
  152. *
  153. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  154. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  155. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  156. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  157. */
  158. function keys(Bytes32ToBytes32Map storage map) internal view returns (bytes32[] memory) {
  159. return map._keys.values();
  160. }
  161. // UintToUintMap
  162. struct UintToUintMap {
  163. Bytes32ToBytes32Map _inner;
  164. }
  165. /**
  166. * @dev Adds a key-value pair to a map, or updates the value for an existing
  167. * key. O(1).
  168. *
  169. * Returns true if the key was added to the map, that is if it was not
  170. * already present.
  171. */
  172. function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
  173. return set(map._inner, bytes32(key), bytes32(value));
  174. }
  175. /**
  176. * @dev Removes a value from a map. O(1).
  177. *
  178. * Returns true if the key was removed from the map, that is if it was present.
  179. */
  180. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  181. return remove(map._inner, bytes32(key));
  182. }
  183. /**
  184. * @dev Removes all the entries from a map. O(n).
  185. *
  186. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  187. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  188. */
  189. function clear(UintToUintMap storage map) internal {
  190. clear(map._inner);
  191. }
  192. /**
  193. * @dev Returns true if the key is in the map. O(1).
  194. */
  195. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  196. return contains(map._inner, bytes32(key));
  197. }
  198. /**
  199. * @dev Returns the number of elements in the map. O(1).
  200. */
  201. function length(UintToUintMap storage map) internal view returns (uint256) {
  202. return length(map._inner);
  203. }
  204. /**
  205. * @dev Returns the element stored at position `index` in the map. O(1).
  206. * Note that there are no guarantees on the ordering of values inside the
  207. * array, and it may change when more values are added or removed.
  208. *
  209. * Requirements:
  210. *
  211. * - `index` must be strictly less than {length}.
  212. */
  213. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256 key, uint256 value) {
  214. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  215. return (uint256(atKey), uint256(val));
  216. }
  217. /**
  218. * @dev Tries to returns the value associated with `key`. O(1).
  219. * Does not revert if `key` is not in the map.
  220. */
  221. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool exists, uint256 value) {
  222. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  223. return (success, uint256(val));
  224. }
  225. /**
  226. * @dev Returns the value associated with `key`. O(1).
  227. *
  228. * Requirements:
  229. *
  230. * - `key` must be in the map.
  231. */
  232. function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  233. return uint256(get(map._inner, bytes32(key)));
  234. }
  235. /**
  236. * @dev Return the an array containing all the keys
  237. *
  238. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  239. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  240. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  241. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  242. */
  243. function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
  244. bytes32[] memory store = keys(map._inner);
  245. uint256[] memory result;
  246. assembly ("memory-safe") {
  247. result := store
  248. }
  249. return result;
  250. }
  251. // UintToAddressMap
  252. struct UintToAddressMap {
  253. Bytes32ToBytes32Map _inner;
  254. }
  255. /**
  256. * @dev Adds a key-value pair to a map, or updates the value for an existing
  257. * key. O(1).
  258. *
  259. * Returns true if the key was added to the map, that is if it was not
  260. * already present.
  261. */
  262. function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
  263. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  264. }
  265. /**
  266. * @dev Removes a value from a map. O(1).
  267. *
  268. * Returns true if the key was removed from the map, that is if it was present.
  269. */
  270. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  271. return remove(map._inner, bytes32(key));
  272. }
  273. /**
  274. * @dev Removes all the entries from a map. O(n).
  275. *
  276. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  277. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  278. */
  279. function clear(UintToAddressMap storage map) internal {
  280. clear(map._inner);
  281. }
  282. /**
  283. * @dev Returns true if the key is in the map. O(1).
  284. */
  285. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  286. return contains(map._inner, bytes32(key));
  287. }
  288. /**
  289. * @dev Returns the number of elements in the map. O(1).
  290. */
  291. function length(UintToAddressMap storage map) internal view returns (uint256) {
  292. return length(map._inner);
  293. }
  294. /**
  295. * @dev Returns the element stored at position `index` in the map. O(1).
  296. * Note that there are no guarantees on the ordering of values inside the
  297. * array, and it may change when more values are added or removed.
  298. *
  299. * Requirements:
  300. *
  301. * - `index` must be strictly less than {length}.
  302. */
  303. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256 key, address value) {
  304. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  305. return (uint256(atKey), address(uint160(uint256(val))));
  306. }
  307. /**
  308. * @dev Tries to returns the value associated with `key`. O(1).
  309. * Does not revert if `key` is not in the map.
  310. */
  311. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool exists, address value) {
  312. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  313. return (success, address(uint160(uint256(val))));
  314. }
  315. /**
  316. * @dev Returns the value associated with `key`. O(1).
  317. *
  318. * Requirements:
  319. *
  320. * - `key` must be in the map.
  321. */
  322. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  323. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  324. }
  325. /**
  326. * @dev Return the an array containing all the keys
  327. *
  328. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  329. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  330. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  331. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  332. */
  333. function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
  334. bytes32[] memory store = keys(map._inner);
  335. uint256[] memory result;
  336. assembly ("memory-safe") {
  337. result := store
  338. }
  339. return result;
  340. }
  341. // UintToBytes32Map
  342. struct UintToBytes32Map {
  343. Bytes32ToBytes32Map _inner;
  344. }
  345. /**
  346. * @dev Adds a key-value pair to a map, or updates the value for an existing
  347. * key. O(1).
  348. *
  349. * Returns true if the key was added to the map, that is if it was not
  350. * already present.
  351. */
  352. function set(UintToBytes32Map storage map, uint256 key, bytes32 value) internal returns (bool) {
  353. return set(map._inner, bytes32(key), value);
  354. }
  355. /**
  356. * @dev Removes a value from a map. O(1).
  357. *
  358. * Returns true if the key was removed from the map, that is if it was present.
  359. */
  360. function remove(UintToBytes32Map storage map, uint256 key) internal returns (bool) {
  361. return remove(map._inner, bytes32(key));
  362. }
  363. /**
  364. * @dev Removes all the entries from a map. O(n).
  365. *
  366. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  367. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  368. */
  369. function clear(UintToBytes32Map storage map) internal {
  370. clear(map._inner);
  371. }
  372. /**
  373. * @dev Returns true if the key is in the map. O(1).
  374. */
  375. function contains(UintToBytes32Map storage map, uint256 key) internal view returns (bool) {
  376. return contains(map._inner, bytes32(key));
  377. }
  378. /**
  379. * @dev Returns the number of elements in the map. O(1).
  380. */
  381. function length(UintToBytes32Map storage map) internal view returns (uint256) {
  382. return length(map._inner);
  383. }
  384. /**
  385. * @dev Returns the element stored at position `index` in the map. O(1).
  386. * Note that there are no guarantees on the ordering of values inside the
  387. * array, and it may change when more values are added or removed.
  388. *
  389. * Requirements:
  390. *
  391. * - `index` must be strictly less than {length}.
  392. */
  393. function at(UintToBytes32Map storage map, uint256 index) internal view returns (uint256 key, bytes32 value) {
  394. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  395. return (uint256(atKey), val);
  396. }
  397. /**
  398. * @dev Tries to returns the value associated with `key`. O(1).
  399. * Does not revert if `key` is not in the map.
  400. */
  401. function tryGet(UintToBytes32Map storage map, uint256 key) internal view returns (bool exists, bytes32 value) {
  402. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  403. return (success, val);
  404. }
  405. /**
  406. * @dev Returns the value associated with `key`. O(1).
  407. *
  408. * Requirements:
  409. *
  410. * - `key` must be in the map.
  411. */
  412. function get(UintToBytes32Map storage map, uint256 key) internal view returns (bytes32) {
  413. return get(map._inner, bytes32(key));
  414. }
  415. /**
  416. * @dev Return the an array containing all the keys
  417. *
  418. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  419. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  420. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  421. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  422. */
  423. function keys(UintToBytes32Map storage map) internal view returns (uint256[] memory) {
  424. bytes32[] memory store = keys(map._inner);
  425. uint256[] memory result;
  426. assembly ("memory-safe") {
  427. result := store
  428. }
  429. return result;
  430. }
  431. // AddressToUintMap
  432. struct AddressToUintMap {
  433. Bytes32ToBytes32Map _inner;
  434. }
  435. /**
  436. * @dev Adds a key-value pair to a map, or updates the value for an existing
  437. * key. O(1).
  438. *
  439. * Returns true if the key was added to the map, that is if it was not
  440. * already present.
  441. */
  442. function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
  443. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  444. }
  445. /**
  446. * @dev Removes a value from a map. O(1).
  447. *
  448. * Returns true if the key was removed from the map, that is if it was present.
  449. */
  450. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  451. return remove(map._inner, bytes32(uint256(uint160(key))));
  452. }
  453. /**
  454. * @dev Removes all the entries from a map. O(n).
  455. *
  456. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  457. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  458. */
  459. function clear(AddressToUintMap storage map) internal {
  460. clear(map._inner);
  461. }
  462. /**
  463. * @dev Returns true if the key is in the map. O(1).
  464. */
  465. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  466. return contains(map._inner, bytes32(uint256(uint160(key))));
  467. }
  468. /**
  469. * @dev Returns the number of elements in the map. O(1).
  470. */
  471. function length(AddressToUintMap storage map) internal view returns (uint256) {
  472. return length(map._inner);
  473. }
  474. /**
  475. * @dev Returns the element stored at position `index` in the map. O(1).
  476. * Note that there are no guarantees on the ordering of values inside the
  477. * array, and it may change when more values are added or removed.
  478. *
  479. * Requirements:
  480. *
  481. * - `index` must be strictly less than {length}.
  482. */
  483. function at(AddressToUintMap storage map, uint256 index) internal view returns (address key, uint256 value) {
  484. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  485. return (address(uint160(uint256(atKey))), uint256(val));
  486. }
  487. /**
  488. * @dev Tries to returns the value associated with `key`. O(1).
  489. * Does not revert if `key` is not in the map.
  490. */
  491. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool exists, uint256 value) {
  492. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  493. return (success, uint256(val));
  494. }
  495. /**
  496. * @dev Returns the value associated with `key`. O(1).
  497. *
  498. * Requirements:
  499. *
  500. * - `key` must be in the map.
  501. */
  502. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  503. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  504. }
  505. /**
  506. * @dev Return the an array containing all the keys
  507. *
  508. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  509. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  510. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  511. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  512. */
  513. function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
  514. bytes32[] memory store = keys(map._inner);
  515. address[] memory result;
  516. assembly ("memory-safe") {
  517. result := store
  518. }
  519. return result;
  520. }
  521. // AddressToAddressMap
  522. struct AddressToAddressMap {
  523. Bytes32ToBytes32Map _inner;
  524. }
  525. /**
  526. * @dev Adds a key-value pair to a map, or updates the value for an existing
  527. * key. O(1).
  528. *
  529. * Returns true if the key was added to the map, that is if it was not
  530. * already present.
  531. */
  532. function set(AddressToAddressMap storage map, address key, address value) internal returns (bool) {
  533. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(uint256(uint160(value))));
  534. }
  535. /**
  536. * @dev Removes a value from a map. O(1).
  537. *
  538. * Returns true if the key was removed from the map, that is if it was present.
  539. */
  540. function remove(AddressToAddressMap storage map, address key) internal returns (bool) {
  541. return remove(map._inner, bytes32(uint256(uint160(key))));
  542. }
  543. /**
  544. * @dev Removes all the entries from a map. O(n).
  545. *
  546. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  547. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  548. */
  549. function clear(AddressToAddressMap storage map) internal {
  550. clear(map._inner);
  551. }
  552. /**
  553. * @dev Returns true if the key is in the map. O(1).
  554. */
  555. function contains(AddressToAddressMap storage map, address key) internal view returns (bool) {
  556. return contains(map._inner, bytes32(uint256(uint160(key))));
  557. }
  558. /**
  559. * @dev Returns the number of elements in the map. O(1).
  560. */
  561. function length(AddressToAddressMap storage map) internal view returns (uint256) {
  562. return length(map._inner);
  563. }
  564. /**
  565. * @dev Returns the element stored at position `index` in the map. O(1).
  566. * Note that there are no guarantees on the ordering of values inside the
  567. * array, and it may change when more values are added or removed.
  568. *
  569. * Requirements:
  570. *
  571. * - `index` must be strictly less than {length}.
  572. */
  573. function at(AddressToAddressMap storage map, uint256 index) internal view returns (address key, address value) {
  574. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  575. return (address(uint160(uint256(atKey))), address(uint160(uint256(val))));
  576. }
  577. /**
  578. * @dev Tries to returns the value associated with `key`. O(1).
  579. * Does not revert if `key` is not in the map.
  580. */
  581. function tryGet(AddressToAddressMap storage map, address key) internal view returns (bool exists, address value) {
  582. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  583. return (success, address(uint160(uint256(val))));
  584. }
  585. /**
  586. * @dev Returns the value associated with `key`. O(1).
  587. *
  588. * Requirements:
  589. *
  590. * - `key` must be in the map.
  591. */
  592. function get(AddressToAddressMap storage map, address key) internal view returns (address) {
  593. return address(uint160(uint256(get(map._inner, bytes32(uint256(uint160(key)))))));
  594. }
  595. /**
  596. * @dev Return the an array containing all the keys
  597. *
  598. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  599. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  600. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  601. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  602. */
  603. function keys(AddressToAddressMap storage map) internal view returns (address[] memory) {
  604. bytes32[] memory store = keys(map._inner);
  605. address[] memory result;
  606. assembly ("memory-safe") {
  607. result := store
  608. }
  609. return result;
  610. }
  611. // AddressToBytes32Map
  612. struct AddressToBytes32Map {
  613. Bytes32ToBytes32Map _inner;
  614. }
  615. /**
  616. * @dev Adds a key-value pair to a map, or updates the value for an existing
  617. * key. O(1).
  618. *
  619. * Returns true if the key was added to the map, that is if it was not
  620. * already present.
  621. */
  622. function set(AddressToBytes32Map storage map, address key, bytes32 value) internal returns (bool) {
  623. return set(map._inner, bytes32(uint256(uint160(key))), value);
  624. }
  625. /**
  626. * @dev Removes a value from a map. O(1).
  627. *
  628. * Returns true if the key was removed from the map, that is if it was present.
  629. */
  630. function remove(AddressToBytes32Map storage map, address key) internal returns (bool) {
  631. return remove(map._inner, bytes32(uint256(uint160(key))));
  632. }
  633. /**
  634. * @dev Removes all the entries from a map. O(n).
  635. *
  636. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  637. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  638. */
  639. function clear(AddressToBytes32Map storage map) internal {
  640. clear(map._inner);
  641. }
  642. /**
  643. * @dev Returns true if the key is in the map. O(1).
  644. */
  645. function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) {
  646. return contains(map._inner, bytes32(uint256(uint160(key))));
  647. }
  648. /**
  649. * @dev Returns the number of elements in the map. O(1).
  650. */
  651. function length(AddressToBytes32Map storage map) internal view returns (uint256) {
  652. return length(map._inner);
  653. }
  654. /**
  655. * @dev Returns the element stored at position `index` in the map. O(1).
  656. * Note that there are no guarantees on the ordering of values inside the
  657. * array, and it may change when more values are added or removed.
  658. *
  659. * Requirements:
  660. *
  661. * - `index` must be strictly less than {length}.
  662. */
  663. function at(AddressToBytes32Map storage map, uint256 index) internal view returns (address key, bytes32 value) {
  664. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  665. return (address(uint160(uint256(atKey))), val);
  666. }
  667. /**
  668. * @dev Tries to returns the value associated with `key`. O(1).
  669. * Does not revert if `key` is not in the map.
  670. */
  671. function tryGet(AddressToBytes32Map storage map, address key) internal view returns (bool exists, bytes32 value) {
  672. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  673. return (success, val);
  674. }
  675. /**
  676. * @dev Returns the value associated with `key`. O(1).
  677. *
  678. * Requirements:
  679. *
  680. * - `key` must be in the map.
  681. */
  682. function get(AddressToBytes32Map storage map, address key) internal view returns (bytes32) {
  683. return get(map._inner, bytes32(uint256(uint160(key))));
  684. }
  685. /**
  686. * @dev Return the an array containing all the keys
  687. *
  688. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  689. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  690. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  691. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  692. */
  693. function keys(AddressToBytes32Map storage map) internal view returns (address[] memory) {
  694. bytes32[] memory store = keys(map._inner);
  695. address[] memory result;
  696. assembly ("memory-safe") {
  697. result := store
  698. }
  699. return result;
  700. }
  701. // Bytes32ToUintMap
  702. struct Bytes32ToUintMap {
  703. Bytes32ToBytes32Map _inner;
  704. }
  705. /**
  706. * @dev Adds a key-value pair to a map, or updates the value for an existing
  707. * key. O(1).
  708. *
  709. * Returns true if the key was added to the map, that is if it was not
  710. * already present.
  711. */
  712. function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
  713. return set(map._inner, key, bytes32(value));
  714. }
  715. /**
  716. * @dev Removes a value from a map. O(1).
  717. *
  718. * Returns true if the key was removed from the map, that is if it was present.
  719. */
  720. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  721. return remove(map._inner, key);
  722. }
  723. /**
  724. * @dev Removes all the entries from a map. O(n).
  725. *
  726. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  727. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  728. */
  729. function clear(Bytes32ToUintMap storage map) internal {
  730. clear(map._inner);
  731. }
  732. /**
  733. * @dev Returns true if the key is in the map. O(1).
  734. */
  735. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  736. return contains(map._inner, key);
  737. }
  738. /**
  739. * @dev Returns the number of elements in the map. O(1).
  740. */
  741. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  742. return length(map._inner);
  743. }
  744. /**
  745. * @dev Returns the element stored at position `index` in the map. O(1).
  746. * Note that there are no guarantees on the ordering of values inside the
  747. * array, and it may change when more values are added or removed.
  748. *
  749. * Requirements:
  750. *
  751. * - `index` must be strictly less than {length}.
  752. */
  753. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32 key, uint256 value) {
  754. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  755. return (atKey, uint256(val));
  756. }
  757. /**
  758. * @dev Tries to returns the value associated with `key`. O(1).
  759. * Does not revert if `key` is not in the map.
  760. */
  761. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool exists, uint256 value) {
  762. (bool success, bytes32 val) = tryGet(map._inner, key);
  763. return (success, uint256(val));
  764. }
  765. /**
  766. * @dev Returns the value associated with `key`. O(1).
  767. *
  768. * Requirements:
  769. *
  770. * - `key` must be in the map.
  771. */
  772. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  773. return uint256(get(map._inner, key));
  774. }
  775. /**
  776. * @dev Return the an array containing all the keys
  777. *
  778. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  779. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  780. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  781. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  782. */
  783. function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
  784. bytes32[] memory store = keys(map._inner);
  785. bytes32[] memory result;
  786. assembly ("memory-safe") {
  787. result := store
  788. }
  789. return result;
  790. }
  791. // Bytes32ToAddressMap
  792. struct Bytes32ToAddressMap {
  793. Bytes32ToBytes32Map _inner;
  794. }
  795. /**
  796. * @dev Adds a key-value pair to a map, or updates the value for an existing
  797. * key. O(1).
  798. *
  799. * Returns true if the key was added to the map, that is if it was not
  800. * already present.
  801. */
  802. function set(Bytes32ToAddressMap storage map, bytes32 key, address value) internal returns (bool) {
  803. return set(map._inner, key, bytes32(uint256(uint160(value))));
  804. }
  805. /**
  806. * @dev Removes a value from a map. O(1).
  807. *
  808. * Returns true if the key was removed from the map, that is if it was present.
  809. */
  810. function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) {
  811. return remove(map._inner, key);
  812. }
  813. /**
  814. * @dev Removes all the entries from a map. O(n).
  815. *
  816. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  817. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  818. */
  819. function clear(Bytes32ToAddressMap storage map) internal {
  820. clear(map._inner);
  821. }
  822. /**
  823. * @dev Returns true if the key is in the map. O(1).
  824. */
  825. function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) {
  826. return contains(map._inner, key);
  827. }
  828. /**
  829. * @dev Returns the number of elements in the map. O(1).
  830. */
  831. function length(Bytes32ToAddressMap storage map) internal view returns (uint256) {
  832. return length(map._inner);
  833. }
  834. /**
  835. * @dev Returns the element stored at position `index` in the map. O(1).
  836. * Note that there are no guarantees on the ordering of values inside the
  837. * array, and it may change when more values are added or removed.
  838. *
  839. * Requirements:
  840. *
  841. * - `index` must be strictly less than {length}.
  842. */
  843. function at(Bytes32ToAddressMap storage map, uint256 index) internal view returns (bytes32 key, address value) {
  844. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  845. return (atKey, address(uint160(uint256(val))));
  846. }
  847. /**
  848. * @dev Tries to returns the value associated with `key`. O(1).
  849. * Does not revert if `key` is not in the map.
  850. */
  851. function tryGet(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool exists, address value) {
  852. (bool success, bytes32 val) = tryGet(map._inner, key);
  853. return (success, address(uint160(uint256(val))));
  854. }
  855. /**
  856. * @dev Returns the value associated with `key`. O(1).
  857. *
  858. * Requirements:
  859. *
  860. * - `key` must be in the map.
  861. */
  862. function get(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (address) {
  863. return address(uint160(uint256(get(map._inner, key))));
  864. }
  865. /**
  866. * @dev Return the an array containing all the keys
  867. *
  868. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  869. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  870. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  871. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  872. */
  873. function keys(Bytes32ToAddressMap storage map) internal view returns (bytes32[] memory) {
  874. bytes32[] memory store = keys(map._inner);
  875. bytes32[] memory result;
  876. assembly ("memory-safe") {
  877. result := store
  878. }
  879. return result;
  880. }
  881. }