diff --git a/include/hibf/hierarchical_interleaved_bloom_filter.hpp b/include/hibf/hierarchical_interleaved_bloom_filter.hpp index 5f026707..0dfbb1b4 100644 --- a/include/hibf/hierarchical_interleaved_bloom_filter.hpp +++ b/include/hibf/hierarchical_interleaved_bloom_filter.hpp @@ -105,6 +105,8 @@ class hierarchical_interleaved_bloom_filter ~hierarchical_interleaved_bloom_filter() = default; //!< Defaulted. hierarchical_interleaved_bloom_filter(config const & configuration); + hierarchical_interleaved_bloom_filter(std::function input_fn, + std::filesystem::path const & layout_filename); //!\} //!\brief The individual interleaved Bloom filters. diff --git a/src/hierarchical_interleaved_bloom_filter.cpp b/src/hierarchical_interleaved_bloom_filter.cpp index 8545010c..d321e9dd 100644 --- a/src/hierarchical_interleaved_bloom_filter.cpp +++ b/src/hierarchical_interleaved_bloom_filter.cpp @@ -199,4 +199,20 @@ hierarchical_interleaved_bloom_filter::hierarchical_interleaved_bloom_filter(con build_index(*this, configuration, layout); } +hierarchical_interleaved_bloom_filter::hierarchical_interleaved_bloom_filter( + std::function input_fn, + std::filesystem::path const & layout_filename) +{ + // read config and layout from file + std::ifstream layout_file{layout_filename}; + config configuration; + layout::layout hibf_layout; + configuration.read_from(layout_file); + hibf_layout.read_from(layout_file); + + configuration.input_fn = input_fn; // set input as it cannot be serialized. + + build_index(*this, configuration, hibf_layout); +} + } // namespace hibf diff --git a/test/unit/hibf/hierarchical_interleaved_bloom_filter_test.cpp b/test/unit/hibf/hierarchical_interleaved_bloom_filter_test.cpp index 0638382c..067f0ff2 100644 --- a/test/unit/hibf/hierarchical_interleaved_bloom_filter_test.cpp +++ b/test/unit/hibf/hierarchical_interleaved_bloom_filter_test.cpp @@ -9,11 +9,13 @@ #include // for size_t #include // for function +#include #include // for vector, allocator #include // for insert_iterator, config #include // for hierarchical_interleaved_bloom_filter #include // for expect_range_eq, EXPECT_RANGE_EQ +#include TEST(hibf_test, test_specific_hash_values) { @@ -41,6 +43,58 @@ TEST(hibf_test, test_specific_hash_values) } } +TEST(hibf_test, build_from_layout) +{ + // range of range of sequences + std::vector> hashes{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u}, {1u, 2u, 3u, 4u, 5u}}; + + auto input_fn = [&](size_t const num, hibf::insert_iterator it) + { + for (auto const hash : hashes[num]) + it = hash; + }; + + hibf::test::tmp_directory tmp{}; + std::filesystem::path layout_filename{tmp.path()/"dummy.layout"}; + + { + std::ofstream stream{layout_filename}; + stream << "##CONFIG:\n"; + stream << "##{\n"; + stream << "## \"config\": {\n"; + stream << "## \"version\": 1,\n"; + stream << "## \"number_of_user_bins\": 2,\n"; + stream << "## \"number_of_hash_functions\": 2,\n"; + stream << "## \"maximum_false_positive_rate\": 0.05,\n"; + stream << "## \"threads\": 1,\n"; + stream << "## \"sketch_bits\": 12,\n"; + stream << "## \"tmax\": 64,\n"; + stream << "## \"alpha\": 1.2,\n"; + stream << "## \"max_rearrangement_ratio\": 0.5,\n"; + stream << "## \"disable_estimate_union\": false,\n"; + stream << "## \"disable_rearrangement\": true,\n"; + stream << "## \"disable_cutoffs\": false\n"; + stream << "## }\n"; + stream << "##}\n"; + stream << "##ENDCONFIG\n"; + stream << "#HIGH_LEVEL_IBF max_bin_id:0\n"; + stream << "#USER_BIN_IDX\tTECHNICAL_BIN_INDICES\tNUMBER_OF_TECHNICAL_BINS\n"; + stream << "1\t0\t34\n"; + stream << "0\t34\t30\n"; + } + + hibf::hierarchical_interleaved_bloom_filter hibf{input_fn, layout_filename}; + + { + std::vector query{1, 2, 3, 4, 5}; + + auto agent = hibf.membership_agent(); + auto result = agent.bulk_contains(query, 2); + + EXPECT_RANGE_EQ(result, (std::vector{0u, 1u})); + } +} + // #ifdef HIBF_HAS_SEQAN3 // #include