Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include <cstdlib> | ||
4 | #include <optional> | ||
5 | |||
6 | #include "../Common.h" | ||
7 | #include "../util/TraitImpl.h" | ||
8 | |||
9 | namespace CXXIter { | ||
10 | |||
11 | // ################################################################################################ | ||
12 | // SKIP WHILE | ||
13 | // ################################################################################################ | ||
14 | namespace op { | ||
15 | /** @private */ | ||
16 | template<typename TChainInput, typename TSkipPredicate> | ||
17 | class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] SkipWhile : public IterApi<SkipWhile<TChainInput, TSkipPredicate>> { | ||
18 | friend struct trait::Iterator<SkipWhile<TChainInput, TSkipPredicate>>; | ||
19 | private: | ||
20 | TChainInput input; | ||
21 | TSkipPredicate skipPredicate; | ||
22 | bool skipEnded = false; | ||
23 | public: | ||
24 | 28 | constexpr SkipWhile(TChainInput&& input, TSkipPredicate skipPredicate) : input(std::move(input)), skipPredicate(skipPredicate) {} | |
25 | }; | ||
26 | } | ||
27 | // ------------------------------------------------------------------------------------------------ | ||
28 | /** @private */ | ||
29 | template<typename TChainInput, typename TSkipPredicate> | ||
30 | struct trait::Iterator<op::SkipWhile<TChainInput, TSkipPredicate>> { | ||
31 | using ChainInputIterator = trait::Iterator<TChainInput>; | ||
32 | using InputItem = typename TChainInput::Item; | ||
33 | // CXXIter Interface | ||
34 | using Self = op::SkipWhile<TChainInput, TSkipPredicate>; | ||
35 | using Item = InputItem; | ||
36 | |||
37 | 37 | static constexpr inline IterValue<Item> next(Self& self) { | |
38 | 22 | while(true) { | |
39 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
37 | auto item = ChainInputIterator::next(self.input); |
40 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
|
49 | if(!item.has_value()) [[unlikely]] { return {}; } |
41 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
34 | if(self.skipEnded) { return item; } |
42 |
3/4✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 4 times.
|
33 | if(!self.skipPredicate(item.value())) { |
43 | 11 | self.skipEnded = true; | |
44 | 11 | return item; | |
45 | } | ||
46 | } | ||
47 | } | ||
48 | 2 | static constexpr inline SizeHint sizeHint(const Self& self) { | |
49 | 2 | SizeHint input = ChainInputIterator::sizeHint(self.input); | |
50 | 4 | return SizeHint(0, input.upperBound); | |
51 | } | ||
52 | static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); } | ||
53 | }; | ||
54 | |||
55 | } | ||
56 |