CXXIter 0.2
Loading...
Searching...
No Matches
SizeHint.h
1#pragma once
2
3#include <optional>
4#include <limits>
5#include <cmath>
6
7namespace CXXIter {
8
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 constexpr size_t expectedResultSize(size_t min = 0) const { return std::min(min, upperBound.value_or(lowerBound)); }
22
23 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 static constexpr std::optional<size_t> upperBoundMin(std::optional<size_t> upperBound1, std::optional<size_t> upperBound2) {
30 if(!upperBound1.has_value()) { return upperBound2; }
31 if(!upperBound2.has_value()) { return upperBound1; }
32 return std::min(upperBound1.value(), upperBound2.value());
33 }
34 void subtract(size_t cnt) {
35 lowerBound = (lowerBound > cnt) ? (lowerBound - cnt) : 0;
36 if(upperBound) {
37 upperBound = (upperBound.value() > cnt) ? (upperBound.value() - cnt) : 0;
38 }
39 }
40 void add(const SizeHint& o) {
41 lowerBound += o.lowerBound;
42 if(upperBound.has_value() && o.upperBound.has_value()) {
43 upperBound = upperBound.value() + o.upperBound.value();
44 } else {
45 upperBound = {};
46 }
47 }
48 };
49
50}
CXXIter.
Definition: CXXIter.h:48
Structure holding the bounds of a CXXIter iterator's estimated length.
Definition: SizeHint.h:15