CXXIter 0.2
Loading...
Searching...
No Matches
FlagLast.h
1#pragma once
2
3#include <utility>
4
5#include "../Common.h"
6#include "../util/TraitImpl.h"
7
8namespace CXXIter {
9
10 // ################################################################################################
11 // MAP
12 // ################################################################################################
13 namespace op {
15 template<typename TChainInput>
16 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] FlagLast : public IterApi<FlagLast<TChainInput>> {
17 friend struct trait::Iterator<FlagLast<TChainInput>>;
18 friend struct trait::DoubleEndedIterator<FlagLast<TChainInput>>;
19 friend struct trait::ExactSizeIterator<FlagLast<TChainInput>>;
20
21 using InputItem = typename TChainInput::Item;
22 private:
23 TChainInput input;
24 bool initialized = false;
25 IterValue<InputItem> nextValue;
26 public:
27 constexpr FlagLast(TChainInput&& input) : input(std::move(input)) {}
28 };
29 }
30 // ------------------------------------------------------------------------------------------------
32 template<typename TChainInput>
33 struct trait::Iterator<op::FlagLast<TChainInput>> {
34 using ChainInputIterator = trait::Iterator<TChainInput>;
35 using InputItem = typename TChainInput::Item;
36 // CXXIter Interface
37 using Self = op::FlagLast<TChainInput>;
38 using Item = std::pair<InputItem, bool>;
39
40 static constexpr inline IterValue<Item> next(Self& self) {
41 if(!self.initialized) [[unlikely]] {
42 // initialize by populating nextValue with the first element from the input iterator
43 self.nextValue = ChainInputIterator::next(self.input);
44 self.initialized = true;
45 }
46 // next value is empty - the previous element was the last one.
47 if(!self.nextValue.has_value()) [[unlikely]] { return {}; }
48
49 // Return the current nextValue, but fetch the next one from the input and flag the
50 // returned element with a boolean that specifies whether there was a further element.
51 Item item = { std::forward<InputItem>(self.nextValue.value()), false };
52 self.nextValue = ChainInputIterator::next(self.input);
53 item.second = !self.nextValue.has_value();
54 return item;
55 }
56 static constexpr inline SizeHint sizeHint(const Self& self) { return ChainInputIterator::sizeHint(self.input); }
57 static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); }
58 };
60 template<CXXIterExactSizeIterator TChainInput>
61 struct trait::ExactSizeIterator<op::FlagLast<TChainInput>> {
62 static constexpr inline size_t size(const op::FlagLast<TChainInput>& self) { return trait::ExactSizeIterator<TChainInput>::size(self.input); }
63 };
64
65}
CXXIter.
Definition: CXXIter.h:48