XX CMake Package
Loading...
Searching...
No Matches
XXRange.h
Go to the documentation of this file.
1#ifndef XXRangeH
2#define XXRangeH
3
4#include "XXCppExportDef.h"
5
6#include <cstddef>
7#include <iterator> // For std::forward_iterator_tag
8#include <type_traits>
9
10namespace XX
11{
12 // optional compiler tests
13
19
20 template <typename TestType>
21 using isIntegerType = typename std::enable_if<std::is_integral<TestType>::value, bool>::type;
22
23 template <typename TestType>
24 using isFloatType = typename std::enable_if<std::is_floating_point<TestType>::value, bool>::type;
25
26 template <typename TestType>
27 using isSigned = typename std::enable_if<std::is_signed<TestType>::value, bool>::type;
28
29 template <typename TestType>
30 using isUnsigned = typename std::enable_if<!std::is_signed<TestType>::value, bool>::type;
31
33
34 struct Range
35 {
38
39 template <typename DataType>
40 class Finder
41 {
42 public:
44
45 public:
47 template <typename TestType = DataType, isSigned<TestType> = true>
48 void reset();
49
51 template <typename TestType = DataType, isUnsigned<TestType> = true>
52 void reset();
53
55 void init(const DataType& value);
56
58 void observe(const DataType& value);
59
60 const DataType& min() const;
61 const DataType& max() const;
62 DataType diff() const;
63
64 // only for integer types
65 template <typename TestType = DataType, isIntegerType<TestType> = true>
66 size_t length() const;
67
68 template <typename TestType = DataType, isIntegerType<TestType> = true>
69 DataType value(const size_t index) const;
70
71 private:
72 DataType minValue;
73 DataType maxValue;
74 };
75
78
80 {
81 public:
82 Mapper(const float& minInput, const float& maxInput, const float& minOutput, const float& maxOutput);
83
84 public:
85 float operator()(const float& input) const;
86
87 void setMinInput(const float& value);
88 void setMaxInput(const float& value);
89 void setMinOutput(const float& value);
90 void setMaxOutput(const float& value);
91
92 private:
93 void updateScale();
94
95 private:
96 float minInput;
97 float maxInput;
98 float minOutput;
99 float maxOutput;
100 float scale;
101 };
102
105
106 class Spread
107 {
108 public:
109 class Iterator
110 {
111 public: // for std:algorithm
112 using iterator_category = std::forward_iterator_tag;
113 using difference_type = std::ptrdiff_t;
114 using value_type = int;
115 using pointer = void;
116 using reference = void;
117
118 public:
119 int operator*() const;
120 Iterator& operator++();
121
122 bool operator==(const Iterator& other) const;
123 bool operator!=(const Iterator& other) const;
124
125 private:
126 Iterator(int startValue, int step); // start iterator
127 Iterator(int endValue); // end iterator
128
129 private:
130 friend class Spread;
131
132 private:
133 int number;
134 const int step;
135 const bool isEnd;
136 };
137
138 public:
139 Spread(int max);
140 Spread(int min, int max, int step = 1);
141
142 public:
145
146 private:
147 const int min;
148 const int max;
149 const int step;
150 };
151
157
159
160 template <typename DataType>
161 static const DataType& clamp(const DataType& value, const DataType& min, const DataType& max);
162
164
165 template <typename DataType>
166 static const DataType& min(const DataType& value1, const DataType& value2);
167
169
170 template <typename DataType>
171 static const DataType& max(const DataType& value1, const DataType& value2);
172
174 };
175} // namespace XX
176
177#ifndef XXRangeHPP
178#include "XXRange.hpp"
179#endif // NOT XXRangeHPP
180
181#endif // NOT XXRangeH
#define XXCPP_DECLSPEC
Definition XXCppExportDef.h:23
void observe(const DataType &value)
test value and update min / max
size_t length() const
void init(const DataType &value)
min and max to value
DataType diff() const
void reset()
min to datatype max and max to datatype min
DataType value(const size_t index) const
const DataType & min() const
const DataType & max() const
Mapper(const float &minInput, const float &maxInput, const float &minOutput, const float &maxOutput)
void setMaxInput(const float &value)
void setMinOutput(const float &value)
void setMaxOutput(const float &value)
float operator()(const float &input) const
void setMinInput(const float &value)
Definition XXRange.h:110
friend class Spread
Definition XXRange.h:130
std::ptrdiff_t difference_type
Definition XXRange.h:113
void pointer
Definition XXRange.h:115
bool operator==(const Iterator &other) const
bool operator!=(const Iterator &other) const
std::forward_iterator_tag iterator_category
Definition XXRange.h:112
int value_type
Definition XXRange.h:114
void reference
Definition XXRange.h:116
Spread(int min, int max, int step=1)
Iterator begin()
typename std::enable_if<!std::is_signed< TestType >::value, bool >::type isUnsigned
Definition XXRange.h:30
typename std::enable_if< std::is_signed< TestType >::value, bool >::type isSigned
Definition XXRange.h:27
typename std::enable_if< std::is_integral< TestType >::value, bool >::type isIntegerType
Definition XXRange.h:21
typename std::enable_if< std::is_floating_point< TestType >::value, bool >::type isFloatType
Definition XXRange.h:24
static const DataType & min(const DataType &value1, const DataType &value2)
return the minimum of two values
static const DataType & clamp(const DataType &value, const DataType &min, const DataType &max)
clamp a value to a range defined by a min and max value
static const DataType & max(const DataType &value1, const DataType &value2)
return the maximum of two values
Definition XXPopulatedAbstract.h:11
Definition XXRange.h:35