Skip to main content

Uint16Set

Git Source

based on https://github.com/rob-Hitchens/SetTypes/blob/master/contracts/UintSet.sol

Key sets with enumeration and delete. Uses mappings for random and existence checks and dynamic arrays for enumeration. Key uniqueness is enforced.

Sets are unordered. Delete operations reorder keys. All operations have a fixed gas cost at any scale, O(1). author: Rob Hitchens

Functions

insert

insert a key.

duplicate keys are not permitted.

function insert(Set storage self, uint16 key) internal returns (bool keyAlreadyExists);

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.
keyuint16value to insert.

Returns

NameTypeDescription
keyAlreadyExistsboolwhether the key already existed in the set

remove

remove a key.

If the key does not exist, this function is a no-op and returns true.

function remove(Set storage self, uint16 key) internal returns (bool isSetEmpty);

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.
keyuint16value to remove.

Returns

NameTypeDescription
isSetEmptyboolwhether the key did not exist or the set still has items after removal

count

count the keys.

function count(
Set storage self
) internal view returns (uint16);

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.

exists

check if a key is in the Set.

function exists(Set storage self, uint16 key) internal view returns (bool);

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.
keyuint16value to check.

Returns

NameTypeDescription
<none>boolbool true: Set member, false: not a Set member.

Structs

Set

struct Set {
mapping(uint16 => uint16) keyPointers;
uint16[] keyList;
}