Bool

Concurrent Bool Array

The Bool contract is a concurrent array specialized for storing boolean values. It inherits from the Base contract to utilize container functionalities for boolean storage.

Constructor

constructor()

Functions

push

function push(bool elem) public virtual

Add a boolean element to the concurrent array.

  • Parameters:

    • elem: The boolean element to add to the array.

pop

function pop() public virtual returns (bool)

Remove and return the last boolean element from the concurrent array.

get

function get(uint256 idx) public virtual returns (bool)

Retrieve the boolean element at the given index from the concurrent array.

  • Parameters:

    • idx: The index of the Boolean element to retrieve.

set

function set(uint256 idx, bool elem) public

Set the boolean element at the given index in the concurrent array.

  • Parameters:

    • idx: The index where the boolean element should be stored.

    • elem: The boolean element to be stored at the specified index.

Example

The examples below demonstrates the usage of the Bool contract, which is a concurrent array specialized for storing boolean values, and the test cases in BoolTest verify the correctness of its functionalities for adding, retrieving, setting, and removing boolean elements.

The code example illustrates demonstrates how this concurrent data structure can be effectively used for managing the Boolean values within a smart contract.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;

import "./Bool.sol";

contract BoolTest {
    Bool boolContainer = new Bool();
    
    constructor() {     
        require(boolContainer.length() == 0); 
    
        boolContainer.push(true);
        boolContainer.push(false);
        boolContainer.push(false);
        boolContainer.push(true);
        require(boolContainer.length() == 4); 

       require(boolContainer.get(0));
        require(!boolContainer.get(1));
        require(!boolContainer.get(2));
        require(boolContainer.get(3));

        boolContainer.set(0, false);
        boolContainer.set(1, true);
        boolContainer.set(2, true);
        boolContainer.set(3, false);

        require(!boolContainer.get(0));
        require(boolContainer.get(1));
        require(boolContainer.get(2));
        require(!boolContainer.get(3));

        require(!boolContainer.pop());
        require(boolContainer.pop());
        require(boolContainer.pop());
        require(!boolContainer.pop());
        require(boolContainer.length() == 0);  
    }
}

Last updated