Hyper API for C++ 0.0.25080
Hyper client library for C++ applications
Loading...
Searching...
No Matches
Numeric.hpp
Go to the documentation of this file.
1
5#ifndef TABLEAU_HYPER_NUMERIC_HPP
6#define TABLEAU_HYPER_NUMERIC_HPP
7
8#include <cstdint>
10#include <string>
11#include <type_traits>
12#include <hyperapi/hyperapi.h>
13
14class BigNumeric_rawValue_Test;
15class Numeric_rawValue_Test;
16
17namespace std {
19template <>
20struct hash<hyper_data128_t> {
22 size_t operator()(hyper_data128_t n) const noexcept {
23 return hash<uint64_t>()(n.data[0]) ^ hash<uint64_t>()(n.data[1]);
24 }
25};
26}
27
28namespace hyperapi {
29template <unsigned precision_value, unsigned scale_value>
30class Numeric;
31namespace internal {
32template <typename ReturnType>
33struct ValueAccess;
34struct ValueInserter;
35template <unsigned p, unsigned s>
36// NOLINTNEXTLINE(readability-redundant-typename)
37typename Numeric<p, s>::data_t numericInternalRepresentation(const Numeric<p, s>& numeric) noexcept;
38}
40inline bool operator==(const hyper_data128_t& a, const hyper_data128_t& b) noexcept { return (a.data[0] == b.data[0]) && (a.data[1] == b.data[1]); }
42inline bool operator>(const hyper_data128_t& a, const hyper_data128_t& b) noexcept {
43 if (a.data[1] != b.data[1]) return (static_cast<int64_t>(a.data[1]) > static_cast<int64_t>(b.data[1]));
44 if (a.data[0] != b.data[0]) return (a.data[0] > b.data[0]);
45 return false;
46}
47
51template <unsigned precision_value, unsigned scale_value>
52class Numeric final {
53 public:
54 static constexpr unsigned precision = precision_value;
55 static constexpr unsigned scale = scale_value;
56 static_assert(precision >= scale, "The precision of a numeric must be greater or equal than the scale");
57 static_assert(precision <= 38, "Precision must be at most 38");
58 // NOLINTNEXTLINE(modernize-type-traits, readability-redundant-typename)
59 using data_t = typename std::conditional<precision <= 18, int64_t, hyper_data128_t>::type;
60
64 Numeric() noexcept = default;
65
70 Numeric(short value);
75 Numeric(unsigned short value);
80 Numeric(int value);
85 Numeric(unsigned value);
90 Numeric(long value);
95 Numeric(unsigned long value);
100 Numeric(long long value);
105 Numeric(unsigned long long value);
110 Numeric(float value);
115 Numeric(double value);
120 Numeric(long double value);
121
126 template <unsigned otherPrecision, unsigned otherScale>
127 explicit Numeric(hyperapi::Numeric<otherPrecision, otherScale> other);
128
133 explicit Numeric(hyperapi::string_view value);
134
139 std::string stringValue() const;
140
145 int64_t intValue() const;
150 operator int64_t() const { return intValue(); }
151
155 float floatValue() const noexcept;
159 operator float() const noexcept { return floatValue(); }
160
164 double doubleValue() const noexcept;
168 operator double() const noexcept { return doubleValue(); }
169
174 std::string toString() const;
175
179 friend bool operator==(const Numeric& a, const Numeric& b) noexcept { return a.value_ == b.value_; }
183 friend bool operator>(const Numeric& a, const Numeric& b) noexcept { return a.value_ > b.value_; }
187 friend bool operator!=(const Numeric& a, const Numeric& b) noexcept { return !(a == b); }
191 friend bool operator<(const Numeric& a, const Numeric& b) noexcept { return (b > a); }
195 friend bool operator<=(const Numeric& a, const Numeric& b) noexcept { return !(a > b); }
199 friend bool operator>=(const Numeric& a, const Numeric& b) noexcept { return !(a < b); }
200
202 friend std::ostream& operator<<(std::ostream& os, const Numeric& obj) { return os << obj.toString(); }
203
204 private:
205 friend class ::Numeric_rawValue_Test;
206 friend class ::BigNumeric_rawValue_Test;
207 template <unsigned otherPrecision, unsigned otherScale>
208 friend class Numeric;
209 friend class Inserter;
210 template <typename ReturnType>
211 friend struct internal::ValueAccess;
212 friend struct internal::ValueInserter;
213 template <typename T>
214 friend struct std::hash;
215 // NOLINTNEXTLINE(readability-redundant-typename)
216 friend typename Numeric<precision, scale>::data_t internal::numericInternalRepresentation(const Numeric<precision, scale>& numeric) noexcept;
217
218 struct raw_t {};
219
225 explicit Numeric(data_t rawNumeric, raw_t) noexcept
226 : value_(rawNumeric) {
227 }
228
237 explicit Numeric(int64_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
238
247 explicit Numeric(hyper_data128_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
248
252 data_t value_{};
253};
254}
255
256namespace std {
258template <unsigned p, unsigned s>
259struct hash<hyperapi::Numeric<p, s>> {
261 size_t operator()(hyperapi::Numeric<p, s> n) const noexcept {
262 return std::hash<typename hyperapi::Numeric<p, s>::data_t>()(n.value_);
263 }
264};
265}
266
267#include <hyperapi/impl/Numeric.impl.hpp> // IWYU pragma: export
268
269#endif
An inserter.
Definition Inserter.hpp:23
A fixed-point numeric data value with scale fraction digits and precision digits overall.
Definition Numeric.hpp:52
float floatValue() const noexcept
Gets a float representation of this value; may lose accuracy.
friend std::ostream & operator<<(std::ostream &os, const Numeric &obj)
Stream output operator.
Definition Numeric.hpp:202
std::string toString() const
Gets a string representation for debugging.
Numeric() noexcept=default
Default constructor.
friend bool operator<(const Numeric &a, const Numeric &b) noexcept
Less than operator.
Definition Numeric.hpp:191
int64_t intValue() const
Gets an integer representation of this value; if the value has fraction digits, these will be truncat...
friend bool operator==(const Numeric &a, const Numeric &b) noexcept
Equality operator.
Definition Numeric.hpp:179
friend bool operator>(const Numeric &a, const Numeric &b) noexcept
Greater operator.
Definition Numeric.hpp:183
friend bool operator<=(const Numeric &a, const Numeric &b) noexcept
Less than or equal operator.
Definition Numeric.hpp:195
friend bool operator!=(const Numeric &a, const Numeric &b) noexcept
Not equal operator.
Definition Numeric.hpp:187
friend bool operator>=(const Numeric &a, const Numeric &b) noexcept
Greater or equal operator.
Definition Numeric.hpp:199
double doubleValue() const noexcept
Gets a double representation of this value; may lose accuracy.
std::string stringValue() const
Gets an exact string representation, which is round-trip compatible with the constructor,...
Describes an object that can refer to a constant, contiguous sequence of char-like objects.
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:14
bool operator==(const DatabaseName &a, const DatabaseName &b) noexcept
Equality operator.
bool operator>(const DatabaseName &a, const DatabaseName &b) noexcept
Greater operator.
size_t operator()(hyper_data128_t n) const noexcept
Calculates the hash value of the given hyper_data128_t.
Definition Numeric.hpp:22
size_t operator()(hyperapi::Numeric< p, s > n) const noexcept
Calculates the hash value of the given numeric.
Definition Numeric.hpp:261