Skip to content

Commit

Permalink
static_filter_map tests + bench
Browse files Browse the repository at this point in the history
  • Loading branch information
fhamonic committed Sep 6, 2024
1 parent c17b182 commit 96db950
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
4 changes: 2 additions & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class MelonConan(ConanFile):
generators = "CMakeToolchain", "CMakeDeps"

def requirements(self):
self.requires("range-v3/[>=0.11.0]")
self.requires("fmt/[>=10.0.0]")
self.requires("range-v3/[>=0.11.0]", transitive_headers=True)
self.requires("fmt/[>=10.0.0]", transitive_headers=True)
self.test_requires("gtest/[>=1.10.0 <cci]")

def validate(self):
Expand Down
3 changes: 1 addition & 2 deletions include/melon/algorithm/breadth_first_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class breadth_first_search {
vertex_map_t<_Graph, bool> _reached_map;

[[no_unique_address]] vertex_map_if<_Traits::store_pred_vertices, _Graph,
vertex>
_pred_vertices_map;
vertex> _pred_vertices_map;
[[no_unique_address]] vertex_map_if<_Traits::store_pred_arcs, _Graph, arc>
_pred_arcs_map;
[[no_unique_address]] vertex_map_if<_Traits::store_distances, _Graph, int>
Expand Down
8 changes: 4 additions & 4 deletions include/melon/container/static_filter_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class static_filter_map {
}
}
constexpr void _bump_down() noexcept {
if(_local_index++ == 0) {
if(_local_index-- == 0) {
--_p;
_local_index = N - 1;
}
Expand Down Expand Up @@ -317,10 +317,10 @@ class static_filter_map {
++cursor;
span_type shifted = (*cursor._p) >> cursor._local_index;
if(shifted == span_type{0}) {
do {
++cursor._p;
} while(cursor < end_it && *cursor._p == span_type{0});
cursor._local_index = 0;
do {
if(++cursor._p > end_it._p) return cursor;
} while(*cursor._p == span_type{0});
shifted = *cursor._p;
}
cursor._local_index +=
Expand Down
6 changes: 4 additions & 2 deletions include/melon/views/subgraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class subgraph : public graph_view_base {
public:
template <typename _G, typename _VF = true_map, typename _AF = true_map>
requires(!__detail::__specialization_of<_G, subgraph>)
[[nodiscard]] constexpr explicit subgraph(_G && g, _VF && vertex_filter = true_map{},
_AF && arc_filter = true_map{})
[[nodiscard]] constexpr explicit subgraph(_G && g,
_VF && vertex_filter = true_map{},
_AF && arc_filter = true_map{})
: _graph(views::graph_all(std::forward<_G>(g)))
, _vertex_filter(views::mapping_all(std::forward<_VF>(vertex_filter)))
, _arc_filter(views::mapping_all(std::forward<_AF>(arc_filter))) {}
Expand Down Expand Up @@ -161,6 +162,7 @@ class subgraph : public graph_view_base {
return std::views::filter(
melon::out_arcs(_graph, v),
[this](const arc & a) { return _arc_filter[a]; });
// return arc_filter.filter(melon::out_arcs(_graph, v));
} else {
return std::views::filter(
melon::out_arcs(_graph, v), [this](const arc & a) {
Expand Down
52 changes: 51 additions & 1 deletion test/static_filter_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ GTEST_TEST(static_map_bool, iterator_extensive_read) {
}

GTEST_TEST(static_map_bool, filter) {
const std::size_t nb_bools = 153;
const std::size_t nb_bools = 8971;
static_filter_map<std::size_t> map(nb_bools, false);
std::vector<std::size_t> indices;

Expand All @@ -161,4 +161,54 @@ GTEST_TEST(static_map_bool, filter) {

ASSERT_TRUE(EQ_MULTISETS(
map.filter(std::views::iota(int{0}, int{nb_bools})), indices));
}

GTEST_TEST(static_map_bool, filter_bench) {
const double density = 0.9;
const std::size_t nb_bools = 153546;
static_filter_map<std::size_t> map(nb_bools, false);
std::vector<std::size_t> indices;

auto gen = std::bind(std::uniform_real_distribution<>(0, 1),
std::default_random_engine());

for(std::size_t i = 0; i < nb_bools; ++i) {
if(gen() > density) continue;
indices.emplace_back(i);
map[i] = true;
}

static_assert(std::random_access_iterator<std::vector<bool>::iterator>);
static_assert(
std::random_access_iterator<static_map<std::size_t, bool>::iterator>);

{
auto start = std::chrono::high_resolution_clock::now();

std::size_t cpt = 0;
for(auto && i :
map.filter(std::views::iota(std::size_t{0}, nb_bools))) {
++cpt;
}
ASSERT_EQ(cpt, indices.size());

auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<std::chrono::microseconds>(stop - start);
std::cout << "map filter : " << duration.count() << " us" << std::endl;
}

{
auto start = std::chrono::high_resolution_clock::now();

std::size_t cpt = 0;
for(std::size_t i = 0; i < nb_bools; ++i) {
if(!map[i]) continue;
++cpt;
}
ASSERT_EQ(cpt, indices.size());

auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<std::chrono::microseconds>(stop - start);
std::cout << "manual filter : " << duration.count() << " us" << std::endl;
}
}

0 comments on commit 96db950

Please sign in to comment.