Skip to content

Latest commit

 

History

History
473 lines (397 loc) · 14.2 KB

EnumerableBytes4Set.md

File metadata and controls

473 lines (397 loc) · 14.2 KB

Library for managing loan sets.

  • (EnumerableBytes4Set.sol)

View Source: contracts/mixins/EnumerableBytes4Set.sol

EnumerableBytes4Set 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 EnumerableBytes4Set for EnumerableBytes4Set.Bytes4Set;.

Structs

Bytes4Set

struct Bytes4Set {
 mapping(bytes4 => uint256) index,
 bytes4[] values
}

Functions


addBytes4

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

function addBytes4(struct EnumerableBytes4Set.Bytes4Set set, bytes4 value) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set The set of values.
value bytes4 The new value to add. *

Returns

False if the value was already in the set.

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

removeBytes4

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

function removeBytes4(struct EnumerableBytes4Set.Bytes4Set set, bytes4 value) internal nonpayable
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set The set of values.
value bytes4 The value to remove. *

Returns

False if the value was not present in the set.

Source Code
function removeBytes4(Bytes4Set storage set, bytes4 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) {
                bytes4 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 EnumerableBytes4Set.Bytes4Set set, bytes4 value) internal view
returns(bool)

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set The set of values.
value bytes4 The value to find. *

Returns

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

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

enumerate

Get all set values. *

function enumerate(struct EnumerableBytes4Set.Bytes4Set set, uint256 start, uint256 count) internal view
returns(output bytes4[])

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set 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(
        Bytes4Set storage set,
        uint256 start,
        uint256 count
    ) internal view returns (bytes4[] 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 bytes4[](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 EnumerableBytes4Set.Bytes4Set set) internal view
returns(uint256)

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set The set of values. *

Returns

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

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

get

Get an item from the set by its index. *

function get(struct EnumerableBytes4Set.Bytes4Set set, uint256 index) internal view
returns(bytes4)

Arguments

Name Type Description
set struct EnumerableBytes4Set.Bytes4Set 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(Bytes4Set storage set, uint256 index) internal view returns (bytes4) {
        return set.values[index];
    }

Contracts