EnumerableMap.sol 17 KB

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