CXXIter 0.2
Loading...
Searching...
No Matches
TakeWhile.h
1#pragma once
2
3#include <utility>
4#include <optional>
5#include <algorithm>
6
7#include "../Common.h"
8#include "../util/TraitImpl.h"
9
10namespace CXXIter {
11
12 // ################################################################################################
13 // TAKE WHILE
14 // ################################################################################################
15 namespace op {
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 constexpr TakeWhile(TChainInput&& input, TTakePredicate takePredicate) : input(std::move(input)), takePredicate(takePredicate) {}
25 };
26 }
27 // ------------------------------------------------------------------------------------------------
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 static constexpr inline IterValue<Item> next(Self& self) {
38 auto item = ChainInputIterator::next(self.input);
39 if(!item.has_value()) [[unlikely]] { return {}; }
40 // end iterator directly after takePredicate returned false the first time
41 if(self.takePredicate(item.value()) == false) { return {}; }
42 return item;
43 }
44 static constexpr inline SizeHint sizeHint(const Self& self) {
45 SizeHint input = ChainInputIterator::sizeHint(self.input);
46 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}
CXXIter.
Definition: CXXIter.h:48