Skip to content

Commit

Permalink
A little cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Applencourt committed Jul 26, 2023
1 parent b11c00d commit 70905f4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
8 changes: 2 additions & 6 deletions .secret_cpp/bench_psi.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include <random>
#include <algorithm>
#include "qpx.hpp"
Expand Down Expand Up @@ -89,12 +88,9 @@ static void BinarySearchesLookup(benchmark::State& state) {
std::sort(psi.begin(), psi.end());

std::vector<det_t> vals{psi.begin(), psi.end()};
std::vector<bool> result(vals.size());
std::vector<bool> 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);
Expand Down
2 changes: 2 additions & 0 deletions .secret_cpp/category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <unordered_map>
#include <vector>
#include <array>
#if !defined(DOCTEST_CONFIG_DISABLE)
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#endif
#include <doctest/doctest.h>
#include <qpx.hpp>

Expand Down
30 changes: 16 additions & 14 deletions .secret_cpp/include/psi.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
#pragma once

template<class ForwardIterator, class ForwardIteratorBool>
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
// |-----------|----------------|
{
// Botom Half [v_first, v)
// 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<class T1, class T2, class T3>
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<class T1, class T2>
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);
}
3 changes: 2 additions & 1 deletion .secret_cpp/include/qpx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ struct std::hash<det_t> {
}
};

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 << ")";
}

Expand Down

0 comments on commit 70905f4

Please sign in to comment.