Skip to content

Commit 7621458

Browse files
authored
Merge pull request #67 from h-2/doxygen196
[fix] minor optimisation
2 parents c4fc4a0 + 68efb04 commit 7621458

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

include/bio/ranges/views/char_to.hpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,27 @@ class char_to_cigar_view<urng_t>::iterator
354354
//!\brief Dereference operator returns element currently pointed at.
355355
constexpr value_type operator*() const
356356
{
357-
alphabet::cigar c;
358-
359-
if constexpr (std::contiguous_iterator<uit_t>)
357+
if (value == default_value)
360358
{
361-
std::string_view v{uit_left, uit_right};
362-
c.assign_string(v);
359+
alphabet::cigar c;
360+
361+
if constexpr (std::contiguous_iterator<uit_t>)
362+
{
363+
std::string_view v{uit_left, uit_right};
364+
c.assign_string(v);
365+
}
366+
else
367+
{
368+
std::string s;
369+
std::ranges::copy(uit_left, uit_right, std::back_inserter(s));
370+
c.assign_string(s);
371+
}
372+
return c;
363373
}
364374
else
365375
{
366-
std::string s;
367-
std::ranges::copy(uit_left, uit_right, std::back_inserter(s));
368-
c.assign_string(s);
376+
return value;
369377
}
370-
return c;
371378
}
372379

373380
//!\brief Return pointer to this iterator.

test/unit/ranges/views/view_char_to_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,14 @@ TEST(view_char_to_cigar, typical_const)
195195
EXPECT_FALSE((std::ranges::output_range<decltype(v), bio::alphabet::cigar>));
196196
EXPECT_FALSE((std::ranges::output_range<decltype(v), char>));
197197

198-
auto const it = v.begin();
199-
EXPECT_EQ(*it, (bio::alphabet::cigar{42, 'I'_cigar_op}));
198+
auto it = v.begin();
199+
EXPECT_EQ(*it, (bio::alphabet::cigar{42, 'I'_cigar_op})); // updates the cache
200+
EXPECT_EQ(*it, (bio::alphabet::cigar{42, 'I'_cigar_op})); // uses the cache
201+
EXPECT_EQ(*std::as_const(it), (bio::alphabet::cigar{42, 'I'_cigar_op})); // uses the cache
202+
203+
auto const it2 = v.begin();
204+
EXPECT_EQ(*it2, (bio::alphabet::cigar{42, 'I'_cigar_op})); // does not update cache, because const
205+
EXPECT_EQ(*it2, (bio::alphabet::cigar{42, 'I'_cigar_op})); // does not use cache, because it wasn't set
200206
}
201207

202208
TEST(view_char_to_cigar, empty)

0 commit comments

Comments
 (0)