-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: algo/numeric/bin_search_integer.hpp
- Loading branch information
1 parent
34674a2
commit 8b5a6e1
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
#include <cstddef> | ||
#include <iostream> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
/// `p(ok)` を満たし、かつ `ng_` に最も近い `ok` を二分探索で求める。 | ||
/// - 戻り値の値域は `min(ok_,ng_) - 1 <= ok <= max(ok_,ng_) + 1` | ||
/// - `p(x) == true` (`∀x ∈ [ok_, ng_]`) ならば `ng_` を返す。 | ||
/// - `p(x) == false` (`∀x ∈ [ok_, ng_]`) ならば `[ok_, ng_]` の範囲外で `ok_` に最も近い値を返す。 | ||
template < | ||
class Int, | ||
class PredicateFn, | ||
class SignedInt = std::make_signed_t<Int>, | ||
std::enable_if_t<std::is_integral_v<Int>, std::nullptr_t> = nullptr> | ||
SignedInt bin_search_integer(const Int ok_, const Int ng_, PredicateFn&& p) { | ||
SignedInt ng, ok; | ||
if (ng_ < ok_) { | ||
ng = ng_ - 1; | ||
ok = ok_ + 1; | ||
while (ok - ng > 1) { | ||
auto mid = ng + ((ok - ng) >> 1); | ||
(p((Int)mid) ? ok : ng) = mid; | ||
} | ||
} else { | ||
ng = ng_ + 1; | ||
ok = ok_ - 1; | ||
while (ng - ok > 1) { | ||
auto mid = ok + ((ng - ok) >> 1); | ||
(p((Int)mid) ? ok : ng) = mid; | ||
} | ||
} | ||
|
||
#ifdef ARMKN_DEBUG | ||
if (ng == (SignedInt)ok_) { | ||
std::clog << "\x1b[31;1m[Warn] ALL p(x) made FALSE\x1b[;m"; | ||
std::clog << " initial range: (ok_,ng_)=(" << ok_ << ", " << ng_ << ") / result: (ok,ng)=(" | ||
<< ok << ", " << ng << ')' << std::endl; | ||
} | ||
#endif | ||
return ok; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <armkn/algo/numeric/bin_search_integer.hpp> | ||
#include <armkn/util/alias/stdint.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
#define fn(expr) ([&](u32 i) { return (expr); }) | ||
|
||
TEST_CASE("bin_search_integer") { | ||
std::vector<int> a{1, 3, 3, 3, 3, 6}; | ||
|
||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 1)) == 0); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 2)) == 1); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 3)) == 1); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 4)) == 5); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 5)) == 5); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 6)) == 5); | ||
CHECK(bin_search_integer(5u, 0u, fn(a[i] >= 7)) == 6); | ||
|
||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 1)) == -1); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 2)) == 0); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 3)) == 0); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 4)) == 4); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 5)) == 4); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 6)) == 4); | ||
CHECK(bin_search_integer(0u, 5u, fn(a[i] < 7)) == 5); | ||
} |