Skip to content

Added contains function to ArrayHelper #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![GitPOAP Badge](https://public-api.gitpoap.io/v1/repo/dl-solarity/solidity-lib/badge)](https://www.gitpoap.io/gh/dl-solarity/solidity-lib)

# Solidity Library for Savvies by Distributed Lab
# Solidity Library for Savvies

Solidity modules and utilities that **go far beyond mediocre solidity**.

- Implementation of [**Contracts Registry**](https://eips.ethereum.org/EIPS/eip-6224) pattern
- Versatile access control smart contracts (Merkle whitelists, RBAC)
- Versatile access control smart contracts (**Merkle whitelists**, **RBAC**)
- Enhanced and simplified [**Diamond**](https://eips.ethereum.org/EIPS/eip-2535) pattern
- Advanced data structures (**Vector**, **PriorityQueue**, **AVLTree**)
- Advanced data structures (**Vector**, **DynamicSet**, **PriorityQueue**, **AVLTree**)
- ZK-friendly [**Sparse Merkle Tree**](https://docs.iden3.io/publications/pdfs/Merkle-Tree.pdf) and [**Incremental Merkle Tree**](https://github.com/runtimeverification/deposit-contract-verification/blob/master/deposit-contract-verification.pdf) implementations
- Flexible finance primitives (**Staking**, **Vesting**)
- Robust UniswapV2 and UniswapV3 oracles
- Lightweight SBT implementation
- Utilities to ease work with memory, ERC20 decimals, arrays, sets, and ZK proofs

Built with the help of [Openzeppelin Contracts](https://github.com/OpenZeppelin/openzeppelin-contracts) (4.9.5).
Built with the help of [Openzeppelin Contracts](https://github.com/OpenZeppelin/openzeppelin-contracts) (4.9.6).

## Overview

Expand Down
26 changes: 26 additions & 0 deletions contracts/libs/arrays/ArrayHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ library ArrayHelper {
return high_;
}

/**
* @notice The function that searches for the `element_` and returns whether it is present in the array.
* The time complexity is O(log n)
* @param array the array to search in
* @param element_ the element
* @return whether the `element_` is present in the array
*/
function contains(uint256[] storage array, uint256 element_) internal view returns (bool) {
(uint256 low_, uint256 high_) = (0, array.length);

while (low_ < high_) {
uint256 mid_ = Math.average(low_, high_);
uint256 midElement_ = array[mid_];

if (midElement_ == element_) {
return true;
} else if (midElement_ > element_) {
high_ = mid_;
} else {
low_ = mid_ + 1;
}
}

return false;
}

/**
* @notice The function that calculates the sum of all array elements from `beginIndex_` to
* `endIndex_` inclusive using its prefix sum array
Expand Down
4 changes: 4 additions & 0 deletions contracts/mock/libs/arrays/ArrayHelperMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ contract ArrayHelperMock {
return _tmpArr.upperBound(element_);
}

function contains(uint256 element_) external view returns (bool) {
return _tmpArr.contains(element_);
}

function getRangeSum(uint256 beginIndex_, uint256 endIndex_) external view returns (uint256) {
return _tmpArr.getRangeSum(beginIndex_, endIndex_);
}
Expand Down
Loading
Loading