EnumerableMap.sol 17 KB

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