EnumerableMap.sol 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. *
  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 repetition as possible, we write it in
  48. // terms of a generic Map type with bytes32 keys and values. The Map implementation uses private functions,
  49. // and user-facing implementations such as `UintToAddressMap` are just wrappers around the underlying Map.
  50. // This means that we can only create new EnumerableMaps for types that fit in bytes32.
  51. /**
  52. * @dev Query for a nonexistent map key.
  53. */
  54. error EnumerableMapNonexistentKey(bytes32 key);
  55. struct Bytes32ToBytes32Map {
  56. // Storage of keys
  57. EnumerableSet.Bytes32Set _keys;
  58. mapping(bytes32 key => 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. if (value == 0 && !contains(map, key)) {
  128. revert EnumerableMapNonexistentKey(key);
  129. }
  130. return value;
  131. }
  132. /**
  133. * @dev Return the an array containing all the keys
  134. *
  135. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  136. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  137. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  138. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  139. */
  140. function keys(Bytes32ToBytes32Map storage map) internal view returns (bytes32[] memory) {
  141. return map._keys.values();
  142. }
  143. // UintToUintMap
  144. struct UintToUintMap {
  145. Bytes32ToBytes32Map _inner;
  146. }
  147. /**
  148. * @dev Adds a key-value pair to a map, or updates the value for an existing
  149. * key. O(1).
  150. *
  151. * Returns true if the key was added to the map, that is if it was not
  152. * already present.
  153. */
  154. function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
  155. return set(map._inner, bytes32(key), bytes32(value));
  156. }
  157. /**
  158. * @dev Removes a value from a map. O(1).
  159. *
  160. * Returns true if the key was removed from the map, that is if it was present.
  161. */
  162. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  163. return remove(map._inner, bytes32(key));
  164. }
  165. /**
  166. * @dev Returns true if the key is in the map. O(1).
  167. */
  168. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  169. return contains(map._inner, bytes32(key));
  170. }
  171. /**
  172. * @dev Returns the number of elements in the map. O(1).
  173. */
  174. function length(UintToUintMap storage map) internal view returns (uint256) {
  175. return length(map._inner);
  176. }
  177. /**
  178. * @dev Returns the element stored at position `index` in the map. O(1).
  179. * Note that there are no guarantees on the ordering of values inside the
  180. * array, and it may change when more values are added or removed.
  181. *
  182. * Requirements:
  183. *
  184. * - `index` must be strictly less than {length}.
  185. */
  186. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
  187. (bytes32 key, bytes32 value) = at(map._inner, index);
  188. return (uint256(key), uint256(value));
  189. }
  190. /**
  191. * @dev Tries to returns the value associated with `key`. O(1).
  192. * Does not revert if `key` is not in the map.
  193. */
  194. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
  195. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  196. return (success, uint256(value));
  197. }
  198. /**
  199. * @dev Returns the value associated with `key`. O(1).
  200. *
  201. * Requirements:
  202. *
  203. * - `key` must be in the map.
  204. */
  205. function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  206. return uint256(get(map._inner, bytes32(key)));
  207. }
  208. /**
  209. * @dev Return the an array containing all the keys
  210. *
  211. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  212. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  213. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  214. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  215. */
  216. function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
  217. bytes32[] memory store = keys(map._inner);
  218. uint256[] memory result;
  219. /// @solidity memory-safe-assembly
  220. assembly {
  221. result := store
  222. }
  223. return result;
  224. }
  225. // UintToAddressMap
  226. struct UintToAddressMap {
  227. Bytes32ToBytes32Map _inner;
  228. }
  229. /**
  230. * @dev Adds a key-value pair to a map, or updates the value for an existing
  231. * key. O(1).
  232. *
  233. * Returns true if the key was added to the map, that is if it was not
  234. * already present.
  235. */
  236. function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
  237. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  238. }
  239. /**
  240. * @dev Removes a value from a map. O(1).
  241. *
  242. * Returns true if the key was removed from the map, that is if it was present.
  243. */
  244. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  245. return remove(map._inner, bytes32(key));
  246. }
  247. /**
  248. * @dev Returns true if the key is in the map. O(1).
  249. */
  250. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  251. return contains(map._inner, bytes32(key));
  252. }
  253. /**
  254. * @dev Returns the number of elements in the map. O(1).
  255. */
  256. function length(UintToAddressMap storage map) internal view returns (uint256) {
  257. return length(map._inner);
  258. }
  259. /**
  260. * @dev Returns the element stored at position `index` in the map. O(1).
  261. * Note that there are no guarantees on the ordering of values inside the
  262. * array, and it may change when more values are added or removed.
  263. *
  264. * Requirements:
  265. *
  266. * - `index` must be strictly less than {length}.
  267. */
  268. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  269. (bytes32 key, bytes32 value) = at(map._inner, index);
  270. return (uint256(key), address(uint160(uint256(value))));
  271. }
  272. /**
  273. * @dev Tries to returns the value associated with `key`. O(1).
  274. * Does not revert if `key` is not in the map.
  275. */
  276. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  277. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  278. return (success, address(uint160(uint256(value))));
  279. }
  280. /**
  281. * @dev Returns the value associated with `key`. O(1).
  282. *
  283. * Requirements:
  284. *
  285. * - `key` must be in the map.
  286. */
  287. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  288. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  289. }
  290. /**
  291. * @dev Return the an array containing all the keys
  292. *
  293. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  294. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  295. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  296. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  297. */
  298. function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
  299. bytes32[] memory store = keys(map._inner);
  300. uint256[] memory result;
  301. /// @solidity memory-safe-assembly
  302. assembly {
  303. result := store
  304. }
  305. return result;
  306. }
  307. // AddressToUintMap
  308. struct AddressToUintMap {
  309. Bytes32ToBytes32Map _inner;
  310. }
  311. /**
  312. * @dev Adds a key-value pair to a map, or updates the value for an existing
  313. * key. O(1).
  314. *
  315. * Returns true if the key was added to the map, that is if it was not
  316. * already present.
  317. */
  318. function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
  319. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  320. }
  321. /**
  322. * @dev Removes a value from a map. O(1).
  323. *
  324. * Returns true if the key was removed from the map, that is if it was present.
  325. */
  326. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  327. return remove(map._inner, bytes32(uint256(uint160(key))));
  328. }
  329. /**
  330. * @dev Returns true if the key is in the map. O(1).
  331. */
  332. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  333. return contains(map._inner, bytes32(uint256(uint160(key))));
  334. }
  335. /**
  336. * @dev Returns the number of elements in the map. O(1).
  337. */
  338. function length(AddressToUintMap storage map) internal view returns (uint256) {
  339. return length(map._inner);
  340. }
  341. /**
  342. * @dev Returns the element stored at position `index` in the map. O(1).
  343. * Note that there are no guarantees on the ordering of values inside the
  344. * array, and it may change when more values are added or removed.
  345. *
  346. * Requirements:
  347. *
  348. * - `index` must be strictly less than {length}.
  349. */
  350. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  351. (bytes32 key, bytes32 value) = at(map._inner, index);
  352. return (address(uint160(uint256(key))), uint256(value));
  353. }
  354. /**
  355. * @dev Tries to returns the value associated with `key`. O(1).
  356. * Does not revert if `key` is not in the map.
  357. */
  358. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  359. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  360. return (success, uint256(value));
  361. }
  362. /**
  363. * @dev Returns the value associated with `key`. O(1).
  364. *
  365. * Requirements:
  366. *
  367. * - `key` must be in the map.
  368. */
  369. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  370. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  371. }
  372. /**
  373. * @dev Return the an array containing all the keys
  374. *
  375. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  376. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  377. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  378. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  379. */
  380. function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
  381. bytes32[] memory store = keys(map._inner);
  382. address[] memory result;
  383. /// @solidity memory-safe-assembly
  384. assembly {
  385. result := store
  386. }
  387. return result;
  388. }
  389. // Bytes32ToUintMap
  390. struct Bytes32ToUintMap {
  391. Bytes32ToBytes32Map _inner;
  392. }
  393. /**
  394. * @dev Adds a key-value pair to a map, or updates the value for an existing
  395. * key. O(1).
  396. *
  397. * Returns true if the key was added to the map, that is if it was not
  398. * already present.
  399. */
  400. function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
  401. return set(map._inner, key, bytes32(value));
  402. }
  403. /**
  404. * @dev Removes a value from a map. O(1).
  405. *
  406. * Returns true if the key was removed from the map, that is if it was present.
  407. */
  408. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  409. return remove(map._inner, key);
  410. }
  411. /**
  412. * @dev Returns true if the key is in the map. O(1).
  413. */
  414. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  415. return contains(map._inner, key);
  416. }
  417. /**
  418. * @dev Returns the number of elements in the map. O(1).
  419. */
  420. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  421. return length(map._inner);
  422. }
  423. /**
  424. * @dev Returns the element stored at position `index` in the map. O(1).
  425. * Note that there are no guarantees on the ordering of values inside the
  426. * array, and it may change when more values are added or removed.
  427. *
  428. * Requirements:
  429. *
  430. * - `index` must be strictly less than {length}.
  431. */
  432. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  433. (bytes32 key, bytes32 value) = at(map._inner, index);
  434. return (key, uint256(value));
  435. }
  436. /**
  437. * @dev Tries to returns the value associated with `key`. O(1).
  438. * Does not revert if `key` is not in the map.
  439. */
  440. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  441. (bool success, bytes32 value) = tryGet(map._inner, key);
  442. return (success, uint256(value));
  443. }
  444. /**
  445. * @dev Returns the value associated with `key`. O(1).
  446. *
  447. * Requirements:
  448. *
  449. * - `key` must be in the map.
  450. */
  451. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  452. return uint256(get(map._inner, key));
  453. }
  454. /**
  455. * @dev Return the an array containing all the keys
  456. *
  457. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  458. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  459. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  460. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  461. */
  462. function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
  463. bytes32[] memory store = keys(map._inner);
  464. bytes32[] memory result;
  465. /// @solidity memory-safe-assembly
  466. assembly {
  467. result := store
  468. }
  469. return result;
  470. }
  471. }