EnumerableMap.sol 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (utils/structs/EnumerableMap.sol)
  3. // This file was procedurally generated from scripts/generate/templates/EnumerableMap.js.
  4. pragma solidity ^0.8.19;
  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. /**
  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 => 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. /// @solidity memory-safe-assembly
  224. assembly {
  225. result := store
  226. }
  227. return result;
  228. }
  229. // UintToAddressMap
  230. struct UintToAddressMap {
  231. Bytes32ToBytes32Map _inner;
  232. }
  233. /**
  234. * @dev Adds a key-value pair to a map, or updates the value for an existing
  235. * key. O(1).
  236. *
  237. * Returns true if the key was added to the map, that is if it was not
  238. * already present.
  239. */
  240. function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
  241. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  242. }
  243. /**
  244. * @dev Removes a value from a map. O(1).
  245. *
  246. * Returns true if the key was removed from the map, that is if it was present.
  247. */
  248. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  249. return remove(map._inner, bytes32(key));
  250. }
  251. /**
  252. * @dev Returns true if the key is in the map. O(1).
  253. */
  254. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  255. return contains(map._inner, bytes32(key));
  256. }
  257. /**
  258. * @dev Returns the number of elements in the map. O(1).
  259. */
  260. function length(UintToAddressMap storage map) internal view returns (uint256) {
  261. return length(map._inner);
  262. }
  263. /**
  264. * @dev Returns the element stored at position `index` in the map. O(1).
  265. * Note that there are no guarantees on the ordering of values inside the
  266. * array, and it may change when more values are added or removed.
  267. *
  268. * Requirements:
  269. *
  270. * - `index` must be strictly less than {length}.
  271. */
  272. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  273. (bytes32 key, bytes32 value) = at(map._inner, index);
  274. return (uint256(key), address(uint160(uint256(value))));
  275. }
  276. /**
  277. * @dev Tries to returns the value associated with `key`. O(1).
  278. * Does not revert if `key` is not in the map.
  279. */
  280. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  281. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  282. return (success, address(uint160(uint256(value))));
  283. }
  284. /**
  285. * @dev Returns the value associated with `key`. O(1).
  286. *
  287. * Requirements:
  288. *
  289. * - `key` must be in the map.
  290. */
  291. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  292. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  293. }
  294. /**
  295. * @dev Return the an array containing all the keys
  296. *
  297. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  298. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  299. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  300. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  301. */
  302. function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
  303. bytes32[] memory store = keys(map._inner);
  304. uint256[] memory result;
  305. /// @solidity memory-safe-assembly
  306. assembly {
  307. result := store
  308. }
  309. return result;
  310. }
  311. // AddressToUintMap
  312. struct AddressToUintMap {
  313. Bytes32ToBytes32Map _inner;
  314. }
  315. /**
  316. * @dev Adds a key-value pair to a map, or updates the value for an existing
  317. * key. O(1).
  318. *
  319. * Returns true if the key was added to the map, that is if it was not
  320. * already present.
  321. */
  322. function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
  323. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  324. }
  325. /**
  326. * @dev Removes a value from a map. O(1).
  327. *
  328. * Returns true if the key was removed from the map, that is if it was present.
  329. */
  330. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  331. return remove(map._inner, bytes32(uint256(uint160(key))));
  332. }
  333. /**
  334. * @dev Returns true if the key is in the map. O(1).
  335. */
  336. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  337. return contains(map._inner, bytes32(uint256(uint160(key))));
  338. }
  339. /**
  340. * @dev Returns the number of elements in the map. O(1).
  341. */
  342. function length(AddressToUintMap storage map) internal view returns (uint256) {
  343. return length(map._inner);
  344. }
  345. /**
  346. * @dev Returns the element stored at position `index` in the map. O(1).
  347. * Note that there are no guarantees on the ordering of values inside the
  348. * array, and it may change when more values are added or removed.
  349. *
  350. * Requirements:
  351. *
  352. * - `index` must be strictly less than {length}.
  353. */
  354. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  355. (bytes32 key, bytes32 value) = at(map._inner, index);
  356. return (address(uint160(uint256(key))), uint256(value));
  357. }
  358. /**
  359. * @dev Tries to returns the value associated with `key`. O(1).
  360. * Does not revert if `key` is not in the map.
  361. */
  362. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  363. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  364. return (success, uint256(value));
  365. }
  366. /**
  367. * @dev Returns the value associated with `key`. O(1).
  368. *
  369. * Requirements:
  370. *
  371. * - `key` must be in the map.
  372. */
  373. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  374. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  375. }
  376. /**
  377. * @dev Return the an array containing all the keys
  378. *
  379. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  380. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  381. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  382. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  383. */
  384. function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
  385. bytes32[] memory store = keys(map._inner);
  386. address[] memory result;
  387. /// @solidity memory-safe-assembly
  388. assembly {
  389. result := store
  390. }
  391. return result;
  392. }
  393. // Bytes32ToUintMap
  394. struct Bytes32ToUintMap {
  395. Bytes32ToBytes32Map _inner;
  396. }
  397. /**
  398. * @dev Adds a key-value pair to a map, or updates the value for an existing
  399. * key. O(1).
  400. *
  401. * Returns true if the key was added to the map, that is if it was not
  402. * already present.
  403. */
  404. function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
  405. return set(map._inner, key, bytes32(value));
  406. }
  407. /**
  408. * @dev Removes a value from a map. O(1).
  409. *
  410. * Returns true if the key was removed from the map, that is if it was present.
  411. */
  412. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  413. return remove(map._inner, key);
  414. }
  415. /**
  416. * @dev Returns true if the key is in the map. O(1).
  417. */
  418. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  419. return contains(map._inner, key);
  420. }
  421. /**
  422. * @dev Returns the number of elements in the map. O(1).
  423. */
  424. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  425. return length(map._inner);
  426. }
  427. /**
  428. * @dev Returns the element stored at position `index` in the map. O(1).
  429. * Note that there are no guarantees on the ordering of values inside the
  430. * array, and it may change when more values are added or removed.
  431. *
  432. * Requirements:
  433. *
  434. * - `index` must be strictly less than {length}.
  435. */
  436. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  437. (bytes32 key, bytes32 value) = at(map._inner, index);
  438. return (key, uint256(value));
  439. }
  440. /**
  441. * @dev Tries to returns the value associated with `key`. O(1).
  442. * Does not revert if `key` is not in the map.
  443. */
  444. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  445. (bool success, bytes32 value) = tryGet(map._inner, key);
  446. return (success, uint256(value));
  447. }
  448. /**
  449. * @dev Returns the value associated with `key`. O(1).
  450. *
  451. * Requirements:
  452. *
  453. * - `key` must be in the map.
  454. */
  455. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  456. return uint256(get(map._inner, key));
  457. }
  458. /**
  459. * @dev Return the an array containing all the keys
  460. *
  461. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  462. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  463. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  464. * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
  465. */
  466. function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
  467. bytes32[] memory store = keys(map._inner);
  468. bytes32[] memory result;
  469. /// @solidity memory-safe-assembly
  470. assembly {
  471. result := store
  472. }
  473. return result;
  474. }
  475. }