Skip to content

Commit

Permalink
[MISC] Make favourite_child_idx and stdoptional.
Browse files Browse the repository at this point in the history
  • Loading branch information
smehringer committed Aug 30, 2023
1 parent 54c1d9e commit 0837765
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion include/hibf/layout/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#pragma once

#include <optional>
#include <vector> // for vector, operator==

#include <hibf/layout/layout.hpp>
Expand Down Expand Up @@ -43,7 +44,7 @@ struct graph
size_t parent_bin_index{};
size_t max_bin_index{};
size_t number_of_technical_bins{};
int32_t favourite_child_idx{-1};
std::optional<uint32_t> favourite_child_idx{std::nullopt};
std::vector<layout::layout::user_bin> remaining_records{}; // non-merged bins (either split or single)

// Doesn't work, because the type is incomplete. To compare node, a comparison for the children member is needed.
Expand Down
12 changes: 8 additions & 4 deletions src/hierarchical_interleaved_bloom_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ size_t hierarchical_build(hierarchical_interleaved_bloom_filter & hibf,

auto initialise_max_bin_kmers = [&]() -> size_t
{
if (current_node.favourite_child_idx != -1) // max bin is a merged bin
if (current_node.favourite_child_idx.has_value()) // max bin is a merged bin
{
// recursively initialize favourite child first
ibf_positions[current_node.max_bin_index] =
hierarchical_build(hibf, kmers, current_node.children[current_node.favourite_child_idx], data, false);
hierarchical_build(hibf,

Check warning on line 54 in src/hierarchical_interleaved_bloom_filter.cpp

View check run for this annotation

Codecov / codecov/patch

src/hierarchical_interleaved_bloom_filter.cpp#L53-L54

Added lines #L53 - L54 were not covered by tests
kmers,
current_node.children[current_node.favourite_child_idx.value()],

Check warning on line 56 in src/hierarchical_interleaved_bloom_filter.cpp

View check run for this annotation

Codecov / codecov/patch

src/hierarchical_interleaved_bloom_filter.cpp#L56

Added line #L56 was not covered by tests
data,
false);
return 1;
}
else // max bin is not a merged bin
Expand Down Expand Up @@ -101,7 +105,7 @@ size_t hierarchical_build(hierarchical_interleaved_bloom_filter & hibf,
{
auto & child = children[index];

if (index != current_node.favourite_child_idx)
if (index != current_node.favourite_child_idx.value_or(-1))
{
robin_hood::unordered_flat_set<uint64_t> kmers{};
size_t const ibf_pos = hierarchical_build(hibf, kmers, child, data, false);
Expand All @@ -121,7 +125,7 @@ size_t hierarchical_build(hierarchical_interleaved_bloom_filter & hibf,
loop_over_children();

// If max bin was a merged bin, process all remaining records, otherwise the first one has already been processed
size_t const start{(current_node.favourite_child_idx != -1) ? 0u : 1u};
size_t const start{(current_node.favourite_child_idx.has_value()) ? 0u : 1u};
for (size_t i = start; i < current_node.remaining_records.size(); ++i)
{
auto const & record = current_node.remaining_records[i];
Expand Down
4 changes: 2 additions & 2 deletions src/layout/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void update_header_node_data(std::vector<layout::layout::max_bin> const & header
}
}

seqan::hibf::layout::graph::node new_child{{}, bin_indices.back(), max_id, 0u, -1, {}};
seqan::hibf::layout::graph::node new_child{{}, bin_indices.back(), max_id, 0u, std::nullopt, {}};
parent->children.push_back(new_child);

if (parent->max_bin_index == bin_indices.back())
Expand Down Expand Up @@ -116,7 +116,7 @@ graph::graph(seqan::hibf::layout::layout const & hibf_layout)
root.parent_bin_index = 0;
root.max_bin_index = hibf_layout.top_level_max_bin_id;
root.number_of_technical_bins = 0;
root.favourite_child_idx = -1; // not known yet
root.favourite_child_idx = std::nullopt; // not known yet
root.remaining_records = {};

update_header_node_data(hibf_layout.max_bins, *this);
Expand Down
12 changes: 6 additions & 6 deletions test/unit/hibf/layout/graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(hibf_graph.root.parent_bin_index, 0);
EXPECT_EQ(hibf_graph.root.max_bin_index, 0);
EXPECT_EQ(hibf_graph.root.number_of_technical_bins, 5);
EXPECT_EQ(hibf_graph.root.favourite_child_idx, 0);
EXPECT_EQ(hibf_graph.root.favourite_child_idx.value(), 0);
ASSERT_EQ(hibf_graph.root.remaining_records.size(), 2);
EXPECT_EQ(hibf_graph.root.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{1, std::vector<size_t>{}, 1, 2}));
Expand All @@ -72,7 +72,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(root_child0.parent_bin_index, 0);
EXPECT_EQ(root_child0.max_bin_index, 1);
EXPECT_EQ(root_child0.number_of_technical_bins, 5);
EXPECT_EQ(root_child0.favourite_child_idx, 1);
EXPECT_EQ(root_child0.favourite_child_idx.value(), 1);
ASSERT_EQ(root_child0.remaining_records.size(), 3);
EXPECT_EQ(root_child0.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{7, std::vector<size_t>{0}, 1, 2}));
Expand All @@ -85,7 +85,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(root_child1.parent_bin_index, 1);
EXPECT_EQ(root_child1.max_bin_index, 26);
EXPECT_EQ(root_child1.number_of_technical_bins, 64);
EXPECT_EQ(root_child1.favourite_child_idx, -1);
EXPECT_EQ(root_child1.favourite_child_idx.has_value(), false);
ASSERT_EQ(root_child1.remaining_records.size(), 3);
EXPECT_EQ(root_child1.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{3, std::vector<size_t>{1}, 19, 26}));
Expand All @@ -98,7 +98,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(c0_child0.parent_bin_index, 0);
EXPECT_EQ(c0_child0.max_bin_index, 4);
EXPECT_EQ(c0_child0.number_of_technical_bins, 5);
EXPECT_EQ(c0_child0.favourite_child_idx, -1);
EXPECT_EQ(c0_child0.favourite_child_idx.has_value(), false);
ASSERT_EQ(c0_child0.remaining_records.size(), 4);
EXPECT_EQ(c0_child0.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{11, std::vector<size_t>{0, 0}, 1, 4}));
Expand All @@ -113,7 +113,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(c0_child1.parent_bin_index, 1);
EXPECT_EQ(c0_child1.max_bin_index, 34);
EXPECT_EQ(c0_child1.number_of_technical_bins, 64);
EXPECT_EQ(c0_child1.favourite_child_idx, -1);
EXPECT_EQ(c0_child1.favourite_child_idx.has_value(), false);
ASSERT_EQ(c0_child1.remaining_records.size(), 3);
EXPECT_EQ(c0_child1.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{9, std::vector<size_t>{0, 1}, 15, 34}));
Expand All @@ -126,7 +126,7 @@ TEST(layout_test, printing_max_bins)
EXPECT_EQ(c0_c0_child0.parent_bin_index, 0);
EXPECT_EQ(c0_c0_child0.max_bin_index, 30);
EXPECT_EQ(c0_c0_child0.number_of_technical_bins, 64);
EXPECT_EQ(c0_c0_child0.favourite_child_idx, -1);
EXPECT_EQ(c0_c0_child0.favourite_child_idx.has_value(), false);
ASSERT_EQ(c0_c0_child0.remaining_records.size(), 5);
EXPECT_EQ(c0_c0_child0.remaining_records[0],
(seqan::hibf::layout::layout::user_bin{16, std::vector<size_t>{0, 0, 0}, 11, 30}));
Expand Down

0 comments on commit 0837765

Please sign in to comment.