GCC Code Coverage Report


Directory: ./
File: include/CXXIter/src/op/TakeN.h
Date: 2023-01-04 16:32:12
Exec Total Coverage
Lines: 14 14 100.0%
Functions: 15 15 100.0%
Branches: 6 10 60.0%

Line Branch Exec Source
1 #pragma once
2
3 #include <utility>
4
5 #include "../Common.h"
6
7 namespace CXXIter {
8
9 // ################################################################################################
10 // TAKE WHILE
11 // ################################################################################################
12 namespace op {
13 /** @private */
14 template<typename TChainInput>
15 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] TakeN : public IterApi<TakeN<TChainInput>> {
16 friend struct trait::Iterator<TakeN<TChainInput>>;
17 private:
18 TChainInput input;
19 size_t n;
20 size_t remaining;
21 public:
22 27 constexpr TakeN(TChainInput&& input, size_t n) : input(std::move(input)), n(n), remaining(n) {}
23 };
24 }
25 // ------------------------------------------------------------------------------------------------
26 /** @private */
27 template<typename TChainInput>
28 struct trait::Iterator<op::TakeN<TChainInput>> {
29 using ChainInputIterator = trait::Iterator<TChainInput>;
30 using InputItem = typename TChainInput::Item;
31 // CXXIter Interface
32 using Self = op::TakeN<TChainInput>;
33 using Item = InputItem;
34
35 139 static constexpr inline IterValue<Item> next(Self& self) {
36
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 106 times.
139 if(self.remaining == 0) [[unlikely]] { return {}; }
37
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
129 auto item = ChainInputIterator::next(self.input);
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 106 times.
129 if(!item.has_value()) [[unlikely]] { return {}; }
39 127 self.remaining -= 1;
40 127 return item;
41 }
42 11 static constexpr inline SizeHint sizeHint(const Self& self) {
43
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
11 SizeHint input = ChainInputIterator::sizeHint(self.input);
44 return SizeHint(
45 11 std::min(input.lowerBound, self.n),
46 11 SizeHint::upperBoundMin(input.upperBound, self.n)
47
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
22 );
48 }
49 static constexpr inline size_t advanceBy(Self& self, size_t n) {
50 size_t skipN = ChainInputIterator::advanceby(self.input, n);
51 if(self.n < skipN) { self.n = 0; } else { self.n -= skipN; }
52 return skipN;
53 }
54 };
55 /** @private */
56 template<CXXIterExactSizeIterator TChainInput>
57 struct trait::ExactSizeIterator<op::TakeN<TChainInput>> {
58 1 static constexpr inline size_t size(const op::TakeN<TChainInput>& self) {
59 1 return trait::Iterator<op::TakeN<TChainInput>>::sizeHint(self).lowerBound;
60 }
61 };
62 }
63