EnumerableMap.sol 18 KB

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