diff --git a/CHANGELOG.md b/CHANGELOG.md index 415d1a8e5c6..fdd0581a0df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -175,6 +175,8 @@ Note that 3.1.0 will be the first API stable release and interfaces in this rele ([\#1410](https://github.com/seqan/seqan3/pull/1410)). * Renamed `seqan3::views::all` to `seqan3::views::type_reduce` ([\#1410](https://github.com/seqan/seqan3/pull/1410)). +* Added size() function to `seqan3::views::kmer_hash` + ([\#1722](https://github.com/seqan/seqan3/pull/1722)). #### Search diff --git a/include/seqan3/range/views/kmer_hash.hpp b/include/seqan3/range/views/kmer_hash.hpp index 5b4a334e135..f31027c2b22 100644 --- a/include/seqan3/range/views/kmer_hash.hpp +++ b/include/seqan3/range/views/kmer_hash.hpp @@ -174,6 +174,17 @@ class kmer_hash_view : public std::ranges::view_interface return end(); } //!\} + + /*!\brief Returns the size of the range, if the underlying range is a std::ranges::sized_range. + * \returns Size of range. + */ + uint64_t size() + requires std::ranges::sized_range + { + if (std::ranges::size(urange) >= shape_.size()) + return std::ranges::size(urange) - shape_.size() + 1; + return 0; + } }; /*!\brief Iterator for calculating hash values via a given seqan3::shape. diff --git a/test/unit/range/views/view_kmer_hash_test.cpp b/test/unit/range/views/view_kmer_hash_test.cpp index ef8a06f431c..bb038469340 100644 --- a/test/unit/range/views/view_kmer_hash_test.cpp +++ b/test/unit/range/views/view_kmer_hash_test.cpp @@ -137,3 +137,16 @@ TEST_F(kmer_hash_test, issue1643) auto k_mer_size_25_view = text_23_elements | seqan3::views::kmer_hash(seqan3::ungapped{25u}); EXPECT_TRUE(k_mer_size_25_view.empty()); } + +// https://github.com/seqan/seqan3/issues/1719 +TEST_F(kmer_hash_test, issue1719) +{ + uint64_t const expected = 0; + std::vector sequence{""_dna5}; + auto v = sequence | seqan3::views::kmer_hash(seqan3::ungapped{25}); + EXPECT_EQ(expected, v.size()); + + std::vector sequence2{"ACGATCGATCGTAGCTACTGAGC"_dna5}; + auto v2 = sequence2 | seqan3::views::kmer_hash(seqan3::ungapped{25}); + EXPECT_EQ(expected, v2.size()); +}