-
-
Save rmartinho/f3d5cfb3c62cae75d867db546a0f13a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct adaptor : public ranges::adaptor_base { | |
public: | |
adaptor() = default; | |
explicit adaptor(ranges::range_sentinel_t<Rng> end) | |
: state(), end(end) {} | |
typename Encoding::code_unit get(ranges::range_iterator_t<Rng> it) const { | |
if(position < 0) { | |
if(depleted) { | |
encoded = Encoding::flush(state); | |
} else { | |
encoded = Encoding::encode_one(*it, state); | |
} | |
position = 0; | |
} | |
return encoded[position]; | |
} | |
void next(ranges::range_iterator_t<Rng>& it) { | |
++position; | |
if(position == static_cast<ptrdiff_t>(encoded.size())) { | |
if(it == end) { | |
depleted = true; | |
} | |
if(!depleted) ++it; | |
position = -1; | |
} | |
} | |
bool equal(ranges::range_iterator_t<Rng> it0, ranges::range_iterator_t<Rng> it1, adaptor const & other) const { | |
return it0 == it1 && position == other.position && depleted == other.depleted; | |
} | |
private: | |
mutable std::vector<typename Encoding::code_unit> encoded; | |
mutable std::ptrdiff_t position = -1; | |
mutable typename Encoding::state state; | |
bool depleted = false; | |
ranges::range_sentinel_t<Rng> end; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment