EnumerableMap.sol 39 KB

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