diff --git a/.secret_cpp/bench_psi.cpp b/.secret_cpp/bench_psi.cpp index b67090b..6df6913 100644 --- a/.secret_cpp/bench_psi.cpp +++ b/.secret_cpp/bench_psi.cpp @@ -1,4 +1,3 @@ - #include #include #include "qpx.hpp" @@ -89,12 +88,9 @@ static void BinarySearchesLookup(benchmark::State& state) { std::sort(psi.begin(), psi.end()); std::vector vals{psi.begin(), psi.end()}; - std::vector result(vals.size()); + std::vector result(vals.size(), false); - for(auto _ : state) { - binary_searchs(psi.begin(), psi.end(), vals.begin(), vals.end(), vals.begin() + vals.size() / 2, - result.begin() + vals.size() / 2); - } + for(auto _ : state) { binary_searchs(psi, vals, result); } state.counters["LookupRate"] = benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate); diff --git a/.secret_cpp/category.cpp b/.secret_cpp/category.cpp index 1a65f0c..8fd39fd 100644 --- a/.secret_cpp/category.cpp +++ b/.secret_cpp/category.cpp @@ -6,7 +6,9 @@ #include #include #include +#if !defined(DOCTEST_CONFIG_DISABLE) #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#endif #include #include diff --git a/.secret_cpp/include/psi.hpp b/.secret_cpp/include/psi.hpp index 1aa6d22..599bfba 100644 --- a/.secret_cpp/include/psi.hpp +++ b/.secret_cpp/include/psi.hpp @@ -1,16 +1,17 @@ #pragma once template -void binary_searchs(ForwardIterator first, ForwardIterator last, ForwardIterator v_first, - ForwardIterator v_last, ForwardIterator v, ForwardIteratorBool i) { - // Return: `*v` ∈? [first, last) - // Precondition: `v` ∈ `v_first`, `v_last`. +void binary_searchs_imp(ForwardIterator first, ForwardIterator last, ForwardIterator v_first, + ForwardIterator v_last, ForwardIterator v, ForwardIteratorBool i) { + // Return: *v ∈? [first, last). + // Precondition: v ∈ [v_first, v_last) - // pos of `*v` inside `v` array using binary search + // pos is pointer ∈ [first, last) pointing `*v`. auto pos = std::lower_bound(first, last, *v); - // Find it or not find it? - *i = (pos != last && !(*v < *pos)); - // Find new values to search for + // Lazy: only set i, if found + if(pos != last && !(*v < *pos)) *i = true; + + // Find the new `v` to search for // v_first v v_last // |-----------|----------------| { @@ -18,18 +19,19 @@ void binary_searchs(ForwardIterator first, ForwardIterator last, ForwardIterator // End the recursion when v_first == v, we search for all our values // if distance == 1, need to search for v_first auto d = std::distance(v_first, v); - if(pos != first && d > 1) binary_searchs(first, pos, v_first, v - 1, v - d / 2, i - d / 2); + if(pos != first && d > 0) + binary_searchs_imp(first, pos, v_first, v - 1, v - (d + 1) / 2, i - (d + 1) / 2); } { // Top Half (v, v_last) // We cannot deference v_last, so we stop the recursion when d == 1 auto d = std::distance(v, v_last); - if(pos != last && d > 1) binary_searchs(pos, last, v + 1, v_last, v + d / 2, i + d / 2); + if(pos != last && d > 1) binary_searchs_imp(pos, last, v + 1, v_last, v + d / 2, i + d / 2); } } -template -void binary_searchs(T1& enumerable, T2& values, T3& result) { - binary_searchs(enumerable.begin(), enumerable.end(), values.begin(), values.end(), - values.begin() + values.size() / 2, result.begin() + values.size() / 2); +template +void binary_searchs(T1& enumerable, T1& values, T2& result) { + binary_searchs_imp(enumerable.begin(), enumerable.end(), values.begin(), values.end(), + values.begin() + values.size() / 2, result.begin() + values.size() / 2); } diff --git a/.secret_cpp/include/qpx.hpp b/.secret_cpp/include/qpx.hpp index 534f72d..4105d0e 100644 --- a/.secret_cpp/include/qpx.hpp +++ b/.secret_cpp/include/qpx.hpp @@ -56,7 +56,8 @@ struct std::hash { } }; -std::ostream& operator<<(std::ostream& os, const det_t& obj) { +// Should be moved in the cpp of det +inline std::ostream& operator<<(std::ostream& os, const det_t& obj) { return os << "(" << obj.alpha << "," << obj.beta << ")"; }