CXXIter 0.2
Loading...
Searching...
No Matches
SkipWhile.h
1#pragma once
2
3#include <cstdlib>
4#include <optional>
5
6#include "../Common.h"
7#include "../util/TraitImpl.h"
8
9namespace CXXIter {
10
11 // ################################################################################################
12 // SKIP WHILE
13 // ################################################################################################
14 namespace op {
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 constexpr SkipWhile(TChainInput&& input, TSkipPredicate skipPredicate) : input(std::move(input)), skipPredicate(skipPredicate) {}
25 };
26 }
27 // ------------------------------------------------------------------------------------------------
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 static constexpr inline IterValue<Item> next(Self& self) {
38 while(true) {
39 auto item = ChainInputIterator::next(self.input);
40 if(!item.has_value()) [[unlikely]] { return {}; }
41 if(self.skipEnded) { return item; }
42 if(!self.skipPredicate(item.value())) {
43 self.skipEnded = true;
44 return item;
45 }
46 }
47 }
48 static constexpr inline SizeHint sizeHint(const Self& self) {
49 SizeHint input = ChainInputIterator::sizeHint(self.input);
50 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}
CXXIter.
Definition: CXXIter.h:48