CXXIter 0.2
Loading...
Searching...
No Matches
Filter.h
1#pragma once
2
3#include "../Common.h"
4#include "../util/TraitImpl.h"
5
6namespace CXXIter {
7
8 // ################################################################################################
9 // FILTER
10 // ################################################################################################
11 namespace op {
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 constexpr Filter(TChainInput&& input, TFilterFn filterFn) : input(std::move(input)), filterFn(filterFn) {}
24 };
25 }
26 // ------------------------------------------------------------------------------------------------
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 static constexpr inline IterValue<Item> next(Self& self) {
36 while(true) {
37 auto item = ChainInputIterator::next(self.input);
38 if(!item.has_value()) [[unlikely]] { return {}; }
39 if(self.filterFn(item.value())) { return item; }
40 }
41 }
42 static constexpr inline SizeHint sizeHint(const Self& self) {
43 SizeHint input = ChainInputIterator::sizeHint(self.input);
44 return SizeHint(0, input.upperBound);
45 }
46 static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); }
47 };
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 static constexpr inline IterValue<Item> nextBack(Self& self) {
57 while(true) {
58 auto item = ChainInputIterator::nextBack(self.input);
59 if(!item.has_value()) [[unlikely]] { return {}; }
60 if(self.filterFn(item.value())) { return item; }
61 }
62 }
63 };
64
65}
CXXIter.
Definition: CXXIter.h:48