Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include <utility> | ||
4 | |||
5 | #include "../Common.h" | ||
6 | #include "../util/TraitImpl.h" | ||
7 | |||
8 | namespace CXXIter { | ||
9 | |||
10 | // ################################################################################################ | ||
11 | // MAP | ||
12 | // ################################################################################################ | ||
13 | namespace op { | ||
14 | /** @private */ | ||
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 | 10 | constexpr FlagLast(TChainInput&& input) : input(std::move(input)) {} | |
28 | }; | ||
29 | } | ||
30 | // ------------------------------------------------------------------------------------------------ | ||
31 | /** @private */ | ||
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 | 30 | static constexpr inline IterValue<Item> next(Self& self) { | |
41 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
|
30 | if(!self.initialized) [[unlikely]] { |
42 | // initialize by populating nextValue with the first element from the input iterator | ||
43 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | self.nextValue = ChainInputIterator::next(self.input); |
44 | 8 | self.initialized = true; | |
45 | } | ||
46 | // next value is empty - the previous element was the last one. | ||
47 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11 times.
|
30 | 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 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
22 | Item item = { std::forward<InputItem>(self.nextValue.value()), false }; |
52 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
22 | self.nextValue = ChainInputIterator::next(self.input); |
53 | 22 | item.second = !self.nextValue.has_value(); | |
54 | 22 | return item; | |
55 | } | ||
56 | 10 | 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 | }; | ||
59 | /** @private */ | ||
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 | } | ||
66 |