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