From 4c2dadc7055a21c6ae177e2c1af55709f611f66d Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 21:39:26 +0000 Subject: [PATCH 1/7] Add capped variants to EnumerableMapLib --- prep/gen-enumerable-map-lib.js | 10 ++ src/utils/EnumerableMapLib.sol | 198 +++++++++++++++++++++++++++++++ src/utils/g/EnumerableMapLib.sol | 198 +++++++++++++++++++++++++++++++ 3 files changed, 406 insertions(+) diff --git a/prep/gen-enumerable-map-lib.js b/prep/gen-enumerable-map-lib.js index 416b358435..53c5d2a423 100644 --- a/prep/gen-enumerable-map-lib.js +++ b/prep/gen-enumerable-map-lib.js @@ -26,11 +26,21 @@ async function main() { s += 'function set(' + mt + ' storage map, ' + f + ' key, ' + t + ' value) internal returns (bool) {\n'; s += 'map._values[key] = value;\nreturn EnumerableSetLib.add(map._keys, key);\n}\n\n'; + s += '/// @dev Adds a key-value pair to the map, or updates the value for an existing key.\n'; + s += '/// Returns true if `key` was added to the map, that is if it was not already present.\n'; + s += '/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.\n'; + s += 'function set(' + mt + ' storage map, ' + f + ' key, ' + t + ' value, uint256 cap) internal returns (bool) {\n'; + s += 'map._values[key] = value;\nreturn EnumerableSetLib.add(map._keys, key, cap);\n}\n\n'; + s += '/// @dev Removes a key-value pair from the map.\n'; s += '/// Returns true if `key` was removed from the map, that is if it was present.\n'; s += 'function remove(' + mt + ' storage map, ' + f + ' key) internal returns (bool) {\n'; s += 'delete map._values[key];\nreturn EnumerableSetLib.remove(map._keys, key);\n}\n\n'; + s += '/// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`.\n' + s += 'function update(' + mt+ ' storage map, ' + f + ' key, ' + t + ' value, bool isAdd, uint256 cap) internal returns (bool) {\n'; + s += 'return isAdd ? set(map, key, value, cap) : remove(map, key);\n}\n\n'; + s += '/// @dev Returns true if the key is in the map.\n'; s += 'function contains(' + mt + ' storage map, ' + f + ' key) internal view returns (bool) {\n'; s += 'return EnumerableSetLib.contains(map._keys, key);\n}\n\n'; diff --git a/src/utils/EnumerableMapLib.sol b/src/utils/EnumerableMapLib.sol index 9681411568..e94ba2799f 100644 --- a/src/utils/EnumerableMapLib.sol +++ b/src/utils/EnumerableMapLib.sol @@ -86,6 +86,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) { @@ -93,6 +104,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToBytes32Map storage map, + bytes32 key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -145,6 +167,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToUint256Map storage map, bytes32 key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToUint256Map storage map, bytes32 key) internal returns (bool) { @@ -152,6 +185,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToUint256Map storage map, + bytes32 key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToUint256Map storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -204,6 +248,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToAddressMap storage map, bytes32 key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) { @@ -211,6 +266,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToAddressMap storage map, + bytes32 key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -263,6 +329,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToBytes32Map storage map, uint256 key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToBytes32Map storage map, uint256 key) internal returns (bool) { @@ -270,6 +347,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToBytes32Map storage map, + uint256 key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToBytes32Map storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -322,6 +410,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToUint256Map storage map, uint256 key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToUint256Map storage map, uint256 key) internal returns (bool) { @@ -329,6 +428,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToUint256Map storage map, + uint256 key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToUint256Map storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -381,6 +491,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToAddressMap storage map, uint256 key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToAddressMap storage map, uint256 key) internal returns (bool) { @@ -388,6 +509,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToAddressMap storage map, + uint256 key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToAddressMap storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -440,6 +572,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToBytes32Map storage map, address key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToBytes32Map storage map, address key) internal returns (bool) { @@ -447,6 +590,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToBytes32Map storage map, + address key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -499,6 +653,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToUint256Map storage map, address key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToUint256Map storage map, address key) internal returns (bool) { @@ -506,6 +671,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToUint256Map storage map, + address key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToUint256Map storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -558,6 +734,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToAddressMap storage map, address key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToAddressMap storage map, address key) internal returns (bool) { @@ -565,6 +752,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToAddressMap storage map, + address key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToAddressMap storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); diff --git a/src/utils/g/EnumerableMapLib.sol b/src/utils/g/EnumerableMapLib.sol index e2c91834a7..b4a5e3c140 100644 --- a/src/utils/g/EnumerableMapLib.sol +++ b/src/utils/g/EnumerableMapLib.sol @@ -98,6 +98,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) { @@ -105,6 +116,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToBytes32Map storage map, + bytes32 key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -157,6 +179,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToUint256Map storage map, bytes32 key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToUint256Map storage map, bytes32 key) internal returns (bool) { @@ -164,6 +197,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToUint256Map storage map, + bytes32 key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToUint256Map storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -216,6 +260,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Bytes32ToAddressMap storage map, bytes32 key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) { @@ -223,6 +278,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Bytes32ToAddressMap storage map, + bytes32 key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -275,6 +341,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToBytes32Map storage map, uint256 key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToBytes32Map storage map, uint256 key) internal returns (bool) { @@ -282,6 +359,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToBytes32Map storage map, + uint256 key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToBytes32Map storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -334,6 +422,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToUint256Map storage map, uint256 key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToUint256Map storage map, uint256 key) internal returns (bool) { @@ -341,6 +440,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToUint256Map storage map, + uint256 key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToUint256Map storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -393,6 +503,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(Uint256ToAddressMap storage map, uint256 key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(Uint256ToAddressMap storage map, uint256 key) internal returns (bool) { @@ -400,6 +521,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + Uint256ToAddressMap storage map, + uint256 key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(Uint256ToAddressMap storage map, uint256 key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -452,6 +584,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToBytes32Map storage map, address key, bytes32 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToBytes32Map storage map, address key) internal returns (bool) { @@ -459,6 +602,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToBytes32Map storage map, + address key, + bytes32 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -511,6 +665,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToUint256Map storage map, address key, uint256 value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToUint256Map storage map, address key) internal returns (bool) { @@ -518,6 +683,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToUint256Map storage map, + address key, + uint256 value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToUint256Map storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); @@ -570,6 +746,17 @@ library EnumerableMapLib { return EnumerableSetLib.add(map._keys, key); } + /// @dev Adds a key-value pair to the map, or updates the value for an existing key. + /// Returns true if `key` was added to the map, that is if it was not already present. + /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + function set(AddressToAddressMap storage map, address key, address value, uint256 cap) + internal + returns (bool) + { + map._values[key] = value; + return EnumerableSetLib.add(map._keys, key, cap); + } + /// @dev Removes a key-value pair from the map. /// Returns true if `key` was removed from the map, that is if it was present. function remove(AddressToAddressMap storage map, address key) internal returns (bool) { @@ -577,6 +764,17 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } + /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + function update( + AddressToAddressMap storage map, + address key, + address value, + bool isAdd, + uint256 cap + ) internal returns (bool) { + return isAdd ? set(map, key, value, cap) : remove(map, key); + } + /// @dev Returns true if the key is in the map. function contains(AddressToAddressMap storage map, address key) internal view returns (bool) { return EnumerableSetLib.contains(map._keys, key); From 7b871ab0e3907135f2bb1664d331d39d5f564503 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 21:40:43 +0000 Subject: [PATCH 2/7] Regen docs --- docs/utils/enumerablemaplib.md | 261 +++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) diff --git a/docs/utils/enumerablemaplib.md b/docs/utils/enumerablemaplib.md index edd0483033..1b43d371c2 100644 --- a/docs/utils/enumerablemaplib.md +++ b/docs/utils/enumerablemaplib.md @@ -133,6 +133,21 @@ function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Bytes32ToBytes32Map,bytes32,bytes32,uint256) + +```solidity +function set( + Bytes32ToBytes32Map storage map, + bytes32 key, + bytes32 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Bytes32ToBytes32Map,bytes32) ```solidity @@ -144,6 +159,20 @@ function remove(Bytes32ToBytes32Map storage map, bytes32 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Bytes32ToBytes32Map,bytes32,bytes32,bool,uint256) + +```solidity +function update( + Bytes32ToBytes32Map storage map, + bytes32 key, + bytes32 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Bytes32ToBytes32Map,bytes32) ```solidity @@ -221,6 +250,21 @@ function set(Bytes32ToUint256Map storage map, bytes32 key, uint256 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Bytes32ToUint256Map,bytes32,uint256,uint256) + +```solidity +function set( + Bytes32ToUint256Map storage map, + bytes32 key, + uint256 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Bytes32ToUint256Map,bytes32) ```solidity @@ -232,6 +276,20 @@ function remove(Bytes32ToUint256Map storage map, bytes32 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Bytes32ToUint256Map,bytes32,uint256,bool,uint256) + +```solidity +function update( + Bytes32ToUint256Map storage map, + bytes32 key, + uint256 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Bytes32ToUint256Map,bytes32) ```solidity @@ -309,6 +367,21 @@ function set(Bytes32ToAddressMap storage map, bytes32 key, address value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Bytes32ToAddressMap,bytes32,address,uint256) + +```solidity +function set( + Bytes32ToAddressMap storage map, + bytes32 key, + address value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Bytes32ToAddressMap,bytes32) ```solidity @@ -320,6 +393,20 @@ function remove(Bytes32ToAddressMap storage map, bytes32 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Bytes32ToAddressMap,bytes32,address,bool,uint256) + +```solidity +function update( + Bytes32ToAddressMap storage map, + bytes32 key, + address value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Bytes32ToAddressMap,bytes32) ```solidity @@ -397,6 +484,21 @@ function set(Uint256ToBytes32Map storage map, uint256 key, bytes32 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Uint256ToBytes32Map,uint256,bytes32,uint256) + +```solidity +function set( + Uint256ToBytes32Map storage map, + uint256 key, + bytes32 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Uint256ToBytes32Map,uint256) ```solidity @@ -408,6 +510,20 @@ function remove(Uint256ToBytes32Map storage map, uint256 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Uint256ToBytes32Map,uint256,bytes32,bool,uint256) + +```solidity +function update( + Uint256ToBytes32Map storage map, + uint256 key, + bytes32 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Uint256ToBytes32Map,uint256) ```solidity @@ -485,6 +601,21 @@ function set(Uint256ToUint256Map storage map, uint256 key, uint256 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Uint256ToUint256Map,uint256,uint256,uint256) + +```solidity +function set( + Uint256ToUint256Map storage map, + uint256 key, + uint256 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Uint256ToUint256Map,uint256) ```solidity @@ -496,6 +627,20 @@ function remove(Uint256ToUint256Map storage map, uint256 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Uint256ToUint256Map,uint256,uint256,bool,uint256) + +```solidity +function update( + Uint256ToUint256Map storage map, + uint256 key, + uint256 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Uint256ToUint256Map,uint256) ```solidity @@ -573,6 +718,21 @@ function set(Uint256ToAddressMap storage map, uint256 key, address value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(Uint256ToAddressMap,uint256,address,uint256) + +```solidity +function set( + Uint256ToAddressMap storage map, + uint256 key, + address value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(Uint256ToAddressMap,uint256) ```solidity @@ -584,6 +744,20 @@ function remove(Uint256ToAddressMap storage map, uint256 key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(Uint256ToAddressMap,uint256,address,bool,uint256) + +```solidity +function update( + Uint256ToAddressMap storage map, + uint256 key, + address value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(Uint256ToAddressMap,uint256) ```solidity @@ -661,6 +835,21 @@ function set(AddressToBytes32Map storage map, address key, bytes32 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(AddressToBytes32Map,address,bytes32,uint256) + +```solidity +function set( + AddressToBytes32Map storage map, + address key, + bytes32 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(AddressToBytes32Map,address) ```solidity @@ -672,6 +861,20 @@ function remove(AddressToBytes32Map storage map, address key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(AddressToBytes32Map,address,bytes32,bool,uint256) + +```solidity +function update( + AddressToBytes32Map storage map, + address key, + bytes32 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(AddressToBytes32Map,address) ```solidity @@ -749,6 +952,21 @@ function set(AddressToUint256Map storage map, address key, uint256 value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(AddressToUint256Map,address,uint256,uint256) + +```solidity +function set( + AddressToUint256Map storage map, + address key, + uint256 value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(AddressToUint256Map,address) ```solidity @@ -760,6 +978,20 @@ function remove(AddressToUint256Map storage map, address key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(AddressToUint256Map,address,uint256,bool,uint256) + +```solidity +function update( + AddressToUint256Map storage map, + address key, + uint256 value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(AddressToUint256Map,address) ```solidity @@ -837,6 +1069,21 @@ function set(AddressToAddressMap storage map, address key, address value) Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. +### set(AddressToAddressMap,address,address,uint256) + +```solidity +function set( + AddressToAddressMap storage map, + address key, + address value, + uint256 cap +) internal returns (bool) +``` + +Adds a key-value pair to the map, or updates the value for an existing key. +Returns true if `key` was added to the map, that is if it was not already present. +Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + ### remove(AddressToAddressMap,address) ```solidity @@ -848,6 +1095,20 @@ function remove(AddressToAddressMap storage map, address key) Removes a key-value pair from the map. Returns true if `key` was removed from the map, that is if it was present. +### update(AddressToAddressMap,address,address,bool,uint256) + +```solidity +function update( + AddressToAddressMap storage map, + address key, + address value, + bool isAdd, + uint256 cap +) internal returns (bool) +``` + +Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + ### contains(AddressToAddressMap,address) ```solidity From 14e39844ff4fd4d4d20f046ce891cc9848ceb0ac Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 21:46:23 +0000 Subject: [PATCH 3/7] Regen docs --- docs/utils/enumerablemaplib.md | 36 ++++++++++++++++---------------- prep/gen-enumerable-map-lib.js | 4 ++-- src/utils/EnumerableMapLib.sol | 36 ++++++++++++++++---------------- src/utils/g/EnumerableMapLib.sol | 36 ++++++++++++++++---------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/docs/utils/enumerablemaplib.md b/docs/utils/enumerablemaplib.md index 1b43d371c2..4e471f8a81 100644 --- a/docs/utils/enumerablemaplib.md +++ b/docs/utils/enumerablemaplib.md @@ -146,7 +146,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Bytes32ToBytes32Map,bytes32) @@ -171,7 +171,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Bytes32ToBytes32Map,bytes32) @@ -263,7 +263,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Bytes32ToUint256Map,bytes32) @@ -288,7 +288,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Bytes32ToUint256Map,bytes32) @@ -380,7 +380,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Bytes32ToAddressMap,bytes32) @@ -405,7 +405,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Bytes32ToAddressMap,bytes32) @@ -497,7 +497,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Uint256ToBytes32Map,uint256) @@ -522,7 +522,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Uint256ToBytes32Map,uint256) @@ -614,7 +614,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Uint256ToUint256Map,uint256) @@ -639,7 +639,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Uint256ToUint256Map,uint256) @@ -731,7 +731,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(Uint256ToAddressMap,uint256) @@ -756,7 +756,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(Uint256ToAddressMap,uint256) @@ -848,7 +848,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(AddressToBytes32Map,address) @@ -873,7 +873,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(AddressToBytes32Map,address) @@ -965,7 +965,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(AddressToUint256Map,address) @@ -990,7 +990,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(AddressToUint256Map,address) @@ -1082,7 +1082,7 @@ function set( Adds a key-value pair to the map, or updates the value for an existing key. Returns true if `key` was added to the map, that is if it was not already present. -Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. +Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. ### remove(AddressToAddressMap,address) @@ -1107,7 +1107,7 @@ function update( ) internal returns (bool) ``` -Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. +Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. ### contains(AddressToAddressMap,address) diff --git a/prep/gen-enumerable-map-lib.js b/prep/gen-enumerable-map-lib.js index 53c5d2a423..35bc8513d3 100644 --- a/prep/gen-enumerable-map-lib.js +++ b/prep/gen-enumerable-map-lib.js @@ -28,7 +28,7 @@ async function main() { s += '/// @dev Adds a key-value pair to the map, or updates the value for an existing key.\n'; s += '/// Returns true if `key` was added to the map, that is if it was not already present.\n'; - s += '/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.\n'; + s += '/// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`.\n'; s += 'function set(' + mt + ' storage map, ' + f + ' key, ' + t + ' value, uint256 cap) internal returns (bool) {\n'; s += 'map._values[key] = value;\nreturn EnumerableSetLib.add(map._keys, key, cap);\n}\n\n'; @@ -37,7 +37,7 @@ async function main() { s += 'function remove(' + mt + ' storage map, ' + f + ' key) internal returns (bool) {\n'; s += 'delete map._values[key];\nreturn EnumerableSetLib.remove(map._keys, key);\n}\n\n'; - s += '/// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`.\n' + s += '/// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`.\n' s += 'function update(' + mt+ ' storage map, ' + f + ' key, ' + t + ' value, bool isAdd, uint256 cap) internal returns (bool) {\n'; s += 'return isAdd ? set(map, key, value, cap) : remove(map, key);\n}\n\n'; diff --git a/src/utils/EnumerableMapLib.sol b/src/utils/EnumerableMapLib.sol index e94ba2799f..57154c6d02 100644 --- a/src/utils/EnumerableMapLib.sol +++ b/src/utils/EnumerableMapLib.sol @@ -88,7 +88,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value, uint256 cap) internal returns (bool) @@ -104,7 +104,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToBytes32Map storage map, bytes32 key, @@ -169,7 +169,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToUint256Map storage map, bytes32 key, uint256 value, uint256 cap) internal returns (bool) @@ -185,7 +185,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToUint256Map storage map, bytes32 key, @@ -250,7 +250,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToAddressMap storage map, bytes32 key, address value, uint256 cap) internal returns (bool) @@ -266,7 +266,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToAddressMap storage map, bytes32 key, @@ -331,7 +331,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToBytes32Map storage map, uint256 key, bytes32 value, uint256 cap) internal returns (bool) @@ -347,7 +347,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToBytes32Map storage map, uint256 key, @@ -412,7 +412,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToUint256Map storage map, uint256 key, uint256 value, uint256 cap) internal returns (bool) @@ -428,7 +428,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToUint256Map storage map, uint256 key, @@ -493,7 +493,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToAddressMap storage map, uint256 key, address value, uint256 cap) internal returns (bool) @@ -509,7 +509,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToAddressMap storage map, uint256 key, @@ -574,7 +574,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToBytes32Map storage map, address key, bytes32 value, uint256 cap) internal returns (bool) @@ -590,7 +590,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToBytes32Map storage map, address key, @@ -655,7 +655,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToUint256Map storage map, address key, uint256 value, uint256 cap) internal returns (bool) @@ -671,7 +671,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToUint256Map storage map, address key, @@ -736,7 +736,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToAddressMap storage map, address key, address value, uint256 cap) internal returns (bool) @@ -752,7 +752,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToAddressMap storage map, address key, diff --git a/src/utils/g/EnumerableMapLib.sol b/src/utils/g/EnumerableMapLib.sol index b4a5e3c140..8a7f961a97 100644 --- a/src/utils/g/EnumerableMapLib.sol +++ b/src/utils/g/EnumerableMapLib.sol @@ -100,7 +100,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value, uint256 cap) internal returns (bool) @@ -116,7 +116,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToBytes32Map storage map, bytes32 key, @@ -181,7 +181,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToUint256Map storage map, bytes32 key, uint256 value, uint256 cap) internal returns (bool) @@ -197,7 +197,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToUint256Map storage map, bytes32 key, @@ -262,7 +262,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Bytes32ToAddressMap storage map, bytes32 key, address value, uint256 cap) internal returns (bool) @@ -278,7 +278,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Bytes32ToAddressMap storage map, bytes32 key, @@ -343,7 +343,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToBytes32Map storage map, uint256 key, bytes32 value, uint256 cap) internal returns (bool) @@ -359,7 +359,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToBytes32Map storage map, uint256 key, @@ -424,7 +424,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToUint256Map storage map, uint256 key, uint256 value, uint256 cap) internal returns (bool) @@ -440,7 +440,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToUint256Map storage map, uint256 key, @@ -505,7 +505,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(Uint256ToAddressMap storage map, uint256 key, address value, uint256 cap) internal returns (bool) @@ -521,7 +521,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( Uint256ToAddressMap storage map, uint256 key, @@ -586,7 +586,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToBytes32Map storage map, address key, bytes32 value, uint256 cap) internal returns (bool) @@ -602,7 +602,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToBytes32Map storage map, address key, @@ -667,7 +667,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToUint256Map storage map, address key, uint256 value, uint256 cap) internal returns (bool) @@ -683,7 +683,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToUint256Map storage map, address key, @@ -748,7 +748,7 @@ library EnumerableMapLib { /// @dev Adds a key-value pair to the map, or updates the value for an existing key. /// Returns true if `key` was added to the map, that is if it was not already present. - /// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`. + /// Reverts if the map grows bigger than the custom on-the-fly capacity `cap`. function set(AddressToAddressMap storage map, address key, address value, uint256 cap) internal returns (bool) @@ -764,7 +764,7 @@ library EnumerableMapLib { return EnumerableSetLib.remove(map._keys, key); } - /// @dev Shorthand for `isAdd ? map.set(value, cap) : map.remove(value)`. + /// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`. function update( AddressToAddressMap storage map, address key, From e809aab7333f4bcd88d962b11580c4b306d6f2eb Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 21:47:45 +0000 Subject: [PATCH 4/7] T --- prep/gen-enumerable-map-lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prep/gen-enumerable-map-lib.js b/prep/gen-enumerable-map-lib.js index 35bc8513d3..5d32be28b5 100644 --- a/prep/gen-enumerable-map-lib.js +++ b/prep/gen-enumerable-map-lib.js @@ -38,7 +38,7 @@ async function main() { s += 'delete map._values[key];\nreturn EnumerableSetLib.remove(map._keys, key);\n}\n\n'; s += '/// @dev Shorthand for `isAdd ? map.set(key, value, cap) : map.remove(key)`.\n' - s += 'function update(' + mt+ ' storage map, ' + f + ' key, ' + t + ' value, bool isAdd, uint256 cap) internal returns (bool) {\n'; + s += 'function update(' + mt + ' storage map, ' + f + ' key, ' + t + ' value, bool isAdd, uint256 cap) internal returns (bool) {\n'; s += 'return isAdd ? set(map, key, value, cap) : remove(map, key);\n}\n\n'; s += '/// @dev Returns true if the key is in the map.\n'; From 9a8018ab17952d4a3e50cdd7bbdc8cf55673f0ce Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 21:57:22 +0000 Subject: [PATCH 5/7] T --- test/EnumerableMapLib.t.sol | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/EnumerableMapLib.t.sol b/test/EnumerableMapLib.t.sol index c29fe0557d..f17f54679a 100644 --- a/test/EnumerableMapLib.t.sol +++ b/test/EnumerableMapLib.t.sol @@ -78,4 +78,31 @@ contract EnumerableMapLibTest is SoladyTest { function get(address key) public view returns (uint256) { return map.get(key); } + + function testMapUpdate(bytes32) public { + uint256 cap = _bound(_random(), 0, 5); + for (uint256 i; i < cap; ++i) { + this.update(address(uint160(i)), i, true, cap); + } + vm.expectRevert(bytes4(keccak256("ExceedsCapacity()"))); + this.update(address(uint160(cap)), _random(), true, cap); + + if (_randomChance(2)) { + for (uint256 i; i < cap; ++i) { + this.update(address(uint160(i)), i, true, cap); + } + } + + for (uint256 i; i < cap; ++i) { + assertEq(map.get(address(uint160(i))), i); + } + for (uint256 i; i < cap; ++i) { + this.update(address(uint160(i)), i, false, cap); + assertEq(map.keys().length, cap - 1 - i); + } + } + + function update(address key, uint256 value, bool isAdd, uint256 cap) public { + map.update(key, value, isAdd, cap); + } } From 30368d18d9fa852923e62159ef37d817bf777635 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 22:00:31 +0000 Subject: [PATCH 6/7] T --- test/EnumerableMapLib.t.sol | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/EnumerableMapLib.t.sol b/test/EnumerableMapLib.t.sol index f17f54679a..7b5d80adc3 100644 --- a/test/EnumerableMapLib.t.sol +++ b/test/EnumerableMapLib.t.sol @@ -79,8 +79,13 @@ contract EnumerableMapLibTest is SoladyTest { return map.get(key); } - function testMapUpdate(bytes32) public { - uint256 cap = _bound(_random(), 0, 5); + function testMapUpdate() public { + for (uint256 i; i < 10; ++i) { + _testMapUpdate(i); + } + } + + function _testMapUpdate(uint256 cap) internal { for (uint256 i; i < cap; ++i) { this.update(address(uint160(i)), i, true, cap); } @@ -98,7 +103,11 @@ contract EnumerableMapLibTest is SoladyTest { } for (uint256 i; i < cap; ++i) { this.update(address(uint160(i)), i, false, cap); - assertEq(map.keys().length, cap - 1 - i); + address[] memory keys = map.keys(); + assertEq(keys.length, cap - 1 - i); + for (uint256 j; j < keys.length; ++j) { + assertEq(map.get(keys[j]), uint160(keys[j])); + } } } From 6992216fd6f141d13f73833dc4707107ed8414f4 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 27 Jun 2025 22:01:08 +0000 Subject: [PATCH 7/7] T --- test/EnumerableMapLib.t.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/EnumerableMapLib.t.sol b/test/EnumerableMapLib.t.sol index 7b5d80adc3..bb018b23e5 100644 --- a/test/EnumerableMapLib.t.sol +++ b/test/EnumerableMapLib.t.sol @@ -92,10 +92,8 @@ contract EnumerableMapLibTest is SoladyTest { vm.expectRevert(bytes4(keccak256("ExceedsCapacity()"))); this.update(address(uint160(cap)), _random(), true, cap); - if (_randomChance(2)) { - for (uint256 i; i < cap; ++i) { - this.update(address(uint160(i)), i, true, cap); - } + for (uint256 i; i < cap; ++i) { + this.update(address(uint160(i)), i, true, cap); } for (uint256 i; i < cap; ++i) {