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 keyaAlreadyExists);

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.
keyuint16value to insert.

Returns

NameTypeDescription
keyaAlreadyExistsboolwhether the key already existed in the set

remove

remove a key.

key to remove must exist.

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

Parameters

NameTypeDescription
selfSetstorage pointer to a Set.
keyuint16value to remove.

Returns

NameTypeDescription
keyDidNotExistboolwhether the key already did not yet exist in the set

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;
}