Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include "../Common.h" | ||
4 | #include "../util/TraitImpl.h" | ||
5 | |||
6 | namespace CXXIter { | ||
7 | |||
8 | // ################################################################################################ | ||
9 | // FILTER | ||
10 | // ################################################################################################ | ||
11 | namespace op { | ||
12 | /** @private */ | ||
13 | template<typename TChainInput, typename TFilterFn> | ||
14 | class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] Filter : public IterApi<Filter<TChainInput, TFilterFn>> { | ||
15 | friend struct trait::Iterator<Filter<TChainInput, TFilterFn>>; | ||
16 | friend struct trait::DoubleEndedIterator<Filter<TChainInput, TFilterFn>>; | ||
17 | private: | ||
18 | using InputItem = typename TChainInput::Item; | ||
19 | |||
20 | TChainInput input; | ||
21 | TFilterFn filterFn; | ||
22 | public: | ||
23 | 104 | constexpr Filter(TChainInput&& input, TFilterFn filterFn) : input(std::move(input)), filterFn(filterFn) {} | |
24 | }; | ||
25 | } | ||
26 | // ------------------------------------------------------------------------------------------------ | ||
27 | /** @private */ | ||
28 | template<typename TChainInput, typename TFilterFn> | ||
29 | struct trait::Iterator<op::Filter<TChainInput, TFilterFn>> { | ||
30 | using ChainInputIterator = trait::Iterator<TChainInput>; | ||
31 | // CXXIter Interface | ||
32 | using Self = op::Filter<TChainInput, TFilterFn>; | ||
33 | using Item = typename ChainInputIterator::Item; | ||
34 | |||
35 | 291 | static constexpr inline IterValue<Item> next(Self& self) { | |
36 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
110 | while(true) { |
37 |
1/2✓ Branch 1 taken 149 times.
✗ Branch 2 not taken.
|
291 | auto item = ChainInputIterator::next(self.input); |
38 |
2/2✓ Branch 1 taken 23 times.
✓ Branch 2 taken 126 times.
|
431 | if(!item.has_value()) [[unlikely]] { return {}; } |
39 |
6/8✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 82 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 8 times.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
|
246 | if(self.filterFn(item.value())) { return item; } |
40 | } | ||
41 | } | ||
42 | 54 | static constexpr inline SizeHint sizeHint(const Self& self) { | |
43 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
54 | SizeHint input = ChainInputIterator::sizeHint(self.input); |
44 | 106 | return SizeHint(0, input.upperBound); | |
45 | } | ||
46 | 4 | static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); } | |
47 | }; | ||
48 | /** @private */ | ||
49 | template<CXXIterDoubleEndedIterator TChainInput, typename TFilterFn> | ||
50 | struct trait::DoubleEndedIterator<op::Filter<TChainInput, TFilterFn>> { | ||
51 | using ChainInputIterator = trait::DoubleEndedIterator<TChainInput>; | ||
52 | // CXXIter Interface | ||
53 | using Self = op::Filter<TChainInput, TFilterFn>; | ||
54 | using Item = typename ChainInputIterator::Item; | ||
55 | |||
56 | 10 | static constexpr inline IterValue<Item> nextBack(Self& self) { | |
57 | 3 | while(true) { | |
58 | 10 | auto item = ChainInputIterator::nextBack(self.input); | |
59 | 15 | if(!item.has_value()) [[unlikely]] { return {}; } | |
60 | 8 | if(self.filterFn(item.value())) { return item; } | |
61 | } | ||
62 | } | ||
63 | }; | ||
64 | |||
65 | } | ||
66 |