EnumerableMap.sol 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (utils/structs/EnumerableMap.sol)
  3. // This file was procedurally generated from scripts/generate/templates/EnumerableMap.js.
  4. pragma solidity ^0.8.0;
  5. import "./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. *
  35. * [WARNING]
  36. * ====
  37. * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
  38. * unusable.
  39. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  40. *
  41. * In order to clean an EnumerableMap, you can either remove all elements one by one or create a fresh instance using an
  42. * array of EnumerableMap.
  43. * ====
  44. */
  45. library EnumerableMap {
  46. using EnumerableSet for EnumerableSet.Bytes32Set;
  47. // To implement this library for multiple types with as little code
  48. // repetition as possible, we write it in terms of a generic Map type with
  49. // bytes32 keys and values.
  50. // The Map implementation uses private functions, and user-facing
  51. // implementations (such as Uint256ToAddressMap) are just wrappers around
  52. // the underlying Map.
  53. // This means that we can only create new EnumerableMaps for types that fit
  54. // in bytes32.
  55. struct Bytes32ToBytes32Map {
  56. // Storage of keys
  57. EnumerableSet.Bytes32Set _keys;
  58. mapping(bytes32 => bytes32) _values;
  59. }
  60. /**
  61. * @dev Adds a key-value pair to a map, or updates the value for an existing
  62. * key. O(1).
  63. *
  64. * Returns true if the key was added to the map, that is if it was not
  65. * already present.
  66. */
  67. function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value) internal returns (bool) {
  68. map._values[key] = value;
  69. return map._keys.add(key);
  70. }
  71. /**
  72. * @dev Removes a key-value pair from a map. O(1).
  73. *
  74. * Returns true if the key was removed from the map, that is if it was present.
  75. */
  76. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
  77. delete map._values[key];
  78. return map._keys.remove(key);
  79. }
  80. /**
  81. * @dev Returns true if the key is in the map. O(1).
  82. */
  83. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
  84. return map._keys.contains(key);
  85. }
  86. /**
  87. * @dev Returns the number of key-value pairs in the map. O(1).
  88. */
  89. function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
  90. return map._keys.length();
  91. }
  92. /**
  93. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  94. *
  95. * Note that there are no guarantees on the ordering of entries inside the
  96. * array, and it may change when more entries are added or removed.
  97. *
  98. * Requirements:
  99. *
  100. * - `index` must be strictly less than {length}.
  101. */
  102. function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32, bytes32) {
  103. bytes32 key = map._keys.at(index);
  104. return (key, map._values[key]);
  105. }
  106. /**
  107. * @dev Tries to returns the value associated with `key`. O(1).
  108. * Does not revert if `key` is not in the map.
  109. */
  110. function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
  111. bytes32 value = map._values[key];
  112. if (value == bytes32(0)) {
  113. return (contains(map, key), bytes32(0));
  114. } else {
  115. return (true, value);
  116. }
  117. }
  118. /**
  119. * @dev Returns the value associated with `key`. O(1).
  120. *
  121. * Requirements:
  122. *
  123. * - `key` must be in the map.
  124. */
  125. function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
  126. bytes32 value = map._values[key];
  127. require(value != 0 || contains(map, key), "EnumerableMap: nonexistent key");
  128. return value;
  129. }
  130. /**
  131. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  132. *
  133. * CAUTION: This function is deprecated because it requires allocating memory for the error
  134. * message unnecessarily. For custom revert reasons use {tryGet}.
  135. */
  136. function get(
  137. Bytes32ToBytes32Map storage map,
  138. bytes32 key,
  139. string memory errorMessage
  140. ) internal view returns (bytes32) {
  141. bytes32 value = map._values[key];
  142. require(value != 0 || contains(map, key), errorMessage);
  143. return value;
  144. }
  145. /**
  146. * @dev Return the an array containing all the keys
  147. *
  148. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  149. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  150. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  151. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  152. */
  153. function keys(Bytes32ToBytes32Map storage map) internal view returns (bytes32[] memory) {
  154. return map._keys.values();
  155. }
  156. // UintToUintMap
  157. struct UintToUintMap {
  158. Bytes32ToBytes32Map _inner;
  159. }
  160. /**
  161. * @dev Adds a key-value pair to a map, or updates the value for an existing
  162. * key. O(1).
  163. *
  164. * Returns true if the key was added to the map, that is if it was not
  165. * already present.
  166. */
  167. function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
  168. return set(map._inner, bytes32(key), bytes32(value));
  169. }
  170. /**
  171. * @dev Removes a value from a map. O(1).
  172. *
  173. * Returns true if the key was removed from the map, that is if it was present.
  174. */
  175. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  176. return remove(map._inner, bytes32(key));
  177. }
  178. /**
  179. * @dev Returns true if the key is in the map. O(1).
  180. */
  181. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  182. return contains(map._inner, bytes32(key));
  183. }
  184. /**
  185. * @dev Returns the number of elements in the map. O(1).
  186. */
  187. function length(UintToUintMap storage map) internal view returns (uint256) {
  188. return length(map._inner);
  189. }
  190. /**
  191. * @dev Returns the element stored at position `index` in the map. O(1).
  192. * Note that there are no guarantees on the ordering of values inside the
  193. * array, and it may change when more values are added or removed.
  194. *
  195. * Requirements:
  196. *
  197. * - `index` must be strictly less than {length}.
  198. */
  199. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
  200. (bytes32 key, bytes32 value) = at(map._inner, index);
  201. return (uint256(key), uint256(value));
  202. }
  203. /**
  204. * @dev Tries to returns the value associated with `key`. O(1).
  205. * Does not revert if `key` is not in the map.
  206. */
  207. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
  208. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  209. return (success, uint256(value));
  210. }
  211. /**
  212. * @dev Returns the value associated with `key`. O(1).
  213. *
  214. * Requirements:
  215. *
  216. * - `key` must be in the map.
  217. */
  218. function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  219. return uint256(get(map._inner, bytes32(key)));
  220. }
  221. /**
  222. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  223. *
  224. * CAUTION: This function is deprecated because it requires allocating memory for the error
  225. * message unnecessarily. For custom revert reasons use {tryGet}.
  226. */
  227. function get(UintToUintMap storage map, uint256 key, string memory errorMessage) internal view returns (uint256) {
  228. return uint256(get(map._inner, bytes32(key), errorMessage));
  229. }
  230. /**
  231. * @dev Return the an array containing all the keys
  232. *
  233. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  234. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  235. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  236. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  237. */
  238. function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
  239. bytes32[] memory store = keys(map._inner);
  240. uint256[] memory result;
  241. /// @solidity memory-safe-assembly
  242. assembly {
  243. result := store
  244. }
  245. return result;
  246. }
  247. // UintToAddressMap
  248. struct UintToAddressMap {
  249. Bytes32ToBytes32Map _inner;
  250. }
  251. /**
  252. * @dev Adds a key-value pair to a map, or updates the value for an existing
  253. * key. O(1).
  254. *
  255. * Returns true if the key was added to the map, that is if it was not
  256. * already present.
  257. */
  258. function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
  259. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  260. }
  261. /**
  262. * @dev Removes a value from a map. O(1).
  263. *
  264. * Returns true if the key was removed from the map, that is if it was present.
  265. */
  266. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  267. return remove(map._inner, bytes32(key));
  268. }
  269. /**
  270. * @dev Returns true if the key is in the map. O(1).
  271. */
  272. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  273. return contains(map._inner, bytes32(key));
  274. }
  275. /**
  276. * @dev Returns the number of elements in the map. O(1).
  277. */
  278. function length(UintToAddressMap storage map) internal view returns (uint256) {
  279. return length(map._inner);
  280. }
  281. /**
  282. * @dev Returns the element stored at position `index` in the map. O(1).
  283. * Note that there are no guarantees on the ordering of values inside the
  284. * array, and it may change when more values are added or removed.
  285. *
  286. * Requirements:
  287. *
  288. * - `index` must be strictly less than {length}.
  289. */
  290. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  291. (bytes32 key, bytes32 value) = at(map._inner, index);
  292. return (uint256(key), address(uint160(uint256(value))));
  293. }
  294. /**
  295. * @dev Tries to returns the value associated with `key`. O(1).
  296. * Does not revert if `key` is not in the map.
  297. */
  298. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  299. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  300. return (success, address(uint160(uint256(value))));
  301. }
  302. /**
  303. * @dev Returns the value associated with `key`. O(1).
  304. *
  305. * Requirements:
  306. *
  307. * - `key` must be in the map.
  308. */
  309. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  310. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  311. }
  312. /**
  313. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  314. *
  315. * CAUTION: This function is deprecated because it requires allocating memory for the error
  316. * message unnecessarily. For custom revert reasons use {tryGet}.
  317. */
  318. function get(
  319. UintToAddressMap storage map,
  320. uint256 key,
  321. string memory errorMessage
  322. ) internal view returns (address) {
  323. return address(uint160(uint256(get(map._inner, bytes32(key), errorMessage))));
  324. }
  325. /**
  326. * @dev Return the an array containing all the keys
  327. *
  328. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  329. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  330. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  331. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  332. */
  333. function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
  334. bytes32[] memory store = keys(map._inner);
  335. uint256[] memory result;
  336. /// @solidity memory-safe-assembly
  337. assembly {
  338. result := store
  339. }
  340. return result;
  341. }
  342. // AddressToUintMap
  343. struct AddressToUintMap {
  344. Bytes32ToBytes32Map _inner;
  345. }
  346. /**
  347. * @dev Adds a key-value pair to a map, or updates the value for an existing
  348. * key. O(1).
  349. *
  350. * Returns true if the key was added to the map, that is if it was not
  351. * already present.
  352. */
  353. function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
  354. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  355. }
  356. /**
  357. * @dev Removes a value from a map. O(1).
  358. *
  359. * Returns true if the key was removed from the map, that is if it was present.
  360. */
  361. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  362. return remove(map._inner, bytes32(uint256(uint160(key))));
  363. }
  364. /**
  365. * @dev Returns true if the key is in the map. O(1).
  366. */
  367. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  368. return contains(map._inner, bytes32(uint256(uint160(key))));
  369. }
  370. /**
  371. * @dev Returns the number of elements in the map. O(1).
  372. */
  373. function length(AddressToUintMap storage map) internal view returns (uint256) {
  374. return length(map._inner);
  375. }
  376. /**
  377. * @dev Returns the element stored at position `index` in the map. O(1).
  378. * Note that there are no guarantees on the ordering of values inside the
  379. * array, and it may change when more values are added or removed.
  380. *
  381. * Requirements:
  382. *
  383. * - `index` must be strictly less than {length}.
  384. */
  385. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  386. (bytes32 key, bytes32 value) = at(map._inner, index);
  387. return (address(uint160(uint256(key))), uint256(value));
  388. }
  389. /**
  390. * @dev Tries to returns the value associated with `key`. O(1).
  391. * Does not revert if `key` is not in the map.
  392. */
  393. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  394. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  395. return (success, uint256(value));
  396. }
  397. /**
  398. * @dev Returns the value associated with `key`. O(1).
  399. *
  400. * Requirements:
  401. *
  402. * - `key` must be in the map.
  403. */
  404. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  405. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  406. }
  407. /**
  408. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  409. *
  410. * CAUTION: This function is deprecated because it requires allocating memory for the error
  411. * message unnecessarily. For custom revert reasons use {tryGet}.
  412. */
  413. function get(
  414. AddressToUintMap storage map,
  415. address key,
  416. string memory errorMessage
  417. ) internal view returns (uint256) {
  418. return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
  419. }
  420. /**
  421. * @dev Return the an array containing all the keys
  422. *
  423. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  424. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  425. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  426. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  427. */
  428. function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
  429. bytes32[] memory store = keys(map._inner);
  430. address[] memory result;
  431. /// @solidity memory-safe-assembly
  432. assembly {
  433. result := store
  434. }
  435. return result;
  436. }
  437. // Bytes32ToUintMap
  438. struct Bytes32ToUintMap {
  439. Bytes32ToBytes32Map _inner;
  440. }
  441. /**
  442. * @dev Adds a key-value pair to a map, or updates the value for an existing
  443. * key. O(1).
  444. *
  445. * Returns true if the key was added to the map, that is if it was not
  446. * already present.
  447. */
  448. function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
  449. return set(map._inner, key, bytes32(value));
  450. }
  451. /**
  452. * @dev Removes a value from a map. O(1).
  453. *
  454. * Returns true if the key was removed from the map, that is if it was present.
  455. */
  456. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  457. return remove(map._inner, key);
  458. }
  459. /**
  460. * @dev Returns true if the key is in the map. O(1).
  461. */
  462. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  463. return contains(map._inner, key);
  464. }
  465. /**
  466. * @dev Returns the number of elements in the map. O(1).
  467. */
  468. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  469. return length(map._inner);
  470. }
  471. /**
  472. * @dev Returns the element stored at position `index` in the map. O(1).
  473. * Note that there are no guarantees on the ordering of values inside the
  474. * array, and it may change when more values are added or removed.
  475. *
  476. * Requirements:
  477. *
  478. * - `index` must be strictly less than {length}.
  479. */
  480. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  481. (bytes32 key, bytes32 value) = at(map._inner, index);
  482. return (key, uint256(value));
  483. }
  484. /**
  485. * @dev Tries to returns the value associated with `key`. O(1).
  486. * Does not revert if `key` is not in the map.
  487. */
  488. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  489. (bool success, bytes32 value) = tryGet(map._inner, key);
  490. return (success, uint256(value));
  491. }
  492. /**
  493. * @dev Returns the value associated with `key`. O(1).
  494. *
  495. * Requirements:
  496. *
  497. * - `key` must be in the map.
  498. */
  499. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  500. return uint256(get(map._inner, key));
  501. }
  502. /**
  503. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  504. *
  505. * CAUTION: This function is deprecated because it requires allocating memory for the error
  506. * message unnecessarily. For custom revert reasons use {tryGet}.
  507. */
  508. function get(
  509. Bytes32ToUintMap storage map,
  510. bytes32 key,
  511. string memory errorMessage
  512. ) internal view returns (uint256) {
  513. return uint256(get(map._inner, key, errorMessage));
  514. }
  515. /**
  516. * @dev Return the an array containing all the keys
  517. *
  518. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  519. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  520. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  521. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  522. */
  523. function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
  524. bytes32[] memory store = keys(map._inner);
  525. bytes32[] memory result;
  526. /// @solidity memory-safe-assembly
  527. assembly {
  528. result := store
  529. }
  530. return result;
  531. }
  532. }