caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

12 KiB

EnumerableSetLib

Library for managing enumerable sets in storage.

Note:

In many applications, the number of elements in an enumerable set is small. This enumerable set implementation avoids storing the length and indices for up to 3 elements. Once the length exceeds 3 for the first time, the length and indices will be initialized. The amortized cost of adding elements is O(1).

The AddressSet implementation packs the length with the 0th entry.

All enumerable sets except Uint8Set use a pop and swap mechanism to remove elements. This means that the iteration order of elements can change between element removals.

Custom Errors

IndexOutOfBounds()

error IndexOutOfBounds()

The index must be less than the length.

ValueIsZeroSentinel()

error ValueIsZeroSentinel()

The value cannot be the zero sentinel.

ExceedsCapacity()

error ExceedsCapacity()

Cannot accommodate a new unique value with the capacity.

Constants

NOT_FOUND

uint256 internal constant NOT_FOUND = type(uint256).max

The index to represent a value that does not exist.

Structs

AddressSet

struct AddressSet {
    uint256 _spacer;
}

An enumerable address set in storage.

Bytes32Set

struct Bytes32Set {
    uint256 _spacer;
}

An enumerable bytes32 set in storage.

Uint256Set

struct Uint256Set {
    uint256 _spacer;
}

An enumerable uint256 set in storage.

Int256Set

struct Int256Set {
    uint256 _spacer;
}

An enumerable int256 set in storage.

Uint8Set

struct Uint8Set {
    uint256 data;
}

An enumerable uint8 set in storage. Useful for enums.

Getters / Setters

length(AddressSet)

function length(AddressSet storage set)
    internal
    view
    returns (uint256 result)

Returns the number of elements in the set.

length(Bytes32Set)

function length(Bytes32Set storage set)
    internal
    view
    returns (uint256 result)

Returns the number of elements in the set.

length(Uint256Set)

function length(Uint256Set storage set)
    internal
    view
    returns (uint256 result)

Returns the number of elements in the set.

length(Int256Set)

function length(Int256Set storage set)
    internal
    view
    returns (uint256 result)

Returns the number of elements in the set.

length(Uint8Set)

function length(Uint8Set storage set)
    internal
    view
    returns (uint256 result)

Returns the number of elements in the set.

contains(AddressSet,address)

function contains(AddressSet storage set, address value)
    internal
    view
    returns (bool result)

Returns whether value is in the set.

contains(Bytes32Set,bytes32)

function contains(Bytes32Set storage set, bytes32 value)
    internal
    view
    returns (bool result)

Returns whether value is in the set.

contains(Uint256Set,uint256)

function contains(Uint256Set storage set, uint256 value)
    internal
    view
    returns (bool result)

Returns whether value is in the set.

contains(Int256Set,int256)

function contains(Int256Set storage set, int256 value)
    internal
    view
    returns (bool result)

Returns whether value is in the set.

contains(Uint8Set,uint8)

function contains(Uint8Set storage set, uint8 value)
    internal
    view
    returns (bool result)

Returns whether value is in the set.

add(AddressSet,address)

function add(AddressSet storage set, address value)
    internal
    returns (bool result)

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

add(Bytes32Set,bytes32)

function add(Bytes32Set storage set, bytes32 value)
    internal
    returns (bool result)

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

add(Uint256Set,uint256)

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

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

add(Int256Set,int256)

function add(Int256Set storage set, int256 value)
    internal
    returns (bool result)

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

add(Uint8Set,uint8)

function add(Uint8Set storage set, uint8 value)
    internal
    returns (bool result)

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

add(AddressSet,address,uint256)

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)

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)

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)

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)

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)

function remove(AddressSet storage set, address value)
    internal
    returns (bool result)

Removes value from the set. Returns whether value was in the set.

remove(Bytes32Set,bytes32)

function remove(Bytes32Set storage set, bytes32 value)
    internal
    returns (bool result)

Removes value from the set. Returns whether value was in the set.

remove(Uint256Set,uint256)

function remove(Uint256Set storage set, uint256 value)
    internal
    returns (bool result)

Removes value from the set. Returns whether value was in the set.

remove(Int256Set,int256)

function remove(Int256Set storage set, int256 value)
    internal
    returns (bool result)

Removes value from the set. Returns whether value was in the set.

remove(Uint8Set,uint8)

function remove(Uint8Set storage set, uint8 value)
    internal
    returns (bool result)

Removes value from the set. Returns whether value was in the set.

update(AddressSet,address,bool,uint256)

function update(
    AddressSet storage set,
    address value,
    bool isAdd,
    uint256 cap
) internal returns (bool)

Shorthand for isAdd ? set.add(value, cap) : set.remove(value).

update(Bytes32Set,bytes32,bool,uint256)

function update(
    Bytes32Set storage set,
    bytes32 value,
    bool isAdd,
    uint256 cap
) internal returns (bool)

Shorthand for isAdd ? set.add(value, cap) : set.remove(value).

update(Uint256Set,uint256,bool,uint256)

function update(
    Uint256Set storage set,
    uint256 value,
    bool isAdd,
    uint256 cap
) internal returns (bool)

Shorthand for isAdd ? set.add(value, cap) : set.remove(value).

update(Int256Set,int256,bool,uint256)

function update(
    Int256Set storage set,
    int256 value,
    bool isAdd,
    uint256 cap
) internal returns (bool)

Shorthand for isAdd ? set.add(value, cap) : set.remove(value).

update(Uint8Set,uint8,bool,uint256)

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

Shorthand for isAdd ? set.add(value, cap) : set.remove(value).

values(AddressSet)

function values(AddressSet storage set)
    internal
    view
    returns (address[] memory result)

Returns all of the values in the set.
Note: This can consume more gas than the block gas limit for large sets.

values(Bytes32Set)

function values(Bytes32Set storage set)
    internal
    view
    returns (bytes32[] memory result)

Returns all of the values in the set.
Note: This can consume more gas than the block gas limit for large sets.

values(Uint256Set)

function values(Uint256Set storage set)
    internal
    view
    returns (uint256[] memory result)

Returns all of the values in the set.
Note: This can consume more gas than the block gas limit for large sets.

values(Int256Set)

function values(Int256Set storage set)
    internal
    view
    returns (int256[] memory result)

Returns all of the values in the set.
Note: This can consume more gas than the block gas limit for large sets.

values(Uint8Set)

function values(Uint8Set storage set)
    internal
    view
    returns (uint8[] memory result)

Returns all of the values in the set.

at(AddressSet,uint256)

function at(AddressSet storage set, uint256 i)
    internal
    view
    returns (address result)

Returns the element at index i in the set. Reverts if i is out-of-bounds.

at(Bytes32Set,uint256)

function at(Bytes32Set storage set, uint256 i)
    internal
    view
    returns (bytes32 result)

Returns the element at index i in the set. Reverts if i is out-of-bounds.

at(Uint256Set,uint256)

function at(Uint256Set storage set, uint256 i)
    internal
    view
    returns (uint256 result)

Returns the element at index i in the set. Reverts if i is out-of-bounds.

at(Int256Set,uint256)

function at(Int256Set storage set, uint256 i)
    internal
    view
    returns (int256 result)

Returns the element at index i in the set. Reverts if i is out-of-bounds.

at(Uint8Set,uint256)

function at(Uint8Set storage set, uint256 i)
    internal
    view
    returns (uint8 result)

Returns the element at index i in the set. Reverts if i is out-of-bounds.

indexOf(AddressSet,address)

function indexOf(AddressSet storage set, address value)
    internal
    view
    returns (uint256 result)

Returns the index of value. Returns NOT_FOUND if the value does not exist.

indexOf(Bytes32Set,bytes32)

function indexOf(Bytes32Set storage set, bytes32 value)
    internal
    view
    returns (uint256 result)

Returns the index of value. Returns NOT_FOUND if the value does not exist.

indexOf(Uint256Set,uint256)

function indexOf(Uint256Set storage set, uint256 i)
    internal
    view
    returns (uint256 result)

Returns the index of value. Returns NOT_FOUND if the value does not exist.

indexOf(Int256Set,int256)

function indexOf(Int256Set storage set, int256 i)
    internal
    view
    returns (uint256 result)

Returns the index of value. Returns NOT_FOUND if the value does not exist.

indexOf(Uint8Set,uint8)

function indexOf(Uint8Set storage set, uint8 value)
    internal
    view
    returns (uint256 result)

Returns the index of value. Returns NOT_FOUND if the value does not exist.