Skip to content

Commit

Permalink
✨ EnumerableSetLib capped add (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Feb 7, 2025
1 parent 3df9c61 commit 5d2520b
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 65 deletions.
80 changes: 65 additions & 15 deletions docs/utils/enumerablesetlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,61 @@ function add(Uint8Set storage set, uint8 value)

Adds `value` to the set. Returns whether `value` was not in the set.

### add(AddressSet,address,uint256)

```solidity
function add(AddressSet storage set, address value, uint256 cap)
internal
returns (bool result)
```

Adds `value` to the set. Returns whether `value` was not in the set.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.

### add(Bytes32Set,bytes32,uint256)

```solidity
function add(Bytes32Set storage set, bytes32 value, uint256 cap)
internal
returns (bool result)
```

Adds `value` to the set. Returns whether `value` was not in the set.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.

### add(Uint256Set,uint256,uint256)

```solidity
function add(Uint256Set storage set, uint256 value, uint256 cap)
internal
returns (bool result)
```

Adds `value` to the set. Returns whether `value` was not in the set.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.

### add(Int256Set,int256,uint256)

```solidity
function add(Int256Set storage set, int256 value, uint256 cap)
internal
returns (bool result)
```

Adds `value` to the set. Returns whether `value` was not in the set.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.

### add(Uint8Set,uint8,uint256)

```solidity
function add(Uint8Set storage set, uint8 value, uint256 cap)
internal
returns (bool result)
```

Adds `value` to the set. Returns whether `value` was not in the set.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.

### remove(AddressSet,address)

```solidity
Expand Down Expand Up @@ -317,11 +372,10 @@ function update(
address value,
bool isAdd,
uint256 cap
) internal returns (bool result)
) internal returns (bool)
```

Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.

### update(Bytes32Set,bytes32,bool,uint256)

Expand All @@ -331,11 +385,10 @@ function update(
bytes32 value,
bool isAdd,
uint256 cap
) internal returns (bool result)
) internal returns (bool)
```

Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.

### update(Uint256Set,uint256,bool,uint256)

Expand All @@ -345,11 +398,10 @@ function update(
uint256 value,
bool isAdd,
uint256 cap
) internal returns (bool result)
) internal returns (bool)
```

Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.

### update(Int256Set,int256,bool,uint256)

Expand All @@ -359,22 +411,20 @@ function update(
int256 value,
bool isAdd,
uint256 cap
) internal returns (bool result)
) internal returns (bool)
```

Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.

### update(Uint8Set,uint8,bool,uint256)

```solidity
function update(Uint8Set storage set, uint8 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
```

Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.

### values(AddressSet)

Expand Down
79 changes: 54 additions & 25 deletions src/utils/EnumerableSetLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,45 @@ library EnumerableSetLib {
}
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(AddressSet storage set, address value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Bytes32Set storage set, bytes32 value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Uint256Set storage set, uint256 value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Int256Set storage set, int256 value, uint256 cap) internal returns (bool result) {
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Uint8Set storage set, uint8 value, uint256 cap) internal returns (bool result) {
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Removes `value` from the set. Returns whether `value` was in the set.
function remove(AddressSet storage set, address value) internal returns (bool result) {
bytes32 rootSlot = _rootSlot(set);
Expand Down Expand Up @@ -498,54 +537,44 @@ library EnumerableSetLib {
}
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(AddressSet storage set, address value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Bytes32Set storage set, bytes32 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Uint256Set storage set, uint256 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Int256Set storage set, int256 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Uint8Set storage set, uint8 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Returns all of the values in the set.
Expand Down
79 changes: 54 additions & 25 deletions src/utils/g/EnumerableSetLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,45 @@ library EnumerableSetLib {
}
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(AddressSet storage set, address value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Bytes32Set storage set, bytes32 value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Uint256Set storage set, uint256 value, uint256 cap)
internal
returns (bool result)
{
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Int256Set storage set, int256 value, uint256 cap) internal returns (bool result) {
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Adds `value` to the set. Returns whether `value` was not in the set.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
function add(Uint8Set storage set, uint8 value, uint256 cap) internal returns (bool result) {
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
}

/// @dev Removes `value` from the set. Returns whether `value` was in the set.
function remove(AddressSet storage set, address value) internal returns (bool result) {
bytes32 rootSlot = _rootSlot(set);
Expand Down Expand Up @@ -506,54 +545,44 @@ library EnumerableSetLib {
}
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(AddressSet storage set, address value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Bytes32Set storage set, bytes32 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Uint256Set storage set, uint256 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Int256Set storage set, int256 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Shorthand for `isAdd ? set.add(value) : set.remove(value)`.
/// Reverts if the set grows bigger than the custom on-the-fly capacity `cap`.
/// @dev Shorthand for `isAdd ? set.add(value, cap) : set.remove(value)`.
function update(Uint8Set storage set, uint8 value, bool isAdd, uint256 cap)
internal
returns (bool result)
returns (bool)
{
if (!isAdd) return remove(set, value);
if (result = add(set, value)) if (length(set) > cap) revert ExceedsCapacity();
return isAdd ? add(set, value, cap) : remove(set, value);
}

/// @dev Returns all of the values in the set.
Expand Down

0 comments on commit 5d2520b

Please sign in to comment.