Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
decimal.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_DECIMAL_H
19 #define ARROW_DECIMAL_H
20 
21 #include <array>
22 #include <cstdint>
23 #include <string>
24 #include <type_traits>
25 
26 #include "arrow/status.h"
27 #include "arrow/util/macros.h"
28 #include "arrow/util/visibility.h"
29 
30 namespace arrow {
31 
39 class ARROW_EXPORT Decimal128 {
40  public:
42  constexpr Decimal128(int64_t high, uint64_t low) : high_bits_(high), low_bits_(low) {}
43 
45  constexpr Decimal128() : Decimal128(0, 0) {}
46 
48  template <typename T,
49  typename = typename std::enable_if<std::is_integral<T>::value, T>::type>
50  constexpr Decimal128(T value)
51  : Decimal128(static_cast<int64_t>(value) >= 0 ? 0 : -1,
52  static_cast<uint64_t>(value)) {}
53 
55  explicit Decimal128(const std::string& value);
56 
59  explicit Decimal128(const uint8_t* bytes);
60 
62  Decimal128& Negate();
63 
65  Decimal128& operator+=(const Decimal128& right);
66 
68  Decimal128& operator-=(const Decimal128& right);
69 
71  Decimal128& operator*=(const Decimal128& right);
72 
82  Status Divide(const Decimal128& divisor, Decimal128* result,
83  Decimal128* remainder) const;
84 
86  Decimal128& operator/=(const Decimal128& right);
87 
89  explicit operator char() const;
90 
92  Decimal128& operator|=(const Decimal128& right);
93 
95  Decimal128& operator&=(const Decimal128& right);
96 
98  Decimal128& operator<<=(uint32_t bits);
99 
101  Decimal128& operator>>=(uint32_t bits);
102 
104  int64_t high_bits() const { return high_bits_; }
105 
107  uint64_t low_bits() const { return low_bits_; }
108 
110  std::array<uint8_t, 16> ToBytes() const;
111 
114  std::string ToString(int precision, int scale) const;
115 
118  static Status FromString(const std::string& s, Decimal128* out,
119  int* precision = NULLPTR, int* scale = NULLPTR);
120 
121  private:
122  int64_t high_bits_;
123  uint64_t low_bits_;
124 };
125 
126 ARROW_EXPORT bool operator==(const Decimal128& left, const Decimal128& right);
127 ARROW_EXPORT bool operator!=(const Decimal128& left, const Decimal128& right);
128 ARROW_EXPORT bool operator<(const Decimal128& left, const Decimal128& right);
129 ARROW_EXPORT bool operator<=(const Decimal128& left, const Decimal128& right);
130 ARROW_EXPORT bool operator>(const Decimal128& left, const Decimal128& right);
131 ARROW_EXPORT bool operator>=(const Decimal128& left, const Decimal128& right);
132 
133 ARROW_EXPORT Decimal128 operator-(const Decimal128& operand);
134 ARROW_EXPORT Decimal128 operator+(const Decimal128& left, const Decimal128& right);
135 ARROW_EXPORT Decimal128 operator-(const Decimal128& left, const Decimal128& right);
136 ARROW_EXPORT Decimal128 operator*(const Decimal128& left, const Decimal128& right);
137 ARROW_EXPORT Decimal128 operator/(const Decimal128& left, const Decimal128& right);
138 ARROW_EXPORT Decimal128 operator%(const Decimal128& left, const Decimal128& right);
139 
140 } // namespace arrow
141 
142 #endif // ARROW_DECIMAL_H
Decimal128 operator*(const Decimal128 &left, const Decimal128 &right)
constexpr Decimal128(T value)
Convert any integer value into an Decimal128.
Definition: decimal.h:50
int64_t high_bits() const
Get the high bits of the two&#39;s complement representation of the number.
Definition: decimal.h:104
constexpr Decimal128(int64_t high, uint64_t low)
Create an Decimal128 from the two&#39;s complement representation.
Definition: decimal.h:42
#define NULLPTR
Definition: macros.h:69
Decimal128 operator%(const Decimal128 &left, const Decimal128 &right)
bool operator<(const Decimal128 &left, const Decimal128 &right)
bool operator<=(const Decimal128 &left, const Decimal128 &right)
Decimal128 operator-(const Decimal128 &operand)
Definition: status.h:106
Decimal128 operator/(const Decimal128 &left, const Decimal128 &right)
bool operator!=(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:94
Decimal128 operator+(const Decimal128 &left, const Decimal128 &right)
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
constexpr Decimal128()
Empty constructor creates an Decimal128 with a value of 0.
Definition: decimal.h:45
bool operator==(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:89
bool operator>=(const Decimal128 &left, const Decimal128 &right)
bool operator>(const Decimal128 &left, const Decimal128 &right)
Represents a signed 128-bit integer in two&#39;s complement.
Definition: decimal.h:39
uint64_t low_bits() const
Get the low bits of the two&#39;s complement representation of the number.
Definition: decimal.h:107