Hyper API for C++ 0.0.19691
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>
11#include <string>
12#include <type_traits>
13#include <hyperapi/hyperapi.h>
14
15class BigNumeric_rawValue_Test;
16class Numeric_rawValue_Test;
17
18namespace std {
20template <>
21struct hash<hyper_data128_t> {
23 size_t operator()(hyper_data128_t n) const noexcept {
24 return hash<uint64_t>()(n.data[0]) ^ hash<uint64_t>()(n.data[1]);
25 }
26};
27}
28
29namespace hyperapi {
30namespace internal {
31template <unsigned p, unsigned s>
32typename Numeric<p, s>::data_t numericInternalRepresentation(const Numeric<p, s>& numeric) noexcept;
33}
35inline 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]); }
37inline bool operator>(const hyper_data128_t& a, const hyper_data128_t& b) noexcept {
38 if (a.data[1] != b.data[1]) return (static_cast<int64_t>(a.data[1]) > static_cast<int64_t>(b.data[1]));
39 if (a.data[0] != b.data[0]) return (a.data[0] > b.data[0]);
40 return false;
41}
42
46template <unsigned precision_value, unsigned scale_value>
47class Numeric final {
48 public:
49 static constexpr unsigned precision = precision_value;
50 static constexpr unsigned scale = scale_value;
51 static_assert(precision >= scale, "The precision of a numeric must be greater or equal than the scale");
52 static_assert(precision <= 38, "Precision must be at most 38");
53 // NOLINTNEXTLINE(modernize-type-traits)
54 using data_t = typename std::conditional<precision <= 18, int64_t, hyper_data128_t>::type;
55
59 Numeric() noexcept {}
60
65 Numeric(short value);
70 Numeric(unsigned short value);
75 Numeric(int value);
80 Numeric(unsigned value);
85 Numeric(long value);
90 Numeric(unsigned long value);
95 Numeric(long long value);
100 Numeric(unsigned long long value);
105 Numeric(float value);
110 Numeric(double value);
115 Numeric(long double value);
116
121 template <unsigned otherPrecision, unsigned otherScale>
123
129
134 std::string stringValue() const;
135
140 int64_t intValue() const;
145 operator int64_t() const { return intValue(); }
146
150 float floatValue() const noexcept;
154 operator float() const noexcept { return floatValue(); }
155
159 double doubleValue() const noexcept;
163 operator double() const noexcept { return doubleValue(); }
164
169 std::string toString() const;
170
174 friend bool operator==(const Numeric& a, const Numeric& b) noexcept { return a.value_ == b.value_; }
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 == b); }
186 friend bool operator<(const Numeric& a, const Numeric& b) noexcept { return (b > a); }
190 friend bool operator<=(const Numeric& a, const Numeric& b) noexcept { return !(a > b); }
194 friend bool operator>=(const Numeric& a, const Numeric& b) noexcept { return !(a < b); }
195
197 friend std::ostream& operator<<(std::ostream& os, const Numeric& obj) { return os << obj.toString(); }
198
199 private:
200 friend class ::Numeric_rawValue_Test;
201 friend class ::BigNumeric_rawValue_Test;
202 template <unsigned otherPrecision, unsigned otherScale>
203 friend class Numeric;
204 friend class Inserter;
205 template <typename ReturnType>
206 friend struct internal::ValueAccess;
207 friend struct internal::ValueInserter;
208 template <typename T>
209 friend struct std::hash;
210 friend typename Numeric<precision, scale>::data_t internal::numericInternalRepresentation(const Numeric<precision, scale>& numeric) noexcept;
211
212 struct raw_t {};
213
219 explicit Numeric(data_t rawNumeric, raw_t) noexcept
220 : value_(rawNumeric) {
221 }
222
231 explicit Numeric(int64_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
232
241 explicit Numeric(hyper_data128_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
242
246 data_t value_{};
247};
248}
249
250namespace std {
252template <unsigned p, unsigned s>
253struct hash<hyperapi::Numeric<p, s>> {
255 size_t operator()(hyperapi::Numeric<p, s> n) const noexcept {
256 return std::hash<typename hyperapi::Numeric<p, s>::data_t>()(n.value_);
257 }
258};
259}
260
261#include <hyperapi/impl/Numeric.impl.hpp>
262
263#endif
An inserter.
Definition Inserter.hpp:24
A fixed-point numeric data value with scale fraction digits and precision digits overall.
Definition Numeric.hpp:47
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:197
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:186
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:174
friend bool operator>(const Numeric &a, const Numeric &b) noexcept
Greater operator.
Definition Numeric.hpp:178
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:59
friend bool operator<=(const Numeric &a, const Numeric &b) noexcept
Less than or equal operator.
Definition Numeric.hpp:190
friend bool operator!=(const Numeric &a, const Numeric &b) noexcept
Not equal operator.
Definition Numeric.hpp:182
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:194
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:15
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:23
size_t operator()(hyperapi::Numeric< p, s > n) const noexcept
Calculates the hash value of the given numeric.
Definition Numeric.hpp:255