EnumerableMap.sol 48 KB

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