GCC Code Coverage Report


Directory: ./
File: include/CXXIter/src/SizeHint.h
Date: 2023-01-04 16:32:12
Exec Total Coverage
Lines: 17 17 100.0%
Functions: 5 5 100.0%
Branches: 13 16 81.2%

Line Branch Exec Source
1 #pragma once
2
3 #include <optional>
4 #include <limits>
5 #include <cmath>
6
7 namespace CXXIter {
8
9 /**
10 * @brief Structure holding the bounds of a CXXIter iterator's estimated length.
11 * @details This structure contains a lowerBound and an optional upper bound.
12 * Both are initialized from the source's length (if any), and subsequently edited
13 * by chained iteration accordingly.
14 */
15 struct SizeHint {
16 constexpr static size_t INFINITE = std::numeric_limits<size_t>::max();
17
18 size_t lowerBound;
19 std::optional<size_t> upperBound;
20
21 157 constexpr size_t expectedResultSize(size_t min = 0) const { return std::min(min, upperBound.value_or(lowerBound)); }
22
23 392 constexpr SizeHint(size_t lowerBound = 0, std::optional<size_t> upperBound = {}) : lowerBound(lowerBound), upperBound(upperBound) {}
24
25 static constexpr std::optional<size_t> upperBoundMax(std::optional<size_t> upperBound1, std::optional<size_t> upperBound2) {
26 if(!upperBound1.has_value() || !upperBound2.has_value()) { return {}; } // no upperbound is like Infinity -> higher
27 return std::max(upperBound1.value(), upperBound2.value());
28 }
29 78 static constexpr std::optional<size_t> upperBoundMin(std::optional<size_t> upperBound1, std::optional<size_t> upperBound2) {
30
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 50 times.
78 if(!upperBound1.has_value()) { return upperBound2; }
31
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if(!upperBound2.has_value()) { return upperBound1; }
32 50 return std::min(upperBound1.value(), upperBound2.value());
33 }
34 14 void subtract(size_t cnt) {
35
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5 times.
14 lowerBound = (lowerBound > cnt) ? (lowerBound - cnt) : 0;
36
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if(upperBound) {
37
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 5 times.
14 upperBound = (upperBound.value() > cnt) ? (upperBound.value() - cnt) : 0;
38 }
39 14 }
40 8 void add(const SizeHint& o) {
41 8 lowerBound += o.lowerBound;
42
5/6
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 1 times.
8 if(upperBound.has_value() && o.upperBound.has_value()) {
43 7 upperBound = upperBound.value() + o.upperBound.value();
44 } else {
45 1 upperBound = {};
46 }
47 8 }
48 };
49
50 }
51