Skip to content

Latest commit

 

History

History
586 lines (482 loc) · 16.9 KB

EnumerableBytes32Set.md

File metadata and controls

586 lines (482 loc) · 16.9 KB

Library for managing loan sets.

  • (EnumerableBytes32Set.sol)

View Source: contracts/mixins/EnumerableBytes32Set.sol

EnumerableBytes32Set contract

Sets have the following properties:

    • Elements are added, removed, and checked for existence in constant time (O(1)).
  • Elements are enumerated in O(n). No guarantees are made on the ordering.
  • Include with using EnumerableBytes32Set for EnumerableBytes32Set.Bytes32Set;.

Structs

Bytes32Set

struct Bytes32Set {
 mapping(bytes32 => uint256) index,
 bytes32[] values
}

Functions


addAddress

Add an address value to a set. O(1). *

function addAddress(struct EnumerableBytes32Set.Bytes32Set set, address addrvalue) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
addrvalue address The address to add. *

Returns

False if the value was already in the set.

Source Code
function addAddress(Bytes32Set storage set, address addrvalue) internal returns (bool) {
        bytes32 value;
        assembly {
            value := addrvalue
        }
        return addBytes32(set, value);
    }

addBytes32

Add a value to a set. O(1). *

function addBytes32(struct EnumerableBytes32Set.Bytes32Set set, bytes32 value) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
value bytes32 The new value to add. *

Returns

False if the value was already in the set.

Source Code
function addBytes32(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        if (!contains(set, value)) {
            set.index[value] = set.values.push(value);
            return true;
        } else {
            return false;
        }
    }

removeAddress

Remove an address value from a set. O(1). *

function removeAddress(struct EnumerableBytes32Set.Bytes32Set set, address addrvalue) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
addrvalue address The address to remove. *

Returns

False if the address was not present in the set.

Source Code
function removeAddress(Bytes32Set storage set, address addrvalue) internal returns (bool) {
        bytes32 value;
        assembly {
            value := addrvalue
        }
        return removeBytes32(set, value);
    }

removeBytes32

Remove a value from a set. O(1). *

function removeBytes32(struct EnumerableBytes32Set.Bytes32Set set, bytes32 value) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
value bytes32 The value to remove. *

Returns

False if the value was not present in the set.

Source Code
function removeBytes32(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        if (contains(set, value)) {
            uint256 toDeleteIndex = set.index[value] - 1;
            uint256 lastIndex = set.values.length - 1;

            /// If the element we're deleting is the last one,
            /// we can just remove it without doing a swap.
            if (lastIndex != toDeleteIndex) {
                bytes32 lastValue = set.values[lastIndex];

                /// Move the last value to the index where the deleted value is.
                set.values[toDeleteIndex] = lastValue;

                /// Update the index for the moved value.
                set.index[lastValue] = toDeleteIndex + 1; // All indexes are 1-based
            }

            /// Delete the index entry for the deleted value.
            delete set.index[value];

            /// Delete the old entry for the moved value.
            set.values.pop();

            return true;
        } else {
            return false;
        }
    }

contains

Find out whether a value exists in the set. *

function contains(struct EnumerableBytes32Set.Bytes32Set set, bytes32 value) internal view
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
value bytes32 The value to find. *

Returns

True if the value is in the set. O(1).

Source Code
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return set.index[value] != 0;
    }

containsAddress

Returns true if the value is in the set. O(1).

function containsAddress(struct EnumerableBytes32Set.Bytes32Set set, address addrvalue) internal view
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set
addrvalue address
Source Code
function containsAddress(Bytes32Set storage set, address addrvalue)
        internal
        view
        returns (bool)
    {
        bytes32 value;
        assembly {
            value := addrvalue
        }
        return set.index[value] != 0;
    }

enumerate

Get all set values. *

function enumerate(struct EnumerableBytes32Set.Bytes32Set set, uint256 start, uint256 count) internal view
returns(output bytes32[])

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
start uint256 The offset of the returning set.
count uint256 The limit of number of values to return. *

Returns

An array with all values in the set. O(N). *

Source Code
function enumerate(
        Bytes32Set storage set,
        uint256 start,
        uint256 count
    ) internal view returns (bytes32[] memory output) {
        uint256 end = start + count;
        require(end >= start, "addition overflow");
        end = set.values.length < end ? set.values.length : end;
        if (end == 0 || start >= end) {
            return output;
        }

        output = new bytes32[](end - start);
        for (uint256 i; i < end - start; i++) {
            output[i] = set.values[i + start];
        }
        return output;
    }

length

Get the legth of the set. *

function length(struct EnumerableBytes32Set.Bytes32Set set) internal view
returns(uint256)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values. *

Returns

the number of elements on the set. O(1).

Source Code
function length(Bytes32Set storage set) internal view returns (uint256) {
        return set.values.length;
    }

get

Get an item from the set by its index. *

function get(struct EnumerableBytes32Set.Bytes32Set set, uint256 index) internal view
returns(bytes32)

Arguments

Name Type Description
set struct EnumerableBytes32Set.Bytes32Set The set of values.
index uint256 The index of the value to return. *

Returns

the element stored at position index in the set. O(1).

Source Code
function get(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return set.values[index];
    }

Contracts