123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.3.0) (utils/structs/EnumerableMap.sol)
- // This file was procedurally generated from scripts/generate/templates/EnumerableMap.js.
- pragma solidity ^0.8.20;
- import {EnumerableSet} from "./EnumerableSet.sol";
- /**
- * @dev Library for managing an enumerable variant of Solidity's
- * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
- * type.
- *
- * Maps have the following properties:
- *
- * - Entries are added, removed, and checked for existence in constant time
- * (O(1)).
- * - Entries are enumerated in O(n). No guarantees are made on the ordering.
- * - Map can be cleared (all entries removed) in O(n).
- *
- * ```solidity
- * contract Example {
- * // Add the library methods
- * using EnumerableMap for EnumerableMap.UintToAddressMap;
- *
- * // Declare a set state variable
- * EnumerableMap.UintToAddressMap private myMap;
- * }
- * ```
- *
- * The following map types are supported:
- *
- * - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
- * - `address -> uint256` (`AddressToUintMap`) since v4.6.0
- * - `bytes32 -> bytes32` (`Bytes32ToBytes32Map`) since v4.6.0
- * - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
- * - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
- * - `uint256 -> bytes32` (`UintToBytes32Map`) since v5.1.0
- * - `address -> address` (`AddressToAddressMap`) since v5.1.0
- * - `address -> bytes32` (`AddressToBytes32Map`) since v5.1.0
- * - `bytes32 -> address` (`Bytes32ToAddressMap`) since v5.1.0
- * - `bytes -> bytes` (`BytesToBytesMap`) since v5.4.0
- *
- * [WARNING]
- * ====
- * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
- * unusable.
- * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
- *
- * 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.
- * ====
- */
- library EnumerableMap {
- using EnumerableSet for *;
- // To implement this library for multiple types with as little code repetition as possible, we write it in
- // terms of a generic Map type with bytes32 keys and values. The Map implementation uses private functions,
- // and user-facing implementations such as `UintToAddressMap` are just wrappers around the underlying Map.
- // This means that we can only create new EnumerableMaps for types that fit in bytes32.
- /**
- * @dev Query for a nonexistent map key.
- */
- error EnumerableMapNonexistentKey(bytes32 key);
- struct Bytes32ToBytes32Map {
- // Storage of keys
- EnumerableSet.Bytes32Set _keys;
- mapping(bytes32 key => bytes32) _values;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value) internal returns (bool) {
- map._values[key] = value;
- return map._keys.add(key);
- }
- /**
- * @dev Removes a key-value pair from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
- delete map._values[key];
- return map._keys.remove(key);
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(Bytes32ToBytes32Map storage map) internal {
- uint256 len = length(map);
- for (uint256 i = 0; i < len; ++i) {
- delete map._values[map._keys.at(i)];
- }
- map._keys.clear();
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
- return map._keys.contains(key);
- }
- /**
- * @dev Returns the number of key-value pairs in the map. O(1).
- */
- function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
- return map._keys.length();
- }
- /**
- * @dev Returns the key-value pair stored at position `index` in the map. O(1).
- *
- * Note that there are no guarantees on the ordering of entries inside the
- * array, and it may change when more entries are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32 key, bytes32 value) {
- bytes32 atKey = map._keys.at(index);
- return (atKey, map._values[atKey]);
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool exists, bytes32 value) {
- bytes32 val = map._values[key];
- if (val == bytes32(0)) {
- return (contains(map, key), bytes32(0));
- } else {
- return (true, val);
- }
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
- bytes32 value = map._values[key];
- if (value == 0 && !contains(map, key)) {
- revert EnumerableMapNonexistentKey(key);
- }
- return value;
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(Bytes32ToBytes32Map storage map) internal view returns (bytes32[] memory) {
- return map._keys.values();
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(
- Bytes32ToBytes32Map storage map,
- uint256 start,
- uint256 end
- ) internal view returns (bytes32[] memory) {
- return map._keys.values(start, end);
- }
- // UintToUintMap
- struct UintToUintMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
- return set(map._inner, bytes32(key), bytes32(value));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
- return remove(map._inner, bytes32(key));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(UintToUintMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
- return contains(map._inner, bytes32(key));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(UintToUintMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(UintToUintMap storage map, uint256 index) internal view returns (uint256 key, uint256 value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (uint256(atKey), uint256(val));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool exists, uint256 value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
- return (success, uint256(val));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
- return uint256(get(map._inner, bytes32(key)));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToUintMap storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // UintToAddressMap
- struct UintToAddressMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
- return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
- return remove(map._inner, bytes32(key));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(UintToAddressMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
- return contains(map._inner, bytes32(key));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(UintToAddressMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256 key, address value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (uint256(atKey), address(uint160(uint256(val))));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool exists, address value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
- return (success, address(uint160(uint256(val))));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
- return address(uint160(uint256(get(map._inner, bytes32(key)))));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToAddressMap storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // UintToBytes32Map
- struct UintToBytes32Map {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(UintToBytes32Map storage map, uint256 key, bytes32 value) internal returns (bool) {
- return set(map._inner, bytes32(key), value);
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(UintToBytes32Map storage map, uint256 key) internal returns (bool) {
- return remove(map._inner, bytes32(key));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(UintToBytes32Map storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(UintToBytes32Map storage map, uint256 key) internal view returns (bool) {
- return contains(map._inner, bytes32(key));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(UintToBytes32Map storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(UintToBytes32Map storage map, uint256 index) internal view returns (uint256 key, bytes32 value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (uint256(atKey), val);
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(UintToBytes32Map storage map, uint256 key) internal view returns (bool exists, bytes32 value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
- return (success, val);
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(UintToBytes32Map storage map, uint256 key) internal view returns (bytes32) {
- return get(map._inner, bytes32(key));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToBytes32Map storage map) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(UintToBytes32Map storage map, uint256 start, uint256 end) internal view returns (uint256[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- uint256[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // AddressToUintMap
- struct AddressToUintMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
- return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(AddressToUintMap storage map, address key) internal returns (bool) {
- return remove(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(AddressToUintMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
- return contains(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(AddressToUintMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(AddressToUintMap storage map, uint256 index) internal view returns (address key, uint256 value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (address(uint160(uint256(atKey))), uint256(val));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(AddressToUintMap storage map, address key) internal view returns (bool exists, uint256 value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
- return (success, uint256(val));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
- return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(AddressToUintMap storage map, uint256 start, uint256 end) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // AddressToAddressMap
- struct AddressToAddressMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(AddressToAddressMap storage map, address key, address value) internal returns (bool) {
- return set(map._inner, bytes32(uint256(uint160(key))), bytes32(uint256(uint160(value))));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(AddressToAddressMap storage map, address key) internal returns (bool) {
- return remove(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(AddressToAddressMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(AddressToAddressMap storage map, address key) internal view returns (bool) {
- return contains(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(AddressToAddressMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(AddressToAddressMap storage map, uint256 index) internal view returns (address key, address value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (address(uint160(uint256(atKey))), address(uint160(uint256(val))));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(AddressToAddressMap storage map, address key) internal view returns (bool exists, address value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
- return (success, address(uint160(uint256(val))));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(AddressToAddressMap storage map, address key) internal view returns (address) {
- return address(uint160(uint256(get(map._inner, bytes32(uint256(uint160(key)))))));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(AddressToAddressMap storage map) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(
- AddressToAddressMap storage map,
- uint256 start,
- uint256 end
- ) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // AddressToBytes32Map
- struct AddressToBytes32Map {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(AddressToBytes32Map storage map, address key, bytes32 value) internal returns (bool) {
- return set(map._inner, bytes32(uint256(uint160(key))), value);
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(AddressToBytes32Map storage map, address key) internal returns (bool) {
- return remove(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(AddressToBytes32Map storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) {
- return contains(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(AddressToBytes32Map storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(AddressToBytes32Map storage map, uint256 index) internal view returns (address key, bytes32 value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (address(uint160(uint256(atKey))), val);
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(AddressToBytes32Map storage map, address key) internal view returns (bool exists, bytes32 value) {
- (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
- return (success, val);
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(AddressToBytes32Map storage map, address key) internal view returns (bytes32) {
- return get(map._inner, bytes32(uint256(uint160(key))));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(AddressToBytes32Map storage map) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(
- AddressToBytes32Map storage map,
- uint256 start,
- uint256 end
- ) internal view returns (address[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- address[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // Bytes32ToUintMap
- struct Bytes32ToUintMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
- return set(map._inner, key, bytes32(value));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
- return remove(map._inner, key);
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(Bytes32ToUintMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
- return contains(map._inner, key);
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32 key, uint256 value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (atKey, uint256(val));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool exists, uint256 value) {
- (bool success, bytes32 val) = tryGet(map._inner, key);
- return (success, uint256(val));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
- return uint256(get(map._inner, key));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
- bytes32[] memory store = keys(map._inner);
- bytes32[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(Bytes32ToUintMap storage map, uint256 start, uint256 end) internal view returns (bytes32[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- bytes32[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- // Bytes32ToAddressMap
- struct Bytes32ToAddressMap {
- Bytes32ToBytes32Map _inner;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(Bytes32ToAddressMap storage map, bytes32 key, address value) internal returns (bool) {
- return set(map._inner, key, bytes32(uint256(uint160(value))));
- }
- /**
- * @dev Removes a value from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) {
- return remove(map._inner, key);
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(Bytes32ToAddressMap storage map) internal {
- clear(map._inner);
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) {
- return contains(map._inner, key);
- }
- /**
- * @dev Returns the number of elements in the map. O(1).
- */
- function length(Bytes32ToAddressMap storage map) internal view returns (uint256) {
- return length(map._inner);
- }
- /**
- * @dev Returns the element stored at position `index` in the map. O(1).
- * Note that there are no guarantees on the ordering of values inside the
- * array, and it may change when more values are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(Bytes32ToAddressMap storage map, uint256 index) internal view returns (bytes32 key, address value) {
- (bytes32 atKey, bytes32 val) = at(map._inner, index);
- return (atKey, address(uint160(uint256(val))));
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool exists, address value) {
- (bool success, bytes32 val) = tryGet(map._inner, key);
- return (success, address(uint160(uint256(val))));
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (address) {
- return address(uint160(uint256(get(map._inner, key))));
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(Bytes32ToAddressMap storage map) internal view returns (bytes32[] memory) {
- bytes32[] memory store = keys(map._inner);
- bytes32[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(
- Bytes32ToAddressMap storage map,
- uint256 start,
- uint256 end
- ) internal view returns (bytes32[] memory) {
- bytes32[] memory store = keys(map._inner, start, end);
- bytes32[] memory result;
- assembly ("memory-safe") {
- result := store
- }
- return result;
- }
- /**
- * @dev Query for a nonexistent map key.
- */
- error EnumerableMapNonexistentBytesKey(bytes key);
- struct BytesToBytesMap {
- // Storage of keys
- EnumerableSet.BytesSet _keys;
- mapping(bytes key => bytes) _values;
- }
- /**
- * @dev Adds a key-value pair to a map, or updates the value for an existing
- * key. O(1).
- *
- * Returns true if the key was added to the map, that is if it was not
- * already present.
- */
- function set(BytesToBytesMap storage map, bytes memory key, bytes memory value) internal returns (bool) {
- map._values[key] = value;
- return map._keys.add(key);
- }
- /**
- * @dev Removes a key-value pair from a map. O(1).
- *
- * Returns true if the key was removed from the map, that is if it was present.
- */
- function remove(BytesToBytesMap storage map, bytes memory key) internal returns (bool) {
- delete map._values[key];
- return map._keys.remove(key);
- }
- /**
- * @dev Removes all the entries from a map. O(n).
- *
- * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
- * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
- */
- function clear(BytesToBytesMap storage map) internal {
- uint256 len = length(map);
- for (uint256 i = 0; i < len; ++i) {
- delete map._values[map._keys.at(i)];
- }
- map._keys.clear();
- }
- /**
- * @dev Returns true if the key is in the map. O(1).
- */
- function contains(BytesToBytesMap storage map, bytes memory key) internal view returns (bool) {
- return map._keys.contains(key);
- }
- /**
- * @dev Returns the number of key-value pairs in the map. O(1).
- */
- function length(BytesToBytesMap storage map) internal view returns (uint256) {
- return map._keys.length();
- }
- /**
- * @dev Returns the key-value pair stored at position `index` in the map. O(1).
- *
- * Note that there are no guarantees on the ordering of entries inside the
- * array, and it may change when more entries are added or removed.
- *
- * Requirements:
- *
- * - `index` must be strictly less than {length}.
- */
- function at(
- BytesToBytesMap storage map,
- uint256 index
- ) internal view returns (bytes memory key, bytes memory value) {
- key = map._keys.at(index);
- value = map._values[key];
- }
- /**
- * @dev Tries to return the value associated with `key`. O(1).
- * Does not revert if `key` is not in the map.
- */
- function tryGet(
- BytesToBytesMap storage map,
- bytes memory key
- ) internal view returns (bool exists, bytes memory value) {
- value = map._values[key];
- exists = bytes(value).length != 0 || contains(map, key);
- }
- /**
- * @dev Returns the value associated with `key`. O(1).
- *
- * Requirements:
- *
- * - `key` must be in the map.
- */
- function get(BytesToBytesMap storage map, bytes memory key) internal view returns (bytes memory value) {
- bool exists;
- (exists, value) = tryGet(map, key);
- if (!exists) {
- revert EnumerableMapNonexistentBytesKey(key);
- }
- }
- /**
- * @dev Returns an array containing all the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(BytesToBytesMap storage map) internal view returns (bytes[] memory) {
- return map._keys.values();
- }
- /**
- * @dev Returns an array containing a slice of the keys
- *
- * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
- * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
- * this function has an unbounded cost, and using it as part of a state-changing function may render the function
- * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
- */
- function keys(BytesToBytesMap storage map, uint256 start, uint256 end) internal view returns (bytes[] memory) {
- return map._keys.values(start, end);
- }
- }
|