Skip to content
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

✨ Add inSorted to LibSort #1095

Merged
merged 1 commit into from
Sep 30, 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
Add inSorted to LibSort
  • Loading branch information
Vectorized committed Sep 30, 2024
commit b7486a5e50c343c3a4a48bc5c463a4eb3464f883
15 changes: 15 additions & 0 deletions src/utils/LibSort.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/LibSort.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down