Hyper API for C++ 0.0.24081
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>
36typename Numeric<p, s>::data_t numericInternalRepresentation(const Numeric<p, s>& numeric) noexcept;
37}
39inline 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]); }
41inline bool operator>(const hyper_data128_t& a, const hyper_data128_t& b) noexcept {
42 if (a.data[1] != b.data[1]) return (static_cast<int64_t>(a.data[1]) > static_cast<int64_t>(b.data[1]));
43 if (a.data[0] != b.data[0]) return (a.data[0] > b.data[0]);
44 return false;
45}
46
50template <unsigned precision_value, unsigned scale_value>
51class Numeric final {
52 public:
53 static constexpr unsigned precision = precision_value;
54 static constexpr unsigned scale = scale_value;
55 static_assert(precision >= scale, "The precision of a numeric must be greater or equal than the scale");
56 static_assert(precision <= 38, "Precision must be at most 38");
57 // NOLINTNEXTLINE(modernize-type-traits)
58 using data_t = typename std::conditional<precision <= 18, int64_t, hyper_data128_t>::type;
59
63 Numeric() noexcept {}
64
69 Numeric(short value);
74 Numeric(unsigned short value);
79 Numeric(int value);
84 Numeric(unsigned value);
89 Numeric(long value);
94 Numeric(unsigned long value);
99 Numeric(long long value);
104 Numeric(unsigned long long value);
109 Numeric(float value);
114 Numeric(double value);
119 Numeric(long double value);
120
125 template <unsigned otherPrecision, unsigned otherScale>
127
133
138 std::string stringValue() const;
139
144 int64_t intValue() const;
149 operator int64_t() const { return intValue(); }
150
154 float floatValue() const noexcept;
158 operator float() const noexcept { return floatValue(); }
159
163 double doubleValue() const noexcept;
167 operator double() const noexcept { return doubleValue(); }
168
173 std::string toString() const;
174
178 friend bool operator==(const Numeric& a, const Numeric& b) noexcept { return a.value_ == b.value_; }
182 friend bool operator>(const Numeric& a, const Numeric& b) noexcept { return a.value_ > b.value_; }
186 friend bool operator!=(const Numeric& a, const Numeric& b) noexcept { return !(a == b); }
190 friend bool operator<(const Numeric& a, const Numeric& b) noexcept { return (b > a); }
194 friend bool operator<=(const Numeric& a, const Numeric& b) noexcept { return !(a > b); }
198 friend bool operator>=(const Numeric& a, const Numeric& b) noexcept { return !(a < b); }
199
201 friend std::ostream& operator<<(std::ostream& os, const Numeric& obj) { return os << obj.toString(); }
202
203 private:
204 friend class ::Numeric_rawValue_Test;
205 friend class ::BigNumeric_rawValue_Test;
206 template <unsigned otherPrecision, unsigned otherScale>
207 friend class Numeric;
208 friend class Inserter;
209 template <typename ReturnType>
210 friend struct internal::ValueAccess;
211 friend struct internal::ValueInserter;
212 template <typename T>
213 friend struct std::hash;
214 friend typename Numeric<precision, scale>::data_t internal::numericInternalRepresentation(const Numeric<precision, scale>& numeric) noexcept;
215
216 struct raw_t {};
217
223 explicit Numeric(data_t rawNumeric, raw_t) noexcept
224 : value_(rawNumeric) {
225 }
226
235 explicit Numeric(int64_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
236
245 explicit Numeric(hyper_data128_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
246
250 data_t value_{};
251};
252}
253
254namespace std {
256template <unsigned p, unsigned s>
257struct hash<hyperapi::Numeric<p, s>> {
259 size_t operator()(hyperapi::Numeric<p, s> n) const noexcept {
260 return std::hash<typename hyperapi::Numeric<p, s>::data_t>()(n.value_);
261 }
262};
263}
264
265#include <hyperapi/impl/Numeric.impl.hpp> // IWYU pragma: export
266
267#endif
An inserter.
Definition Inserter.hpp:23
A fixed-point numeric data value with scale fraction digits and precision digits overall.
Definition Numeric.hpp:51
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:201
std::string toString() const
Gets a string representation for debugging.
friend bool operator<(const Numeric &a, const Numeric &b) noexcept
Less than operator.
Definition Numeric.hpp:190
int64_t intValue() const
Gets an integer representation of this value; if the value has fraction digits, these will be truncat...
Numeric(short value)
Creates a numeric value from an integer.
Numeric(unsigned short value)
Creates a numeric value from an integer.
friend bool operator==(const Numeric &a, const Numeric &b) noexcept
Equality operator.
Definition Numeric.hpp:178
friend bool operator>(const Numeric &a, const Numeric &b) noexcept
Greater operator.
Definition Numeric.hpp:182
Numeric(unsigned long long value)
Creates a numeric value from an integer.
Numeric(hyperapi::Numeric< otherPrecision, otherScale > other)
Creates a numeric value from another numeric value with different precision and scale.
Numeric(unsigned value)
Creates a numeric value from an integer.
Numeric() noexcept
Default constructor.
Definition Numeric.hpp:63
friend bool operator<=(const Numeric &a, const Numeric &b) noexcept
Less than or equal operator.
Definition Numeric.hpp:194
friend bool operator!=(const Numeric &a, const Numeric &b) noexcept
Not equal operator.
Definition Numeric.hpp:186
Numeric(unsigned long value)
Creates a numeric value from an integer.
Numeric(hyperapi::string_view value)
Creates a numeric value from a string representation.
Numeric(long value)
Creates a numeric value from an integer.
friend bool operator>=(const Numeric &a, const Numeric &b) noexcept
Greater or equal operator.
Definition Numeric.hpp:198
double doubleValue() const noexcept
Gets a double representation of this value; may lose accuracy.
Numeric(long long value)
Creates a numeric value from an integer.
std::string stringValue() const
Gets an exact string representation, which is round-trip compatible with the constructor,...
Numeric(long double value)
Creates a numeric value from a double; may lose accuracy.
Numeric(int value)
Creates a numeric value from an integer.
Numeric(float value)
Creates a numeric value from a float; may lose accuracy.
Numeric(double value)
Creates a numeric value from a double; may lose accuracy.
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:259