EnumerableMap.sol 32 KB

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