Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include <utility> | ||
4 | #include <cstdlib> | ||
5 | #include <algorithm> | ||
6 | |||
7 | #include "../Common.h" | ||
8 | #include "../util/SaturatingArithmetic.h" | ||
9 | #include "../util/TraitImpl.h" | ||
10 | |||
11 | namespace CXXIter { | ||
12 | |||
13 | // ################################################################################################ | ||
14 | // INTERSPERSER | ||
15 | // ################################################################################################ | ||
16 | namespace op { | ||
17 | /** @private */ | ||
18 | template<typename TChainInput, typename TSeparatorInput> | ||
19 | class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] Intersperser : public IterApi<Intersperser<TChainInput, TSeparatorInput>> { | ||
20 | friend struct trait::Iterator<Intersperser<TChainInput, TSeparatorInput>>; | ||
21 | friend struct trait::ExactSizeIterator<Intersperser<TChainInput, TSeparatorInput>>; | ||
22 | enum class IntersperserState { Uninitialized, Item, Separator }; | ||
23 | private: | ||
24 | TChainInput input; | ||
25 | TSeparatorInput separatorInput; | ||
26 | IterValue<typename TChainInput::Item> nextItem; | ||
27 | IntersperserState intersperserState = IntersperserState::Uninitialized; | ||
28 | public: | ||
29 | 34 | constexpr Intersperser(TChainInput&& input, TSeparatorInput&& separatorInput) : input(std::move(input)), separatorInput(std::move(separatorInput)) {} | |
30 | }; | ||
31 | } | ||
32 | // ------------------------------------------------------------------------------------------------ | ||
33 | /** @private */ | ||
34 | template<typename TChainInput, typename TSeparatorInput> | ||
35 | struct trait::Iterator<op::Intersperser<TChainInput, TSeparatorInput>> { | ||
36 | using ChainInputIterator = trait::Iterator<TChainInput>; | ||
37 | using SeparatorInputIterator = trait::Iterator<TSeparatorInput>; | ||
38 | // CXXIter Interface | ||
39 | using Self = op::Intersperser<TChainInput, TSeparatorInput>; | ||
40 | using Item = typename ChainInputIterator::Item; | ||
41 | |||
42 | 56 | static constexpr inline IterValue<Item> next(Self& self) { | |
43 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
|
56 | if(self.intersperserState == Self::IntersperserState::Uninitialized) [[unlikely]] { |
44 | 8 | self.nextItem = ChainInputIterator::next(self.input); | |
45 | 8 | self.intersperserState = Self::IntersperserState::Item; | |
46 | } | ||
47 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
|
56 | if(!self.nextItem.has_value()) [[unlikely]] { return {}; } |
48 | |||
49 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
|
52 | if(self.intersperserState == Self::IntersperserState::Item) { |
50 | 28 | self.intersperserState = Self::IntersperserState::Separator; | |
51 | 28 | IterValue<Item> item; | |
52 | 28 | item.swap(self.nextItem); | |
53 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
28 | self.nextItem = ChainInputIterator::next(self.input); |
54 | 28 | return item; | |
55 | 6 | } else { | |
56 | 24 | self.intersperserState = Self::IntersperserState::Item; | |
57 | 24 | return SeparatorInputIterator::next(self.separatorInput); | |
58 | } | ||
59 | } | ||
60 | 21 | static constexpr inline SizeHint sizeHint(const Self& self) { | |
61 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
21 | SizeHint input = ChainInputIterator::sizeHint(self.input); |
62 | 21 | SizeHint separator = SeparatorInputIterator::sizeHint(self.separatorInput); | |
63 | |||
64 | 21 | SizeHint result = input; | |
65 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
21 | if(result.lowerBound > 0) { |
66 | 18 | size_t sepCnt = std::min((result.lowerBound - 1), separator.lowerBound); | |
67 | 18 | result.lowerBound = (util::SaturatingArithmetic<size_t>(sepCnt) + sepCnt + 1).get(); | |
68 | } | ||
69 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
|
21 | if(result.upperBound.value_or(0) > 0) { |
70 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
14 | size_t sepCnt = std::min((result.upperBound.value() - 1), separator.upperBound.value_or(SizeHint::INFINITE)); |
71 | 14 | result.upperBound = (util::SaturatingArithmetic<size_t>(sepCnt) + sepCnt + 1).get(); | |
72 | } | ||
73 | 42 | return result; | |
74 | } | ||
75 | static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); } | ||
76 | }; | ||
77 | /** @private */ | ||
78 | template<CXXIterExactSizeIterator TChainInput, CXXIterExactSizeIterator TSeparatorInput> | ||
79 | struct trait::ExactSizeIterator<op::Intersperser<TChainInput, TSeparatorInput>> { | ||
80 | 5 | static constexpr inline size_t size(const op::Intersperser<TChainInput, TSeparatorInput>& self) { | |
81 | 5 | return trait::Iterator<op::Intersperser<TChainInput, TSeparatorInput>>::sizeHint(self).lowerBound; | |
82 | } | ||
83 | }; | ||
84 | |||
85 | } | ||
86 |