EnumerableMap.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.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. * ```
  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 unusable.
  38. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  39. *
  40. * In order to clean an EnumerableMap, you can either remove all elements one by one or create a fresh instance using an array of EnumerableMap.
  41. * ====
  42. */
  43. library EnumerableMap {
  44. using EnumerableSet for EnumerableSet.Bytes32Set;
  45. // To implement this library for multiple types with as little code
  46. // repetition as possible, we write it in terms of a generic Map type with
  47. // bytes32 keys and values.
  48. // The Map implementation uses private functions, and user-facing
  49. // implementations (such as Uint256ToAddressMap) are just wrappers around
  50. // the underlying Map.
  51. // This means that we can only create new EnumerableMaps for types that fit
  52. // in bytes32.
  53. struct Bytes32ToBytes32Map {
  54. // Storage of keys
  55. EnumerableSet.Bytes32Set _keys;
  56. mapping(bytes32 => bytes32) _values;
  57. }
  58. /**
  59. * @dev Adds a key-value pair to a map, or updates the value for an existing
  60. * key. O(1).
  61. *
  62. * Returns true if the key was added to the map, that is if it was not
  63. * already present.
  64. */
  65. function set(
  66. Bytes32ToBytes32Map storage map,
  67. bytes32 key,
  68. bytes32 value
  69. ) internal returns (bool) {
  70. map._values[key] = value;
  71. return map._keys.add(key);
  72. }
  73. /**
  74. * @dev Removes a key-value pair from a map. O(1).
  75. *
  76. * Returns true if the key was removed from the map, that is if it was present.
  77. */
  78. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
  79. delete map._values[key];
  80. return map._keys.remove(key);
  81. }
  82. /**
  83. * @dev Returns true if the key is in the map. O(1).
  84. */
  85. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
  86. return map._keys.contains(key);
  87. }
  88. /**
  89. * @dev Returns the number of key-value pairs in the map. O(1).
  90. */
  91. function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
  92. return map._keys.length();
  93. }
  94. /**
  95. * @dev Returns the key-value pair stored at position `index` in the map. O(1).
  96. *
  97. * Note that there are no guarantees on the ordering of entries inside the
  98. * array, and it may change when more entries are added or removed.
  99. *
  100. * Requirements:
  101. *
  102. * - `index` must be strictly less than {length}.
  103. */
  104. function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32, bytes32) {
  105. bytes32 key = map._keys.at(index);
  106. return (key, map._values[key]);
  107. }
  108. /**
  109. * @dev Tries to returns the value associated with `key`. O(1).
  110. * Does not revert if `key` is not in the map.
  111. */
  112. function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
  113. bytes32 value = map._values[key];
  114. if (value == bytes32(0)) {
  115. return (contains(map, key), bytes32(0));
  116. } else {
  117. return (true, value);
  118. }
  119. }
  120. /**
  121. * @dev Returns the value associated with `key`. O(1).
  122. *
  123. * Requirements:
  124. *
  125. * - `key` must be in the map.
  126. */
  127. function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
  128. bytes32 value = map._values[key];
  129. require(value != 0 || contains(map, key), "EnumerableMap: nonexistent key");
  130. return value;
  131. }
  132. /**
  133. * @dev Same as {_get}, with a custom error message when `key` is not in the map.
  134. *
  135. * CAUTION: This function is deprecated because it requires allocating memory for the error
  136. * message unnecessarily. For custom revert reasons use {_tryGet}.
  137. */
  138. function get(
  139. Bytes32ToBytes32Map storage map,
  140. bytes32 key,
  141. string memory errorMessage
  142. ) internal view returns (bytes32) {
  143. bytes32 value = map._values[key];
  144. require(value != 0 || contains(map, key), errorMessage);
  145. return value;
  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(
  159. UintToUintMap storage map,
  160. uint256 key,
  161. uint256 value
  162. ) internal returns (bool) {
  163. return set(map._inner, bytes32(key), bytes32(value));
  164. }
  165. /**
  166. * @dev Removes a value from a set. O(1).
  167. *
  168. * Returns true if the key was removed from the map, that is if it was present.
  169. */
  170. function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
  171. return remove(map._inner, bytes32(key));
  172. }
  173. /**
  174. * @dev Returns true if the key is in the map. O(1).
  175. */
  176. function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
  177. return contains(map._inner, bytes32(key));
  178. }
  179. /**
  180. * @dev Returns the number of elements in the map. O(1).
  181. */
  182. function length(UintToUintMap storage map) internal view returns (uint256) {
  183. return length(map._inner);
  184. }
  185. /**
  186. * @dev Returns the element stored at position `index` in the set. O(1).
  187. * Note that there are no guarantees on the ordering of values inside the
  188. * array, and it may change when more values are added or removed.
  189. *
  190. * Requirements:
  191. *
  192. * - `index` must be strictly less than {length}.
  193. */
  194. function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
  195. (bytes32 key, bytes32 value) = at(map._inner, index);
  196. return (uint256(key), uint256(value));
  197. }
  198. /**
  199. * @dev Tries to returns the value associated with `key`. O(1).
  200. * Does not revert if `key` is not in the map.
  201. */
  202. function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
  203. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  204. return (success, uint256(value));
  205. }
  206. /**
  207. * @dev Returns the value associated with `key`. O(1).
  208. *
  209. * Requirements:
  210. *
  211. * - `key` must be in the map.
  212. */
  213. function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
  214. return uint256(get(map._inner, bytes32(key)));
  215. }
  216. /**
  217. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  218. *
  219. * CAUTION: This function is deprecated because it requires allocating memory for the error
  220. * message unnecessarily. For custom revert reasons use {tryGet}.
  221. */
  222. function get(
  223. UintToUintMap storage map,
  224. uint256 key,
  225. string memory errorMessage
  226. ) internal view returns (uint256) {
  227. return uint256(get(map._inner, bytes32(key), errorMessage));
  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(
  241. UintToAddressMap storage map,
  242. uint256 key,
  243. address value
  244. ) internal returns (bool) {
  245. return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
  246. }
  247. /**
  248. * @dev Removes a value from a set. O(1).
  249. *
  250. * Returns true if the key was removed from the map, that is if it was present.
  251. */
  252. function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
  253. return remove(map._inner, bytes32(key));
  254. }
  255. /**
  256. * @dev Returns true if the key is in the map. O(1).
  257. */
  258. function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
  259. return contains(map._inner, bytes32(key));
  260. }
  261. /**
  262. * @dev Returns the number of elements in the map. O(1).
  263. */
  264. function length(UintToAddressMap storage map) internal view returns (uint256) {
  265. return length(map._inner);
  266. }
  267. /**
  268. * @dev Returns the element stored at position `index` in the set. O(1).
  269. * Note that there are no guarantees on the ordering of values inside the
  270. * array, and it may change when more values are added or removed.
  271. *
  272. * Requirements:
  273. *
  274. * - `index` must be strictly less than {length}.
  275. */
  276. function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
  277. (bytes32 key, bytes32 value) = at(map._inner, index);
  278. return (uint256(key), address(uint160(uint256(value))));
  279. }
  280. /**
  281. * @dev Tries to returns the value associated with `key`. O(1).
  282. * Does not revert if `key` is not in the map.
  283. */
  284. function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
  285. (bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
  286. return (success, address(uint160(uint256(value))));
  287. }
  288. /**
  289. * @dev Returns the value associated with `key`. O(1).
  290. *
  291. * Requirements:
  292. *
  293. * - `key` must be in the map.
  294. */
  295. function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
  296. return address(uint160(uint256(get(map._inner, bytes32(key)))));
  297. }
  298. /**
  299. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  300. *
  301. * CAUTION: This function is deprecated because it requires allocating memory for the error
  302. * message unnecessarily. For custom revert reasons use {tryGet}.
  303. */
  304. function get(
  305. UintToAddressMap storage map,
  306. uint256 key,
  307. string memory errorMessage
  308. ) internal view returns (address) {
  309. return address(uint160(uint256(get(map._inner, bytes32(key), errorMessage))));
  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(
  323. AddressToUintMap storage map,
  324. address key,
  325. uint256 value
  326. ) internal returns (bool) {
  327. return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
  328. }
  329. /**
  330. * @dev Removes a value from a set. O(1).
  331. *
  332. * Returns true if the key was removed from the map, that is if it was present.
  333. */
  334. function remove(AddressToUintMap storage map, address key) internal returns (bool) {
  335. return remove(map._inner, bytes32(uint256(uint160(key))));
  336. }
  337. /**
  338. * @dev Returns true if the key is in the map. O(1).
  339. */
  340. function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
  341. return contains(map._inner, bytes32(uint256(uint160(key))));
  342. }
  343. /**
  344. * @dev Returns the number of elements in the map. O(1).
  345. */
  346. function length(AddressToUintMap storage map) internal view returns (uint256) {
  347. return length(map._inner);
  348. }
  349. /**
  350. * @dev Returns the element stored at position `index` in the set. O(1).
  351. * Note that there are no guarantees on the ordering of values inside the
  352. * array, and it may change when more values are added or removed.
  353. *
  354. * Requirements:
  355. *
  356. * - `index` must be strictly less than {length}.
  357. */
  358. function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
  359. (bytes32 key, bytes32 value) = at(map._inner, index);
  360. return (address(uint160(uint256(key))), uint256(value));
  361. }
  362. /**
  363. * @dev Tries to returns the value associated with `key`. O(1).
  364. * Does not revert if `key` is not in the map.
  365. */
  366. function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
  367. (bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
  368. return (success, uint256(value));
  369. }
  370. /**
  371. * @dev Returns the value associated with `key`. O(1).
  372. *
  373. * Requirements:
  374. *
  375. * - `key` must be in the map.
  376. */
  377. function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
  378. return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
  379. }
  380. /**
  381. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  382. *
  383. * CAUTION: This function is deprecated because it requires allocating memory for the error
  384. * message unnecessarily. For custom revert reasons use {tryGet}.
  385. */
  386. function get(
  387. AddressToUintMap storage map,
  388. address key,
  389. string memory errorMessage
  390. ) internal view returns (uint256) {
  391. return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
  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(
  405. Bytes32ToUintMap storage map,
  406. bytes32 key,
  407. uint256 value
  408. ) internal returns (bool) {
  409. return set(map._inner, key, bytes32(value));
  410. }
  411. /**
  412. * @dev Removes a value from a set. O(1).
  413. *
  414. * Returns true if the key was removed from the map, that is if it was present.
  415. */
  416. function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
  417. return remove(map._inner, key);
  418. }
  419. /**
  420. * @dev Returns true if the key is in the map. O(1).
  421. */
  422. function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
  423. return contains(map._inner, key);
  424. }
  425. /**
  426. * @dev Returns the number of elements in the map. O(1).
  427. */
  428. function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
  429. return length(map._inner);
  430. }
  431. /**
  432. * @dev Returns the element stored at position `index` in the set. O(1).
  433. * Note that there are no guarantees on the ordering of values inside the
  434. * array, and it may change when more values are added or removed.
  435. *
  436. * Requirements:
  437. *
  438. * - `index` must be strictly less than {length}.
  439. */
  440. function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
  441. (bytes32 key, bytes32 value) = at(map._inner, index);
  442. return (key, uint256(value));
  443. }
  444. /**
  445. * @dev Tries to returns the value associated with `key`. O(1).
  446. * Does not revert if `key` is not in the map.
  447. */
  448. function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
  449. (bool success, bytes32 value) = tryGet(map._inner, key);
  450. return (success, uint256(value));
  451. }
  452. /**
  453. * @dev Returns the value associated with `key`. O(1).
  454. *
  455. * Requirements:
  456. *
  457. * - `key` must be in the map.
  458. */
  459. function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
  460. return uint256(get(map._inner, key));
  461. }
  462. /**
  463. * @dev Same as {get}, with a custom error message when `key` is not in the map.
  464. *
  465. * CAUTION: This function is deprecated because it requires allocating memory for the error
  466. * message unnecessarily. For custom revert reasons use {tryGet}.
  467. */
  468. function get(
  469. Bytes32ToUintMap storage map,
  470. bytes32 key,
  471. string memory errorMessage
  472. ) internal view returns (uint256) {
  473. return uint256(get(map._inner, key, errorMessage));
  474. }
  475. }