EnumerableMap.sol 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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 return 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 Returns 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. /**
  163. * @dev Returns an array containing a slice of the keys
  164. *
  165. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  166. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  167. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  168. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  169. */
  170. function keys(
  171. Bytes32ToBytes32Map storage map,
  172. uint256 start,
  173. uint256 end
  174. ) internal view returns (bytes32[] memory) {
  175. return map._keys.values(start, end);
  176. }
  177. // UintToUintMap
  178. struct UintToUintMap {
  179. Bytes32ToBytes32Map _inner;
  180. }
  181. /**
  182. * @dev Adds a key-value pair to a map, or updates the value for an existing
  183. * key. O(1).
  184. *
  185. * Returns true if the key was added to the map, that is if it was not
  186. * already present.
  187. */
  188. function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
  189. return set(map._inner, bytes32(key), bytes32(value));
  190. }
  191. /**
  192. * @dev Removes a value from a map. O(1).
  193. *
  194. * Returns true if the key was removed from the map, that is if it was present.
  195. */
  196. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  197. return remove(map._inner, bytes32(key));
  198. }
  199. /**
  200. * @dev Removes all the entries from a map. O(n).
  201. *
  202. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  203. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  204. */
  205. function clear(UintToUintMap storage map) internal {
  206. clear(map._inner);
  207. }
  208. /**
  209. * @dev Returns true if the key is in the map. O(1).
  210. */
  211. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  212. return contains(map._inner, bytes32(key));
  213. }
  214. /**
  215. * @dev Returns the number of elements in the map. O(1).
  216. */
  217. function length(UintToUintMap storage map) internal view returns (uint256) {
  218. return length(map._inner);
  219. }
  220. /**
  221. * @dev Returns the element stored at position `index` in the map. O(1).
  222. * Note that there are no guarantees on the ordering of values inside the
  223. * array, and it may change when more values are added or removed.
  224. *
  225. * Requirements:
  226. *
  227. * - `index` must be strictly less than {length}.
  228. */
  229. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256 key, uint256 value) {
  230. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  231. return (uint256(atKey), uint256(val));
  232. }
  233. /**
  234. * @dev Tries to return the value associated with `key`. O(1).
  235. * Does not revert if `key` is not in the map.
  236. */
  237. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool exists, uint256 value) {
  238. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  239. return (success, uint256(val));
  240. }
  241. /**
  242. * @dev Returns the value associated with `key`. O(1).
  243. *
  244. * Requirements:
  245. *
  246. * - `key` must be in the map.
  247. */
  248. function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  249. return uint256(get(map._inner, bytes32(key)));
  250. }
  251. /**
  252. * @dev Returns an array containing all the keys
  253. *
  254. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  255. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  256. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  257. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  258. */
  259. function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
  260. bytes32[] memory store = keys(map._inner);
  261. uint256[] memory result;
  262. assembly ("memory-safe") {
  263. result := store
  264. }
  265. return result;
  266. }
  267. /**
  268. * @dev Returns an array containing a slice of the keys
  269. *
  270. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  271. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  272. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  273. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  274. */
  275. function keys(UintToUintMap storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
  276. bytes32[] memory store = keys(map._inner, start, end);
  277. uint256[] memory result;
  278. assembly ("memory-safe") {
  279. result := store
  280. }
  281. return result;
  282. }
  283. // UintToAddressMap
  284. struct UintToAddressMap {
  285. Bytes32ToBytes32Map _inner;
  286. }
  287. /**
  288. * @dev Adds a key-value pair to a map, or updates the value for an existing
  289. * key. O(1).
  290. *
  291. * Returns true if the key was added to the map, that is if it was not
  292. * already present.
  293. */
  294. function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
  295. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  296. }
  297. /**
  298. * @dev Removes a value from a map. O(1).
  299. *
  300. * Returns true if the key was removed from the map, that is if it was present.
  301. */
  302. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  303. return remove(map._inner, bytes32(key));
  304. }
  305. /**
  306. * @dev Removes all the entries from a map. O(n).
  307. *
  308. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  309. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  310. */
  311. function clear(UintToAddressMap storage map) internal {
  312. clear(map._inner);
  313. }
  314. /**
  315. * @dev Returns true if the key is in the map. O(1).
  316. */
  317. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  318. return contains(map._inner, bytes32(key));
  319. }
  320. /**
  321. * @dev Returns the number of elements in the map. O(1).
  322. */
  323. function length(UintToAddressMap storage map) internal view returns (uint256) {
  324. return length(map._inner);
  325. }
  326. /**
  327. * @dev Returns the element stored at position `index` in the map. O(1).
  328. * Note that there are no guarantees on the ordering of values inside the
  329. * array, and it may change when more values are added or removed.
  330. *
  331. * Requirements:
  332. *
  333. * - `index` must be strictly less than {length}.
  334. */
  335. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256 key, address value) {
  336. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  337. return (uint256(atKey), address(uint160(uint256(val))));
  338. }
  339. /**
  340. * @dev Tries to return the value associated with `key`. O(1).
  341. * Does not revert if `key` is not in the map.
  342. */
  343. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool exists, address value) {
  344. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  345. return (success, address(uint160(uint256(val))));
  346. }
  347. /**
  348. * @dev Returns the value associated with `key`. O(1).
  349. *
  350. * Requirements:
  351. *
  352. * - `key` must be in the map.
  353. */
  354. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  355. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  356. }
  357. /**
  358. * @dev Returns an array containing all the keys
  359. *
  360. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  361. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  362. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  363. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  364. */
  365. function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
  366. bytes32[] memory store = keys(map._inner);
  367. uint256[] memory result;
  368. assembly ("memory-safe") {
  369. result := store
  370. }
  371. return result;
  372. }
  373. /**
  374. * @dev Returns an array containing a slice of the keys
  375. *
  376. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  377. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  378. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  379. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  380. */
  381. function keys(UintToAddressMap storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
  382. bytes32[] memory store = keys(map._inner, start, end);
  383. uint256[] memory result;
  384. assembly ("memory-safe") {
  385. result := store
  386. }
  387. return result;
  388. }
  389. // UintToBytes32Map
  390. struct UintToBytes32Map {
  391. Bytes32ToBytes32Map _inner;
  392. }
  393. /**
  394. * @dev Adds a key-value pair to a map, or updates the value for an existing
  395. * key. O(1).
  396. *
  397. * Returns true if the key was added to the map, that is if it was not
  398. * already present.
  399. */
  400. function set(UintToBytes32Map storage map, uint256 key, bytes32 value) internal returns (bool) {
  401. return set(map._inner, bytes32(key), value);
  402. }
  403. /**
  404. * @dev Removes a value from a map. O(1).
  405. *
  406. * Returns true if the key was removed from the map, that is if it was present.
  407. */
  408. function remove(UintToBytes32Map storage map, uint256 key) internal returns (bool) {
  409. return remove(map._inner, bytes32(key));
  410. }
  411. /**
  412. * @dev Removes all the entries from a map. O(n).
  413. *
  414. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  415. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  416. */
  417. function clear(UintToBytes32Map storage map) internal {
  418. clear(map._inner);
  419. }
  420. /**
  421. * @dev Returns true if the key is in the map. O(1).
  422. */
  423. function contains(UintToBytes32Map storage map, uint256 key) internal view returns (bool) {
  424. return contains(map._inner, bytes32(key));
  425. }
  426. /**
  427. * @dev Returns the number of elements in the map. O(1).
  428. */
  429. function length(UintToBytes32Map storage map) internal view returns (uint256) {
  430. return length(map._inner);
  431. }
  432. /**
  433. * @dev Returns the element stored at position `index` in the map. O(1).
  434. * Note that there are no guarantees on the ordering of values inside the
  435. * array, and it may change when more values are added or removed.
  436. *
  437. * Requirements:
  438. *
  439. * - `index` must be strictly less than {length}.
  440. */
  441. function at(UintToBytes32Map storage map, uint256 index) internal view returns (uint256 key, bytes32 value) {
  442. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  443. return (uint256(atKey), val);
  444. }
  445. /**
  446. * @dev Tries to return the value associated with `key`. O(1).
  447. * Does not revert if `key` is not in the map.
  448. */
  449. function tryGet(UintToBytes32Map storage map, uint256 key) internal view returns (bool exists, bytes32 value) {
  450. (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
  451. return (success, val);
  452. }
  453. /**
  454. * @dev Returns the value associated with `key`. O(1).
  455. *
  456. * Requirements:
  457. *
  458. * - `key` must be in the map.
  459. */
  460. function get(UintToBytes32Map storage map, uint256 key) internal view returns (bytes32) {
  461. return get(map._inner, bytes32(key));
  462. }
  463. /**
  464. * @dev Returns an array containing all the keys
  465. *
  466. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  467. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  468. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  469. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  470. */
  471. function keys(UintToBytes32Map storage map) internal view returns (uint256[] memory) {
  472. bytes32[] memory store = keys(map._inner);
  473. uint256[] memory result;
  474. assembly ("memory-safe") {
  475. result := store
  476. }
  477. return result;
  478. }
  479. /**
  480. * @dev Returns an array containing a slice of the keys
  481. *
  482. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  483. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  484. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  485. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  486. */
  487. function keys(UintToBytes32Map storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
  488. bytes32[] memory store = keys(map._inner, start, end);
  489. uint256[] memory result;
  490. assembly ("memory-safe") {
  491. result := store
  492. }
  493. return result;
  494. }
  495. // AddressToUintMap
  496. struct AddressToUintMap {
  497. Bytes32ToBytes32Map _inner;
  498. }
  499. /**
  500. * @dev Adds a key-value pair to a map, or updates the value for an existing
  501. * key. O(1).
  502. *
  503. * Returns true if the key was added to the map, that is if it was not
  504. * already present.
  505. */
  506. function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
  507. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  508. }
  509. /**
  510. * @dev Removes a value from a map. O(1).
  511. *
  512. * Returns true if the key was removed from the map, that is if it was present.
  513. */
  514. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  515. return remove(map._inner, bytes32(uint256(uint160(key))));
  516. }
  517. /**
  518. * @dev Removes all the entries from a map. O(n).
  519. *
  520. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  521. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  522. */
  523. function clear(AddressToUintMap storage map) internal {
  524. clear(map._inner);
  525. }
  526. /**
  527. * @dev Returns true if the key is in the map. O(1).
  528. */
  529. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  530. return contains(map._inner, bytes32(uint256(uint160(key))));
  531. }
  532. /**
  533. * @dev Returns the number of elements in the map. O(1).
  534. */
  535. function length(AddressToUintMap storage map) internal view returns (uint256) {
  536. return length(map._inner);
  537. }
  538. /**
  539. * @dev Returns the element stored at position `index` in the map. O(1).
  540. * Note that there are no guarantees on the ordering of values inside the
  541. * array, and it may change when more values are added or removed.
  542. *
  543. * Requirements:
  544. *
  545. * - `index` must be strictly less than {length}.
  546. */
  547. function at(AddressToUintMap storage map, uint256 index) internal view returns (address key, uint256 value) {
  548. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  549. return (address(uint160(uint256(atKey))), uint256(val));
  550. }
  551. /**
  552. * @dev Tries to return the value associated with `key`. O(1).
  553. * Does not revert if `key` is not in the map.
  554. */
  555. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool exists, uint256 value) {
  556. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  557. return (success, uint256(val));
  558. }
  559. /**
  560. * @dev Returns the value associated with `key`. O(1).
  561. *
  562. * Requirements:
  563. *
  564. * - `key` must be in the map.
  565. */
  566. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  567. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  568. }
  569. /**
  570. * @dev Returns an array containing all the keys
  571. *
  572. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  573. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  574. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  575. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  576. */
  577. function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
  578. bytes32[] memory store = keys(map._inner);
  579. address[] memory result;
  580. assembly ("memory-safe") {
  581. result := store
  582. }
  583. return result;
  584. }
  585. /**
  586. * @dev Returns an array containing a slice of the keys
  587. *
  588. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  589. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  590. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  591. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  592. */
  593. function keys(AddressToUintMap storage map, uint256 start, uint256 end) internal view returns (address[] memory) {
  594. bytes32[] memory store = keys(map._inner, start, end);
  595. address[] memory result;
  596. assembly ("memory-safe") {
  597. result := store
  598. }
  599. return result;
  600. }
  601. // AddressToAddressMap
  602. struct AddressToAddressMap {
  603. Bytes32ToBytes32Map _inner;
  604. }
  605. /**
  606. * @dev Adds a key-value pair to a map, or updates the value for an existing
  607. * key. O(1).
  608. *
  609. * Returns true if the key was added to the map, that is if it was not
  610. * already present.
  611. */
  612. function set(AddressToAddressMap storage map, address key, address value) internal returns (bool) {
  613. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(uint256(uint160(value))));
  614. }
  615. /**
  616. * @dev Removes a value from a map. O(1).
  617. *
  618. * Returns true if the key was removed from the map, that is if it was present.
  619. */
  620. function remove(AddressToAddressMap storage map, address key) internal returns (bool) {
  621. return remove(map._inner, bytes32(uint256(uint160(key))));
  622. }
  623. /**
  624. * @dev Removes all the entries from a map. O(n).
  625. *
  626. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  627. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  628. */
  629. function clear(AddressToAddressMap storage map) internal {
  630. clear(map._inner);
  631. }
  632. /**
  633. * @dev Returns true if the key is in the map. O(1).
  634. */
  635. function contains(AddressToAddressMap storage map, address key) internal view returns (bool) {
  636. return contains(map._inner, bytes32(uint256(uint160(key))));
  637. }
  638. /**
  639. * @dev Returns the number of elements in the map. O(1).
  640. */
  641. function length(AddressToAddressMap storage map) internal view returns (uint256) {
  642. return length(map._inner);
  643. }
  644. /**
  645. * @dev Returns the element stored at position `index` in the map. O(1).
  646. * Note that there are no guarantees on the ordering of values inside the
  647. * array, and it may change when more values are added or removed.
  648. *
  649. * Requirements:
  650. *
  651. * - `index` must be strictly less than {length}.
  652. */
  653. function at(AddressToAddressMap storage map, uint256 index) internal view returns (address key, address value) {
  654. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  655. return (address(uint160(uint256(atKey))), address(uint160(uint256(val))));
  656. }
  657. /**
  658. * @dev Tries to return the value associated with `key`. O(1).
  659. * Does not revert if `key` is not in the map.
  660. */
  661. function tryGet(AddressToAddressMap storage map, address key) internal view returns (bool exists, address value) {
  662. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  663. return (success, address(uint160(uint256(val))));
  664. }
  665. /**
  666. * @dev Returns the value associated with `key`. O(1).
  667. *
  668. * Requirements:
  669. *
  670. * - `key` must be in the map.
  671. */
  672. function get(AddressToAddressMap storage map, address key) internal view returns (address) {
  673. return address(uint160(uint256(get(map._inner, bytes32(uint256(uint160(key)))))));
  674. }
  675. /**
  676. * @dev Returns an array containing all the keys
  677. *
  678. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  679. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  680. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  681. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  682. */
  683. function keys(AddressToAddressMap storage map) internal view returns (address[] memory) {
  684. bytes32[] memory store = keys(map._inner);
  685. address[] memory result;
  686. assembly ("memory-safe") {
  687. result := store
  688. }
  689. return result;
  690. }
  691. /**
  692. * @dev Returns an array containing a slice of the keys
  693. *
  694. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  695. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  696. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  697. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  698. */
  699. function keys(
  700. AddressToAddressMap storage map,
  701. uint256 start,
  702. uint256 end
  703. ) internal view returns (address[] memory) {
  704. bytes32[] memory store = keys(map._inner, start, end);
  705. address[] memory result;
  706. assembly ("memory-safe") {
  707. result := store
  708. }
  709. return result;
  710. }
  711. // AddressToBytes32Map
  712. struct AddressToBytes32Map {
  713. Bytes32ToBytes32Map _inner;
  714. }
  715. /**
  716. * @dev Adds a key-value pair to a map, or updates the value for an existing
  717. * key. O(1).
  718. *
  719. * Returns true if the key was added to the map, that is if it was not
  720. * already present.
  721. */
  722. function set(AddressToBytes32Map storage map, address key, bytes32 value) internal returns (bool) {
  723. return set(map._inner, bytes32(uint256(uint160(key))), value);
  724. }
  725. /**
  726. * @dev Removes a value from a map. O(1).
  727. *
  728. * Returns true if the key was removed from the map, that is if it was present.
  729. */
  730. function remove(AddressToBytes32Map storage map, address key) internal returns (bool) {
  731. return remove(map._inner, bytes32(uint256(uint160(key))));
  732. }
  733. /**
  734. * @dev Removes all the entries from a map. O(n).
  735. *
  736. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  737. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  738. */
  739. function clear(AddressToBytes32Map storage map) internal {
  740. clear(map._inner);
  741. }
  742. /**
  743. * @dev Returns true if the key is in the map. O(1).
  744. */
  745. function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) {
  746. return contains(map._inner, bytes32(uint256(uint160(key))));
  747. }
  748. /**
  749. * @dev Returns the number of elements in the map. O(1).
  750. */
  751. function length(AddressToBytes32Map storage map) internal view returns (uint256) {
  752. return length(map._inner);
  753. }
  754. /**
  755. * @dev Returns the element stored at position `index` in the map. O(1).
  756. * Note that there are no guarantees on the ordering of values inside the
  757. * array, and it may change when more values are added or removed.
  758. *
  759. * Requirements:
  760. *
  761. * - `index` must be strictly less than {length}.
  762. */
  763. function at(AddressToBytes32Map storage map, uint256 index) internal view returns (address key, bytes32 value) {
  764. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  765. return (address(uint160(uint256(atKey))), val);
  766. }
  767. /**
  768. * @dev Tries to return the value associated with `key`. O(1).
  769. * Does not revert if `key` is not in the map.
  770. */
  771. function tryGet(AddressToBytes32Map storage map, address key) internal view returns (bool exists, bytes32 value) {
  772. (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  773. return (success, val);
  774. }
  775. /**
  776. * @dev Returns the value associated with `key`. O(1).
  777. *
  778. * Requirements:
  779. *
  780. * - `key` must be in the map.
  781. */
  782. function get(AddressToBytes32Map storage map, address key) internal view returns (bytes32) {
  783. return get(map._inner, bytes32(uint256(uint160(key))));
  784. }
  785. /**
  786. * @dev Returns an array containing all the keys
  787. *
  788. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  789. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  790. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  791. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  792. */
  793. function keys(AddressToBytes32Map storage map) internal view returns (address[] memory) {
  794. bytes32[] memory store = keys(map._inner);
  795. address[] memory result;
  796. assembly ("memory-safe") {
  797. result := store
  798. }
  799. return result;
  800. }
  801. /**
  802. * @dev Returns an array containing a slice of the keys
  803. *
  804. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  805. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  806. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  807. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  808. */
  809. function keys(
  810. AddressToBytes32Map storage map,
  811. uint256 start,
  812. uint256 end
  813. ) internal view returns (address[] memory) {
  814. bytes32[] memory store = keys(map._inner, start, end);
  815. address[] memory result;
  816. assembly ("memory-safe") {
  817. result := store
  818. }
  819. return result;
  820. }
  821. // Bytes32ToUintMap
  822. struct Bytes32ToUintMap {
  823. Bytes32ToBytes32Map _inner;
  824. }
  825. /**
  826. * @dev Adds a key-value pair to a map, or updates the value for an existing
  827. * key. O(1).
  828. *
  829. * Returns true if the key was added to the map, that is if it was not
  830. * already present.
  831. */
  832. function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
  833. return set(map._inner, key, bytes32(value));
  834. }
  835. /**
  836. * @dev Removes a value from a map. O(1).
  837. *
  838. * Returns true if the key was removed from the map, that is if it was present.
  839. */
  840. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  841. return remove(map._inner, key);
  842. }
  843. /**
  844. * @dev Removes all the entries from a map. O(n).
  845. *
  846. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  847. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  848. */
  849. function clear(Bytes32ToUintMap storage map) internal {
  850. clear(map._inner);
  851. }
  852. /**
  853. * @dev Returns true if the key is in the map. O(1).
  854. */
  855. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  856. return contains(map._inner, key);
  857. }
  858. /**
  859. * @dev Returns the number of elements in the map. O(1).
  860. */
  861. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  862. return length(map._inner);
  863. }
  864. /**
  865. * @dev Returns the element stored at position `index` in the map. O(1).
  866. * Note that there are no guarantees on the ordering of values inside the
  867. * array, and it may change when more values are added or removed.
  868. *
  869. * Requirements:
  870. *
  871. * - `index` must be strictly less than {length}.
  872. */
  873. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32 key, uint256 value) {
  874. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  875. return (atKey, uint256(val));
  876. }
  877. /**
  878. * @dev Tries to return the value associated with `key`. O(1).
  879. * Does not revert if `key` is not in the map.
  880. */
  881. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool exists, uint256 value) {
  882. (bool success, bytes32 val) = tryGet(map._inner, key);
  883. return (success, uint256(val));
  884. }
  885. /**
  886. * @dev Returns the value associated with `key`. O(1).
  887. *
  888. * Requirements:
  889. *
  890. * - `key` must be in the map.
  891. */
  892. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  893. return uint256(get(map._inner, key));
  894. }
  895. /**
  896. * @dev Returns an array containing all the keys
  897. *
  898. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  899. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  900. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  901. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  902. */
  903. function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
  904. bytes32[] memory store = keys(map._inner);
  905. bytes32[] memory result;
  906. assembly ("memory-safe") {
  907. result := store
  908. }
  909. return result;
  910. }
  911. /**
  912. * @dev Returns an array containing a slice of the keys
  913. *
  914. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  915. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  916. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  917. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  918. */
  919. function keys(Bytes32ToUintMap storage map, uint256 start, uint256 end) internal view returns (bytes32[] memory) {
  920. bytes32[] memory store = keys(map._inner, start, end);
  921. bytes32[] memory result;
  922. assembly ("memory-safe") {
  923. result := store
  924. }
  925. return result;
  926. }
  927. // Bytes32ToAddressMap
  928. struct Bytes32ToAddressMap {
  929. Bytes32ToBytes32Map _inner;
  930. }
  931. /**
  932. * @dev Adds a key-value pair to a map, or updates the value for an existing
  933. * key. O(1).
  934. *
  935. * Returns true if the key was added to the map, that is if it was not
  936. * already present.
  937. */
  938. function set(Bytes32ToAddressMap storage map, bytes32 key, address value) internal returns (bool) {
  939. return set(map._inner, key, bytes32(uint256(uint160(value))));
  940. }
  941. /**
  942. * @dev Removes a value from a map. O(1).
  943. *
  944. * Returns true if the key was removed from the map, that is if it was present.
  945. */
  946. function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) {
  947. return remove(map._inner, key);
  948. }
  949. /**
  950. * @dev Removes all the entries from a map. O(n).
  951. *
  952. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  953. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  954. */
  955. function clear(Bytes32ToAddressMap storage map) internal {
  956. clear(map._inner);
  957. }
  958. /**
  959. * @dev Returns true if the key is in the map. O(1).
  960. */
  961. function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) {
  962. return contains(map._inner, key);
  963. }
  964. /**
  965. * @dev Returns the number of elements in the map. O(1).
  966. */
  967. function length(Bytes32ToAddressMap storage map) internal view returns (uint256) {
  968. return length(map._inner);
  969. }
  970. /**
  971. * @dev Returns the element stored at position `index` in the map. O(1).
  972. * Note that there are no guarantees on the ordering of values inside the
  973. * array, and it may change when more values are added or removed.
  974. *
  975. * Requirements:
  976. *
  977. * - `index` must be strictly less than {length}.
  978. */
  979. function at(Bytes32ToAddressMap storage map, uint256 index) internal view returns (bytes32 key, address value) {
  980. (bytes32 atKey, bytes32 val) = at(map._inner, index);
  981. return (atKey, address(uint160(uint256(val))));
  982. }
  983. /**
  984. * @dev Tries to return the value associated with `key`. O(1).
  985. * Does not revert if `key` is not in the map.
  986. */
  987. function tryGet(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool exists, address value) {
  988. (bool success, bytes32 val) = tryGet(map._inner, key);
  989. return (success, address(uint160(uint256(val))));
  990. }
  991. /**
  992. * @dev Returns the value associated with `key`. O(1).
  993. *
  994. * Requirements:
  995. *
  996. * - `key` must be in the map.
  997. */
  998. function get(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (address) {
  999. return address(uint160(uint256(get(map._inner, key))));
  1000. }
  1001. /**
  1002. * @dev Returns an array containing all the keys
  1003. *
  1004. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  1005. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  1006. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  1007. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  1008. */
  1009. function keys(Bytes32ToAddressMap storage map) internal view returns (bytes32[] memory) {
  1010. bytes32[] memory store = keys(map._inner);
  1011. bytes32[] memory result;
  1012. assembly ("memory-safe") {
  1013. result := store
  1014. }
  1015. return result;
  1016. }
  1017. /**
  1018. * @dev Returns an array containing a slice of the keys
  1019. *
  1020. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  1021. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  1022. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  1023. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  1024. */
  1025. function keys(
  1026. Bytes32ToAddressMap storage map,
  1027. uint256 start,
  1028. uint256 end
  1029. ) internal view returns (bytes32[] memory) {
  1030. bytes32[] memory store = keys(map._inner, start, end);
  1031. bytes32[] memory result;
  1032. assembly ("memory-safe") {
  1033. result := store
  1034. }
  1035. return result;
  1036. }
  1037. /**
  1038. * @dev Query for a nonexistent map key.
  1039. */
  1040. error EnumerableMapNonexistentBytesKey(bytes key);
  1041. struct BytesToBytesMap {
  1042. // Storage of keys
  1043. EnumerableSet.BytesSet _keys;
  1044. mapping(bytes key => bytes) _values;
  1045. }
  1046. /**
  1047. * @dev Adds a key-value pair to a map, or updates the value for an existing
  1048. * key. O(1).
  1049. *
  1050. * Returns true if the key was added to the map, that is if it was not
  1051. * already present.
  1052. */
  1053. function set(BytesToBytesMap storage map, bytes memory key, bytes memory value) internal returns (bool) {
  1054. map._values[key] = value;
  1055. return map._keys.add(key);
  1056. }
  1057. /**
  1058. * @dev Removes a key-value pair from a map. O(1).
  1059. *
  1060. * Returns true if the key was removed from the map, that is if it was present.
  1061. */
  1062. function remove(BytesToBytesMap storage map, bytes memory key) internal returns (bool) {
  1063. delete map._values[key];
  1064. return map._keys.remove(key);
  1065. }
  1066. /**
  1067. * @dev Removes all the entries from a map. O(n).
  1068. *
  1069. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  1070. * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
  1071. */
  1072. function clear(BytesToBytesMap storage map) internal {
  1073. uint256 len = length(map);
  1074. for (uint256 i = 0; i < len; ++i) {
  1075. delete map._values[map._keys.at(i)];
  1076. }
  1077. map._keys.clear();
  1078. }
  1079. /**
  1080. * @dev Returns true if the key is in the map. O(1).
  1081. */
  1082. function contains(BytesToBytesMap storage map, bytes memory key) internal view returns (bool) {
  1083. return map._keys.contains(key);
  1084. }
  1085. /**
  1086. * @dev Returns the number of key-value pairs in the map. O(1).
  1087. */
  1088. function length(BytesToBytesMap storage map) internal view returns (uint256) {
  1089. return map._keys.length();
  1090. }
  1091. /**
  1092. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  1093. *
  1094. * Note that there are no guarantees on the ordering of entries inside the
  1095. * array, and it may change when more entries are added or removed.
  1096. *
  1097. * Requirements:
  1098. *
  1099. * - `index` must be strictly less than {length}.
  1100. */
  1101. function at(
  1102. BytesToBytesMap storage map,
  1103. uint256 index
  1104. ) internal view returns (bytes memory key, bytes memory value) {
  1105. key = map._keys.at(index);
  1106. value = map._values[key];
  1107. }
  1108. /**
  1109. * @dev Tries to return the value associated with `key`. O(1).
  1110. * Does not revert if `key` is not in the map.
  1111. */
  1112. function tryGet(
  1113. BytesToBytesMap storage map,
  1114. bytes memory key
  1115. ) internal view returns (bool exists, bytes memory value) {
  1116. value = map._values[key];
  1117. exists = bytes(value).length != 0 || contains(map, key);
  1118. }
  1119. /**
  1120. * @dev Returns the value associated with `key`. O(1).
  1121. *
  1122. * Requirements:
  1123. *
  1124. * - `key` must be in the map.
  1125. */
  1126. function get(BytesToBytesMap storage map, bytes memory key) internal view returns (bytes memory value) {
  1127. bool exists;
  1128. (exists, value) = tryGet(map, key);
  1129. if (!exists) {
  1130. revert EnumerableMapNonexistentBytesKey(key);
  1131. }
  1132. }
  1133. /**
  1134. * @dev Returns an array containing all the keys
  1135. *
  1136. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  1137. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  1138. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  1139. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  1140. */
  1141. function keys(BytesToBytesMap storage map) internal view returns (bytes[] memory) {
  1142. return map._keys.values();
  1143. }
  1144. /**
  1145. * @dev Returns an array containing a slice of the keys
  1146. *
  1147. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  1148. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  1149. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  1150. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  1151. */
  1152. function keys(BytesToBytesMap storage map, uint256 start, uint256 end) internal view returns (bytes[] memory) {
  1153. return map._keys.values(start, end);
  1154. }
  1155. }