From b7486a5e50c343c3a4a48bc5c463a4eb3464f883 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 30 Sep 2024 00:21:07 +0000 Subject: [PATCH] Add inSorted to LibSort --- src/utils/LibSort.sol | 15 +++++++++++++++ test/LibSort.t.sol | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/utils/LibSort.sol b/src/utils/LibSort.sol index dabafe4b8..af3d271cd 100644 --- a/src/utils/LibSort.sol +++ b/src/utils/LibSort.sol @@ -249,6 +249,21 @@ library LibSort { (found, index) = _searchSorted(_toUints(a), uint256(uint160(needle)), 0); } + /// @dev Returns whether `a` contains `needle`. + function inSorted(uint256[] memory a, uint256 needle) internal pure returns (bool found) { + (found,) = searchSorted(a, needle); + } + + /// @dev Returns whether `a` contains `needle`. + function inSorted(int256[] memory a, int256 needle) internal pure returns (bool found) { + (found,) = searchSorted(a, needle); + } + + /// @dev Returns whether `a` contains `needle`. + function inSorted(address[] memory a, address needle) internal pure returns (bool found) { + (found,) = searchSorted(a, needle); + } + /// @dev Reverses the array in-place. function reverse(uint256[] memory a) internal pure { /// @solidity memory-safe-assembly diff --git a/test/LibSort.t.sol b/test/LibSort.t.sol index 4118b9fa0..fbc4dfff1 100644 --- a/test/LibSort.t.sol +++ b/test/LibSort.t.sol @@ -440,6 +440,9 @@ contract LibSortTest is SoladyTest { uint256 randomIndex = _random() % a.length; uint256 value = a[randomIndex]; (bool found, uint256 index) = LibSort.searchSorted(a, value); + if (_randomChance(16)) { + assertEq(LibSort.inSorted(a, value), found); + } assertTrue(found); assertEq(a[index], value); }