Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_base.h +301 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_binary.h +329 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_decimal.h +72 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_dict.h +182 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_nested.h +863 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_primitive.h +202 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_run_end.h +133 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_base.h +370 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_binary.h +971 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_decimal.h +102 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_dict.h +737 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_primitive.h +555 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_run_end.h +303 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_time.h +66 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_union.h +254 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/concatenate.h +37 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/data.h +634 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/diff.h +76 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/util.h +96 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/validate.h +56 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/client_cookie_middleware.h +33 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/client_middleware.h +78 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/pch.h +26 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server.h +328 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server_middleware.h +105 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server_tracing_middleware.h +68 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/test_definitions.h +317 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/test_util.h +264 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/types.h +1147 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/visibility.h +48 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/ProducerConsumerQueue.h +217 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime.h +26 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/date.h +0 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/ios.h +53 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/tz.h +2801 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/tz_private.h +319 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/visibility.h +26 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/double-conversion/fast-dtoa.h +90 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/double-conversion/fixed-dtoa.h +58 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_extras.hpp +649 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_random.hpp +1954 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_uint128.hpp +1008 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/portable-snippets/debug-trap.h +83 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/portable-snippets/safe-math.h +1072 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/strptime.h +35 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/xxhash.h +18 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/xxhash/xxhash.h +0 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/benchmark_util.h +47 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/bloom_filter_reader.h +68 -0
- llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/column_page.h +171 -0
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_base.h
ADDED
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <iosfwd>
|
22 |
+
#include <memory>
|
23 |
+
#include <string>
|
24 |
+
#include <vector>
|
25 |
+
|
26 |
+
#include "arrow/array/data.h"
|
27 |
+
#include "arrow/buffer.h"
|
28 |
+
#include "arrow/compare.h"
|
29 |
+
#include "arrow/result.h"
|
30 |
+
#include "arrow/status.h"
|
31 |
+
#include "arrow/type.h"
|
32 |
+
#include "arrow/util/bit_util.h"
|
33 |
+
#include "arrow/util/macros.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
#include "arrow/visitor.h"
|
36 |
+
|
37 |
+
namespace arrow {
|
38 |
+
|
39 |
+
// ----------------------------------------------------------------------
|
40 |
+
// User array accessor types
|
41 |
+
|
42 |
+
/// \brief Array base type
|
43 |
+
/// Immutable data array with some logical type and some length.
|
44 |
+
///
|
45 |
+
/// Any memory is owned by the respective Buffer instance (or its parents).
|
46 |
+
///
|
47 |
+
/// The base class is only required to have a null bitmap buffer if the null
|
48 |
+
/// count is greater than 0
|
49 |
+
///
|
50 |
+
/// If known, the null count can be provided in the base Array constructor. If
|
51 |
+
/// the null count is not known, pass -1 to indicate that the null count is to
|
52 |
+
/// be computed on the first call to null_count()
|
53 |
+
class ARROW_EXPORT Array {
|
54 |
+
public:
|
55 |
+
virtual ~Array() = default;
|
56 |
+
|
57 |
+
/// \brief Return true if value at index is null. Does not boundscheck
|
58 |
+
bool IsNull(int64_t i) const { return !IsValid(i); }
|
59 |
+
|
60 |
+
/// \brief Return true if value at index is valid (not null). Does not
|
61 |
+
/// boundscheck
|
62 |
+
bool IsValid(int64_t i) const {
|
63 |
+
if (null_bitmap_data_ != NULLPTR) {
|
64 |
+
return bit_util::GetBit(null_bitmap_data_, i + data_->offset);
|
65 |
+
}
|
66 |
+
// Dispatching with a few conditionals like this makes IsNull more
|
67 |
+
// efficient for how it is used in practice. Making IsNull virtual
|
68 |
+
// would add a vtable lookup to every call and prevent inlining +
|
69 |
+
// a potential inner-branch removal.
|
70 |
+
if (type_id() == Type::SPARSE_UNION) {
|
71 |
+
return !internal::IsNullSparseUnion(*data_, i);
|
72 |
+
}
|
73 |
+
if (type_id() == Type::DENSE_UNION) {
|
74 |
+
return !internal::IsNullDenseUnion(*data_, i);
|
75 |
+
}
|
76 |
+
if (type_id() == Type::RUN_END_ENCODED) {
|
77 |
+
return !internal::IsNullRunEndEncoded(*data_, i);
|
78 |
+
}
|
79 |
+
return data_->null_count != data_->length;
|
80 |
+
}
|
81 |
+
|
82 |
+
/// \brief Return a Scalar containing the value of this array at i
|
83 |
+
Result<std::shared_ptr<Scalar>> GetScalar(int64_t i) const;
|
84 |
+
|
85 |
+
/// Size in the number of elements this array contains.
|
86 |
+
int64_t length() const { return data_->length; }
|
87 |
+
|
88 |
+
/// A relative position into another array's data, to enable zero-copy
|
89 |
+
/// slicing. This value defaults to zero
|
90 |
+
int64_t offset() const { return data_->offset; }
|
91 |
+
|
92 |
+
/// The number of null entries in the array. If the null count was not known
|
93 |
+
/// at time of construction (and set to a negative value), then the null
|
94 |
+
/// count will be computed and cached on the first invocation of this
|
95 |
+
/// function
|
96 |
+
int64_t null_count() const;
|
97 |
+
|
98 |
+
/// \brief Computes the logical null count for arrays of all types including
|
99 |
+
/// those that do not have a validity bitmap like union and run-end encoded
|
100 |
+
/// arrays
|
101 |
+
///
|
102 |
+
/// If the array has a validity bitmap, this function behaves the same as
|
103 |
+
/// null_count(). For types that have no validity bitmap, this function will
|
104 |
+
/// recompute the null count every time it is called.
|
105 |
+
///
|
106 |
+
/// \see GetNullCount
|
107 |
+
int64_t ComputeLogicalNullCount() const;
|
108 |
+
|
109 |
+
const std::shared_ptr<DataType>& type() const { return data_->type; }
|
110 |
+
Type::type type_id() const { return data_->type->id(); }
|
111 |
+
|
112 |
+
/// Buffer for the validity (null) bitmap, if any. Note that Union types
|
113 |
+
/// never have a null bitmap.
|
114 |
+
///
|
115 |
+
/// Note that for `null_count == 0` or for null type, this will be null.
|
116 |
+
/// This buffer does not account for any slice offset
|
117 |
+
const std::shared_ptr<Buffer>& null_bitmap() const { return data_->buffers[0]; }
|
118 |
+
|
119 |
+
/// Raw pointer to the null bitmap.
|
120 |
+
///
|
121 |
+
/// Note that for `null_count == 0` or for null type, this will be null.
|
122 |
+
/// This buffer does not account for any slice offset
|
123 |
+
const uint8_t* null_bitmap_data() const { return null_bitmap_data_; }
|
124 |
+
|
125 |
+
/// Equality comparison with another array
|
126 |
+
bool Equals(const Array& arr, const EqualOptions& = EqualOptions::Defaults()) const;
|
127 |
+
bool Equals(const std::shared_ptr<Array>& arr,
|
128 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
129 |
+
|
130 |
+
/// \brief Return the formatted unified diff of arrow::Diff between this
|
131 |
+
/// Array and another Array
|
132 |
+
std::string Diff(const Array& other) const;
|
133 |
+
|
134 |
+
/// Approximate equality comparison with another array
|
135 |
+
///
|
136 |
+
/// epsilon is only used if this is FloatArray or DoubleArray
|
137 |
+
bool ApproxEquals(const std::shared_ptr<Array>& arr,
|
138 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
139 |
+
bool ApproxEquals(const Array& arr,
|
140 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
141 |
+
|
142 |
+
/// Compare if the range of slots specified are equal for the given array and
|
143 |
+
/// this array. end_idx exclusive. This methods does not bounds check.
|
144 |
+
bool RangeEquals(int64_t start_idx, int64_t end_idx, int64_t other_start_idx,
|
145 |
+
const Array& other,
|
146 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
147 |
+
bool RangeEquals(int64_t start_idx, int64_t end_idx, int64_t other_start_idx,
|
148 |
+
const std::shared_ptr<Array>& other,
|
149 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
150 |
+
bool RangeEquals(const Array& other, int64_t start_idx, int64_t end_idx,
|
151 |
+
int64_t other_start_idx,
|
152 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
153 |
+
bool RangeEquals(const std::shared_ptr<Array>& other, int64_t start_idx,
|
154 |
+
int64_t end_idx, int64_t other_start_idx,
|
155 |
+
const EqualOptions& = EqualOptions::Defaults()) const;
|
156 |
+
|
157 |
+
/// \brief Apply the ArrayVisitor::Visit() method specialized to the array type
|
158 |
+
Status Accept(ArrayVisitor* visitor) const;
|
159 |
+
|
160 |
+
/// Construct a zero-copy view of this array with the given type.
|
161 |
+
///
|
162 |
+
/// This method checks if the types are layout-compatible.
|
163 |
+
/// Nested types are traversed in depth-first order. Data buffers must have
|
164 |
+
/// the same item sizes, even though the logical types may be different.
|
165 |
+
/// An error is returned if the types are not layout-compatible.
|
166 |
+
Result<std::shared_ptr<Array>> View(const std::shared_ptr<DataType>& type) const;
|
167 |
+
|
168 |
+
/// \brief Construct a copy of the array with all buffers on destination
|
169 |
+
/// Memory Manager
|
170 |
+
///
|
171 |
+
/// This method recursively copies the array's buffers and those of its children
|
172 |
+
/// onto the destination MemoryManager device and returns the new Array.
|
173 |
+
Result<std::shared_ptr<Array>> CopyTo(const std::shared_ptr<MemoryManager>& to) const;
|
174 |
+
|
175 |
+
/// \brief Construct a new array attempting to zero-copy view if possible.
|
176 |
+
///
|
177 |
+
/// Like CopyTo this method recursively goes through all of the array's buffers
|
178 |
+
/// and those of it's children and first attempts to create zero-copy
|
179 |
+
/// views on the destination MemoryManager device. If it can't, it falls back
|
180 |
+
/// to performing a copy. See Buffer::ViewOrCopy.
|
181 |
+
Result<std::shared_ptr<Array>> ViewOrCopyTo(
|
182 |
+
const std::shared_ptr<MemoryManager>& to) const;
|
183 |
+
|
184 |
+
/// Construct a zero-copy slice of the array with the indicated offset and
|
185 |
+
/// length
|
186 |
+
///
|
187 |
+
/// \param[in] offset the position of the first element in the constructed
|
188 |
+
/// slice
|
189 |
+
/// \param[in] length the length of the slice. If there are not enough
|
190 |
+
/// elements in the array, the length will be adjusted accordingly
|
191 |
+
///
|
192 |
+
/// \return a new object wrapped in std::shared_ptr<Array>
|
193 |
+
std::shared_ptr<Array> Slice(int64_t offset, int64_t length) const;
|
194 |
+
|
195 |
+
/// Slice from offset until end of the array
|
196 |
+
std::shared_ptr<Array> Slice(int64_t offset) const;
|
197 |
+
|
198 |
+
/// Input-checking variant of Array::Slice
|
199 |
+
Result<std::shared_ptr<Array>> SliceSafe(int64_t offset, int64_t length) const;
|
200 |
+
/// Input-checking variant of Array::Slice
|
201 |
+
Result<std::shared_ptr<Array>> SliceSafe(int64_t offset) const;
|
202 |
+
|
203 |
+
const std::shared_ptr<ArrayData>& data() const { return data_; }
|
204 |
+
|
205 |
+
int num_fields() const { return static_cast<int>(data_->child_data.size()); }
|
206 |
+
|
207 |
+
/// \return PrettyPrint representation of array suitable for debugging
|
208 |
+
std::string ToString() const;
|
209 |
+
|
210 |
+
/// \brief Perform cheap validation checks to determine obvious inconsistencies
|
211 |
+
/// within the array's internal data.
|
212 |
+
///
|
213 |
+
/// This is O(k) where k is the number of descendents.
|
214 |
+
///
|
215 |
+
/// \return Status
|
216 |
+
Status Validate() const;
|
217 |
+
|
218 |
+
/// \brief Perform extensive validation checks to determine inconsistencies
|
219 |
+
/// within the array's internal data.
|
220 |
+
///
|
221 |
+
/// This is potentially O(k*n) where k is the number of descendents and n
|
222 |
+
/// is the array length.
|
223 |
+
///
|
224 |
+
/// \return Status
|
225 |
+
Status ValidateFull() const;
|
226 |
+
|
227 |
+
protected:
|
228 |
+
Array() = default;
|
229 |
+
ARROW_DEFAULT_MOVE_AND_ASSIGN(Array);
|
230 |
+
|
231 |
+
std::shared_ptr<ArrayData> data_;
|
232 |
+
const uint8_t* null_bitmap_data_ = NULLPTR;
|
233 |
+
|
234 |
+
/// Protected method for constructors
|
235 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
236 |
+
if (data->buffers.size() > 0) {
|
237 |
+
null_bitmap_data_ = data->GetValuesSafe<uint8_t>(0, /*offset=*/0);
|
238 |
+
} else {
|
239 |
+
null_bitmap_data_ = NULLPTR;
|
240 |
+
}
|
241 |
+
data_ = data;
|
242 |
+
}
|
243 |
+
|
244 |
+
private:
|
245 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(Array);
|
246 |
+
|
247 |
+
ARROW_FRIEND_EXPORT friend void PrintTo(const Array& x, std::ostream* os);
|
248 |
+
};
|
249 |
+
|
250 |
+
static inline std::ostream& operator<<(std::ostream& os, const Array& x) {
|
251 |
+
os << x.ToString();
|
252 |
+
return os;
|
253 |
+
}
|
254 |
+
|
255 |
+
/// Base class for non-nested arrays
|
256 |
+
class ARROW_EXPORT FlatArray : public Array {
|
257 |
+
protected:
|
258 |
+
using Array::Array;
|
259 |
+
};
|
260 |
+
|
261 |
+
/// Base class for arrays of fixed-size logical types
|
262 |
+
class ARROW_EXPORT PrimitiveArray : public FlatArray {
|
263 |
+
public:
|
264 |
+
PrimitiveArray(const std::shared_ptr<DataType>& type, int64_t length,
|
265 |
+
const std::shared_ptr<Buffer>& data,
|
266 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
267 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
268 |
+
|
269 |
+
/// Does not account for any slice offset
|
270 |
+
const std::shared_ptr<Buffer>& values() const { return data_->buffers[1]; }
|
271 |
+
|
272 |
+
protected:
|
273 |
+
PrimitiveArray() : raw_values_(NULLPTR) {}
|
274 |
+
|
275 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
276 |
+
this->Array::SetData(data);
|
277 |
+
raw_values_ = data->GetValuesSafe<uint8_t>(1, /*offset=*/0);
|
278 |
+
}
|
279 |
+
|
280 |
+
explicit PrimitiveArray(const std::shared_ptr<ArrayData>& data) { SetData(data); }
|
281 |
+
|
282 |
+
const uint8_t* raw_values_;
|
283 |
+
};
|
284 |
+
|
285 |
+
/// Degenerate null type Array
|
286 |
+
class ARROW_EXPORT NullArray : public FlatArray {
|
287 |
+
public:
|
288 |
+
using TypeClass = NullType;
|
289 |
+
|
290 |
+
explicit NullArray(const std::shared_ptr<ArrayData>& data) { SetData(data); }
|
291 |
+
explicit NullArray(int64_t length);
|
292 |
+
|
293 |
+
private:
|
294 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
295 |
+
null_bitmap_data_ = NULLPTR;
|
296 |
+
data->null_count = data->length;
|
297 |
+
data_ = data;
|
298 |
+
}
|
299 |
+
};
|
300 |
+
|
301 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_binary.h
ADDED
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Array accessor classes for Binary, LargeBinary, String, LargeString,
|
19 |
+
// FixedSizeBinary
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
#include <optional>
|
26 |
+
#include <string>
|
27 |
+
#include <string_view>
|
28 |
+
#include <vector>
|
29 |
+
|
30 |
+
#include "arrow/array/array_base.h"
|
31 |
+
#include "arrow/array/data.h"
|
32 |
+
#include "arrow/buffer.h"
|
33 |
+
#include "arrow/stl_iterator.h"
|
34 |
+
#include "arrow/type.h"
|
35 |
+
#include "arrow/util/checked_cast.h"
|
36 |
+
#include "arrow/util/macros.h"
|
37 |
+
#include "arrow/util/visibility.h"
|
38 |
+
|
39 |
+
namespace arrow {
|
40 |
+
|
41 |
+
/// \addtogroup binary-arrays
|
42 |
+
///
|
43 |
+
/// @{
|
44 |
+
|
45 |
+
// ----------------------------------------------------------------------
|
46 |
+
// Binary and String
|
47 |
+
|
48 |
+
/// Base class for variable-sized binary arrays, regardless of offset size
|
49 |
+
/// and logical interpretation.
|
50 |
+
template <typename TYPE>
|
51 |
+
class BaseBinaryArray : public FlatArray {
|
52 |
+
public:
|
53 |
+
using TypeClass = TYPE;
|
54 |
+
using offset_type = typename TypeClass::offset_type;
|
55 |
+
using IteratorType = stl::ArrayIterator<BaseBinaryArray<TYPE>>;
|
56 |
+
|
57 |
+
/// Return the pointer to the given elements bytes
|
58 |
+
// XXX should GetValue(int64_t i) return a string_view?
|
59 |
+
const uint8_t* GetValue(int64_t i, offset_type* out_length) const {
|
60 |
+
// Account for base offset
|
61 |
+
i += data_->offset;
|
62 |
+
const offset_type pos = raw_value_offsets_[i];
|
63 |
+
*out_length = raw_value_offsets_[i + 1] - pos;
|
64 |
+
return raw_data_ + pos;
|
65 |
+
}
|
66 |
+
|
67 |
+
/// \brief Get binary value as a string_view
|
68 |
+
///
|
69 |
+
/// \param i the value index
|
70 |
+
/// \return the view over the selected value
|
71 |
+
std::string_view GetView(int64_t i) const {
|
72 |
+
// Account for base offset
|
73 |
+
i += data_->offset;
|
74 |
+
const offset_type pos = raw_value_offsets_[i];
|
75 |
+
return std::string_view(reinterpret_cast<const char*>(raw_data_ + pos),
|
76 |
+
raw_value_offsets_[i + 1] - pos);
|
77 |
+
}
|
78 |
+
|
79 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
80 |
+
return *IteratorType(*this, i);
|
81 |
+
}
|
82 |
+
|
83 |
+
/// \brief Get binary value as a string_view
|
84 |
+
/// Provided for consistency with other arrays.
|
85 |
+
///
|
86 |
+
/// \param i the value index
|
87 |
+
/// \return the view over the selected value
|
88 |
+
std::string_view Value(int64_t i) const { return GetView(i); }
|
89 |
+
|
90 |
+
/// \brief Get binary value as a std::string
|
91 |
+
///
|
92 |
+
/// \param i the value index
|
93 |
+
/// \return the value copied into a std::string
|
94 |
+
std::string GetString(int64_t i) const { return std::string(GetView(i)); }
|
95 |
+
|
96 |
+
/// Note that this buffer does not account for any slice offset
|
97 |
+
std::shared_ptr<Buffer> value_offsets() const { return data_->buffers[1]; }
|
98 |
+
|
99 |
+
/// Note that this buffer does not account for any slice offset
|
100 |
+
std::shared_ptr<Buffer> value_data() const { return data_->buffers[2]; }
|
101 |
+
|
102 |
+
const offset_type* raw_value_offsets() const {
|
103 |
+
return raw_value_offsets_ + data_->offset;
|
104 |
+
}
|
105 |
+
|
106 |
+
const uint8_t* raw_data() const { return raw_data_; }
|
107 |
+
|
108 |
+
/// \brief Return the data buffer absolute offset of the data for the value
|
109 |
+
/// at the passed index.
|
110 |
+
///
|
111 |
+
/// Does not perform boundschecking
|
112 |
+
offset_type value_offset(int64_t i) const {
|
113 |
+
return raw_value_offsets_[i + data_->offset];
|
114 |
+
}
|
115 |
+
|
116 |
+
/// \brief Return the length of the data for the value at the passed index.
|
117 |
+
///
|
118 |
+
/// Does not perform boundschecking
|
119 |
+
offset_type value_length(int64_t i) const {
|
120 |
+
i += data_->offset;
|
121 |
+
return raw_value_offsets_[i + 1] - raw_value_offsets_[i];
|
122 |
+
}
|
123 |
+
|
124 |
+
/// \brief Return the total length of the memory in the data buffer
|
125 |
+
/// referenced by this array. If the array has been sliced then this may be
|
126 |
+
/// less than the size of the data buffer (data_->buffers[2]).
|
127 |
+
offset_type total_values_length() const {
|
128 |
+
if (data_->length > 0) {
|
129 |
+
return raw_value_offsets_[data_->length + data_->offset] -
|
130 |
+
raw_value_offsets_[data_->offset];
|
131 |
+
} else {
|
132 |
+
return 0;
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
IteratorType begin() const { return IteratorType(*this); }
|
137 |
+
|
138 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
139 |
+
|
140 |
+
protected:
|
141 |
+
// For subclasses
|
142 |
+
BaseBinaryArray() = default;
|
143 |
+
|
144 |
+
// Protected method for constructors
|
145 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
146 |
+
this->Array::SetData(data);
|
147 |
+
raw_value_offsets_ = data->GetValuesSafe<offset_type>(1, /*offset=*/0);
|
148 |
+
raw_data_ = data->GetValuesSafe<uint8_t>(2, /*offset=*/0);
|
149 |
+
}
|
150 |
+
|
151 |
+
const offset_type* raw_value_offsets_ = NULLPTR;
|
152 |
+
const uint8_t* raw_data_ = NULLPTR;
|
153 |
+
};
|
154 |
+
|
155 |
+
/// Concrete Array class for variable-size binary data
|
156 |
+
class ARROW_EXPORT BinaryArray : public BaseBinaryArray<BinaryType> {
|
157 |
+
public:
|
158 |
+
explicit BinaryArray(const std::shared_ptr<ArrayData>& data);
|
159 |
+
|
160 |
+
BinaryArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
161 |
+
const std::shared_ptr<Buffer>& data,
|
162 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
163 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
164 |
+
|
165 |
+
protected:
|
166 |
+
// For subclasses such as StringArray
|
167 |
+
BinaryArray() : BaseBinaryArray() {}
|
168 |
+
};
|
169 |
+
|
170 |
+
/// Concrete Array class for variable-size string (utf-8) data
|
171 |
+
class ARROW_EXPORT StringArray : public BinaryArray {
|
172 |
+
public:
|
173 |
+
using TypeClass = StringType;
|
174 |
+
|
175 |
+
explicit StringArray(const std::shared_ptr<ArrayData>& data);
|
176 |
+
|
177 |
+
StringArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
178 |
+
const std::shared_ptr<Buffer>& data,
|
179 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
180 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
181 |
+
|
182 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
183 |
+
///
|
184 |
+
/// This check is also implied by ValidateFull()
|
185 |
+
Status ValidateUTF8() const;
|
186 |
+
};
|
187 |
+
|
188 |
+
/// Concrete Array class for large variable-size binary data
|
189 |
+
class ARROW_EXPORT LargeBinaryArray : public BaseBinaryArray<LargeBinaryType> {
|
190 |
+
public:
|
191 |
+
explicit LargeBinaryArray(const std::shared_ptr<ArrayData>& data);
|
192 |
+
|
193 |
+
LargeBinaryArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
194 |
+
const std::shared_ptr<Buffer>& data,
|
195 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
196 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
197 |
+
|
198 |
+
protected:
|
199 |
+
// For subclasses such as LargeStringArray
|
200 |
+
LargeBinaryArray() : BaseBinaryArray() {}
|
201 |
+
};
|
202 |
+
|
203 |
+
/// Concrete Array class for large variable-size string (utf-8) data
|
204 |
+
class ARROW_EXPORT LargeStringArray : public LargeBinaryArray {
|
205 |
+
public:
|
206 |
+
using TypeClass = LargeStringType;
|
207 |
+
|
208 |
+
explicit LargeStringArray(const std::shared_ptr<ArrayData>& data);
|
209 |
+
|
210 |
+
LargeStringArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
211 |
+
const std::shared_ptr<Buffer>& data,
|
212 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
213 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
214 |
+
|
215 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
216 |
+
///
|
217 |
+
/// This check is also implied by ValidateFull()
|
218 |
+
Status ValidateUTF8() const;
|
219 |
+
};
|
220 |
+
|
221 |
+
// ----------------------------------------------------------------------
|
222 |
+
// BinaryView and StringView
|
223 |
+
|
224 |
+
/// Concrete Array class for variable-size binary view data using the
|
225 |
+
/// BinaryViewType::c_type struct to reference in-line or out-of-line string values
|
226 |
+
class ARROW_EXPORT BinaryViewArray : public FlatArray {
|
227 |
+
public:
|
228 |
+
using TypeClass = BinaryViewType;
|
229 |
+
using IteratorType = stl::ArrayIterator<BinaryViewArray>;
|
230 |
+
using c_type = BinaryViewType::c_type;
|
231 |
+
|
232 |
+
explicit BinaryViewArray(std::shared_ptr<ArrayData> data);
|
233 |
+
|
234 |
+
BinaryViewArray(std::shared_ptr<DataType> type, int64_t length,
|
235 |
+
std::shared_ptr<Buffer> views, BufferVector data_buffers,
|
236 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
237 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
238 |
+
|
239 |
+
// For API compatibility with BinaryArray etc.
|
240 |
+
std::string_view GetView(int64_t i) const;
|
241 |
+
std::string GetString(int64_t i) const { return std::string{GetView(i)}; }
|
242 |
+
|
243 |
+
const auto& values() const { return data_->buffers[1]; }
|
244 |
+
const c_type* raw_values() const { return raw_values_; }
|
245 |
+
|
246 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
247 |
+
return *IteratorType(*this, i);
|
248 |
+
}
|
249 |
+
|
250 |
+
IteratorType begin() const { return IteratorType(*this); }
|
251 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
252 |
+
|
253 |
+
protected:
|
254 |
+
using FlatArray::FlatArray;
|
255 |
+
|
256 |
+
void SetData(std::shared_ptr<ArrayData> data) {
|
257 |
+
FlatArray::SetData(std::move(data));
|
258 |
+
raw_values_ = data_->GetValuesSafe<c_type>(1);
|
259 |
+
}
|
260 |
+
|
261 |
+
const c_type* raw_values_;
|
262 |
+
};
|
263 |
+
|
264 |
+
/// Concrete Array class for variable-size string view (utf-8) data using
|
265 |
+
/// BinaryViewType::c_type to reference in-line or out-of-line string values
|
266 |
+
class ARROW_EXPORT StringViewArray : public BinaryViewArray {
|
267 |
+
public:
|
268 |
+
using TypeClass = StringViewType;
|
269 |
+
|
270 |
+
explicit StringViewArray(std::shared_ptr<ArrayData> data);
|
271 |
+
|
272 |
+
using BinaryViewArray::BinaryViewArray;
|
273 |
+
|
274 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
275 |
+
///
|
276 |
+
/// This check is also implied by ValidateFull()
|
277 |
+
Status ValidateUTF8() const;
|
278 |
+
};
|
279 |
+
|
280 |
+
// ----------------------------------------------------------------------
|
281 |
+
// Fixed width binary
|
282 |
+
|
283 |
+
/// Concrete Array class for fixed-size binary data
|
284 |
+
class ARROW_EXPORT FixedSizeBinaryArray : public PrimitiveArray {
|
285 |
+
public:
|
286 |
+
using TypeClass = FixedSizeBinaryType;
|
287 |
+
using IteratorType = stl::ArrayIterator<FixedSizeBinaryArray>;
|
288 |
+
|
289 |
+
explicit FixedSizeBinaryArray(const std::shared_ptr<ArrayData>& data);
|
290 |
+
|
291 |
+
FixedSizeBinaryArray(const std::shared_ptr<DataType>& type, int64_t length,
|
292 |
+
const std::shared_ptr<Buffer>& data,
|
293 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
294 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
295 |
+
|
296 |
+
const uint8_t* GetValue(int64_t i) const;
|
297 |
+
const uint8_t* Value(int64_t i) const { return GetValue(i); }
|
298 |
+
|
299 |
+
std::string_view GetView(int64_t i) const {
|
300 |
+
return std::string_view(reinterpret_cast<const char*>(GetValue(i)), byte_width());
|
301 |
+
}
|
302 |
+
|
303 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
304 |
+
return *IteratorType(*this, i);
|
305 |
+
}
|
306 |
+
|
307 |
+
std::string GetString(int64_t i) const { return std::string(GetView(i)); }
|
308 |
+
|
309 |
+
int32_t byte_width() const { return byte_width_; }
|
310 |
+
|
311 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width_; }
|
312 |
+
|
313 |
+
IteratorType begin() const { return IteratorType(*this); }
|
314 |
+
|
315 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
316 |
+
|
317 |
+
protected:
|
318 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
319 |
+
this->PrimitiveArray::SetData(data);
|
320 |
+
byte_width_ =
|
321 |
+
internal::checked_cast<const FixedSizeBinaryType&>(*type()).byte_width();
|
322 |
+
}
|
323 |
+
|
324 |
+
int32_t byte_width_;
|
325 |
+
};
|
326 |
+
|
327 |
+
/// @}
|
328 |
+
|
329 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_decimal.h
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <string>
|
23 |
+
|
24 |
+
#include "arrow/array/array_binary.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/type.h"
|
27 |
+
#include "arrow/util/visibility.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
|
31 |
+
/// \addtogroup numeric-arrays
|
32 |
+
///
|
33 |
+
/// @{
|
34 |
+
|
35 |
+
// ----------------------------------------------------------------------
|
36 |
+
// Decimal128Array
|
37 |
+
|
38 |
+
/// Concrete Array class for 128-bit decimal data
|
39 |
+
class ARROW_EXPORT Decimal128Array : public FixedSizeBinaryArray {
|
40 |
+
public:
|
41 |
+
using TypeClass = Decimal128Type;
|
42 |
+
|
43 |
+
using FixedSizeBinaryArray::FixedSizeBinaryArray;
|
44 |
+
|
45 |
+
/// \brief Construct Decimal128Array from ArrayData instance
|
46 |
+
explicit Decimal128Array(const std::shared_ptr<ArrayData>& data);
|
47 |
+
|
48 |
+
std::string FormatValue(int64_t i) const;
|
49 |
+
};
|
50 |
+
|
51 |
+
// Backward compatibility
|
52 |
+
using DecimalArray = Decimal128Array;
|
53 |
+
|
54 |
+
// ----------------------------------------------------------------------
|
55 |
+
// Decimal256Array
|
56 |
+
|
57 |
+
/// Concrete Array class for 256-bit decimal data
|
58 |
+
class ARROW_EXPORT Decimal256Array : public FixedSizeBinaryArray {
|
59 |
+
public:
|
60 |
+
using TypeClass = Decimal256Type;
|
61 |
+
|
62 |
+
using FixedSizeBinaryArray::FixedSizeBinaryArray;
|
63 |
+
|
64 |
+
/// \brief Construct Decimal256Array from ArrayData instance
|
65 |
+
explicit Decimal256Array(const std::shared_ptr<ArrayData>& data);
|
66 |
+
|
67 |
+
std::string FormatValue(int64_t i) const;
|
68 |
+
};
|
69 |
+
|
70 |
+
/// @}
|
71 |
+
|
72 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_dict.h
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
|
23 |
+
#include "arrow/array/array_base.h"
|
24 |
+
#include "arrow/array/data.h"
|
25 |
+
#include "arrow/result.h"
|
26 |
+
#include "arrow/status.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/util/macros.h"
|
29 |
+
#include "arrow/util/visibility.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
|
33 |
+
// ----------------------------------------------------------------------
|
34 |
+
// DictionaryArray
|
35 |
+
|
36 |
+
/// \brief Array type for dictionary-encoded data with a
|
37 |
+
/// data-dependent dictionary
|
38 |
+
///
|
39 |
+
/// A dictionary array contains an array of non-negative integers (the
|
40 |
+
/// "dictionary indices") along with a data type containing a "dictionary"
|
41 |
+
/// corresponding to the distinct values represented in the data.
|
42 |
+
///
|
43 |
+
/// For example, the array
|
44 |
+
///
|
45 |
+
/// ["foo", "bar", "foo", "bar", "foo", "bar"]
|
46 |
+
///
|
47 |
+
/// with dictionary ["bar", "foo"], would have dictionary array representation
|
48 |
+
///
|
49 |
+
/// indices: [1, 0, 1, 0, 1, 0]
|
50 |
+
/// dictionary: ["bar", "foo"]
|
51 |
+
///
|
52 |
+
/// The indices in principle may be any integer type.
|
53 |
+
class ARROW_EXPORT DictionaryArray : public Array {
|
54 |
+
public:
|
55 |
+
using TypeClass = DictionaryType;
|
56 |
+
|
57 |
+
explicit DictionaryArray(const std::shared_ptr<ArrayData>& data);
|
58 |
+
|
59 |
+
DictionaryArray(const std::shared_ptr<DataType>& type,
|
60 |
+
const std::shared_ptr<Array>& indices,
|
61 |
+
const std::shared_ptr<Array>& dictionary);
|
62 |
+
|
63 |
+
/// \brief Construct DictionaryArray from dictionary and indices
|
64 |
+
/// array and validate
|
65 |
+
///
|
66 |
+
/// This function does the validation of the indices and input type. It checks if
|
67 |
+
/// all indices are non-negative and smaller than the size of the dictionary.
|
68 |
+
///
|
69 |
+
/// \param[in] type a dictionary type
|
70 |
+
/// \param[in] dictionary the dictionary with same value type as the
|
71 |
+
/// type object
|
72 |
+
/// \param[in] indices an array of non-negative integers smaller than the
|
73 |
+
/// size of the dictionary
|
74 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
75 |
+
const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& indices,
|
76 |
+
const std::shared_ptr<Array>& dictionary);
|
77 |
+
|
78 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
79 |
+
const std::shared_ptr<Array>& indices, const std::shared_ptr<Array>& dictionary) {
|
80 |
+
return FromArrays(::arrow::dictionary(indices->type(), dictionary->type()), indices,
|
81 |
+
dictionary);
|
82 |
+
}
|
83 |
+
|
84 |
+
/// \brief Transpose this DictionaryArray
|
85 |
+
///
|
86 |
+
/// This method constructs a new dictionary array with the given dictionary
|
87 |
+
/// type, transposing indices using the transpose map. The type and the
|
88 |
+
/// transpose map are typically computed using DictionaryUnifier.
|
89 |
+
///
|
90 |
+
/// \param[in] type the new type object
|
91 |
+
/// \param[in] dictionary the new dictionary
|
92 |
+
/// \param[in] transpose_map transposition array of this array's indices
|
93 |
+
/// into the target array's indices
|
94 |
+
/// \param[in] pool a pool to allocate the array data from
|
95 |
+
Result<std::shared_ptr<Array>> Transpose(
|
96 |
+
const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& dictionary,
|
97 |
+
const int32_t* transpose_map, MemoryPool* pool = default_memory_pool()) const;
|
98 |
+
|
99 |
+
Result<std::shared_ptr<Array>> Compact(MemoryPool* pool = default_memory_pool()) const;
|
100 |
+
|
101 |
+
/// \brief Determine whether dictionary arrays may be compared without unification
|
102 |
+
bool CanCompareIndices(const DictionaryArray& other) const;
|
103 |
+
|
104 |
+
/// \brief Return the dictionary for this array, which is stored as
|
105 |
+
/// a member of the ArrayData internal structure
|
106 |
+
const std::shared_ptr<Array>& dictionary() const;
|
107 |
+
const std::shared_ptr<Array>& indices() const;
|
108 |
+
|
109 |
+
/// \brief Return the ith value of indices, cast to int64_t. Not recommended
|
110 |
+
/// for use in performance-sensitive code. Does not validate whether the
|
111 |
+
/// value is null or out-of-bounds.
|
112 |
+
int64_t GetValueIndex(int64_t i) const;
|
113 |
+
|
114 |
+
const DictionaryType* dict_type() const { return dict_type_; }
|
115 |
+
|
116 |
+
private:
|
117 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
118 |
+
const DictionaryType* dict_type_;
|
119 |
+
std::shared_ptr<Array> indices_;
|
120 |
+
|
121 |
+
// Lazily initialized when invoking dictionary()
|
122 |
+
mutable std::shared_ptr<Array> dictionary_;
|
123 |
+
};
|
124 |
+
|
125 |
+
/// \brief Helper class for incremental dictionary unification
|
126 |
+
class ARROW_EXPORT DictionaryUnifier {
|
127 |
+
public:
|
128 |
+
virtual ~DictionaryUnifier() = default;
|
129 |
+
|
130 |
+
/// \brief Construct a DictionaryUnifier
|
131 |
+
/// \param[in] value_type the data type of the dictionaries
|
132 |
+
/// \param[in] pool MemoryPool to use for memory allocations
|
133 |
+
static Result<std::unique_ptr<DictionaryUnifier>> Make(
|
134 |
+
std::shared_ptr<DataType> value_type, MemoryPool* pool = default_memory_pool());
|
135 |
+
|
136 |
+
/// \brief Unify dictionaries across array chunks
|
137 |
+
///
|
138 |
+
/// The dictionaries in the array chunks will be unified, their indices
|
139 |
+
/// accordingly transposed.
|
140 |
+
///
|
141 |
+
/// Only dictionaries with a primitive value type are currently supported.
|
142 |
+
/// However, dictionaries nested inside a more complex type are correctly unified.
|
143 |
+
static Result<std::shared_ptr<ChunkedArray>> UnifyChunkedArray(
|
144 |
+
const std::shared_ptr<ChunkedArray>& array,
|
145 |
+
MemoryPool* pool = default_memory_pool());
|
146 |
+
|
147 |
+
/// \brief Unify dictionaries across the chunks of each table column
|
148 |
+
///
|
149 |
+
/// The dictionaries in each table column will be unified, their indices
|
150 |
+
/// accordingly transposed.
|
151 |
+
///
|
152 |
+
/// Only dictionaries with a primitive value type are currently supported.
|
153 |
+
/// However, dictionaries nested inside a more complex type are correctly unified.
|
154 |
+
static Result<std::shared_ptr<Table>> UnifyTable(
|
155 |
+
const Table& table, MemoryPool* pool = default_memory_pool());
|
156 |
+
|
157 |
+
/// \brief Append dictionary to the internal memo
|
158 |
+
virtual Status Unify(const Array& dictionary) = 0;
|
159 |
+
|
160 |
+
/// \brief Append dictionary and compute transpose indices
|
161 |
+
/// \param[in] dictionary the dictionary values to unify
|
162 |
+
/// \param[out] out_transpose a Buffer containing computed transpose indices
|
163 |
+
/// as int32_t values equal in length to the passed dictionary. The value in
|
164 |
+
/// each slot corresponds to the new index value for each original index
|
165 |
+
/// for a DictionaryArray with the old dictionary
|
166 |
+
virtual Status Unify(const Array& dictionary,
|
167 |
+
std::shared_ptr<Buffer>* out_transpose) = 0;
|
168 |
+
|
169 |
+
/// \brief Return a result DictionaryType with the smallest possible index
|
170 |
+
/// type to accommodate the unified dictionary. The unifier cannot be used
|
171 |
+
/// after this is called
|
172 |
+
virtual Status GetResult(std::shared_ptr<DataType>* out_type,
|
173 |
+
std::shared_ptr<Array>* out_dict) = 0;
|
174 |
+
|
175 |
+
/// \brief Return a unified dictionary with the given index type. If
|
176 |
+
/// the index type is not large enough then an invalid status will be returned.
|
177 |
+
/// The unifier cannot be used after this is called
|
178 |
+
virtual Status GetResultWithIndexType(const std::shared_ptr<DataType>& index_type,
|
179 |
+
std::shared_ptr<Array>* out_dict) = 0;
|
180 |
+
};
|
181 |
+
|
182 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_nested.h
ADDED
@@ -0,0 +1,863 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Array accessor classes for List, LargeList, ListView, LargeListView, FixedSizeList,
|
19 |
+
// Map, Struct, and Union
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
#include <string>
|
26 |
+
#include <utility>
|
27 |
+
#include <vector>
|
28 |
+
|
29 |
+
#include "arrow/array/array_base.h"
|
30 |
+
#include "arrow/array/data.h"
|
31 |
+
#include "arrow/result.h"
|
32 |
+
#include "arrow/status.h"
|
33 |
+
#include "arrow/type.h"
|
34 |
+
#include "arrow/type_fwd.h"
|
35 |
+
#include "arrow/util/checked_cast.h"
|
36 |
+
#include "arrow/util/macros.h"
|
37 |
+
#include "arrow/util/visibility.h"
|
38 |
+
|
39 |
+
namespace arrow {
|
40 |
+
|
41 |
+
/// \addtogroup nested-arrays
|
42 |
+
///
|
43 |
+
/// @{
|
44 |
+
|
45 |
+
// ----------------------------------------------------------------------
|
46 |
+
// VarLengthListLikeArray
|
47 |
+
|
48 |
+
template <typename TYPE>
|
49 |
+
class VarLengthListLikeArray;
|
50 |
+
|
51 |
+
namespace internal {
|
52 |
+
|
53 |
+
// Private helper for [Large]List[View]Array::SetData.
|
54 |
+
// Unfortunately, trying to define VarLengthListLikeArray::SetData outside of this header
|
55 |
+
// doesn't play well with MSVC.
|
56 |
+
template <typename TYPE>
|
57 |
+
void SetListData(VarLengthListLikeArray<TYPE>* self,
|
58 |
+
const std::shared_ptr<ArrayData>& data,
|
59 |
+
Type::type expected_type_id = TYPE::type_id);
|
60 |
+
|
61 |
+
} // namespace internal
|
62 |
+
|
63 |
+
/// Base class for variable-sized list and list-view arrays, regardless of offset size.
|
64 |
+
template <typename TYPE>
|
65 |
+
class VarLengthListLikeArray : public Array {
|
66 |
+
public:
|
67 |
+
using TypeClass = TYPE;
|
68 |
+
using offset_type = typename TypeClass::offset_type;
|
69 |
+
|
70 |
+
const TypeClass* var_length_list_like_type() const { return this->list_type_; }
|
71 |
+
|
72 |
+
/// \brief Return array object containing the list's values
|
73 |
+
///
|
74 |
+
/// Note that this buffer does not account for any slice offset or length.
|
75 |
+
const std::shared_ptr<Array>& values() const { return values_; }
|
76 |
+
|
77 |
+
/// Note that this buffer does not account for any slice offset or length.
|
78 |
+
const std::shared_ptr<Buffer>& value_offsets() const { return data_->buffers[1]; }
|
79 |
+
|
80 |
+
const std::shared_ptr<DataType>& value_type() const { return list_type_->value_type(); }
|
81 |
+
|
82 |
+
/// Return pointer to raw value offsets accounting for any slice offset
|
83 |
+
const offset_type* raw_value_offsets() const {
|
84 |
+
return raw_value_offsets_ + data_->offset;
|
85 |
+
}
|
86 |
+
|
87 |
+
// The following functions will not perform boundschecking
|
88 |
+
|
89 |
+
offset_type value_offset(int64_t i) const {
|
90 |
+
return raw_value_offsets_[i + data_->offset];
|
91 |
+
}
|
92 |
+
|
93 |
+
/// \brief Return the size of the value at a particular index
|
94 |
+
///
|
95 |
+
/// Since non-empty null lists and list-views are possible, avoid calling this
|
96 |
+
/// function when the list at slot i is null.
|
97 |
+
///
|
98 |
+
/// \pre IsValid(i)
|
99 |
+
virtual offset_type value_length(int64_t i) const = 0;
|
100 |
+
|
101 |
+
/// \pre IsValid(i)
|
102 |
+
std::shared_ptr<Array> value_slice(int64_t i) const {
|
103 |
+
return values_->Slice(value_offset(i), value_length(i));
|
104 |
+
}
|
105 |
+
|
106 |
+
protected:
|
107 |
+
friend void internal::SetListData<TYPE>(VarLengthListLikeArray<TYPE>* self,
|
108 |
+
const std::shared_ptr<ArrayData>& data,
|
109 |
+
Type::type expected_type_id);
|
110 |
+
|
111 |
+
const TypeClass* list_type_ = NULLPTR;
|
112 |
+
std::shared_ptr<Array> values_;
|
113 |
+
const offset_type* raw_value_offsets_ = NULLPTR;
|
114 |
+
};
|
115 |
+
|
116 |
+
// ----------------------------------------------------------------------
|
117 |
+
// ListArray / LargeListArray
|
118 |
+
|
119 |
+
template <typename TYPE>
|
120 |
+
class BaseListArray : public VarLengthListLikeArray<TYPE> {
|
121 |
+
public:
|
122 |
+
using TypeClass = TYPE;
|
123 |
+
using offset_type = typename TYPE::offset_type;
|
124 |
+
|
125 |
+
const TypeClass* list_type() const { return this->var_length_list_like_type(); }
|
126 |
+
|
127 |
+
/// \brief Return the size of the value at a particular index
|
128 |
+
///
|
129 |
+
/// Since non-empty null lists are possible, avoid calling this
|
130 |
+
/// function when the list at slot i is null.
|
131 |
+
///
|
132 |
+
/// \pre IsValid(i)
|
133 |
+
offset_type value_length(int64_t i) const final {
|
134 |
+
i += this->data_->offset;
|
135 |
+
return this->raw_value_offsets_[i + 1] - this->raw_value_offsets_[i];
|
136 |
+
}
|
137 |
+
};
|
138 |
+
|
139 |
+
/// Concrete Array class for list data
|
140 |
+
class ARROW_EXPORT ListArray : public BaseListArray<ListType> {
|
141 |
+
public:
|
142 |
+
explicit ListArray(std::shared_ptr<ArrayData> data);
|
143 |
+
|
144 |
+
ListArray(std::shared_ptr<DataType> type, int64_t length,
|
145 |
+
std::shared_ptr<Buffer> value_offsets, std::shared_ptr<Array> values,
|
146 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
147 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
148 |
+
|
149 |
+
/// \brief Construct ListArray from array of offsets and child value array
|
150 |
+
///
|
151 |
+
/// This function does the bare minimum of validation of the offsets and
|
152 |
+
/// input types, and will allocate a new offsets array if necessary (i.e. if
|
153 |
+
/// the offsets contain any nulls). If the offsets do not have nulls, they
|
154 |
+
/// are assumed to be well-formed.
|
155 |
+
///
|
156 |
+
/// If a null_bitmap is not provided, the nulls will be inferred from the offsets'
|
157 |
+
/// null bitmap. But if a null_bitmap is provided, the offsets array can't have nulls.
|
158 |
+
///
|
159 |
+
/// And when a null_bitmap is provided, the offsets array cannot be a slice (i.e. an
|
160 |
+
/// array with offset() > 0).
|
161 |
+
///
|
162 |
+
/// \param[in] offsets Array containing n + 1 offsets encoding length and
|
163 |
+
/// size. Must be of int32 type
|
164 |
+
/// \param[in] values Array containing list values
|
165 |
+
/// \param[in] pool MemoryPool in case new offsets array needs to be
|
166 |
+
/// allocated because of null values
|
167 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
168 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
169 |
+
static Result<std::shared_ptr<ListArray>> FromArrays(
|
170 |
+
const Array& offsets, const Array& values, MemoryPool* pool = default_memory_pool(),
|
171 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
172 |
+
int64_t null_count = kUnknownNullCount);
|
173 |
+
|
174 |
+
static Result<std::shared_ptr<ListArray>> FromArrays(
|
175 |
+
std::shared_ptr<DataType> type, const Array& offsets, const Array& values,
|
176 |
+
MemoryPool* pool = default_memory_pool(),
|
177 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
178 |
+
int64_t null_count = kUnknownNullCount);
|
179 |
+
|
180 |
+
/// \brief Build a ListArray from a ListViewArray
|
181 |
+
static Result<std::shared_ptr<ListArray>> FromListView(const ListViewArray& source,
|
182 |
+
MemoryPool* pool);
|
183 |
+
|
184 |
+
/// \brief Return an Array that is a concatenation of the lists in this array.
|
185 |
+
///
|
186 |
+
/// Note that it's different from `values()` in that it takes into
|
187 |
+
/// consideration of this array's offsets as well as null elements backed
|
188 |
+
/// by non-empty lists (they are skipped, thus copying may be needed).
|
189 |
+
Result<std::shared_ptr<Array>> Flatten(
|
190 |
+
MemoryPool* memory_pool = default_memory_pool()) const;
|
191 |
+
|
192 |
+
/// \brief Return list offsets as an Int32Array
|
193 |
+
///
|
194 |
+
/// The returned array will not have a validity bitmap, so you cannot expect
|
195 |
+
/// to pass it to ListArray::FromArrays() and get back the same list array
|
196 |
+
/// if the original one has nulls.
|
197 |
+
std::shared_ptr<Array> offsets() const;
|
198 |
+
|
199 |
+
protected:
|
200 |
+
// This constructor defers SetData to a derived array class
|
201 |
+
ListArray() = default;
|
202 |
+
|
203 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
204 |
+
};
|
205 |
+
|
206 |
+
/// Concrete Array class for large list data (with 64-bit offsets)
|
207 |
+
class ARROW_EXPORT LargeListArray : public BaseListArray<LargeListType> {
|
208 |
+
public:
|
209 |
+
explicit LargeListArray(const std::shared_ptr<ArrayData>& data);
|
210 |
+
|
211 |
+
LargeListArray(const std::shared_ptr<DataType>& type, int64_t length,
|
212 |
+
const std::shared_ptr<Buffer>& value_offsets,
|
213 |
+
const std::shared_ptr<Array>& values,
|
214 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
215 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
216 |
+
|
217 |
+
/// \brief Construct LargeListArray from array of offsets and child value array
|
218 |
+
///
|
219 |
+
/// This function does the bare minimum of validation of the offsets and
|
220 |
+
/// input types, and will allocate a new offsets array if necessary (i.e. if
|
221 |
+
/// the offsets contain any nulls). If the offsets do not have nulls, they
|
222 |
+
/// are assumed to be well-formed.
|
223 |
+
///
|
224 |
+
/// If a null_bitmap is not provided, the nulls will be inferred from the offsets'
|
225 |
+
/// null bitmap. But if a null_bitmap is provided, the offsets array can't have nulls.
|
226 |
+
///
|
227 |
+
/// And when a null_bitmap is provided, the offsets array cannot be a slice (i.e. an
|
228 |
+
/// array with offset() > 0).
|
229 |
+
///
|
230 |
+
/// \param[in] offsets Array containing n + 1 offsets encoding length and
|
231 |
+
/// size. Must be of int64 type
|
232 |
+
/// \param[in] values Array containing list values
|
233 |
+
/// \param[in] pool MemoryPool in case new offsets array needs to be
|
234 |
+
/// allocated because of null values
|
235 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
236 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
237 |
+
static Result<std::shared_ptr<LargeListArray>> FromArrays(
|
238 |
+
const Array& offsets, const Array& values, MemoryPool* pool = default_memory_pool(),
|
239 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
240 |
+
int64_t null_count = kUnknownNullCount);
|
241 |
+
|
242 |
+
static Result<std::shared_ptr<LargeListArray>> FromArrays(
|
243 |
+
std::shared_ptr<DataType> type, const Array& offsets, const Array& values,
|
244 |
+
MemoryPool* pool = default_memory_pool(),
|
245 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
246 |
+
int64_t null_count = kUnknownNullCount);
|
247 |
+
|
248 |
+
/// \brief Build a LargeListArray from a LargeListViewArray
|
249 |
+
static Result<std::shared_ptr<LargeListArray>> FromListView(
|
250 |
+
const LargeListViewArray& source, MemoryPool* pool);
|
251 |
+
|
252 |
+
/// \brief Return an Array that is a concatenation of the lists in this array.
|
253 |
+
///
|
254 |
+
/// Note that it's different from `values()` in that it takes into
|
255 |
+
/// consideration of this array's offsets as well as null elements backed
|
256 |
+
/// by non-empty lists (they are skipped, thus copying may be needed).
|
257 |
+
Result<std::shared_ptr<Array>> Flatten(
|
258 |
+
MemoryPool* memory_pool = default_memory_pool()) const;
|
259 |
+
|
260 |
+
/// \brief Return list offsets as an Int64Array
|
261 |
+
std::shared_ptr<Array> offsets() const;
|
262 |
+
|
263 |
+
protected:
|
264 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
265 |
+
};
|
266 |
+
|
267 |
+
// ----------------------------------------------------------------------
|
268 |
+
// ListViewArray / LargeListViewArray
|
269 |
+
|
270 |
+
template <typename TYPE>
|
271 |
+
class BaseListViewArray : public VarLengthListLikeArray<TYPE> {
|
272 |
+
public:
|
273 |
+
using TypeClass = TYPE;
|
274 |
+
using offset_type = typename TYPE::offset_type;
|
275 |
+
|
276 |
+
const TypeClass* list_view_type() const { return this->var_length_list_like_type(); }
|
277 |
+
|
278 |
+
/// \brief Note that this buffer does not account for any slice offset or length.
|
279 |
+
const std::shared_ptr<Buffer>& value_sizes() const { return this->data_->buffers[2]; }
|
280 |
+
|
281 |
+
/// \brief Return pointer to raw value offsets accounting for any slice offset
|
282 |
+
const offset_type* raw_value_sizes() const {
|
283 |
+
return raw_value_sizes_ + this->data_->offset;
|
284 |
+
}
|
285 |
+
|
286 |
+
/// \brief Return the size of the value at a particular index
|
287 |
+
///
|
288 |
+
/// This should not be called if the list-view at slot i is null.
|
289 |
+
/// The returned size in those cases could be any value from 0 to the
|
290 |
+
/// length of the child values array.
|
291 |
+
///
|
292 |
+
/// \pre IsValid(i)
|
293 |
+
offset_type value_length(int64_t i) const final {
|
294 |
+
return this->raw_value_sizes_[i + this->data_->offset];
|
295 |
+
}
|
296 |
+
|
297 |
+
protected:
|
298 |
+
const offset_type* raw_value_sizes_ = NULLPTR;
|
299 |
+
};
|
300 |
+
|
301 |
+
/// \brief Concrete Array class for list-view data
|
302 |
+
class ARROW_EXPORT ListViewArray : public BaseListViewArray<ListViewType> {
|
303 |
+
public:
|
304 |
+
explicit ListViewArray(std::shared_ptr<ArrayData> data);
|
305 |
+
|
306 |
+
ListViewArray(std::shared_ptr<DataType> type, int64_t length,
|
307 |
+
std::shared_ptr<Buffer> value_offsets,
|
308 |
+
std::shared_ptr<Buffer> value_sizes, std::shared_ptr<Array> values,
|
309 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
310 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
311 |
+
|
312 |
+
/// \brief Construct ListViewArray from array of offsets, sizes, and child
|
313 |
+
/// value array
|
314 |
+
///
|
315 |
+
/// Construct a ListViewArray using buffers from offsets and sizes arrays
|
316 |
+
/// that project views into the child values array.
|
317 |
+
///
|
318 |
+
/// This function does the bare minimum of validation of the offsets/sizes and
|
319 |
+
/// input types. The offset and length of the offsets and sizes arrays must
|
320 |
+
/// match and that will be checked, but their contents will be assumed to be
|
321 |
+
/// well-formed.
|
322 |
+
///
|
323 |
+
/// If a null_bitmap is not provided, the nulls will be inferred from the
|
324 |
+
/// offsets's null bitmap. But if a null_bitmap is provided, the offsets array
|
325 |
+
/// can't have nulls.
|
326 |
+
///
|
327 |
+
/// And when a null_bitmap is provided, neither the offsets or sizes array can be a
|
328 |
+
/// slice (i.e. an array with offset() > 0).
|
329 |
+
///
|
330 |
+
/// \param[in] offsets An array of int32 offsets into the values array. NULL values are
|
331 |
+
/// supported if the corresponding values in sizes is NULL or 0.
|
332 |
+
/// \param[in] sizes An array containing the int32 sizes of every view. NULL values are
|
333 |
+
/// taken to represent a NULL list-view in the array being created.
|
334 |
+
/// \param[in] values Array containing list values
|
335 |
+
/// \param[in] pool MemoryPool
|
336 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
337 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
338 |
+
static Result<std::shared_ptr<ListViewArray>> FromArrays(
|
339 |
+
const Array& offsets, const Array& sizes, const Array& values,
|
340 |
+
MemoryPool* pool = default_memory_pool(),
|
341 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
342 |
+
int64_t null_count = kUnknownNullCount);
|
343 |
+
|
344 |
+
static Result<std::shared_ptr<ListViewArray>> FromArrays(
|
345 |
+
std::shared_ptr<DataType> type, const Array& offsets, const Array& sizes,
|
346 |
+
const Array& values, MemoryPool* pool = default_memory_pool(),
|
347 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
348 |
+
int64_t null_count = kUnknownNullCount);
|
349 |
+
|
350 |
+
/// \brief Build a ListViewArray from a ListArray
|
351 |
+
static Result<std::shared_ptr<ListViewArray>> FromList(const ListArray& list_array,
|
352 |
+
MemoryPool* pool);
|
353 |
+
|
354 |
+
/// \brief Return an Array that is a concatenation of the list-views in this array.
|
355 |
+
///
|
356 |
+
/// Note that it's different from `values()` in that it takes into
|
357 |
+
/// consideration this array's offsets (which can be in any order)
|
358 |
+
/// and sizes. Nulls are skipped.
|
359 |
+
///
|
360 |
+
/// This function invokes Concatenate() if list-views are non-contiguous. It
|
361 |
+
/// will try to minimize the number of array slices passed to Concatenate() by
|
362 |
+
/// maximizing the size of each slice (containing as many contiguous
|
363 |
+
/// list-views as possible).
|
364 |
+
Result<std::shared_ptr<Array>> Flatten(
|
365 |
+
MemoryPool* memory_pool = default_memory_pool()) const;
|
366 |
+
|
367 |
+
/// \brief Return list-view offsets as an Int32Array
|
368 |
+
///
|
369 |
+
/// The returned array will not have a validity bitmap, so you cannot expect
|
370 |
+
/// to pass it to ListArray::FromArrays() and get back the same list array
|
371 |
+
/// if the original one has nulls.
|
372 |
+
std::shared_ptr<Array> offsets() const;
|
373 |
+
|
374 |
+
/// \brief Return list-view sizes as an Int32Array
|
375 |
+
///
|
376 |
+
/// The returned array will not have a validity bitmap, so you cannot expect
|
377 |
+
/// to pass it to ListViewArray::FromArrays() and get back the same list
|
378 |
+
/// array if the original one has nulls.
|
379 |
+
std::shared_ptr<Array> sizes() const;
|
380 |
+
|
381 |
+
protected:
|
382 |
+
// This constructor defers SetData to a derived array class
|
383 |
+
ListViewArray() = default;
|
384 |
+
|
385 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
386 |
+
};
|
387 |
+
|
388 |
+
/// \brief Concrete Array class for large list-view data (with 64-bit offsets
|
389 |
+
/// and sizes)
|
390 |
+
class ARROW_EXPORT LargeListViewArray : public BaseListViewArray<LargeListViewType> {
|
391 |
+
public:
|
392 |
+
explicit LargeListViewArray(std::shared_ptr<ArrayData> data);
|
393 |
+
|
394 |
+
LargeListViewArray(std::shared_ptr<DataType> type, int64_t length,
|
395 |
+
std::shared_ptr<Buffer> value_offsets,
|
396 |
+
std::shared_ptr<Buffer> value_sizes, std::shared_ptr<Array> values,
|
397 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
398 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
399 |
+
|
400 |
+
/// \brief Construct LargeListViewArray from array of offsets, sizes, and child
|
401 |
+
/// value array
|
402 |
+
///
|
403 |
+
/// Construct an LargeListViewArray using buffers from offsets and sizes arrays
|
404 |
+
/// that project views into the values array.
|
405 |
+
///
|
406 |
+
/// This function does the bare minimum of validation of the offsets/sizes and
|
407 |
+
/// input types. The offset and length of the offsets and sizes arrays must
|
408 |
+
/// match and that will be checked, but their contents will be assumed to be
|
409 |
+
/// well-formed.
|
410 |
+
///
|
411 |
+
/// If a null_bitmap is not provided, the nulls will be inferred from the offsets' or
|
412 |
+
/// sizes' null bitmap. Only one of these two is allowed to have a null bitmap. But if a
|
413 |
+
/// null_bitmap is provided, the offsets array and the sizes array can't have nulls.
|
414 |
+
///
|
415 |
+
/// And when a null_bitmap is provided, neither the offsets or sizes array can be a
|
416 |
+
/// slice (i.e. an array with offset() > 0).
|
417 |
+
///
|
418 |
+
/// \param[in] offsets An array of int64 offsets into the values array. NULL values are
|
419 |
+
/// supported if the corresponding values in sizes is NULL or 0.
|
420 |
+
/// \param[in] sizes An array containing the int64 sizes of every view. NULL values are
|
421 |
+
/// taken to represent a NULL list-view in the array being created.
|
422 |
+
/// \param[in] values Array containing list values
|
423 |
+
/// \param[in] pool MemoryPool
|
424 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
425 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
426 |
+
static Result<std::shared_ptr<LargeListViewArray>> FromArrays(
|
427 |
+
const Array& offsets, const Array& sizes, const Array& values,
|
428 |
+
MemoryPool* pool = default_memory_pool(),
|
429 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
430 |
+
int64_t null_count = kUnknownNullCount);
|
431 |
+
|
432 |
+
static Result<std::shared_ptr<LargeListViewArray>> FromArrays(
|
433 |
+
std::shared_ptr<DataType> type, const Array& offsets, const Array& sizes,
|
434 |
+
const Array& values, MemoryPool* pool = default_memory_pool(),
|
435 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
436 |
+
int64_t null_count = kUnknownNullCount);
|
437 |
+
|
438 |
+
/// \brief Build a LargeListViewArray from a LargeListArray
|
439 |
+
static Result<std::shared_ptr<LargeListViewArray>> FromList(
|
440 |
+
const LargeListArray& list_array, MemoryPool* pool);
|
441 |
+
|
442 |
+
/// \brief Return an Array that is a concatenation of the large list-views in this
|
443 |
+
/// array.
|
444 |
+
///
|
445 |
+
/// Note that it's different from `values()` in that it takes into
|
446 |
+
/// consideration this array's offsets (which can be in any order)
|
447 |
+
/// and sizes. Nulls are skipped.
|
448 |
+
Result<std::shared_ptr<Array>> Flatten(
|
449 |
+
MemoryPool* memory_pool = default_memory_pool()) const;
|
450 |
+
|
451 |
+
/// \brief Return list-view offsets as an Int64Array
|
452 |
+
///
|
453 |
+
/// The returned array will not have a validity bitmap, so you cannot expect
|
454 |
+
/// to pass it to LargeListArray::FromArrays() and get back the same list array
|
455 |
+
/// if the original one has nulls.
|
456 |
+
std::shared_ptr<Array> offsets() const;
|
457 |
+
|
458 |
+
/// \brief Return list-view sizes as an Int64Array
|
459 |
+
///
|
460 |
+
/// The returned array will not have a validity bitmap, so you cannot expect
|
461 |
+
/// to pass it to LargeListViewArray::FromArrays() and get back the same list
|
462 |
+
/// array if the original one has nulls.
|
463 |
+
std::shared_ptr<Array> sizes() const;
|
464 |
+
|
465 |
+
protected:
|
466 |
+
// This constructor defers SetData to a derived array class
|
467 |
+
LargeListViewArray() = default;
|
468 |
+
|
469 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
470 |
+
};
|
471 |
+
|
472 |
+
// ----------------------------------------------------------------------
|
473 |
+
// MapArray
|
474 |
+
|
475 |
+
/// Concrete Array class for map data
|
476 |
+
///
|
477 |
+
/// NB: "value" in this context refers to a pair of a key and the corresponding item
|
478 |
+
class ARROW_EXPORT MapArray : public ListArray {
|
479 |
+
public:
|
480 |
+
using TypeClass = MapType;
|
481 |
+
|
482 |
+
explicit MapArray(const std::shared_ptr<ArrayData>& data);
|
483 |
+
|
484 |
+
MapArray(const std::shared_ptr<DataType>& type, int64_t length,
|
485 |
+
const std::shared_ptr<Buffer>& value_offsets,
|
486 |
+
const std::shared_ptr<Array>& keys, const std::shared_ptr<Array>& items,
|
487 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
488 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
489 |
+
|
490 |
+
MapArray(const std::shared_ptr<DataType>& type, int64_t length, BufferVector buffers,
|
491 |
+
const std::shared_ptr<Array>& keys, const std::shared_ptr<Array>& items,
|
492 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
493 |
+
|
494 |
+
MapArray(const std::shared_ptr<DataType>& type, int64_t length,
|
495 |
+
const std::shared_ptr<Buffer>& value_offsets,
|
496 |
+
const std::shared_ptr<Array>& values,
|
497 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
498 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
499 |
+
|
500 |
+
/// \brief Construct MapArray from array of offsets and child key, item arrays
|
501 |
+
///
|
502 |
+
/// This function does the bare minimum of validation of the offsets and
|
503 |
+
/// input types, and will allocate a new offsets array if necessary (i.e. if
|
504 |
+
/// the offsets contain any nulls). If the offsets do not have nulls, they
|
505 |
+
/// are assumed to be well-formed
|
506 |
+
///
|
507 |
+
/// \param[in] offsets Array containing n + 1 offsets encoding length and
|
508 |
+
/// size. Must be of int32 type
|
509 |
+
/// \param[in] keys Array containing key values
|
510 |
+
/// \param[in] items Array containing item values
|
511 |
+
/// \param[in] pool MemoryPool in case new offsets array needs to be
|
512 |
+
/// allocated because of null values
|
513 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
514 |
+
const std::shared_ptr<Array>& offsets, const std::shared_ptr<Array>& keys,
|
515 |
+
const std::shared_ptr<Array>& items, MemoryPool* pool = default_memory_pool());
|
516 |
+
|
517 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
518 |
+
std::shared_ptr<DataType> type, const std::shared_ptr<Array>& offsets,
|
519 |
+
const std::shared_ptr<Array>& keys, const std::shared_ptr<Array>& items,
|
520 |
+
MemoryPool* pool = default_memory_pool());
|
521 |
+
|
522 |
+
const MapType* map_type() const { return map_type_; }
|
523 |
+
|
524 |
+
/// \brief Return array object containing all map keys
|
525 |
+
const std::shared_ptr<Array>& keys() const { return keys_; }
|
526 |
+
|
527 |
+
/// \brief Return array object containing all mapped items
|
528 |
+
const std::shared_ptr<Array>& items() const { return items_; }
|
529 |
+
|
530 |
+
/// Validate child data before constructing the actual MapArray.
|
531 |
+
static Status ValidateChildData(
|
532 |
+
const std::vector<std::shared_ptr<ArrayData>>& child_data);
|
533 |
+
|
534 |
+
protected:
|
535 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
536 |
+
|
537 |
+
static Result<std::shared_ptr<Array>> FromArraysInternal(
|
538 |
+
std::shared_ptr<DataType> type, const std::shared_ptr<Array>& offsets,
|
539 |
+
const std::shared_ptr<Array>& keys, const std::shared_ptr<Array>& items,
|
540 |
+
MemoryPool* pool);
|
541 |
+
|
542 |
+
private:
|
543 |
+
const MapType* map_type_;
|
544 |
+
std::shared_ptr<Array> keys_, items_;
|
545 |
+
};
|
546 |
+
|
547 |
+
// ----------------------------------------------------------------------
|
548 |
+
// FixedSizeListArray
|
549 |
+
|
550 |
+
/// Concrete Array class for fixed size list data
|
551 |
+
class ARROW_EXPORT FixedSizeListArray : public Array {
|
552 |
+
public:
|
553 |
+
using TypeClass = FixedSizeListType;
|
554 |
+
using offset_type = TypeClass::offset_type;
|
555 |
+
|
556 |
+
explicit FixedSizeListArray(const std::shared_ptr<ArrayData>& data);
|
557 |
+
|
558 |
+
FixedSizeListArray(const std::shared_ptr<DataType>& type, int64_t length,
|
559 |
+
const std::shared_ptr<Array>& values,
|
560 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
561 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
562 |
+
|
563 |
+
const FixedSizeListType* list_type() const;
|
564 |
+
|
565 |
+
/// \brief Return array object containing the list's values
|
566 |
+
const std::shared_ptr<Array>& values() const;
|
567 |
+
|
568 |
+
const std::shared_ptr<DataType>& value_type() const;
|
569 |
+
|
570 |
+
// The following functions will not perform boundschecking
|
571 |
+
int64_t value_offset(int64_t i) const {
|
572 |
+
i += data_->offset;
|
573 |
+
return list_size_ * i;
|
574 |
+
}
|
575 |
+
/// \brief Return the fixed-size of the values
|
576 |
+
///
|
577 |
+
/// No matter the value of the index parameter, the result is the same.
|
578 |
+
/// So even when the value at slot i is null, this function will return a
|
579 |
+
/// non-zero size.
|
580 |
+
///
|
581 |
+
/// \pre IsValid(i)
|
582 |
+
int32_t value_length(int64_t i = 0) const {
|
583 |
+
ARROW_UNUSED(i);
|
584 |
+
return list_size_;
|
585 |
+
}
|
586 |
+
/// \pre IsValid(i)
|
587 |
+
std::shared_ptr<Array> value_slice(int64_t i) const {
|
588 |
+
return values_->Slice(value_offset(i), value_length(i));
|
589 |
+
}
|
590 |
+
|
591 |
+
/// \brief Return an Array that is a concatenation of the lists in this array.
|
592 |
+
///
|
593 |
+
/// Note that it's different from `values()` in that it takes into
|
594 |
+
/// consideration null elements (they are skipped, thus copying may be needed).
|
595 |
+
Result<std::shared_ptr<Array>> Flatten(
|
596 |
+
MemoryPool* memory_pool = default_memory_pool()) const;
|
597 |
+
|
598 |
+
/// \brief Construct FixedSizeListArray from child value array and value_length
|
599 |
+
///
|
600 |
+
/// \param[in] values Array containing list values
|
601 |
+
/// \param[in] list_size The fixed length of each list
|
602 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
603 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
604 |
+
/// \return Will have length equal to values.length() / list_size
|
605 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
606 |
+
const std::shared_ptr<Array>& values, int32_t list_size,
|
607 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
608 |
+
int64_t null_count = kUnknownNullCount);
|
609 |
+
|
610 |
+
/// \brief Construct FixedSizeListArray from child value array and type
|
611 |
+
///
|
612 |
+
/// \param[in] values Array containing list values
|
613 |
+
/// \param[in] type The fixed sized list type
|
614 |
+
/// \param[in] null_bitmap Optional validity bitmap
|
615 |
+
/// \param[in] null_count Optional null count in null_bitmap
|
616 |
+
/// \return Will have length equal to values.length() / type.list_size()
|
617 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
618 |
+
const std::shared_ptr<Array>& values, std::shared_ptr<DataType> type,
|
619 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
620 |
+
int64_t null_count = kUnknownNullCount);
|
621 |
+
|
622 |
+
protected:
|
623 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
624 |
+
int32_t list_size_;
|
625 |
+
|
626 |
+
private:
|
627 |
+
std::shared_ptr<Array> values_;
|
628 |
+
};
|
629 |
+
|
630 |
+
// ----------------------------------------------------------------------
|
631 |
+
// Struct
|
632 |
+
|
633 |
+
/// Concrete Array class for struct data
|
634 |
+
class ARROW_EXPORT StructArray : public Array {
|
635 |
+
public:
|
636 |
+
using TypeClass = StructType;
|
637 |
+
|
638 |
+
explicit StructArray(const std::shared_ptr<ArrayData>& data);
|
639 |
+
|
640 |
+
StructArray(const std::shared_ptr<DataType>& type, int64_t length,
|
641 |
+
const std::vector<std::shared_ptr<Array>>& children,
|
642 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
643 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
644 |
+
|
645 |
+
/// \brief Return a StructArray from child arrays and field names.
|
646 |
+
///
|
647 |
+
/// The length and data type are automatically inferred from the arguments.
|
648 |
+
/// There should be at least one child array.
|
649 |
+
static Result<std::shared_ptr<StructArray>> Make(
|
650 |
+
const ArrayVector& children, const std::vector<std::string>& field_names,
|
651 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
652 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
653 |
+
|
654 |
+
/// \brief Return a StructArray from child arrays and fields.
|
655 |
+
///
|
656 |
+
/// The length is automatically inferred from the arguments.
|
657 |
+
/// There should be at least one child array. This method does not
|
658 |
+
/// check that field types and child array types are consistent.
|
659 |
+
static Result<std::shared_ptr<StructArray>> Make(
|
660 |
+
const ArrayVector& children, const FieldVector& fields,
|
661 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
662 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
663 |
+
|
664 |
+
const StructType* struct_type() const;
|
665 |
+
|
666 |
+
// Return a shared pointer in case the requestor desires to share ownership
|
667 |
+
// with this array. The returned array has its offset, length and null
|
668 |
+
// count adjusted.
|
669 |
+
const std::shared_ptr<Array>& field(int pos) const;
|
670 |
+
|
671 |
+
const ArrayVector& fields() const;
|
672 |
+
|
673 |
+
/// Returns null if name not found
|
674 |
+
std::shared_ptr<Array> GetFieldByName(const std::string& name) const;
|
675 |
+
|
676 |
+
/// Indicate if field named `name` can be found unambiguously in the struct.
|
677 |
+
Status CanReferenceFieldByName(const std::string& name) const;
|
678 |
+
|
679 |
+
/// Indicate if fields named `names` can be found unambiguously in the struct.
|
680 |
+
Status CanReferenceFieldsByNames(const std::vector<std::string>& names) const;
|
681 |
+
|
682 |
+
/// \brief Flatten this array as a vector of arrays, one for each field
|
683 |
+
///
|
684 |
+
/// \param[in] pool The pool to allocate null bitmaps from, if necessary
|
685 |
+
Result<ArrayVector> Flatten(MemoryPool* pool = default_memory_pool()) const;
|
686 |
+
|
687 |
+
/// \brief Get one of the child arrays, combining its null bitmap
|
688 |
+
/// with the parent struct array's bitmap.
|
689 |
+
///
|
690 |
+
/// \param[in] index Which child array to get
|
691 |
+
/// \param[in] pool The pool to allocate null bitmaps from, if necessary
|
692 |
+
Result<std::shared_ptr<Array>> GetFlattenedField(
|
693 |
+
int index, MemoryPool* pool = default_memory_pool()) const;
|
694 |
+
|
695 |
+
private:
|
696 |
+
// For caching boxed child data
|
697 |
+
// XXX This is not handled in a thread-safe manner.
|
698 |
+
mutable ArrayVector boxed_fields_;
|
699 |
+
};
|
700 |
+
|
701 |
+
// ----------------------------------------------------------------------
|
702 |
+
// Union
|
703 |
+
|
704 |
+
/// Base class for SparseUnionArray and DenseUnionArray
|
705 |
+
class ARROW_EXPORT UnionArray : public Array {
|
706 |
+
public:
|
707 |
+
using type_code_t = int8_t;
|
708 |
+
|
709 |
+
/// Note that this buffer does not account for any slice offset
|
710 |
+
const std::shared_ptr<Buffer>& type_codes() const { return data_->buffers[1]; }
|
711 |
+
|
712 |
+
const type_code_t* raw_type_codes() const { return raw_type_codes_ + data_->offset; }
|
713 |
+
|
714 |
+
/// The logical type code of the value at index.
|
715 |
+
type_code_t type_code(int64_t i) const { return raw_type_codes_[i + data_->offset]; }
|
716 |
+
|
717 |
+
/// The physical child id containing value at index.
|
718 |
+
int child_id(int64_t i) const {
|
719 |
+
return union_type_->child_ids()[raw_type_codes_[i + data_->offset]];
|
720 |
+
}
|
721 |
+
|
722 |
+
const UnionType* union_type() const { return union_type_; }
|
723 |
+
|
724 |
+
UnionMode::type mode() const { return union_type_->mode(); }
|
725 |
+
|
726 |
+
/// \brief Return the given field as an individual array.
|
727 |
+
///
|
728 |
+
/// For sparse unions, the returned array has its offset, length and null
|
729 |
+
/// count adjusted.
|
730 |
+
std::shared_ptr<Array> field(int pos) const;
|
731 |
+
|
732 |
+
protected:
|
733 |
+
void SetData(std::shared_ptr<ArrayData> data);
|
734 |
+
|
735 |
+
const type_code_t* raw_type_codes_;
|
736 |
+
const UnionType* union_type_;
|
737 |
+
|
738 |
+
// For caching boxed child data
|
739 |
+
mutable std::vector<std::shared_ptr<Array>> boxed_fields_;
|
740 |
+
};
|
741 |
+
|
742 |
+
/// Concrete Array class for sparse union data
|
743 |
+
class ARROW_EXPORT SparseUnionArray : public UnionArray {
|
744 |
+
public:
|
745 |
+
using TypeClass = SparseUnionType;
|
746 |
+
|
747 |
+
explicit SparseUnionArray(std::shared_ptr<ArrayData> data);
|
748 |
+
|
749 |
+
SparseUnionArray(std::shared_ptr<DataType> type, int64_t length, ArrayVector children,
|
750 |
+
std::shared_ptr<Buffer> type_ids, int64_t offset = 0);
|
751 |
+
|
752 |
+
/// \brief Construct SparseUnionArray from type_ids and children
|
753 |
+
///
|
754 |
+
/// This function does the bare minimum of validation of the input types.
|
755 |
+
///
|
756 |
+
/// \param[in] type_ids An array of logical type ids for the union type
|
757 |
+
/// \param[in] children Vector of children Arrays containing the data for each type.
|
758 |
+
/// \param[in] type_codes Vector of type codes.
|
759 |
+
static Result<std::shared_ptr<Array>> Make(const Array& type_ids, ArrayVector children,
|
760 |
+
std::vector<type_code_t> type_codes) {
|
761 |
+
return Make(std::move(type_ids), std::move(children), std::vector<std::string>{},
|
762 |
+
std::move(type_codes));
|
763 |
+
}
|
764 |
+
|
765 |
+
/// \brief Construct SparseUnionArray with custom field names from type_ids and children
|
766 |
+
///
|
767 |
+
/// This function does the bare minimum of validation of the input types.
|
768 |
+
///
|
769 |
+
/// \param[in] type_ids An array of logical type ids for the union type
|
770 |
+
/// \param[in] children Vector of children Arrays containing the data for each type.
|
771 |
+
/// \param[in] field_names Vector of strings containing the name of each field.
|
772 |
+
/// \param[in] type_codes Vector of type codes.
|
773 |
+
static Result<std::shared_ptr<Array>> Make(const Array& type_ids, ArrayVector children,
|
774 |
+
std::vector<std::string> field_names = {},
|
775 |
+
std::vector<type_code_t> type_codes = {});
|
776 |
+
|
777 |
+
const SparseUnionType* union_type() const {
|
778 |
+
return internal::checked_cast<const SparseUnionType*>(union_type_);
|
779 |
+
}
|
780 |
+
|
781 |
+
/// \brief Get one of the child arrays, adjusting its null bitmap
|
782 |
+
/// where the union array type code does not match.
|
783 |
+
///
|
784 |
+
/// \param[in] index Which child array to get (i.e. the physical index, not the type
|
785 |
+
/// code) \param[in] pool The pool to allocate null bitmaps from, if necessary
|
786 |
+
Result<std::shared_ptr<Array>> GetFlattenedField(
|
787 |
+
int index, MemoryPool* pool = default_memory_pool()) const;
|
788 |
+
|
789 |
+
protected:
|
790 |
+
void SetData(std::shared_ptr<ArrayData> data);
|
791 |
+
};
|
792 |
+
|
793 |
+
/// \brief Concrete Array class for dense union data
|
794 |
+
///
|
795 |
+
/// Note that union types do not have a validity bitmap
|
796 |
+
class ARROW_EXPORT DenseUnionArray : public UnionArray {
|
797 |
+
public:
|
798 |
+
using TypeClass = DenseUnionType;
|
799 |
+
|
800 |
+
explicit DenseUnionArray(const std::shared_ptr<ArrayData>& data);
|
801 |
+
|
802 |
+
DenseUnionArray(std::shared_ptr<DataType> type, int64_t length, ArrayVector children,
|
803 |
+
std::shared_ptr<Buffer> type_ids,
|
804 |
+
std::shared_ptr<Buffer> value_offsets = NULLPTR, int64_t offset = 0);
|
805 |
+
|
806 |
+
/// \brief Construct DenseUnionArray from type_ids, value_offsets, and children
|
807 |
+
///
|
808 |
+
/// This function does the bare minimum of validation of the offsets and
|
809 |
+
/// input types.
|
810 |
+
///
|
811 |
+
/// \param[in] type_ids An array of logical type ids for the union type
|
812 |
+
/// \param[in] value_offsets An array of signed int32 values indicating the
|
813 |
+
/// relative offset into the respective child array for the type in a given slot.
|
814 |
+
/// The respective offsets for each child value array must be in order / increasing.
|
815 |
+
/// \param[in] children Vector of children Arrays containing the data for each type.
|
816 |
+
/// \param[in] type_codes Vector of type codes.
|
817 |
+
static Result<std::shared_ptr<Array>> Make(const Array& type_ids,
|
818 |
+
const Array& value_offsets,
|
819 |
+
ArrayVector children,
|
820 |
+
std::vector<type_code_t> type_codes) {
|
821 |
+
return Make(type_ids, value_offsets, std::move(children), std::vector<std::string>{},
|
822 |
+
std::move(type_codes));
|
823 |
+
}
|
824 |
+
|
825 |
+
/// \brief Construct DenseUnionArray with custom field names from type_ids,
|
826 |
+
/// value_offsets, and children
|
827 |
+
///
|
828 |
+
/// This function does the bare minimum of validation of the offsets and
|
829 |
+
/// input types.
|
830 |
+
///
|
831 |
+
/// \param[in] type_ids An array of logical type ids for the union type
|
832 |
+
/// \param[in] value_offsets An array of signed int32 values indicating the
|
833 |
+
/// relative offset into the respective child array for the type in a given slot.
|
834 |
+
/// The respective offsets for each child value array must be in order / increasing.
|
835 |
+
/// \param[in] children Vector of children Arrays containing the data for each type.
|
836 |
+
/// \param[in] field_names Vector of strings containing the name of each field.
|
837 |
+
/// \param[in] type_codes Vector of type codes.
|
838 |
+
static Result<std::shared_ptr<Array>> Make(const Array& type_ids,
|
839 |
+
const Array& value_offsets,
|
840 |
+
ArrayVector children,
|
841 |
+
std::vector<std::string> field_names = {},
|
842 |
+
std::vector<type_code_t> type_codes = {});
|
843 |
+
|
844 |
+
const DenseUnionType* union_type() const {
|
845 |
+
return internal::checked_cast<const DenseUnionType*>(union_type_);
|
846 |
+
}
|
847 |
+
|
848 |
+
/// Note that this buffer does not account for any slice offset
|
849 |
+
const std::shared_ptr<Buffer>& value_offsets() const { return data_->buffers[2]; }
|
850 |
+
|
851 |
+
int32_t value_offset(int64_t i) const { return raw_value_offsets_[i + data_->offset]; }
|
852 |
+
|
853 |
+
const int32_t* raw_value_offsets() const { return raw_value_offsets_ + data_->offset; }
|
854 |
+
|
855 |
+
protected:
|
856 |
+
const int32_t* raw_value_offsets_;
|
857 |
+
|
858 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
859 |
+
};
|
860 |
+
|
861 |
+
/// @}
|
862 |
+
|
863 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_primitive.h
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Array accessor types for primitive/C-type-based arrays, such as numbers,
|
19 |
+
// boolean, and temporal types.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
|
26 |
+
#include "arrow/array/array_base.h"
|
27 |
+
#include "arrow/array/data.h"
|
28 |
+
#include "arrow/stl_iterator.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/type_fwd.h" // IWYU pragma: export
|
31 |
+
#include "arrow/type_traits.h"
|
32 |
+
#include "arrow/util/bit_util.h"
|
33 |
+
#include "arrow/util/macros.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
|
36 |
+
namespace arrow {
|
37 |
+
|
38 |
+
/// Concrete Array class for boolean data
|
39 |
+
class ARROW_EXPORT BooleanArray : public PrimitiveArray {
|
40 |
+
public:
|
41 |
+
using TypeClass = BooleanType;
|
42 |
+
using IteratorType = stl::ArrayIterator<BooleanArray>;
|
43 |
+
|
44 |
+
explicit BooleanArray(const std::shared_ptr<ArrayData>& data);
|
45 |
+
|
46 |
+
BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
47 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
48 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
49 |
+
|
50 |
+
bool Value(int64_t i) const {
|
51 |
+
return bit_util::GetBit(reinterpret_cast<const uint8_t*>(raw_values_),
|
52 |
+
i + data_->offset);
|
53 |
+
}
|
54 |
+
|
55 |
+
bool GetView(int64_t i) const { return Value(i); }
|
56 |
+
|
57 |
+
std::optional<bool> operator[](int64_t i) const { return *IteratorType(*this, i); }
|
58 |
+
|
59 |
+
/// \brief Return the number of false (0) values among the valid
|
60 |
+
/// values. Result is not cached.
|
61 |
+
int64_t false_count() const;
|
62 |
+
|
63 |
+
/// \brief Return the number of true (1) values among the valid
|
64 |
+
/// values. Result is not cached.
|
65 |
+
int64_t true_count() const;
|
66 |
+
|
67 |
+
IteratorType begin() const { return IteratorType(*this); }
|
68 |
+
|
69 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
70 |
+
|
71 |
+
protected:
|
72 |
+
using PrimitiveArray::PrimitiveArray;
|
73 |
+
};
|
74 |
+
|
75 |
+
/// \addtogroup numeric-arrays
|
76 |
+
///
|
77 |
+
/// @{
|
78 |
+
|
79 |
+
/// \brief Concrete Array class for numeric data with a corresponding C type
|
80 |
+
///
|
81 |
+
/// This class is templated on the corresponding DataType subclass for the
|
82 |
+
/// given data, for example NumericArray<Int8Type> or NumericArray<Date32Type>.
|
83 |
+
///
|
84 |
+
/// Note that convenience aliases are available for all accepted types
|
85 |
+
/// (for example Int8Array for NumericArray<Int8Type>).
|
86 |
+
template <typename TYPE>
|
87 |
+
class NumericArray : public PrimitiveArray {
|
88 |
+
public:
|
89 |
+
using TypeClass = TYPE;
|
90 |
+
using value_type = typename TypeClass::c_type;
|
91 |
+
using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
|
92 |
+
|
93 |
+
explicit NumericArray(const std::shared_ptr<ArrayData>& data) : PrimitiveArray(data) {}
|
94 |
+
|
95 |
+
// Only enable this constructor without a type argument for types without additional
|
96 |
+
// metadata
|
97 |
+
template <typename T1 = TYPE>
|
98 |
+
NumericArray(enable_if_parameter_free<T1, int64_t> length,
|
99 |
+
const std::shared_ptr<Buffer>& data,
|
100 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
101 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
102 |
+
: PrimitiveArray(TypeTraits<T1>::type_singleton(), length, data, null_bitmap,
|
103 |
+
null_count, offset) {}
|
104 |
+
|
105 |
+
const value_type* raw_values() const {
|
106 |
+
return reinterpret_cast<const value_type*>(raw_values_) + data_->offset;
|
107 |
+
}
|
108 |
+
|
109 |
+
value_type Value(int64_t i) const { return raw_values()[i]; }
|
110 |
+
|
111 |
+
// For API compatibility with BinaryArray etc.
|
112 |
+
value_type GetView(int64_t i) const { return Value(i); }
|
113 |
+
|
114 |
+
std::optional<value_type> operator[](int64_t i) const {
|
115 |
+
return *IteratorType(*this, i);
|
116 |
+
}
|
117 |
+
|
118 |
+
IteratorType begin() const { return IteratorType(*this); }
|
119 |
+
|
120 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
121 |
+
|
122 |
+
protected:
|
123 |
+
using PrimitiveArray::PrimitiveArray;
|
124 |
+
};
|
125 |
+
|
126 |
+
/// DayTimeArray
|
127 |
+
/// ---------------------
|
128 |
+
/// \brief Array of Day and Millisecond values.
|
129 |
+
class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
|
130 |
+
public:
|
131 |
+
using TypeClass = DayTimeIntervalType;
|
132 |
+
using IteratorType = stl::ArrayIterator<DayTimeIntervalArray>;
|
133 |
+
|
134 |
+
explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);
|
135 |
+
|
136 |
+
DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
|
137 |
+
const std::shared_ptr<Buffer>& data,
|
138 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
139 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
140 |
+
|
141 |
+
DayTimeIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
142 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
143 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
144 |
+
|
145 |
+
TypeClass::DayMilliseconds GetValue(int64_t i) const;
|
146 |
+
TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }
|
147 |
+
|
148 |
+
// For compatibility with Take kernel.
|
149 |
+
TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }
|
150 |
+
|
151 |
+
IteratorType begin() const { return IteratorType(*this); }
|
152 |
+
|
153 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
154 |
+
|
155 |
+
std::optional<TypeClass::DayMilliseconds> operator[](int64_t i) const {
|
156 |
+
return *IteratorType(*this, i);
|
157 |
+
}
|
158 |
+
|
159 |
+
int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }
|
160 |
+
|
161 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
|
162 |
+
};
|
163 |
+
|
164 |
+
/// \brief Array of Month, Day and nanosecond values.
|
165 |
+
class ARROW_EXPORT MonthDayNanoIntervalArray : public PrimitiveArray {
|
166 |
+
public:
|
167 |
+
using TypeClass = MonthDayNanoIntervalType;
|
168 |
+
using IteratorType = stl::ArrayIterator<MonthDayNanoIntervalArray>;
|
169 |
+
|
170 |
+
explicit MonthDayNanoIntervalArray(const std::shared_ptr<ArrayData>& data);
|
171 |
+
|
172 |
+
MonthDayNanoIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
|
173 |
+
const std::shared_ptr<Buffer>& data,
|
174 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
175 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
176 |
+
|
177 |
+
MonthDayNanoIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
178 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
179 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
180 |
+
|
181 |
+
TypeClass::MonthDayNanos GetValue(int64_t i) const;
|
182 |
+
TypeClass::MonthDayNanos Value(int64_t i) const { return GetValue(i); }
|
183 |
+
|
184 |
+
// For compatibility with Take kernel.
|
185 |
+
TypeClass::MonthDayNanos GetView(int64_t i) const { return GetValue(i); }
|
186 |
+
|
187 |
+
IteratorType begin() const { return IteratorType(*this); }
|
188 |
+
|
189 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
190 |
+
|
191 |
+
std::optional<TypeClass::MonthDayNanos> operator[](int64_t i) const {
|
192 |
+
return *IteratorType(*this, i);
|
193 |
+
}
|
194 |
+
|
195 |
+
int32_t byte_width() const { return sizeof(TypeClass::MonthDayNanos); }
|
196 |
+
|
197 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
|
198 |
+
};
|
199 |
+
|
200 |
+
/// @}
|
201 |
+
|
202 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_run_end.h
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Array accessor classes run-end encoded arrays
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <cstdint>
|
23 |
+
#include <memory>
|
24 |
+
#include <string>
|
25 |
+
#include <utility>
|
26 |
+
#include <vector>
|
27 |
+
|
28 |
+
#include "arrow/array/array_base.h"
|
29 |
+
#include "arrow/array/data.h"
|
30 |
+
#include "arrow/result.h"
|
31 |
+
#include "arrow/status.h"
|
32 |
+
#include "arrow/type.h"
|
33 |
+
#include "arrow/type_fwd.h"
|
34 |
+
#include "arrow/util/checked_cast.h"
|
35 |
+
#include "arrow/util/macros.h"
|
36 |
+
#include "arrow/util/visibility.h"
|
37 |
+
|
38 |
+
namespace arrow {
|
39 |
+
|
40 |
+
/// \addtogroup run-end-encoded-arrays
|
41 |
+
///
|
42 |
+
/// @{
|
43 |
+
|
44 |
+
// ----------------------------------------------------------------------
|
45 |
+
// RunEndEncoded
|
46 |
+
|
47 |
+
/// \brief Array type for run-end encoded data
|
48 |
+
class ARROW_EXPORT RunEndEncodedArray : public Array {
|
49 |
+
private:
|
50 |
+
std::shared_ptr<Array> run_ends_array_;
|
51 |
+
std::shared_ptr<Array> values_array_;
|
52 |
+
|
53 |
+
public:
|
54 |
+
using TypeClass = RunEndEncodedType;
|
55 |
+
|
56 |
+
explicit RunEndEncodedArray(const std::shared_ptr<ArrayData>& data);
|
57 |
+
|
58 |
+
/// \brief Construct a RunEndEncodedArray from all parameters
|
59 |
+
///
|
60 |
+
/// The length and offset parameters refer to the dimensions of the logical
|
61 |
+
/// array which is the array we would get after expanding all the runs into
|
62 |
+
/// repeated values. As such, length can be much greater than the length of
|
63 |
+
/// the child run_ends and values arrays.
|
64 |
+
RunEndEncodedArray(const std::shared_ptr<DataType>& type, int64_t length,
|
65 |
+
const std::shared_ptr<Array>& run_ends,
|
66 |
+
const std::shared_ptr<Array>& values, int64_t offset = 0);
|
67 |
+
|
68 |
+
/// \brief Construct a RunEndEncodedArray from all parameters
|
69 |
+
///
|
70 |
+
/// The length and offset parameters refer to the dimensions of the logical
|
71 |
+
/// array which is the array we would get after expanding all the runs into
|
72 |
+
/// repeated values. As such, length can be much greater than the length of
|
73 |
+
/// the child run_ends and values arrays.
|
74 |
+
static Result<std::shared_ptr<RunEndEncodedArray>> Make(
|
75 |
+
const std::shared_ptr<DataType>& type, int64_t logical_length,
|
76 |
+
const std::shared_ptr<Array>& run_ends, const std::shared_ptr<Array>& values,
|
77 |
+
int64_t logical_offset = 0);
|
78 |
+
|
79 |
+
/// \brief Construct a RunEndEncodedArray from values and run ends arrays
|
80 |
+
///
|
81 |
+
/// The data type is automatically inferred from the arguments.
|
82 |
+
/// The run_ends and values arrays must have the same length.
|
83 |
+
static Result<std::shared_ptr<RunEndEncodedArray>> Make(
|
84 |
+
int64_t logical_length, const std::shared_ptr<Array>& run_ends,
|
85 |
+
const std::shared_ptr<Array>& values, int64_t logical_offset = 0);
|
86 |
+
|
87 |
+
protected:
|
88 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
89 |
+
|
90 |
+
public:
|
91 |
+
/// \brief Returns an array holding the logical indexes of each run-end
|
92 |
+
///
|
93 |
+
/// The physical offset to the array is applied.
|
94 |
+
const std::shared_ptr<Array>& run_ends() const { return run_ends_array_; }
|
95 |
+
|
96 |
+
/// \brief Returns an array holding the values of each run
|
97 |
+
///
|
98 |
+
/// The physical offset to the array is applied.
|
99 |
+
const std::shared_ptr<Array>& values() const { return values_array_; }
|
100 |
+
|
101 |
+
/// \brief Returns an array holding the logical indexes of each run end
|
102 |
+
///
|
103 |
+
/// If a non-zero logical offset is set, this function allocates a new
|
104 |
+
/// array and rewrites all the run end values to be relative to the logical
|
105 |
+
/// offset and cuts the end of the array to the logical length.
|
106 |
+
Result<std::shared_ptr<Array>> LogicalRunEnds(MemoryPool* pool) const;
|
107 |
+
|
108 |
+
/// \brief Returns an array holding the values of each run
|
109 |
+
///
|
110 |
+
/// If a non-zero logical offset is set, this function allocates a new
|
111 |
+
/// array containing only the values within the logical range.
|
112 |
+
std::shared_ptr<Array> LogicalValues() const;
|
113 |
+
|
114 |
+
/// \brief Find the physical offset of this REE array
|
115 |
+
///
|
116 |
+
/// This function uses binary-search, so it has a O(log N) cost.
|
117 |
+
int64_t FindPhysicalOffset() const;
|
118 |
+
|
119 |
+
/// \brief Find the physical length of this REE array
|
120 |
+
///
|
121 |
+
/// The physical length of an REE is the number of physical values (and
|
122 |
+
/// run-ends) necessary to represent the logical range of values from offset
|
123 |
+
/// to length.
|
124 |
+
///
|
125 |
+
/// Avoid calling this function if the physical length can be established in
|
126 |
+
/// some other way (e.g. when iterating over the runs sequentially until the
|
127 |
+
/// end). This function uses binary-search, so it has a O(log N) cost.
|
128 |
+
int64_t FindPhysicalLength() const;
|
129 |
+
};
|
130 |
+
|
131 |
+
/// @}
|
132 |
+
|
133 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_base.h
ADDED
@@ -0,0 +1,370 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm> // IWYU pragma: keep
|
21 |
+
#include <cstdint>
|
22 |
+
#include <limits>
|
23 |
+
#include <memory>
|
24 |
+
#include <utility>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/array/array_base.h"
|
28 |
+
#include "arrow/array/array_primitive.h"
|
29 |
+
#include "arrow/buffer.h"
|
30 |
+
#include "arrow/buffer_builder.h"
|
31 |
+
#include "arrow/result.h"
|
32 |
+
#include "arrow/status.h"
|
33 |
+
#include "arrow/type_fwd.h"
|
34 |
+
#include "arrow/util/macros.h"
|
35 |
+
#include "arrow/util/visibility.h"
|
36 |
+
|
37 |
+
namespace arrow {
|
38 |
+
|
39 |
+
namespace internal {
|
40 |
+
|
41 |
+
template <class Builder, class V>
|
42 |
+
class ArrayBuilderExtraOps {
|
43 |
+
public:
|
44 |
+
/// \brief Append a value from an optional or null if it has no value.
|
45 |
+
Status AppendOrNull(const std::optional<V>& value) {
|
46 |
+
auto* self = static_cast<Builder*>(this);
|
47 |
+
return value.has_value() ? self->Append(*value) : self->AppendNull();
|
48 |
+
}
|
49 |
+
|
50 |
+
/// \brief Append a value from an optional or null if it has no value.
|
51 |
+
///
|
52 |
+
/// Unsafe methods don't check existing size.
|
53 |
+
void UnsafeAppendOrNull(const std::optional<V>& value) {
|
54 |
+
auto* self = static_cast<Builder*>(this);
|
55 |
+
return value.has_value() ? self->UnsafeAppend(*value) : self->UnsafeAppendNull();
|
56 |
+
}
|
57 |
+
};
|
58 |
+
|
59 |
+
} // namespace internal
|
60 |
+
|
61 |
+
/// \defgroup numeric-builders Concrete builder subclasses for numeric types
|
62 |
+
/// @{
|
63 |
+
/// @}
|
64 |
+
|
65 |
+
/// \defgroup temporal-builders Concrete builder subclasses for temporal types
|
66 |
+
/// @{
|
67 |
+
/// @}
|
68 |
+
|
69 |
+
/// \defgroup binary-builders Concrete builder subclasses for binary types
|
70 |
+
/// @{
|
71 |
+
/// @}
|
72 |
+
|
73 |
+
/// \defgroup nested-builders Concrete builder subclasses for nested types
|
74 |
+
/// @{
|
75 |
+
/// @}
|
76 |
+
|
77 |
+
/// \defgroup dictionary-builders Concrete builder subclasses for dictionary types
|
78 |
+
/// @{
|
79 |
+
/// @}
|
80 |
+
|
81 |
+
/// \defgroup run-end-encoded-builders Concrete builder subclasses for run-end encoded
|
82 |
+
/// arrays
|
83 |
+
/// @{
|
84 |
+
/// @}
|
85 |
+
|
86 |
+
constexpr int64_t kMinBuilderCapacity = 1 << 5;
|
87 |
+
constexpr int64_t kListMaximumElements = std::numeric_limits<int32_t>::max() - 1;
|
88 |
+
|
89 |
+
/// Base class for all data array builders.
|
90 |
+
///
|
91 |
+
/// This class provides a facilities for incrementally building the null bitmap
|
92 |
+
/// (see Append methods) and as a side effect the current number of slots and
|
93 |
+
/// the null count.
|
94 |
+
///
|
95 |
+
/// \note Users are expected to use builders as one of the concrete types below.
|
96 |
+
/// For example, ArrayBuilder* pointing to BinaryBuilder should be downcast before use.
|
97 |
+
class ARROW_EXPORT ArrayBuilder {
|
98 |
+
public:
|
99 |
+
explicit ArrayBuilder(MemoryPool* pool, int64_t alignment = kDefaultBufferAlignment)
|
100 |
+
: pool_(pool), alignment_(alignment), null_bitmap_builder_(pool, alignment) {}
|
101 |
+
|
102 |
+
ARROW_DEFAULT_MOVE_AND_ASSIGN(ArrayBuilder);
|
103 |
+
|
104 |
+
virtual ~ArrayBuilder() = default;
|
105 |
+
|
106 |
+
/// For nested types. Since the objects are owned by this class instance, we
|
107 |
+
/// skip shared pointers and just return a raw pointer
|
108 |
+
ArrayBuilder* child(int i) { return children_[i].get(); }
|
109 |
+
|
110 |
+
const std::shared_ptr<ArrayBuilder>& child_builder(int i) const { return children_[i]; }
|
111 |
+
|
112 |
+
int num_children() const { return static_cast<int>(children_.size()); }
|
113 |
+
|
114 |
+
virtual int64_t length() const { return length_; }
|
115 |
+
int64_t null_count() const { return null_count_; }
|
116 |
+
int64_t capacity() const { return capacity_; }
|
117 |
+
|
118 |
+
/// \brief Ensure that enough memory has been allocated to fit the indicated
|
119 |
+
/// number of total elements in the builder, including any that have already
|
120 |
+
/// been appended. Does not account for reallocations that may be due to
|
121 |
+
/// variable size data, like binary values. To make space for incremental
|
122 |
+
/// appends, use Reserve instead.
|
123 |
+
///
|
124 |
+
/// \param[in] capacity the minimum number of total array values to
|
125 |
+
/// accommodate. Must be greater than the current capacity.
|
126 |
+
/// \return Status
|
127 |
+
virtual Status Resize(int64_t capacity);
|
128 |
+
|
129 |
+
/// \brief Ensure that there is enough space allocated to append the indicated
|
130 |
+
/// number of elements without any further reallocation. Overallocation is
|
131 |
+
/// used in order to minimize the impact of incremental Reserve() calls.
|
132 |
+
/// Note that additional_capacity is relative to the current number of elements
|
133 |
+
/// rather than to the current capacity, so calls to Reserve() which are not
|
134 |
+
/// interspersed with addition of new elements may not increase the capacity.
|
135 |
+
///
|
136 |
+
/// \param[in] additional_capacity the number of additional array values
|
137 |
+
/// \return Status
|
138 |
+
Status Reserve(int64_t additional_capacity) {
|
139 |
+
auto current_capacity = capacity();
|
140 |
+
auto min_capacity = length() + additional_capacity;
|
141 |
+
if (min_capacity <= current_capacity) return Status::OK();
|
142 |
+
|
143 |
+
// leave growth factor up to BufferBuilder
|
144 |
+
auto new_capacity = BufferBuilder::GrowByFactor(current_capacity, min_capacity);
|
145 |
+
return Resize(new_capacity);
|
146 |
+
}
|
147 |
+
|
148 |
+
/// Reset the builder.
|
149 |
+
virtual void Reset();
|
150 |
+
|
151 |
+
/// \brief Append a null value to builder
|
152 |
+
virtual Status AppendNull() = 0;
|
153 |
+
/// \brief Append a number of null values to builder
|
154 |
+
virtual Status AppendNulls(int64_t length) = 0;
|
155 |
+
|
156 |
+
/// \brief Append a non-null value to builder
|
157 |
+
///
|
158 |
+
/// The appended value is an implementation detail, but the corresponding
|
159 |
+
/// memory slot is guaranteed to be initialized.
|
160 |
+
/// This method is useful when appending a null value to a parent nested type.
|
161 |
+
virtual Status AppendEmptyValue() = 0;
|
162 |
+
|
163 |
+
/// \brief Append a number of non-null values to builder
|
164 |
+
///
|
165 |
+
/// The appended values are an implementation detail, but the corresponding
|
166 |
+
/// memory slot is guaranteed to be initialized.
|
167 |
+
/// This method is useful when appending null values to a parent nested type.
|
168 |
+
virtual Status AppendEmptyValues(int64_t length) = 0;
|
169 |
+
|
170 |
+
/// \brief Append a value from a scalar
|
171 |
+
Status AppendScalar(const Scalar& scalar) { return AppendScalar(scalar, 1); }
|
172 |
+
virtual Status AppendScalar(const Scalar& scalar, int64_t n_repeats);
|
173 |
+
virtual Status AppendScalars(const ScalarVector& scalars);
|
174 |
+
|
175 |
+
/// \brief Append a range of values from an array.
|
176 |
+
///
|
177 |
+
/// The given array must be the same type as the builder.
|
178 |
+
virtual Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
179 |
+
int64_t length) {
|
180 |
+
return Status::NotImplemented("AppendArraySlice for builder for ", *type());
|
181 |
+
}
|
182 |
+
|
183 |
+
/// \brief Return result of builder as an internal generic ArrayData
|
184 |
+
/// object. Resets builder except for dictionary builder
|
185 |
+
///
|
186 |
+
/// \param[out] out the finalized ArrayData object
|
187 |
+
/// \return Status
|
188 |
+
virtual Status FinishInternal(std::shared_ptr<ArrayData>* out) = 0;
|
189 |
+
|
190 |
+
/// \brief Return result of builder as an Array object.
|
191 |
+
///
|
192 |
+
/// The builder is reset except for DictionaryBuilder.
|
193 |
+
///
|
194 |
+
/// \param[out] out the finalized Array object
|
195 |
+
/// \return Status
|
196 |
+
Status Finish(std::shared_ptr<Array>* out);
|
197 |
+
|
198 |
+
/// \brief Return result of builder as an Array object.
|
199 |
+
///
|
200 |
+
/// The builder is reset except for DictionaryBuilder.
|
201 |
+
///
|
202 |
+
/// \return The finalized Array object
|
203 |
+
Result<std::shared_ptr<Array>> Finish();
|
204 |
+
|
205 |
+
/// \brief Return the type of the built Array
|
206 |
+
virtual std::shared_ptr<DataType> type() const = 0;
|
207 |
+
|
208 |
+
protected:
|
209 |
+
/// Append to null bitmap
|
210 |
+
Status AppendToBitmap(bool is_valid);
|
211 |
+
|
212 |
+
/// Vector append. Treat each zero byte as a null. If valid_bytes is null
|
213 |
+
/// assume all of length bits are valid.
|
214 |
+
Status AppendToBitmap(const uint8_t* valid_bytes, int64_t length);
|
215 |
+
|
216 |
+
/// Uniform append. Append N times the same validity bit.
|
217 |
+
Status AppendToBitmap(int64_t num_bits, bool value);
|
218 |
+
|
219 |
+
/// Set the next length bits to not null (i.e. valid).
|
220 |
+
Status SetNotNull(int64_t length);
|
221 |
+
|
222 |
+
// Unsafe operations (don't check capacity/don't resize)
|
223 |
+
|
224 |
+
void UnsafeAppendNull() { UnsafeAppendToBitmap(false); }
|
225 |
+
|
226 |
+
// Append to null bitmap, update the length
|
227 |
+
void UnsafeAppendToBitmap(bool is_valid) {
|
228 |
+
null_bitmap_builder_.UnsafeAppend(is_valid);
|
229 |
+
++length_;
|
230 |
+
if (!is_valid) ++null_count_;
|
231 |
+
}
|
232 |
+
|
233 |
+
// Vector append. Treat each zero byte as a nullzero. If valid_bytes is null
|
234 |
+
// assume all of length bits are valid.
|
235 |
+
void UnsafeAppendToBitmap(const uint8_t* valid_bytes, int64_t length) {
|
236 |
+
if (valid_bytes == NULLPTR) {
|
237 |
+
return UnsafeSetNotNull(length);
|
238 |
+
}
|
239 |
+
null_bitmap_builder_.UnsafeAppend(valid_bytes, length);
|
240 |
+
length_ += length;
|
241 |
+
null_count_ = null_bitmap_builder_.false_count();
|
242 |
+
}
|
243 |
+
|
244 |
+
// Vector append. Copy from a given bitmap. If bitmap is null assume
|
245 |
+
// all of length bits are valid.
|
246 |
+
void UnsafeAppendToBitmap(const uint8_t* bitmap, int64_t offset, int64_t length) {
|
247 |
+
if (bitmap == NULLPTR) {
|
248 |
+
return UnsafeSetNotNull(length);
|
249 |
+
}
|
250 |
+
null_bitmap_builder_.UnsafeAppend(bitmap, offset, length);
|
251 |
+
length_ += length;
|
252 |
+
null_count_ = null_bitmap_builder_.false_count();
|
253 |
+
}
|
254 |
+
|
255 |
+
// Append the same validity value a given number of times.
|
256 |
+
void UnsafeAppendToBitmap(const int64_t num_bits, bool value) {
|
257 |
+
if (value) {
|
258 |
+
UnsafeSetNotNull(num_bits);
|
259 |
+
} else {
|
260 |
+
UnsafeSetNull(num_bits);
|
261 |
+
}
|
262 |
+
}
|
263 |
+
|
264 |
+
void UnsafeAppendToBitmap(const std::vector<bool>& is_valid);
|
265 |
+
|
266 |
+
// Set the next validity bits to not null (i.e. valid).
|
267 |
+
void UnsafeSetNotNull(int64_t length);
|
268 |
+
|
269 |
+
// Set the next validity bits to null (i.e. invalid).
|
270 |
+
void UnsafeSetNull(int64_t length);
|
271 |
+
|
272 |
+
static Status TrimBuffer(const int64_t bytes_filled, ResizableBuffer* buffer);
|
273 |
+
|
274 |
+
/// \brief Finish to an array of the specified ArrayType
|
275 |
+
template <typename ArrayType>
|
276 |
+
Status FinishTyped(std::shared_ptr<ArrayType>* out) {
|
277 |
+
std::shared_ptr<Array> out_untyped;
|
278 |
+
ARROW_RETURN_NOT_OK(Finish(&out_untyped));
|
279 |
+
*out = std::static_pointer_cast<ArrayType>(std::move(out_untyped));
|
280 |
+
return Status::OK();
|
281 |
+
}
|
282 |
+
|
283 |
+
// Check the requested capacity for validity
|
284 |
+
Status CheckCapacity(int64_t new_capacity) {
|
285 |
+
if (ARROW_PREDICT_FALSE(new_capacity < 0)) {
|
286 |
+
return Status::Invalid(
|
287 |
+
"Resize capacity must be positive (requested: ", new_capacity, ")");
|
288 |
+
}
|
289 |
+
|
290 |
+
if (ARROW_PREDICT_FALSE(new_capacity < length_)) {
|
291 |
+
return Status::Invalid("Resize cannot downsize (requested: ", new_capacity,
|
292 |
+
", current length: ", length_, ")");
|
293 |
+
}
|
294 |
+
|
295 |
+
return Status::OK();
|
296 |
+
}
|
297 |
+
|
298 |
+
// Check for array type
|
299 |
+
Status CheckArrayType(const std::shared_ptr<DataType>& expected_type,
|
300 |
+
const Array& array, const char* message);
|
301 |
+
Status CheckArrayType(Type::type expected_type, const Array& array,
|
302 |
+
const char* message);
|
303 |
+
|
304 |
+
MemoryPool* pool_;
|
305 |
+
int64_t alignment_;
|
306 |
+
|
307 |
+
TypedBufferBuilder<bool> null_bitmap_builder_;
|
308 |
+
int64_t null_count_ = 0;
|
309 |
+
|
310 |
+
// Array length, so far. Also, the index of the next element to be added
|
311 |
+
int64_t length_ = 0;
|
312 |
+
int64_t capacity_ = 0;
|
313 |
+
|
314 |
+
// Child value array builders. These are owned by this class
|
315 |
+
std::vector<std::shared_ptr<ArrayBuilder>> children_;
|
316 |
+
|
317 |
+
private:
|
318 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(ArrayBuilder);
|
319 |
+
};
|
320 |
+
|
321 |
+
/// \brief Construct an empty ArrayBuilder corresponding to the data
|
322 |
+
/// type
|
323 |
+
/// \param[in] pool the MemoryPool to use for allocations
|
324 |
+
/// \param[in] type the data type to create the builder for
|
325 |
+
/// \param[out] out the created ArrayBuilder
|
326 |
+
ARROW_EXPORT
|
327 |
+
Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
|
328 |
+
std::unique_ptr<ArrayBuilder>* out);
|
329 |
+
|
330 |
+
inline Result<std::unique_ptr<ArrayBuilder>> MakeBuilder(
|
331 |
+
const std::shared_ptr<DataType>& type, MemoryPool* pool = default_memory_pool()) {
|
332 |
+
std::unique_ptr<ArrayBuilder> out;
|
333 |
+
ARROW_RETURN_NOT_OK(MakeBuilder(pool, type, &out));
|
334 |
+
return std::move(out);
|
335 |
+
}
|
336 |
+
|
337 |
+
/// \brief Construct an empty ArrayBuilder corresponding to the data
|
338 |
+
/// type, where any top-level or nested dictionary builders return the
|
339 |
+
/// exact index type specified by the type.
|
340 |
+
ARROW_EXPORT
|
341 |
+
Status MakeBuilderExactIndex(MemoryPool* pool, const std::shared_ptr<DataType>& type,
|
342 |
+
std::unique_ptr<ArrayBuilder>* out);
|
343 |
+
|
344 |
+
inline Result<std::unique_ptr<ArrayBuilder>> MakeBuilderExactIndex(
|
345 |
+
const std::shared_ptr<DataType>& type, MemoryPool* pool = default_memory_pool()) {
|
346 |
+
std::unique_ptr<ArrayBuilder> out;
|
347 |
+
ARROW_RETURN_NOT_OK(MakeBuilderExactIndex(pool, type, &out));
|
348 |
+
return std::move(out);
|
349 |
+
}
|
350 |
+
|
351 |
+
/// \brief Construct an empty DictionaryBuilder initialized optionally
|
352 |
+
/// with a preexisting dictionary
|
353 |
+
/// \param[in] pool the MemoryPool to use for allocations
|
354 |
+
/// \param[in] type the dictionary type to create the builder for
|
355 |
+
/// \param[in] dictionary the initial dictionary, if any. May be nullptr
|
356 |
+
/// \param[out] out the created ArrayBuilder
|
357 |
+
ARROW_EXPORT
|
358 |
+
Status MakeDictionaryBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
|
359 |
+
const std::shared_ptr<Array>& dictionary,
|
360 |
+
std::unique_ptr<ArrayBuilder>* out);
|
361 |
+
|
362 |
+
inline Result<std::unique_ptr<ArrayBuilder>> MakeDictionaryBuilder(
|
363 |
+
const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& dictionary,
|
364 |
+
MemoryPool* pool = default_memory_pool()) {
|
365 |
+
std::unique_ptr<ArrayBuilder> out;
|
366 |
+
ARROW_RETURN_NOT_OK(MakeDictionaryBuilder(pool, type, dictionary, &out));
|
367 |
+
return std::move(out);
|
368 |
+
}
|
369 |
+
|
370 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_binary.h
ADDED
@@ -0,0 +1,971 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <array>
|
21 |
+
#include <cstddef>
|
22 |
+
#include <cstdint>
|
23 |
+
#include <cstring>
|
24 |
+
#include <limits>
|
25 |
+
#include <memory>
|
26 |
+
#include <numeric>
|
27 |
+
#include <string>
|
28 |
+
#include <string_view>
|
29 |
+
#include <vector>
|
30 |
+
|
31 |
+
#include "arrow/array/array_base.h"
|
32 |
+
#include "arrow/array/array_binary.h"
|
33 |
+
#include "arrow/array/builder_base.h"
|
34 |
+
#include "arrow/array/data.h"
|
35 |
+
#include "arrow/buffer.h"
|
36 |
+
#include "arrow/buffer_builder.h"
|
37 |
+
#include "arrow/status.h"
|
38 |
+
#include "arrow/type.h"
|
39 |
+
#include "arrow/util/binary_view_util.h"
|
40 |
+
#include "arrow/util/macros.h"
|
41 |
+
#include "arrow/util/visibility.h"
|
42 |
+
|
43 |
+
namespace arrow {
|
44 |
+
|
45 |
+
/// \addtogroup binary-builders
|
46 |
+
///
|
47 |
+
/// @{
|
48 |
+
|
49 |
+
// ----------------------------------------------------------------------
|
50 |
+
// Binary and String
|
51 |
+
|
52 |
+
template <typename TYPE>
|
53 |
+
class BaseBinaryBuilder
|
54 |
+
: public ArrayBuilder,
|
55 |
+
public internal::ArrayBuilderExtraOps<BaseBinaryBuilder<TYPE>, std::string_view> {
|
56 |
+
public:
|
57 |
+
using TypeClass = TYPE;
|
58 |
+
using offset_type = typename TypeClass::offset_type;
|
59 |
+
|
60 |
+
explicit BaseBinaryBuilder(MemoryPool* pool = default_memory_pool(),
|
61 |
+
int64_t alignment = kDefaultBufferAlignment)
|
62 |
+
: ArrayBuilder(pool, alignment),
|
63 |
+
offsets_builder_(pool, alignment),
|
64 |
+
value_data_builder_(pool, alignment) {}
|
65 |
+
|
66 |
+
BaseBinaryBuilder(const std::shared_ptr<DataType>& type, MemoryPool* pool)
|
67 |
+
: BaseBinaryBuilder(pool) {}
|
68 |
+
|
69 |
+
Status Append(const uint8_t* value, offset_type length) {
|
70 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
71 |
+
UnsafeAppendNextOffset();
|
72 |
+
// Safety check for UBSAN.
|
73 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
74 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(length));
|
75 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Append(value, length));
|
76 |
+
}
|
77 |
+
|
78 |
+
UnsafeAppendToBitmap(true);
|
79 |
+
return Status::OK();
|
80 |
+
}
|
81 |
+
|
82 |
+
Status Append(const char* value, offset_type length) {
|
83 |
+
return Append(reinterpret_cast<const uint8_t*>(value), length);
|
84 |
+
}
|
85 |
+
|
86 |
+
Status Append(std::string_view value) {
|
87 |
+
return Append(value.data(), static_cast<offset_type>(value.size()));
|
88 |
+
}
|
89 |
+
|
90 |
+
/// Extend the last appended value by appending more data at the end
|
91 |
+
///
|
92 |
+
/// Unlike Append, this does not create a new offset.
|
93 |
+
Status ExtendCurrent(const uint8_t* value, offset_type length) {
|
94 |
+
// Safety check for UBSAN.
|
95 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
96 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(length));
|
97 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Append(value, length));
|
98 |
+
}
|
99 |
+
return Status::OK();
|
100 |
+
}
|
101 |
+
|
102 |
+
Status ExtendCurrent(std::string_view value) {
|
103 |
+
return ExtendCurrent(reinterpret_cast<const uint8_t*>(value.data()),
|
104 |
+
static_cast<offset_type>(value.size()));
|
105 |
+
}
|
106 |
+
|
107 |
+
Status AppendNulls(int64_t length) final {
|
108 |
+
const int64_t num_bytes = value_data_builder_.length();
|
109 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
110 |
+
for (int64_t i = 0; i < length; ++i) {
|
111 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
112 |
+
}
|
113 |
+
UnsafeAppendToBitmap(length, false);
|
114 |
+
return Status::OK();
|
115 |
+
}
|
116 |
+
|
117 |
+
Status AppendNull() final {
|
118 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
119 |
+
UnsafeAppendNextOffset();
|
120 |
+
UnsafeAppendToBitmap(false);
|
121 |
+
return Status::OK();
|
122 |
+
}
|
123 |
+
|
124 |
+
Status AppendEmptyValue() final {
|
125 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
126 |
+
UnsafeAppendNextOffset();
|
127 |
+
UnsafeAppendToBitmap(true);
|
128 |
+
return Status::OK();
|
129 |
+
}
|
130 |
+
|
131 |
+
Status AppendEmptyValues(int64_t length) final {
|
132 |
+
const int64_t num_bytes = value_data_builder_.length();
|
133 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
134 |
+
for (int64_t i = 0; i < length; ++i) {
|
135 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
136 |
+
}
|
137 |
+
UnsafeAppendToBitmap(length, true);
|
138 |
+
return Status::OK();
|
139 |
+
}
|
140 |
+
|
141 |
+
/// \brief Append without checking capacity
|
142 |
+
///
|
143 |
+
/// Offsets and data should have been presized using Reserve() and
|
144 |
+
/// ReserveData(), respectively.
|
145 |
+
void UnsafeAppend(const uint8_t* value, offset_type length) {
|
146 |
+
UnsafeAppendNextOffset();
|
147 |
+
value_data_builder_.UnsafeAppend(value, length);
|
148 |
+
UnsafeAppendToBitmap(true);
|
149 |
+
}
|
150 |
+
|
151 |
+
void UnsafeAppend(const char* value, offset_type length) {
|
152 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value), length);
|
153 |
+
}
|
154 |
+
|
155 |
+
void UnsafeAppend(const std::string& value) {
|
156 |
+
UnsafeAppend(value.c_str(), static_cast<offset_type>(value.size()));
|
157 |
+
}
|
158 |
+
|
159 |
+
void UnsafeAppend(std::string_view value) {
|
160 |
+
UnsafeAppend(value.data(), static_cast<offset_type>(value.size()));
|
161 |
+
}
|
162 |
+
|
163 |
+
/// Like ExtendCurrent, but do not check capacity
|
164 |
+
void UnsafeExtendCurrent(const uint8_t* value, offset_type length) {
|
165 |
+
value_data_builder_.UnsafeAppend(value, length);
|
166 |
+
}
|
167 |
+
|
168 |
+
void UnsafeExtendCurrent(std::string_view value) {
|
169 |
+
UnsafeExtendCurrent(reinterpret_cast<const uint8_t*>(value.data()),
|
170 |
+
static_cast<offset_type>(value.size()));
|
171 |
+
}
|
172 |
+
|
173 |
+
void UnsafeAppendNull() {
|
174 |
+
const int64_t num_bytes = value_data_builder_.length();
|
175 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
176 |
+
UnsafeAppendToBitmap(false);
|
177 |
+
}
|
178 |
+
|
179 |
+
void UnsafeAppendEmptyValue() {
|
180 |
+
const int64_t num_bytes = value_data_builder_.length();
|
181 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
182 |
+
UnsafeAppendToBitmap(true);
|
183 |
+
}
|
184 |
+
|
185 |
+
/// \brief Append a sequence of strings in one shot.
|
186 |
+
///
|
187 |
+
/// \param[in] values a vector of strings
|
188 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
189 |
+
/// indicates a valid (non-null) value
|
190 |
+
/// \return Status
|
191 |
+
Status AppendValues(const std::vector<std::string>& values,
|
192 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
193 |
+
std::size_t total_length = std::accumulate(
|
194 |
+
values.begin(), values.end(), 0ULL,
|
195 |
+
[](uint64_t sum, const std::string& str) { return sum + str.size(); });
|
196 |
+
ARROW_RETURN_NOT_OK(Reserve(values.size()));
|
197 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
198 |
+
|
199 |
+
if (valid_bytes != NULLPTR) {
|
200 |
+
for (std::size_t i = 0; i < values.size(); ++i) {
|
201 |
+
UnsafeAppendNextOffset();
|
202 |
+
if (valid_bytes[i]) {
|
203 |
+
value_data_builder_.UnsafeAppend(
|
204 |
+
reinterpret_cast<const uint8_t*>(values[i].data()), values[i].size());
|
205 |
+
}
|
206 |
+
}
|
207 |
+
} else {
|
208 |
+
for (const auto& value : values) {
|
209 |
+
UnsafeAppendNextOffset();
|
210 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(value.data()),
|
211 |
+
value.size());
|
212 |
+
}
|
213 |
+
}
|
214 |
+
|
215 |
+
UnsafeAppendToBitmap(valid_bytes, values.size());
|
216 |
+
return Status::OK();
|
217 |
+
}
|
218 |
+
|
219 |
+
/// \brief Append a sequence of nul-terminated strings in one shot.
|
220 |
+
/// If one of the values is NULL, it is processed as a null
|
221 |
+
/// value even if the corresponding valid_bytes entry is 1.
|
222 |
+
///
|
223 |
+
/// \param[in] values a contiguous C array of nul-terminated char *
|
224 |
+
/// \param[in] length the number of values to append
|
225 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
226 |
+
/// indicates a valid (non-null) value
|
227 |
+
/// \return Status
|
228 |
+
Status AppendValues(const char** values, int64_t length,
|
229 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
230 |
+
std::size_t total_length = 0;
|
231 |
+
std::vector<std::size_t> value_lengths(length);
|
232 |
+
bool have_null_value = false;
|
233 |
+
for (int64_t i = 0; i < length; ++i) {
|
234 |
+
if (values[i] != NULLPTR) {
|
235 |
+
auto value_length = strlen(values[i]);
|
236 |
+
value_lengths[i] = value_length;
|
237 |
+
total_length += value_length;
|
238 |
+
} else {
|
239 |
+
have_null_value = true;
|
240 |
+
}
|
241 |
+
}
|
242 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
243 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
244 |
+
|
245 |
+
if (valid_bytes) {
|
246 |
+
int64_t valid_bytes_offset = 0;
|
247 |
+
for (int64_t i = 0; i < length; ++i) {
|
248 |
+
UnsafeAppendNextOffset();
|
249 |
+
if (valid_bytes[i]) {
|
250 |
+
if (values[i]) {
|
251 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
252 |
+
value_lengths[i]);
|
253 |
+
} else {
|
254 |
+
UnsafeAppendToBitmap(valid_bytes + valid_bytes_offset,
|
255 |
+
i - valid_bytes_offset);
|
256 |
+
UnsafeAppendToBitmap(false);
|
257 |
+
valid_bytes_offset = i + 1;
|
258 |
+
}
|
259 |
+
}
|
260 |
+
}
|
261 |
+
UnsafeAppendToBitmap(valid_bytes + valid_bytes_offset, length - valid_bytes_offset);
|
262 |
+
} else {
|
263 |
+
if (have_null_value) {
|
264 |
+
std::vector<uint8_t> valid_vector(length, 0);
|
265 |
+
for (int64_t i = 0; i < length; ++i) {
|
266 |
+
UnsafeAppendNextOffset();
|
267 |
+
if (values[i]) {
|
268 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
269 |
+
value_lengths[i]);
|
270 |
+
valid_vector[i] = 1;
|
271 |
+
}
|
272 |
+
}
|
273 |
+
UnsafeAppendToBitmap(valid_vector.data(), length);
|
274 |
+
} else {
|
275 |
+
for (int64_t i = 0; i < length; ++i) {
|
276 |
+
UnsafeAppendNextOffset();
|
277 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
278 |
+
value_lengths[i]);
|
279 |
+
}
|
280 |
+
UnsafeAppendToBitmap(NULLPTR, length);
|
281 |
+
}
|
282 |
+
}
|
283 |
+
return Status::OK();
|
284 |
+
}
|
285 |
+
|
286 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
287 |
+
int64_t length) override {
|
288 |
+
auto bitmap = array.GetValues<uint8_t>(0, 0);
|
289 |
+
auto offsets = array.GetValues<offset_type>(1);
|
290 |
+
auto data = array.GetValues<uint8_t>(2, 0);
|
291 |
+
auto total_length = offsets[offset + length] - offsets[offset];
|
292 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
293 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
294 |
+
for (int64_t i = 0; i < length; i++) {
|
295 |
+
if (!bitmap || bit_util::GetBit(bitmap, array.offset + offset + i)) {
|
296 |
+
const offset_type start = offsets[offset + i];
|
297 |
+
const offset_type end = offsets[offset + i + 1];
|
298 |
+
UnsafeAppend(data + start, end - start);
|
299 |
+
} else {
|
300 |
+
UnsafeAppendNull();
|
301 |
+
}
|
302 |
+
}
|
303 |
+
return Status::OK();
|
304 |
+
}
|
305 |
+
|
306 |
+
void Reset() override {
|
307 |
+
ArrayBuilder::Reset();
|
308 |
+
offsets_builder_.Reset();
|
309 |
+
value_data_builder_.Reset();
|
310 |
+
}
|
311 |
+
|
312 |
+
Status ValidateOverflow(int64_t new_bytes) {
|
313 |
+
auto new_size = value_data_builder_.length() + new_bytes;
|
314 |
+
if (ARROW_PREDICT_FALSE(new_size > memory_limit())) {
|
315 |
+
return Status::CapacityError("array cannot contain more than ", memory_limit(),
|
316 |
+
" bytes, have ", new_size);
|
317 |
+
} else {
|
318 |
+
return Status::OK();
|
319 |
+
}
|
320 |
+
}
|
321 |
+
|
322 |
+
Status Resize(int64_t capacity) override {
|
323 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
324 |
+
// One more than requested for offsets
|
325 |
+
ARROW_RETURN_NOT_OK(offsets_builder_.Resize(capacity + 1));
|
326 |
+
return ArrayBuilder::Resize(capacity);
|
327 |
+
}
|
328 |
+
|
329 |
+
/// \brief Ensures there is enough allocated capacity to append the indicated
|
330 |
+
/// number of bytes to the value data buffer without additional allocations
|
331 |
+
Status ReserveData(int64_t elements) {
|
332 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(elements));
|
333 |
+
return value_data_builder_.Reserve(elements);
|
334 |
+
}
|
335 |
+
|
336 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
337 |
+
// Write final offset (values length)
|
338 |
+
ARROW_RETURN_NOT_OK(AppendNextOffset());
|
339 |
+
|
340 |
+
// These buffers' padding zeroed by BufferBuilder
|
341 |
+
std::shared_ptr<Buffer> offsets, value_data, null_bitmap;
|
342 |
+
ARROW_RETURN_NOT_OK(offsets_builder_.Finish(&offsets));
|
343 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Finish(&value_data));
|
344 |
+
ARROW_RETURN_NOT_OK(null_bitmap_builder_.Finish(&null_bitmap));
|
345 |
+
|
346 |
+
*out = ArrayData::Make(type(), length_, {null_bitmap, offsets, value_data},
|
347 |
+
null_count_, 0);
|
348 |
+
Reset();
|
349 |
+
return Status::OK();
|
350 |
+
}
|
351 |
+
|
352 |
+
/// \return data pointer of the value date builder
|
353 |
+
const uint8_t* value_data() const { return value_data_builder_.data(); }
|
354 |
+
/// \return size of values buffer so far
|
355 |
+
int64_t value_data_length() const { return value_data_builder_.length(); }
|
356 |
+
/// \return capacity of values buffer
|
357 |
+
int64_t value_data_capacity() const { return value_data_builder_.capacity(); }
|
358 |
+
|
359 |
+
/// \return data pointer of the value date builder
|
360 |
+
const offset_type* offsets_data() const { return offsets_builder_.data(); }
|
361 |
+
|
362 |
+
/// Temporary access to a value.
|
363 |
+
///
|
364 |
+
/// This pointer becomes invalid on the next modifying operation.
|
365 |
+
const uint8_t* GetValue(int64_t i, offset_type* out_length) const {
|
366 |
+
const offset_type* offsets = offsets_builder_.data();
|
367 |
+
const auto offset = offsets[i];
|
368 |
+
if (i == (length_ - 1)) {
|
369 |
+
*out_length = static_cast<offset_type>(value_data_builder_.length()) - offset;
|
370 |
+
} else {
|
371 |
+
*out_length = offsets[i + 1] - offset;
|
372 |
+
}
|
373 |
+
return value_data_builder_.data() + offset;
|
374 |
+
}
|
375 |
+
|
376 |
+
offset_type offset(int64_t i) const { return offsets_data()[i]; }
|
377 |
+
|
378 |
+
/// Temporary access to a value.
|
379 |
+
///
|
380 |
+
/// This view becomes invalid on the next modifying operation.
|
381 |
+
std::string_view GetView(int64_t i) const {
|
382 |
+
offset_type value_length;
|
383 |
+
const uint8_t* value_data = GetValue(i, &value_length);
|
384 |
+
return std::string_view(reinterpret_cast<const char*>(value_data), value_length);
|
385 |
+
}
|
386 |
+
|
387 |
+
// Cannot make this a static attribute because of linking issues
|
388 |
+
static constexpr int64_t memory_limit() {
|
389 |
+
return std::numeric_limits<offset_type>::max() - 1;
|
390 |
+
}
|
391 |
+
|
392 |
+
protected:
|
393 |
+
TypedBufferBuilder<offset_type> offsets_builder_;
|
394 |
+
TypedBufferBuilder<uint8_t> value_data_builder_;
|
395 |
+
|
396 |
+
Status AppendNextOffset() {
|
397 |
+
const int64_t num_bytes = value_data_builder_.length();
|
398 |
+
return offsets_builder_.Append(static_cast<offset_type>(num_bytes));
|
399 |
+
}
|
400 |
+
|
401 |
+
void UnsafeAppendNextOffset() {
|
402 |
+
const int64_t num_bytes = value_data_builder_.length();
|
403 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
404 |
+
}
|
405 |
+
};
|
406 |
+
|
407 |
+
/// \class BinaryBuilder
|
408 |
+
/// \brief Builder class for variable-length binary data
|
409 |
+
class ARROW_EXPORT BinaryBuilder : public BaseBinaryBuilder<BinaryType> {
|
410 |
+
public:
|
411 |
+
using BaseBinaryBuilder::BaseBinaryBuilder;
|
412 |
+
|
413 |
+
/// \cond FALSE
|
414 |
+
using ArrayBuilder::Finish;
|
415 |
+
/// \endcond
|
416 |
+
|
417 |
+
Status Finish(std::shared_ptr<BinaryArray>* out) { return FinishTyped(out); }
|
418 |
+
|
419 |
+
std::shared_ptr<DataType> type() const override { return binary(); }
|
420 |
+
};
|
421 |
+
|
422 |
+
/// \class StringBuilder
|
423 |
+
/// \brief Builder class for UTF8 strings
|
424 |
+
class ARROW_EXPORT StringBuilder : public BinaryBuilder {
|
425 |
+
public:
|
426 |
+
using BinaryBuilder::BinaryBuilder;
|
427 |
+
|
428 |
+
/// \cond FALSE
|
429 |
+
using ArrayBuilder::Finish;
|
430 |
+
/// \endcond
|
431 |
+
|
432 |
+
Status Finish(std::shared_ptr<StringArray>* out) { return FinishTyped(out); }
|
433 |
+
|
434 |
+
std::shared_ptr<DataType> type() const override { return utf8(); }
|
435 |
+
};
|
436 |
+
|
437 |
+
/// \class LargeBinaryBuilder
|
438 |
+
/// \brief Builder class for large variable-length binary data
|
439 |
+
class ARROW_EXPORT LargeBinaryBuilder : public BaseBinaryBuilder<LargeBinaryType> {
|
440 |
+
public:
|
441 |
+
using BaseBinaryBuilder::BaseBinaryBuilder;
|
442 |
+
|
443 |
+
/// \cond FALSE
|
444 |
+
using ArrayBuilder::Finish;
|
445 |
+
/// \endcond
|
446 |
+
|
447 |
+
Status Finish(std::shared_ptr<LargeBinaryArray>* out) { return FinishTyped(out); }
|
448 |
+
|
449 |
+
std::shared_ptr<DataType> type() const override { return large_binary(); }
|
450 |
+
};
|
451 |
+
|
452 |
+
/// \class LargeStringBuilder
|
453 |
+
/// \brief Builder class for large UTF8 strings
|
454 |
+
class ARROW_EXPORT LargeStringBuilder : public LargeBinaryBuilder {
|
455 |
+
public:
|
456 |
+
using LargeBinaryBuilder::LargeBinaryBuilder;
|
457 |
+
|
458 |
+
/// \cond FALSE
|
459 |
+
using ArrayBuilder::Finish;
|
460 |
+
/// \endcond
|
461 |
+
|
462 |
+
Status Finish(std::shared_ptr<LargeStringArray>* out) { return FinishTyped(out); }
|
463 |
+
|
464 |
+
std::shared_ptr<DataType> type() const override { return large_utf8(); }
|
465 |
+
};
|
466 |
+
|
467 |
+
// ----------------------------------------------------------------------
|
468 |
+
// BinaryViewBuilder, StringViewBuilder
|
469 |
+
//
|
470 |
+
// These builders do not support building raw pointer view arrays.
|
471 |
+
|
472 |
+
namespace internal {
|
473 |
+
|
474 |
+
// We allocate medium-sized memory chunks and accumulate data in those, which
|
475 |
+
// may result in some waste if there are many large-ish strings. If a string
|
476 |
+
// comes along that does not fit into a block, we allocate a new block and
|
477 |
+
// write into that.
|
478 |
+
//
|
479 |
+
// Later we can implement optimizations to continuing filling underfull blocks
|
480 |
+
// after encountering a large string that required allocating a new block.
|
481 |
+
class ARROW_EXPORT StringHeapBuilder {
|
482 |
+
public:
|
483 |
+
static constexpr int64_t kDefaultBlocksize = 32 << 10; // 32KB
|
484 |
+
|
485 |
+
StringHeapBuilder(MemoryPool* pool, int64_t alignment)
|
486 |
+
: pool_(pool), alignment_(alignment) {}
|
487 |
+
|
488 |
+
void SetBlockSize(int64_t blocksize) { blocksize_ = blocksize; }
|
489 |
+
|
490 |
+
using c_type = BinaryViewType::c_type;
|
491 |
+
|
492 |
+
template <bool Safe>
|
493 |
+
std::conditional_t<Safe, Result<c_type>, c_type> Append(const uint8_t* value,
|
494 |
+
int64_t length) {
|
495 |
+
if (length <= BinaryViewType::kInlineSize) {
|
496 |
+
return util::ToInlineBinaryView(value, static_cast<int32_t>(length));
|
497 |
+
}
|
498 |
+
|
499 |
+
if constexpr (Safe) {
|
500 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
501 |
+
}
|
502 |
+
|
503 |
+
auto v =
|
504 |
+
util::ToBinaryView(value, static_cast<int32_t>(length),
|
505 |
+
static_cast<int32_t>(blocks_.size() - 1), current_offset_);
|
506 |
+
|
507 |
+
memcpy(current_out_buffer_, value, static_cast<size_t>(length));
|
508 |
+
current_out_buffer_ += length;
|
509 |
+
current_remaining_bytes_ -= length;
|
510 |
+
current_offset_ += static_cast<int32_t>(length);
|
511 |
+
return v;
|
512 |
+
}
|
513 |
+
|
514 |
+
static constexpr int64_t ValueSizeLimit() {
|
515 |
+
return std::numeric_limits<int32_t>::max();
|
516 |
+
}
|
517 |
+
|
518 |
+
/// \brief Ensure that the indicated number of bytes can be appended via
|
519 |
+
/// UnsafeAppend operations without the need to allocate more memory
|
520 |
+
Status Reserve(int64_t num_bytes) {
|
521 |
+
if (ARROW_PREDICT_FALSE(num_bytes > ValueSizeLimit())) {
|
522 |
+
return Status::CapacityError(
|
523 |
+
"BinaryView or StringView elements cannot reference "
|
524 |
+
"strings larger than 2GB");
|
525 |
+
}
|
526 |
+
if (num_bytes > current_remaining_bytes_) {
|
527 |
+
ARROW_RETURN_NOT_OK(FinishLastBlock());
|
528 |
+
current_remaining_bytes_ = num_bytes > blocksize_ ? num_bytes : blocksize_;
|
529 |
+
ARROW_ASSIGN_OR_RAISE(
|
530 |
+
std::shared_ptr<ResizableBuffer> new_block,
|
531 |
+
AllocateResizableBuffer(current_remaining_bytes_, alignment_, pool_));
|
532 |
+
current_offset_ = 0;
|
533 |
+
current_out_buffer_ = new_block->mutable_data();
|
534 |
+
blocks_.emplace_back(std::move(new_block));
|
535 |
+
}
|
536 |
+
return Status::OK();
|
537 |
+
}
|
538 |
+
|
539 |
+
void Reset() {
|
540 |
+
current_offset_ = 0;
|
541 |
+
current_out_buffer_ = NULLPTR;
|
542 |
+
current_remaining_bytes_ = 0;
|
543 |
+
blocks_.clear();
|
544 |
+
}
|
545 |
+
|
546 |
+
int64_t current_remaining_bytes() const { return current_remaining_bytes_; }
|
547 |
+
|
548 |
+
Result<std::vector<std::shared_ptr<ResizableBuffer>>> Finish() {
|
549 |
+
if (!blocks_.empty()) {
|
550 |
+
ARROW_RETURN_NOT_OK(FinishLastBlock());
|
551 |
+
}
|
552 |
+
current_offset_ = 0;
|
553 |
+
current_out_buffer_ = NULLPTR;
|
554 |
+
current_remaining_bytes_ = 0;
|
555 |
+
return std::move(blocks_);
|
556 |
+
}
|
557 |
+
|
558 |
+
private:
|
559 |
+
Status FinishLastBlock() {
|
560 |
+
if (current_remaining_bytes_ > 0) {
|
561 |
+
// Avoid leaking uninitialized bytes from the allocator
|
562 |
+
ARROW_RETURN_NOT_OK(
|
563 |
+
blocks_.back()->Resize(blocks_.back()->size() - current_remaining_bytes_,
|
564 |
+
/*shrink_to_fit=*/true));
|
565 |
+
blocks_.back()->ZeroPadding();
|
566 |
+
}
|
567 |
+
return Status::OK();
|
568 |
+
}
|
569 |
+
|
570 |
+
MemoryPool* pool_;
|
571 |
+
int64_t alignment_;
|
572 |
+
int64_t blocksize_ = kDefaultBlocksize;
|
573 |
+
std::vector<std::shared_ptr<ResizableBuffer>> blocks_;
|
574 |
+
|
575 |
+
int32_t current_offset_ = 0;
|
576 |
+
uint8_t* current_out_buffer_ = NULLPTR;
|
577 |
+
int64_t current_remaining_bytes_ = 0;
|
578 |
+
};
|
579 |
+
|
580 |
+
} // namespace internal
|
581 |
+
|
582 |
+
class ARROW_EXPORT BinaryViewBuilder : public ArrayBuilder {
|
583 |
+
public:
|
584 |
+
using TypeClass = BinaryViewType;
|
585 |
+
|
586 |
+
// this constructor provided for MakeBuilder compatibility
|
587 |
+
BinaryViewBuilder(const std::shared_ptr<DataType>&, MemoryPool* pool);
|
588 |
+
|
589 |
+
explicit BinaryViewBuilder(MemoryPool* pool = default_memory_pool(),
|
590 |
+
int64_t alignment = kDefaultBufferAlignment)
|
591 |
+
: ArrayBuilder(pool, alignment),
|
592 |
+
data_builder_(pool, alignment),
|
593 |
+
data_heap_builder_(pool, alignment) {}
|
594 |
+
|
595 |
+
/// Set the size for future preallocated data buffers.
|
596 |
+
///
|
597 |
+
/// The default size is 32KB, so after each 32KB of string data appended to the builder
|
598 |
+
/// a new data buffer will be allocated. Adjust this to a larger value to decrease the
|
599 |
+
/// frequency of allocation, or to a smaller value to lower the overhead of each
|
600 |
+
/// allocation.
|
601 |
+
void SetBlockSize(int64_t blocksize) { data_heap_builder_.SetBlockSize(blocksize); }
|
602 |
+
|
603 |
+
/// The number of bytes which can be appended to this builder without allocating another
|
604 |
+
/// data buffer.
|
605 |
+
int64_t current_block_bytes_remaining() const {
|
606 |
+
return data_heap_builder_.current_remaining_bytes();
|
607 |
+
}
|
608 |
+
|
609 |
+
Status Append(const uint8_t* value, int64_t length) {
|
610 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
611 |
+
UnsafeAppendToBitmap(true);
|
612 |
+
ARROW_ASSIGN_OR_RAISE(auto v,
|
613 |
+
data_heap_builder_.Append</*Safe=*/true>(value, length));
|
614 |
+
data_builder_.UnsafeAppend(v);
|
615 |
+
return Status::OK();
|
616 |
+
}
|
617 |
+
|
618 |
+
Status Append(const char* value, int64_t length) {
|
619 |
+
return Append(reinterpret_cast<const uint8_t*>(value), length);
|
620 |
+
}
|
621 |
+
|
622 |
+
Status Append(std::string_view value) {
|
623 |
+
return Append(value.data(), static_cast<int64_t>(value.size()));
|
624 |
+
}
|
625 |
+
|
626 |
+
/// \brief Append without checking capacity
|
627 |
+
///
|
628 |
+
/// Builder should have been presized using Reserve() and ReserveData(),
|
629 |
+
/// respectively, and the value must not be larger than 2GB
|
630 |
+
void UnsafeAppend(const uint8_t* value, int64_t length) {
|
631 |
+
UnsafeAppendToBitmap(true);
|
632 |
+
auto v = data_heap_builder_.Append</*Safe=*/false>(value, length);
|
633 |
+
data_builder_.UnsafeAppend(v);
|
634 |
+
}
|
635 |
+
|
636 |
+
void UnsafeAppend(const char* value, int64_t length) {
|
637 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value), length);
|
638 |
+
}
|
639 |
+
|
640 |
+
void UnsafeAppend(const std::string& value) {
|
641 |
+
UnsafeAppend(value.c_str(), static_cast<int64_t>(value.size()));
|
642 |
+
}
|
643 |
+
|
644 |
+
void UnsafeAppend(std::string_view value) {
|
645 |
+
UnsafeAppend(value.data(), static_cast<int64_t>(value.size()));
|
646 |
+
}
|
647 |
+
|
648 |
+
/// \brief Ensures there is enough allocated available capacity in the
|
649 |
+
/// out-of-line data heap to append the indicated number of bytes without
|
650 |
+
/// additional allocations
|
651 |
+
Status ReserveData(int64_t length);
|
652 |
+
|
653 |
+
Status AppendNulls(int64_t length) final {
|
654 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
655 |
+
data_builder_.UnsafeAppend(length, BinaryViewType::c_type{});
|
656 |
+
UnsafeSetNull(length);
|
657 |
+
return Status::OK();
|
658 |
+
}
|
659 |
+
|
660 |
+
/// \brief Append a single null element
|
661 |
+
Status AppendNull() final {
|
662 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
663 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
664 |
+
UnsafeAppendToBitmap(false);
|
665 |
+
return Status::OK();
|
666 |
+
}
|
667 |
+
|
668 |
+
/// \brief Append a empty element (length-0 inline string)
|
669 |
+
Status AppendEmptyValue() final {
|
670 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
671 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
672 |
+
UnsafeAppendToBitmap(true);
|
673 |
+
return Status::OK();
|
674 |
+
}
|
675 |
+
|
676 |
+
/// \brief Append several empty elements
|
677 |
+
Status AppendEmptyValues(int64_t length) final {
|
678 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
679 |
+
data_builder_.UnsafeAppend(length, BinaryViewType::c_type{});
|
680 |
+
UnsafeSetNotNull(length);
|
681 |
+
return Status::OK();
|
682 |
+
}
|
683 |
+
|
684 |
+
void UnsafeAppendNull() {
|
685 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
686 |
+
UnsafeAppendToBitmap(false);
|
687 |
+
}
|
688 |
+
|
689 |
+
void UnsafeAppendEmptyValue() {
|
690 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
691 |
+
UnsafeAppendToBitmap(true);
|
692 |
+
}
|
693 |
+
|
694 |
+
/// \brief Append a slice of a BinaryViewArray passed as an ArraySpan. Copies
|
695 |
+
/// the underlying out-of-line string memory to avoid memory lifetime issues
|
696 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
697 |
+
int64_t length) override;
|
698 |
+
|
699 |
+
void Reset() override;
|
700 |
+
|
701 |
+
Status Resize(int64_t capacity) override {
|
702 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
703 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
704 |
+
ARROW_RETURN_NOT_OK(data_builder_.Resize(capacity));
|
705 |
+
return ArrayBuilder::Resize(capacity);
|
706 |
+
}
|
707 |
+
|
708 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
709 |
+
|
710 |
+
std::shared_ptr<DataType> type() const override { return binary_view(); }
|
711 |
+
|
712 |
+
protected:
|
713 |
+
TypedBufferBuilder<BinaryViewType::c_type> data_builder_;
|
714 |
+
|
715 |
+
// Accumulates out-of-line data in fixed-size chunks which are then attached
|
716 |
+
// to the resulting ArrayData
|
717 |
+
internal::StringHeapBuilder data_heap_builder_;
|
718 |
+
};
|
719 |
+
|
720 |
+
class ARROW_EXPORT StringViewBuilder : public BinaryViewBuilder {
|
721 |
+
public:
|
722 |
+
using BinaryViewBuilder::BinaryViewBuilder;
|
723 |
+
std::shared_ptr<DataType> type() const override { return utf8_view(); }
|
724 |
+
};
|
725 |
+
|
726 |
+
// ----------------------------------------------------------------------
|
727 |
+
// FixedSizeBinaryBuilder
|
728 |
+
|
729 |
+
class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder {
|
730 |
+
public:
|
731 |
+
using TypeClass = FixedSizeBinaryType;
|
732 |
+
|
733 |
+
explicit FixedSizeBinaryBuilder(const std::shared_ptr<DataType>& type,
|
734 |
+
MemoryPool* pool = default_memory_pool(),
|
735 |
+
int64_t alignment = kDefaultBufferAlignment);
|
736 |
+
|
737 |
+
Status Append(const uint8_t* value) {
|
738 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
739 |
+
UnsafeAppend(value);
|
740 |
+
return Status::OK();
|
741 |
+
}
|
742 |
+
|
743 |
+
Status Append(const char* value) {
|
744 |
+
return Append(reinterpret_cast<const uint8_t*>(value));
|
745 |
+
}
|
746 |
+
|
747 |
+
Status Append(std::string_view view) {
|
748 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
749 |
+
UnsafeAppend(view);
|
750 |
+
return Status::OK();
|
751 |
+
}
|
752 |
+
|
753 |
+
Status Append(const std::string& s) {
|
754 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
755 |
+
UnsafeAppend(s);
|
756 |
+
return Status::OK();
|
757 |
+
}
|
758 |
+
|
759 |
+
Status Append(const Buffer& s) {
|
760 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
761 |
+
UnsafeAppend(s);
|
762 |
+
return Status::OK();
|
763 |
+
}
|
764 |
+
|
765 |
+
Status Append(const std::shared_ptr<Buffer>& s) { return Append(*s); }
|
766 |
+
|
767 |
+
template <size_t NBYTES>
|
768 |
+
Status Append(const std::array<uint8_t, NBYTES>& value) {
|
769 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
770 |
+
UnsafeAppend(
|
771 |
+
std::string_view(reinterpret_cast<const char*>(value.data()), value.size()));
|
772 |
+
return Status::OK();
|
773 |
+
}
|
774 |
+
|
775 |
+
Status AppendValues(const uint8_t* data, int64_t length,
|
776 |
+
const uint8_t* valid_bytes = NULLPTR);
|
777 |
+
|
778 |
+
Status AppendValues(const uint8_t* data, int64_t length, const uint8_t* validity,
|
779 |
+
int64_t bitmap_offset);
|
780 |
+
|
781 |
+
Status AppendNull() final;
|
782 |
+
Status AppendNulls(int64_t length) final;
|
783 |
+
|
784 |
+
Status AppendEmptyValue() final;
|
785 |
+
Status AppendEmptyValues(int64_t length) final;
|
786 |
+
|
787 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
788 |
+
int64_t length) override {
|
789 |
+
return AppendValues(
|
790 |
+
array.GetValues<uint8_t>(1, 0) + ((array.offset + offset) * byte_width_), length,
|
791 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
792 |
+
}
|
793 |
+
|
794 |
+
void UnsafeAppend(const uint8_t* value) {
|
795 |
+
UnsafeAppendToBitmap(true);
|
796 |
+
if (ARROW_PREDICT_TRUE(byte_width_ > 0)) {
|
797 |
+
byte_builder_.UnsafeAppend(value, byte_width_);
|
798 |
+
}
|
799 |
+
}
|
800 |
+
|
801 |
+
void UnsafeAppend(const char* value) {
|
802 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value));
|
803 |
+
}
|
804 |
+
|
805 |
+
void UnsafeAppend(std::string_view value) {
|
806 |
+
#ifndef NDEBUG
|
807 |
+
CheckValueSize(static_cast<size_t>(value.size()));
|
808 |
+
#endif
|
809 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value.data()));
|
810 |
+
}
|
811 |
+
|
812 |
+
void UnsafeAppend(const Buffer& s) { UnsafeAppend(std::string_view{s}); }
|
813 |
+
|
814 |
+
void UnsafeAppend(const std::shared_ptr<Buffer>& s) { UnsafeAppend(*s); }
|
815 |
+
|
816 |
+
void UnsafeAppendNull() {
|
817 |
+
UnsafeAppendToBitmap(false);
|
818 |
+
byte_builder_.UnsafeAppend(/*num_copies=*/byte_width_, 0);
|
819 |
+
}
|
820 |
+
|
821 |
+
Status ValidateOverflow(int64_t new_bytes) const {
|
822 |
+
auto new_size = byte_builder_.length() + new_bytes;
|
823 |
+
if (ARROW_PREDICT_FALSE(new_size > memory_limit())) {
|
824 |
+
return Status::CapacityError("array cannot contain more than ", memory_limit(),
|
825 |
+
" bytes, have ", new_size);
|
826 |
+
} else {
|
827 |
+
return Status::OK();
|
828 |
+
}
|
829 |
+
}
|
830 |
+
|
831 |
+
/// \brief Ensures there is enough allocated capacity to append the indicated
|
832 |
+
/// number of bytes to the value data buffer without additional allocations
|
833 |
+
Status ReserveData(int64_t elements) {
|
834 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(elements));
|
835 |
+
return byte_builder_.Reserve(elements);
|
836 |
+
}
|
837 |
+
|
838 |
+
void Reset() override;
|
839 |
+
Status Resize(int64_t capacity) override;
|
840 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
841 |
+
|
842 |
+
/// \cond FALSE
|
843 |
+
using ArrayBuilder::Finish;
|
844 |
+
/// \endcond
|
845 |
+
|
846 |
+
Status Finish(std::shared_ptr<FixedSizeBinaryArray>* out) { return FinishTyped(out); }
|
847 |
+
|
848 |
+
/// \return size of values buffer so far
|
849 |
+
int64_t value_data_length() const { return byte_builder_.length(); }
|
850 |
+
|
851 |
+
int32_t byte_width() const { return byte_width_; }
|
852 |
+
|
853 |
+
/// Temporary access to a value.
|
854 |
+
///
|
855 |
+
/// This pointer becomes invalid on the next modifying operation.
|
856 |
+
const uint8_t* GetValue(int64_t i) const;
|
857 |
+
|
858 |
+
/// Temporary access to a value.
|
859 |
+
///
|
860 |
+
/// This view becomes invalid on the next modifying operation.
|
861 |
+
std::string_view GetView(int64_t i) const;
|
862 |
+
|
863 |
+
static constexpr int64_t memory_limit() {
|
864 |
+
return std::numeric_limits<int64_t>::max() - 1;
|
865 |
+
}
|
866 |
+
|
867 |
+
std::shared_ptr<DataType> type() const override {
|
868 |
+
return fixed_size_binary(byte_width_);
|
869 |
+
}
|
870 |
+
|
871 |
+
protected:
|
872 |
+
int32_t byte_width_;
|
873 |
+
BufferBuilder byte_builder_;
|
874 |
+
|
875 |
+
/// Temporary access to a value.
|
876 |
+
///
|
877 |
+
/// This pointer becomes invalid on the next modifying operation.
|
878 |
+
uint8_t* GetMutableValue(int64_t i) {
|
879 |
+
uint8_t* data_ptr = byte_builder_.mutable_data();
|
880 |
+
return data_ptr + i * byte_width_;
|
881 |
+
}
|
882 |
+
|
883 |
+
void CheckValueSize(int64_t size);
|
884 |
+
};
|
885 |
+
|
886 |
+
/// @}
|
887 |
+
|
888 |
+
// ----------------------------------------------------------------------
|
889 |
+
// Chunked builders: build a sequence of BinaryArray or StringArray that are
|
890 |
+
// limited to a particular size (to the upper limit of 2GB)
|
891 |
+
|
892 |
+
namespace internal {
|
893 |
+
|
894 |
+
class ARROW_EXPORT ChunkedBinaryBuilder {
|
895 |
+
public:
|
896 |
+
explicit ChunkedBinaryBuilder(int32_t max_chunk_value_length,
|
897 |
+
MemoryPool* pool = default_memory_pool());
|
898 |
+
|
899 |
+
ChunkedBinaryBuilder(int32_t max_chunk_value_length, int32_t max_chunk_length,
|
900 |
+
MemoryPool* pool = default_memory_pool());
|
901 |
+
|
902 |
+
virtual ~ChunkedBinaryBuilder() = default;
|
903 |
+
|
904 |
+
Status Append(const uint8_t* value, int32_t length) {
|
905 |
+
if (ARROW_PREDICT_FALSE(length + builder_->value_data_length() >
|
906 |
+
max_chunk_value_length_)) {
|
907 |
+
if (builder_->value_data_length() == 0) {
|
908 |
+
// The current item is larger than max_chunk_size_;
|
909 |
+
// this chunk will be oversize and hold *only* this item
|
910 |
+
ARROW_RETURN_NOT_OK(builder_->Append(value, length));
|
911 |
+
return NextChunk();
|
912 |
+
}
|
913 |
+
// The current item would cause builder_->value_data_length() to exceed
|
914 |
+
// max_chunk_size_, so finish this chunk and append the current item to the next
|
915 |
+
// chunk
|
916 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
917 |
+
return Append(value, length);
|
918 |
+
}
|
919 |
+
|
920 |
+
if (ARROW_PREDICT_FALSE(builder_->length() == max_chunk_length_)) {
|
921 |
+
// The current item would cause builder_->length() to exceed max_chunk_length_, so
|
922 |
+
// finish this chunk and append the current item to the next chunk
|
923 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
924 |
+
}
|
925 |
+
|
926 |
+
return builder_->Append(value, length);
|
927 |
+
}
|
928 |
+
|
929 |
+
Status Append(std::string_view value) {
|
930 |
+
return Append(reinterpret_cast<const uint8_t*>(value.data()),
|
931 |
+
static_cast<int32_t>(value.size()));
|
932 |
+
}
|
933 |
+
|
934 |
+
Status AppendNull() {
|
935 |
+
if (ARROW_PREDICT_FALSE(builder_->length() == max_chunk_length_)) {
|
936 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
937 |
+
}
|
938 |
+
return builder_->AppendNull();
|
939 |
+
}
|
940 |
+
|
941 |
+
Status Reserve(int64_t values);
|
942 |
+
|
943 |
+
virtual Status Finish(ArrayVector* out);
|
944 |
+
|
945 |
+
protected:
|
946 |
+
Status NextChunk();
|
947 |
+
|
948 |
+
// maximum total character data size per chunk
|
949 |
+
int64_t max_chunk_value_length_;
|
950 |
+
|
951 |
+
// maximum elements allowed per chunk
|
952 |
+
int64_t max_chunk_length_ = kListMaximumElements;
|
953 |
+
|
954 |
+
// when Reserve() would cause builder_ to exceed its max_chunk_length_,
|
955 |
+
// add to extra_capacity_ instead and wait to reserve until the next chunk
|
956 |
+
int64_t extra_capacity_ = 0;
|
957 |
+
|
958 |
+
std::unique_ptr<BinaryBuilder> builder_;
|
959 |
+
std::vector<std::shared_ptr<Array>> chunks_;
|
960 |
+
};
|
961 |
+
|
962 |
+
class ARROW_EXPORT ChunkedStringBuilder : public ChunkedBinaryBuilder {
|
963 |
+
public:
|
964 |
+
using ChunkedBinaryBuilder::ChunkedBinaryBuilder;
|
965 |
+
|
966 |
+
Status Finish(ArrayVector* out) override;
|
967 |
+
};
|
968 |
+
|
969 |
+
} // namespace internal
|
970 |
+
|
971 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_decimal.h
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
|
22 |
+
#include "arrow/array/array_decimal.h"
|
23 |
+
#include "arrow/array/builder_base.h"
|
24 |
+
#include "arrow/array/builder_binary.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/status.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/util/visibility.h"
|
29 |
+
|
30 |
+
namespace arrow {
|
31 |
+
|
32 |
+
/// \addtogroup numeric-builders
|
33 |
+
///
|
34 |
+
/// @{
|
35 |
+
|
36 |
+
class ARROW_EXPORT Decimal128Builder : public FixedSizeBinaryBuilder {
|
37 |
+
public:
|
38 |
+
using TypeClass = Decimal128Type;
|
39 |
+
using ValueType = Decimal128;
|
40 |
+
|
41 |
+
explicit Decimal128Builder(const std::shared_ptr<DataType>& type,
|
42 |
+
MemoryPool* pool = default_memory_pool(),
|
43 |
+
int64_t alignment = kDefaultBufferAlignment);
|
44 |
+
|
45 |
+
using FixedSizeBinaryBuilder::Append;
|
46 |
+
using FixedSizeBinaryBuilder::AppendValues;
|
47 |
+
using FixedSizeBinaryBuilder::Reset;
|
48 |
+
|
49 |
+
Status Append(Decimal128 val);
|
50 |
+
void UnsafeAppend(Decimal128 val);
|
51 |
+
void UnsafeAppend(std::string_view val);
|
52 |
+
|
53 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
54 |
+
|
55 |
+
/// \cond FALSE
|
56 |
+
using ArrayBuilder::Finish;
|
57 |
+
/// \endcond
|
58 |
+
|
59 |
+
Status Finish(std::shared_ptr<Decimal128Array>* out) { return FinishTyped(out); }
|
60 |
+
|
61 |
+
std::shared_ptr<DataType> type() const override { return decimal_type_; }
|
62 |
+
|
63 |
+
protected:
|
64 |
+
std::shared_ptr<Decimal128Type> decimal_type_;
|
65 |
+
};
|
66 |
+
|
67 |
+
class ARROW_EXPORT Decimal256Builder : public FixedSizeBinaryBuilder {
|
68 |
+
public:
|
69 |
+
using TypeClass = Decimal256Type;
|
70 |
+
using ValueType = Decimal256;
|
71 |
+
|
72 |
+
explicit Decimal256Builder(const std::shared_ptr<DataType>& type,
|
73 |
+
MemoryPool* pool = default_memory_pool(),
|
74 |
+
int64_t alignment = kDefaultBufferAlignment);
|
75 |
+
|
76 |
+
using FixedSizeBinaryBuilder::Append;
|
77 |
+
using FixedSizeBinaryBuilder::AppendValues;
|
78 |
+
using FixedSizeBinaryBuilder::Reset;
|
79 |
+
|
80 |
+
Status Append(const Decimal256& val);
|
81 |
+
void UnsafeAppend(const Decimal256& val);
|
82 |
+
void UnsafeAppend(std::string_view val);
|
83 |
+
|
84 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
85 |
+
|
86 |
+
/// \cond FALSE
|
87 |
+
using ArrayBuilder::Finish;
|
88 |
+
/// \endcond
|
89 |
+
|
90 |
+
Status Finish(std::shared_ptr<Decimal256Array>* out) { return FinishTyped(out); }
|
91 |
+
|
92 |
+
std::shared_ptr<DataType> type() const override { return decimal_type_; }
|
93 |
+
|
94 |
+
protected:
|
95 |
+
std::shared_ptr<Decimal256Type> decimal_type_;
|
96 |
+
};
|
97 |
+
|
98 |
+
using DecimalBuilder = Decimal128Builder;
|
99 |
+
|
100 |
+
/// @}
|
101 |
+
|
102 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_dict.h
ADDED
@@ -0,0 +1,737 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <cstdint>
|
22 |
+
#include <memory>
|
23 |
+
#include <type_traits>
|
24 |
+
|
25 |
+
#include "arrow/array/array_base.h"
|
26 |
+
#include "arrow/array/array_binary.h"
|
27 |
+
#include "arrow/array/builder_adaptive.h" // IWYU pragma: export
|
28 |
+
#include "arrow/array/builder_base.h" // IWYU pragma: export
|
29 |
+
#include "arrow/array/builder_primitive.h" // IWYU pragma: export
|
30 |
+
#include "arrow/array/data.h"
|
31 |
+
#include "arrow/array/util.h"
|
32 |
+
#include "arrow/scalar.h"
|
33 |
+
#include "arrow/status.h"
|
34 |
+
#include "arrow/type.h"
|
35 |
+
#include "arrow/type_traits.h"
|
36 |
+
#include "arrow/util/bit_block_counter.h"
|
37 |
+
#include "arrow/util/checked_cast.h"
|
38 |
+
#include "arrow/util/decimal.h"
|
39 |
+
#include "arrow/util/macros.h"
|
40 |
+
#include "arrow/util/visibility.h"
|
41 |
+
|
42 |
+
namespace arrow {
|
43 |
+
|
44 |
+
// ----------------------------------------------------------------------
|
45 |
+
// Dictionary builder
|
46 |
+
|
47 |
+
namespace internal {
|
48 |
+
|
49 |
+
template <typename T, typename Enable = void>
|
50 |
+
struct DictionaryValue {
|
51 |
+
using type = typename T::c_type;
|
52 |
+
using PhysicalType = T;
|
53 |
+
};
|
54 |
+
|
55 |
+
template <typename T>
|
56 |
+
struct DictionaryValue<T, enable_if_base_binary<T>> {
|
57 |
+
using type = std::string_view;
|
58 |
+
using PhysicalType =
|
59 |
+
typename std::conditional<std::is_same<typename T::offset_type, int32_t>::value,
|
60 |
+
BinaryType, LargeBinaryType>::type;
|
61 |
+
};
|
62 |
+
|
63 |
+
template <typename T>
|
64 |
+
struct DictionaryValue<T, enable_if_binary_view_like<T>> {
|
65 |
+
using type = std::string_view;
|
66 |
+
using PhysicalType = BinaryViewType;
|
67 |
+
};
|
68 |
+
|
69 |
+
template <typename T>
|
70 |
+
struct DictionaryValue<T, enable_if_fixed_size_binary<T>> {
|
71 |
+
using type = std::string_view;
|
72 |
+
using PhysicalType = BinaryType;
|
73 |
+
};
|
74 |
+
|
75 |
+
class ARROW_EXPORT DictionaryMemoTable {
|
76 |
+
public:
|
77 |
+
DictionaryMemoTable(MemoryPool* pool, const std::shared_ptr<DataType>& type);
|
78 |
+
DictionaryMemoTable(MemoryPool* pool, const std::shared_ptr<Array>& dictionary);
|
79 |
+
~DictionaryMemoTable();
|
80 |
+
|
81 |
+
Status GetArrayData(int64_t start_offset, std::shared_ptr<ArrayData>* out);
|
82 |
+
|
83 |
+
/// \brief Insert new memo values
|
84 |
+
Status InsertValues(const Array& values);
|
85 |
+
|
86 |
+
int32_t size() const;
|
87 |
+
|
88 |
+
template <typename T>
|
89 |
+
Status GetOrInsert(typename DictionaryValue<T>::type value, int32_t* out) {
|
90 |
+
// We want to keep the DictionaryMemoTable implementation private, also we can't
|
91 |
+
// use extern template classes because of compiler issues (MinGW?). Instead,
|
92 |
+
// we expose explicit function overrides for each supported physical type.
|
93 |
+
const typename DictionaryValue<T>::PhysicalType* physical_type = NULLPTR;
|
94 |
+
return GetOrInsert(physical_type, value, out);
|
95 |
+
}
|
96 |
+
|
97 |
+
private:
|
98 |
+
Status GetOrInsert(const BooleanType*, bool value, int32_t* out);
|
99 |
+
Status GetOrInsert(const Int8Type*, int8_t value, int32_t* out);
|
100 |
+
Status GetOrInsert(const Int16Type*, int16_t value, int32_t* out);
|
101 |
+
Status GetOrInsert(const Int32Type*, int32_t value, int32_t* out);
|
102 |
+
Status GetOrInsert(const Int64Type*, int64_t value, int32_t* out);
|
103 |
+
Status GetOrInsert(const UInt8Type*, uint8_t value, int32_t* out);
|
104 |
+
Status GetOrInsert(const UInt16Type*, uint16_t value, int32_t* out);
|
105 |
+
Status GetOrInsert(const UInt32Type*, uint32_t value, int32_t* out);
|
106 |
+
Status GetOrInsert(const UInt64Type*, uint64_t value, int32_t* out);
|
107 |
+
Status GetOrInsert(const DurationType*, int64_t value, int32_t* out);
|
108 |
+
Status GetOrInsert(const TimestampType*, int64_t value, int32_t* out);
|
109 |
+
Status GetOrInsert(const Date32Type*, int32_t value, int32_t* out);
|
110 |
+
Status GetOrInsert(const Date64Type*, int64_t value, int32_t* out);
|
111 |
+
Status GetOrInsert(const Time32Type*, int32_t value, int32_t* out);
|
112 |
+
Status GetOrInsert(const Time64Type*, int64_t value, int32_t* out);
|
113 |
+
Status GetOrInsert(const MonthDayNanoIntervalType*,
|
114 |
+
MonthDayNanoIntervalType::MonthDayNanos value, int32_t* out);
|
115 |
+
Status GetOrInsert(const DayTimeIntervalType*,
|
116 |
+
DayTimeIntervalType::DayMilliseconds value, int32_t* out);
|
117 |
+
Status GetOrInsert(const MonthIntervalType*, int32_t value, int32_t* out);
|
118 |
+
Status GetOrInsert(const FloatType*, float value, int32_t* out);
|
119 |
+
Status GetOrInsert(const DoubleType*, double value, int32_t* out);
|
120 |
+
|
121 |
+
Status GetOrInsert(const BinaryType*, std::string_view value, int32_t* out);
|
122 |
+
Status GetOrInsert(const LargeBinaryType*, std::string_view value, int32_t* out);
|
123 |
+
Status GetOrInsert(const BinaryViewType*, std::string_view value, int32_t* out);
|
124 |
+
|
125 |
+
class DictionaryMemoTableImpl;
|
126 |
+
std::unique_ptr<DictionaryMemoTableImpl> impl_;
|
127 |
+
};
|
128 |
+
|
129 |
+
} // namespace internal
|
130 |
+
|
131 |
+
/// \addtogroup dictionary-builders
|
132 |
+
///
|
133 |
+
/// @{
|
134 |
+
|
135 |
+
namespace internal {
|
136 |
+
|
137 |
+
/// \brief Array builder for created encoded DictionaryArray from
|
138 |
+
/// dense array
|
139 |
+
///
|
140 |
+
/// Unlike other builders, dictionary builder does not completely
|
141 |
+
/// reset the state on Finish calls.
|
142 |
+
template <typename BuilderType, typename T>
|
143 |
+
class DictionaryBuilderBase : public ArrayBuilder {
|
144 |
+
public:
|
145 |
+
using TypeClass = DictionaryType;
|
146 |
+
using Value = typename DictionaryValue<T>::type;
|
147 |
+
|
148 |
+
// WARNING: the type given below is the value type, not the DictionaryType.
|
149 |
+
// The DictionaryType is instantiated on the Finish() call.
|
150 |
+
template <typename B = BuilderType, typename T1 = T>
|
151 |
+
DictionaryBuilderBase(uint8_t start_int_size,
|
152 |
+
enable_if_t<std::is_base_of<AdaptiveIntBuilderBase, B>::value &&
|
153 |
+
!is_fixed_size_binary_type<T1>::value,
|
154 |
+
const std::shared_ptr<DataType>&>
|
155 |
+
value_type,
|
156 |
+
MemoryPool* pool = default_memory_pool(),
|
157 |
+
int64_t alignment = kDefaultBufferAlignment)
|
158 |
+
: ArrayBuilder(pool, alignment),
|
159 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
160 |
+
delta_offset_(0),
|
161 |
+
byte_width_(-1),
|
162 |
+
indices_builder_(start_int_size, pool, alignment),
|
163 |
+
value_type_(value_type) {}
|
164 |
+
|
165 |
+
template <typename T1 = T>
|
166 |
+
explicit DictionaryBuilderBase(
|
167 |
+
enable_if_t<!is_fixed_size_binary_type<T1>::value, const std::shared_ptr<DataType>&>
|
168 |
+
value_type,
|
169 |
+
MemoryPool* pool = default_memory_pool(),
|
170 |
+
int64_t alignment = kDefaultBufferAlignment)
|
171 |
+
: ArrayBuilder(pool, alignment),
|
172 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
173 |
+
delta_offset_(0),
|
174 |
+
byte_width_(-1),
|
175 |
+
indices_builder_(pool, alignment),
|
176 |
+
value_type_(value_type) {}
|
177 |
+
|
178 |
+
template <typename T1 = T>
|
179 |
+
explicit DictionaryBuilderBase(
|
180 |
+
const std::shared_ptr<DataType>& index_type,
|
181 |
+
enable_if_t<!is_fixed_size_binary_type<T1>::value, const std::shared_ptr<DataType>&>
|
182 |
+
value_type,
|
183 |
+
MemoryPool* pool = default_memory_pool(),
|
184 |
+
int64_t alignment = kDefaultBufferAlignment)
|
185 |
+
: ArrayBuilder(pool, alignment),
|
186 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
187 |
+
delta_offset_(0),
|
188 |
+
byte_width_(-1),
|
189 |
+
indices_builder_(index_type, pool, alignment),
|
190 |
+
value_type_(value_type) {}
|
191 |
+
|
192 |
+
template <typename B = BuilderType, typename T1 = T>
|
193 |
+
DictionaryBuilderBase(uint8_t start_int_size,
|
194 |
+
enable_if_t<std::is_base_of<AdaptiveIntBuilderBase, B>::value &&
|
195 |
+
is_fixed_size_binary_type<T1>::value,
|
196 |
+
const std::shared_ptr<DataType>&>
|
197 |
+
value_type,
|
198 |
+
MemoryPool* pool = default_memory_pool(),
|
199 |
+
int64_t alignment = kDefaultBufferAlignment)
|
200 |
+
: ArrayBuilder(pool, alignment),
|
201 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
202 |
+
delta_offset_(0),
|
203 |
+
byte_width_(static_cast<const T1&>(*value_type).byte_width()),
|
204 |
+
indices_builder_(start_int_size, pool, alignment),
|
205 |
+
value_type_(value_type) {}
|
206 |
+
|
207 |
+
template <typename T1 = T>
|
208 |
+
explicit DictionaryBuilderBase(
|
209 |
+
enable_if_fixed_size_binary<T1, const std::shared_ptr<DataType>&> value_type,
|
210 |
+
MemoryPool* pool = default_memory_pool(),
|
211 |
+
int64_t alignment = kDefaultBufferAlignment)
|
212 |
+
: ArrayBuilder(pool, alignment),
|
213 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
214 |
+
delta_offset_(0),
|
215 |
+
byte_width_(static_cast<const T1&>(*value_type).byte_width()),
|
216 |
+
indices_builder_(pool, alignment),
|
217 |
+
value_type_(value_type) {}
|
218 |
+
|
219 |
+
template <typename T1 = T>
|
220 |
+
explicit DictionaryBuilderBase(
|
221 |
+
const std::shared_ptr<DataType>& index_type,
|
222 |
+
enable_if_fixed_size_binary<T1, const std::shared_ptr<DataType>&> value_type,
|
223 |
+
MemoryPool* pool = default_memory_pool(),
|
224 |
+
int64_t alignment = kDefaultBufferAlignment)
|
225 |
+
: ArrayBuilder(pool, alignment),
|
226 |
+
memo_table_(new internal::DictionaryMemoTable(pool, value_type)),
|
227 |
+
delta_offset_(0),
|
228 |
+
byte_width_(static_cast<const T1&>(*value_type).byte_width()),
|
229 |
+
indices_builder_(index_type, pool, alignment),
|
230 |
+
value_type_(value_type) {}
|
231 |
+
|
232 |
+
template <typename T1 = T>
|
233 |
+
explicit DictionaryBuilderBase(
|
234 |
+
enable_if_parameter_free<T1, MemoryPool*> pool = default_memory_pool())
|
235 |
+
: DictionaryBuilderBase<BuilderType, T1>(TypeTraits<T1>::type_singleton(), pool) {}
|
236 |
+
|
237 |
+
// This constructor doesn't check for errors. Use InsertMemoValues instead.
|
238 |
+
explicit DictionaryBuilderBase(const std::shared_ptr<Array>& dictionary,
|
239 |
+
MemoryPool* pool = default_memory_pool(),
|
240 |
+
int64_t alignment = kDefaultBufferAlignment)
|
241 |
+
: ArrayBuilder(pool, alignment),
|
242 |
+
memo_table_(new internal::DictionaryMemoTable(pool, dictionary)),
|
243 |
+
delta_offset_(0),
|
244 |
+
byte_width_(-1),
|
245 |
+
indices_builder_(pool, alignment),
|
246 |
+
value_type_(dictionary->type()) {}
|
247 |
+
|
248 |
+
~DictionaryBuilderBase() override = default;
|
249 |
+
|
250 |
+
/// \brief The current number of entries in the dictionary
|
251 |
+
int64_t dictionary_length() const { return memo_table_->size(); }
|
252 |
+
|
253 |
+
/// \brief The value byte width (for FixedSizeBinaryType)
|
254 |
+
template <typename T1 = T>
|
255 |
+
enable_if_fixed_size_binary<T1, int32_t> byte_width() const {
|
256 |
+
return byte_width_;
|
257 |
+
}
|
258 |
+
|
259 |
+
/// \brief Append a scalar value
|
260 |
+
Status Append(Value value) {
|
261 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
262 |
+
|
263 |
+
int32_t memo_index;
|
264 |
+
ARROW_RETURN_NOT_OK(memo_table_->GetOrInsert<T>(value, &memo_index));
|
265 |
+
ARROW_RETURN_NOT_OK(indices_builder_.Append(memo_index));
|
266 |
+
length_ += 1;
|
267 |
+
|
268 |
+
return Status::OK();
|
269 |
+
}
|
270 |
+
|
271 |
+
/// \brief Append a fixed-width string (only for FixedSizeBinaryType)
|
272 |
+
template <typename T1 = T>
|
273 |
+
enable_if_fixed_size_binary<T1, Status> Append(const uint8_t* value) {
|
274 |
+
return Append(std::string_view(reinterpret_cast<const char*>(value), byte_width_));
|
275 |
+
}
|
276 |
+
|
277 |
+
/// \brief Append a fixed-width string (only for FixedSizeBinaryType)
|
278 |
+
template <typename T1 = T>
|
279 |
+
enable_if_fixed_size_binary<T1, Status> Append(const char* value) {
|
280 |
+
return Append(std::string_view(value, byte_width_));
|
281 |
+
}
|
282 |
+
|
283 |
+
/// \brief Append a string (only for binary types)
|
284 |
+
template <typename T1 = T>
|
285 |
+
enable_if_binary_like<T1, Status> Append(const uint8_t* value, int32_t length) {
|
286 |
+
return Append(reinterpret_cast<const char*>(value), length);
|
287 |
+
}
|
288 |
+
|
289 |
+
/// \brief Append a string (only for binary types)
|
290 |
+
template <typename T1 = T>
|
291 |
+
enable_if_binary_like<T1, Status> Append(const char* value, int32_t length) {
|
292 |
+
return Append(std::string_view(value, length));
|
293 |
+
}
|
294 |
+
|
295 |
+
/// \brief Append a string (only for string types)
|
296 |
+
template <typename T1 = T>
|
297 |
+
enable_if_string_like<T1, Status> Append(const char* value, int32_t length) {
|
298 |
+
return Append(std::string_view(value, length));
|
299 |
+
}
|
300 |
+
|
301 |
+
/// \brief Append a decimal (only for Decimal128Type)
|
302 |
+
template <typename T1 = T>
|
303 |
+
enable_if_decimal128<T1, Status> Append(const Decimal128& value) {
|
304 |
+
uint8_t data[16];
|
305 |
+
value.ToBytes(data);
|
306 |
+
return Append(data, 16);
|
307 |
+
}
|
308 |
+
|
309 |
+
/// \brief Append a decimal (only for Decimal128Type)
|
310 |
+
template <typename T1 = T>
|
311 |
+
enable_if_decimal256<T1, Status> Append(const Decimal256& value) {
|
312 |
+
uint8_t data[32];
|
313 |
+
value.ToBytes(data);
|
314 |
+
return Append(data, 32);
|
315 |
+
}
|
316 |
+
|
317 |
+
/// \brief Append a scalar null value
|
318 |
+
Status AppendNull() final {
|
319 |
+
length_ += 1;
|
320 |
+
null_count_ += 1;
|
321 |
+
|
322 |
+
return indices_builder_.AppendNull();
|
323 |
+
}
|
324 |
+
|
325 |
+
Status AppendNulls(int64_t length) final {
|
326 |
+
length_ += length;
|
327 |
+
null_count_ += length;
|
328 |
+
|
329 |
+
return indices_builder_.AppendNulls(length);
|
330 |
+
}
|
331 |
+
|
332 |
+
Status AppendEmptyValue() final {
|
333 |
+
length_ += 1;
|
334 |
+
|
335 |
+
return indices_builder_.AppendEmptyValue();
|
336 |
+
}
|
337 |
+
|
338 |
+
Status AppendEmptyValues(int64_t length) final {
|
339 |
+
length_ += length;
|
340 |
+
|
341 |
+
return indices_builder_.AppendEmptyValues(length);
|
342 |
+
}
|
343 |
+
|
344 |
+
Status AppendScalar(const Scalar& scalar, int64_t n_repeats) override {
|
345 |
+
if (!scalar.is_valid) return AppendNulls(n_repeats);
|
346 |
+
|
347 |
+
const auto& dict_ty = internal::checked_cast<const DictionaryType&>(*scalar.type);
|
348 |
+
const DictionaryScalar& dict_scalar =
|
349 |
+
internal::checked_cast<const DictionaryScalar&>(scalar);
|
350 |
+
const auto& dict = internal::checked_cast<const typename TypeTraits<T>::ArrayType&>(
|
351 |
+
*dict_scalar.value.dictionary);
|
352 |
+
ARROW_RETURN_NOT_OK(Reserve(n_repeats));
|
353 |
+
switch (dict_ty.index_type()->id()) {
|
354 |
+
case Type::UINT8:
|
355 |
+
return AppendScalarImpl<UInt8Type>(dict, *dict_scalar.value.index, n_repeats);
|
356 |
+
case Type::INT8:
|
357 |
+
return AppendScalarImpl<Int8Type>(dict, *dict_scalar.value.index, n_repeats);
|
358 |
+
case Type::UINT16:
|
359 |
+
return AppendScalarImpl<UInt16Type>(dict, *dict_scalar.value.index, n_repeats);
|
360 |
+
case Type::INT16:
|
361 |
+
return AppendScalarImpl<Int16Type>(dict, *dict_scalar.value.index, n_repeats);
|
362 |
+
case Type::UINT32:
|
363 |
+
return AppendScalarImpl<UInt32Type>(dict, *dict_scalar.value.index, n_repeats);
|
364 |
+
case Type::INT32:
|
365 |
+
return AppendScalarImpl<Int32Type>(dict, *dict_scalar.value.index, n_repeats);
|
366 |
+
case Type::UINT64:
|
367 |
+
return AppendScalarImpl<UInt64Type>(dict, *dict_scalar.value.index, n_repeats);
|
368 |
+
case Type::INT64:
|
369 |
+
return AppendScalarImpl<Int64Type>(dict, *dict_scalar.value.index, n_repeats);
|
370 |
+
default:
|
371 |
+
return Status::TypeError("Invalid index type: ", dict_ty);
|
372 |
+
}
|
373 |
+
return Status::OK();
|
374 |
+
}
|
375 |
+
|
376 |
+
Status AppendScalars(const ScalarVector& scalars) override {
|
377 |
+
for (const auto& scalar : scalars) {
|
378 |
+
ARROW_RETURN_NOT_OK(AppendScalar(*scalar, /*n_repeats=*/1));
|
379 |
+
}
|
380 |
+
return Status::OK();
|
381 |
+
}
|
382 |
+
|
383 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset, int64_t length) final {
|
384 |
+
// Visit the indices and insert the unpacked values.
|
385 |
+
const auto& dict_ty = internal::checked_cast<const DictionaryType&>(*array.type);
|
386 |
+
// See if possible to avoid using ToArrayData here
|
387 |
+
const typename TypeTraits<T>::ArrayType dict(array.dictionary().ToArrayData());
|
388 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
389 |
+
switch (dict_ty.index_type()->id()) {
|
390 |
+
case Type::UINT8:
|
391 |
+
return AppendArraySliceImpl<uint8_t>(dict, array, offset, length);
|
392 |
+
case Type::INT8:
|
393 |
+
return AppendArraySliceImpl<int8_t>(dict, array, offset, length);
|
394 |
+
case Type::UINT16:
|
395 |
+
return AppendArraySliceImpl<uint16_t>(dict, array, offset, length);
|
396 |
+
case Type::INT16:
|
397 |
+
return AppendArraySliceImpl<int16_t>(dict, array, offset, length);
|
398 |
+
case Type::UINT32:
|
399 |
+
return AppendArraySliceImpl<uint32_t>(dict, array, offset, length);
|
400 |
+
case Type::INT32:
|
401 |
+
return AppendArraySliceImpl<int32_t>(dict, array, offset, length);
|
402 |
+
case Type::UINT64:
|
403 |
+
return AppendArraySliceImpl<uint64_t>(dict, array, offset, length);
|
404 |
+
case Type::INT64:
|
405 |
+
return AppendArraySliceImpl<int64_t>(dict, array, offset, length);
|
406 |
+
default:
|
407 |
+
return Status::TypeError("Invalid index type: ", dict_ty);
|
408 |
+
}
|
409 |
+
return Status::OK();
|
410 |
+
}
|
411 |
+
|
412 |
+
/// \brief Insert values into the dictionary's memo, but do not append any
|
413 |
+
/// indices. Can be used to initialize a new builder with known dictionary
|
414 |
+
/// values
|
415 |
+
/// \param[in] values dictionary values to add to memo. Type must match
|
416 |
+
/// builder type
|
417 |
+
Status InsertMemoValues(const Array& values) {
|
418 |
+
return memo_table_->InsertValues(values);
|
419 |
+
}
|
420 |
+
|
421 |
+
/// \brief Append a whole dense array to the builder
|
422 |
+
template <typename T1 = T>
|
423 |
+
enable_if_t<!is_fixed_size_binary_type<T1>::value, Status> AppendArray(
|
424 |
+
const Array& array) {
|
425 |
+
using ArrayType = typename TypeTraits<T>::ArrayType;
|
426 |
+
|
427 |
+
#ifndef NDEBUG
|
428 |
+
ARROW_RETURN_NOT_OK(ArrayBuilder::CheckArrayType(
|
429 |
+
value_type_, array, "Wrong value type of array to be appended"));
|
430 |
+
#endif
|
431 |
+
|
432 |
+
const auto& concrete_array = static_cast<const ArrayType&>(array);
|
433 |
+
for (int64_t i = 0; i < array.length(); i++) {
|
434 |
+
if (array.IsNull(i)) {
|
435 |
+
ARROW_RETURN_NOT_OK(AppendNull());
|
436 |
+
} else {
|
437 |
+
ARROW_RETURN_NOT_OK(Append(concrete_array.GetView(i)));
|
438 |
+
}
|
439 |
+
}
|
440 |
+
return Status::OK();
|
441 |
+
}
|
442 |
+
|
443 |
+
template <typename T1 = T>
|
444 |
+
enable_if_fixed_size_binary<T1, Status> AppendArray(const Array& array) {
|
445 |
+
#ifndef NDEBUG
|
446 |
+
ARROW_RETURN_NOT_OK(ArrayBuilder::CheckArrayType(
|
447 |
+
value_type_, array, "Wrong value type of array to be appended"));
|
448 |
+
#endif
|
449 |
+
|
450 |
+
const auto& concrete_array = static_cast<const FixedSizeBinaryArray&>(array);
|
451 |
+
for (int64_t i = 0; i < array.length(); i++) {
|
452 |
+
if (array.IsNull(i)) {
|
453 |
+
ARROW_RETURN_NOT_OK(AppendNull());
|
454 |
+
} else {
|
455 |
+
ARROW_RETURN_NOT_OK(Append(concrete_array.GetValue(i)));
|
456 |
+
}
|
457 |
+
}
|
458 |
+
return Status::OK();
|
459 |
+
}
|
460 |
+
|
461 |
+
void Reset() override {
|
462 |
+
// Perform a partial reset. Call ResetFull to also reset the accumulated
|
463 |
+
// dictionary values
|
464 |
+
ArrayBuilder::Reset();
|
465 |
+
indices_builder_.Reset();
|
466 |
+
}
|
467 |
+
|
468 |
+
/// \brief Reset and also clear accumulated dictionary values in memo table
|
469 |
+
void ResetFull() {
|
470 |
+
Reset();
|
471 |
+
memo_table_.reset(new internal::DictionaryMemoTable(pool_, value_type_));
|
472 |
+
}
|
473 |
+
|
474 |
+
Status Resize(int64_t capacity) override {
|
475 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
476 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
477 |
+
ARROW_RETURN_NOT_OK(indices_builder_.Resize(capacity));
|
478 |
+
capacity_ = indices_builder_.capacity();
|
479 |
+
return Status::OK();
|
480 |
+
}
|
481 |
+
|
482 |
+
/// \brief Return dictionary indices and a delta dictionary since the last
|
483 |
+
/// time that Finish or FinishDelta were called, and reset state of builder
|
484 |
+
/// (except the memo table)
|
485 |
+
Status FinishDelta(std::shared_ptr<Array>* out_indices,
|
486 |
+
std::shared_ptr<Array>* out_delta) {
|
487 |
+
std::shared_ptr<ArrayData> indices_data;
|
488 |
+
std::shared_ptr<ArrayData> delta_data;
|
489 |
+
ARROW_RETURN_NOT_OK(FinishWithDictOffset(delta_offset_, &indices_data, &delta_data));
|
490 |
+
*out_indices = MakeArray(indices_data);
|
491 |
+
*out_delta = MakeArray(delta_data);
|
492 |
+
return Status::OK();
|
493 |
+
}
|
494 |
+
|
495 |
+
/// \cond FALSE
|
496 |
+
using ArrayBuilder::Finish;
|
497 |
+
/// \endcond
|
498 |
+
|
499 |
+
Status Finish(std::shared_ptr<DictionaryArray>* out) { return FinishTyped(out); }
|
500 |
+
|
501 |
+
std::shared_ptr<DataType> type() const override {
|
502 |
+
return ::arrow::dictionary(indices_builder_.type(), value_type_);
|
503 |
+
}
|
504 |
+
|
505 |
+
protected:
|
506 |
+
template <typename c_type>
|
507 |
+
Status AppendArraySliceImpl(const typename TypeTraits<T>::ArrayType& dict,
|
508 |
+
const ArraySpan& array, int64_t offset, int64_t length) {
|
509 |
+
const c_type* values = array.GetValues<c_type>(1) + offset;
|
510 |
+
return VisitBitBlocks(
|
511 |
+
array.buffers[0].data, array.offset + offset, length,
|
512 |
+
[&](const int64_t position) {
|
513 |
+
const int64_t index = static_cast<int64_t>(values[position]);
|
514 |
+
if (dict.IsValid(index)) {
|
515 |
+
return Append(dict.GetView(index));
|
516 |
+
}
|
517 |
+
return AppendNull();
|
518 |
+
},
|
519 |
+
[&]() { return AppendNull(); });
|
520 |
+
}
|
521 |
+
|
522 |
+
template <typename IndexType>
|
523 |
+
Status AppendScalarImpl(const typename TypeTraits<T>::ArrayType& dict,
|
524 |
+
const Scalar& index_scalar, int64_t n_repeats) {
|
525 |
+
using ScalarType = typename TypeTraits<IndexType>::ScalarType;
|
526 |
+
const auto index = internal::checked_cast<const ScalarType&>(index_scalar).value;
|
527 |
+
if (index_scalar.is_valid && dict.IsValid(index)) {
|
528 |
+
const auto& value = dict.GetView(index);
|
529 |
+
for (int64_t i = 0; i < n_repeats; i++) {
|
530 |
+
ARROW_RETURN_NOT_OK(Append(value));
|
531 |
+
}
|
532 |
+
return Status::OK();
|
533 |
+
}
|
534 |
+
return AppendNulls(n_repeats);
|
535 |
+
}
|
536 |
+
|
537 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
538 |
+
std::shared_ptr<ArrayData> dictionary;
|
539 |
+
ARROW_RETURN_NOT_OK(FinishWithDictOffset(/*offset=*/0, out, &dictionary));
|
540 |
+
|
541 |
+
// Set type of array data to the right dictionary type
|
542 |
+
(*out)->type = type();
|
543 |
+
(*out)->dictionary = dictionary;
|
544 |
+
return Status::OK();
|
545 |
+
}
|
546 |
+
|
547 |
+
Status FinishWithDictOffset(int64_t dict_offset,
|
548 |
+
std::shared_ptr<ArrayData>* out_indices,
|
549 |
+
std::shared_ptr<ArrayData>* out_dictionary) {
|
550 |
+
// Finalize indices array
|
551 |
+
ARROW_RETURN_NOT_OK(indices_builder_.FinishInternal(out_indices));
|
552 |
+
|
553 |
+
// Generate dictionary array from hash table contents
|
554 |
+
ARROW_RETURN_NOT_OK(memo_table_->GetArrayData(dict_offset, out_dictionary));
|
555 |
+
delta_offset_ = memo_table_->size();
|
556 |
+
|
557 |
+
// Update internals for further uses of this DictionaryBuilder
|
558 |
+
ArrayBuilder::Reset();
|
559 |
+
return Status::OK();
|
560 |
+
}
|
561 |
+
|
562 |
+
std::unique_ptr<DictionaryMemoTable> memo_table_;
|
563 |
+
|
564 |
+
// The size of the dictionary memo at last invocation of Finish, to use in
|
565 |
+
// FinishDelta for computing dictionary deltas
|
566 |
+
int32_t delta_offset_;
|
567 |
+
|
568 |
+
// Only used for FixedSizeBinaryType
|
569 |
+
int32_t byte_width_;
|
570 |
+
|
571 |
+
BuilderType indices_builder_;
|
572 |
+
std::shared_ptr<DataType> value_type_;
|
573 |
+
};
|
574 |
+
|
575 |
+
template <typename BuilderType>
|
576 |
+
class DictionaryBuilderBase<BuilderType, NullType> : public ArrayBuilder {
|
577 |
+
public:
|
578 |
+
template <typename B = BuilderType>
|
579 |
+
DictionaryBuilderBase(
|
580 |
+
enable_if_t<std::is_base_of<AdaptiveIntBuilderBase, B>::value, uint8_t>
|
581 |
+
start_int_size,
|
582 |
+
const std::shared_ptr<DataType>& value_type,
|
583 |
+
MemoryPool* pool = default_memory_pool())
|
584 |
+
: ArrayBuilder(pool), indices_builder_(start_int_size, pool) {}
|
585 |
+
|
586 |
+
explicit DictionaryBuilderBase(const std::shared_ptr<DataType>& value_type,
|
587 |
+
MemoryPool* pool = default_memory_pool())
|
588 |
+
: ArrayBuilder(pool), indices_builder_(pool) {}
|
589 |
+
|
590 |
+
explicit DictionaryBuilderBase(const std::shared_ptr<DataType>& index_type,
|
591 |
+
const std::shared_ptr<DataType>& value_type,
|
592 |
+
MemoryPool* pool = default_memory_pool())
|
593 |
+
: ArrayBuilder(pool), indices_builder_(index_type, pool) {}
|
594 |
+
|
595 |
+
template <typename B = BuilderType>
|
596 |
+
explicit DictionaryBuilderBase(
|
597 |
+
enable_if_t<std::is_base_of<AdaptiveIntBuilderBase, B>::value, uint8_t>
|
598 |
+
start_int_size,
|
599 |
+
MemoryPool* pool = default_memory_pool())
|
600 |
+
: ArrayBuilder(pool), indices_builder_(start_int_size, pool) {}
|
601 |
+
|
602 |
+
explicit DictionaryBuilderBase(MemoryPool* pool = default_memory_pool())
|
603 |
+
: ArrayBuilder(pool), indices_builder_(pool) {}
|
604 |
+
|
605 |
+
explicit DictionaryBuilderBase(const std::shared_ptr<Array>& dictionary,
|
606 |
+
MemoryPool* pool = default_memory_pool())
|
607 |
+
: ArrayBuilder(pool), indices_builder_(pool) {}
|
608 |
+
|
609 |
+
/// \brief Append a scalar null value
|
610 |
+
Status AppendNull() final {
|
611 |
+
length_ += 1;
|
612 |
+
null_count_ += 1;
|
613 |
+
|
614 |
+
return indices_builder_.AppendNull();
|
615 |
+
}
|
616 |
+
|
617 |
+
Status AppendNulls(int64_t length) final {
|
618 |
+
length_ += length;
|
619 |
+
null_count_ += length;
|
620 |
+
|
621 |
+
return indices_builder_.AppendNulls(length);
|
622 |
+
}
|
623 |
+
|
624 |
+
Status AppendEmptyValue() final {
|
625 |
+
length_ += 1;
|
626 |
+
|
627 |
+
return indices_builder_.AppendEmptyValue();
|
628 |
+
}
|
629 |
+
|
630 |
+
Status AppendEmptyValues(int64_t length) final {
|
631 |
+
length_ += length;
|
632 |
+
|
633 |
+
return indices_builder_.AppendEmptyValues(length);
|
634 |
+
}
|
635 |
+
|
636 |
+
/// \brief Append a whole dense array to the builder
|
637 |
+
Status AppendArray(const Array& array) {
|
638 |
+
#ifndef NDEBUG
|
639 |
+
ARROW_RETURN_NOT_OK(ArrayBuilder::CheckArrayType(
|
640 |
+
Type::NA, array, "Wrong value type of array to be appended"));
|
641 |
+
#endif
|
642 |
+
for (int64_t i = 0; i < array.length(); i++) {
|
643 |
+
ARROW_RETURN_NOT_OK(AppendNull());
|
644 |
+
}
|
645 |
+
return Status::OK();
|
646 |
+
}
|
647 |
+
|
648 |
+
Status Resize(int64_t capacity) override {
|
649 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
650 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
651 |
+
|
652 |
+
ARROW_RETURN_NOT_OK(indices_builder_.Resize(capacity));
|
653 |
+
capacity_ = indices_builder_.capacity();
|
654 |
+
return Status::OK();
|
655 |
+
}
|
656 |
+
|
657 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
658 |
+
ARROW_RETURN_NOT_OK(indices_builder_.FinishInternal(out));
|
659 |
+
(*out)->type = dictionary((*out)->type, null());
|
660 |
+
(*out)->dictionary = NullArray(0).data();
|
661 |
+
return Status::OK();
|
662 |
+
}
|
663 |
+
|
664 |
+
/// \cond FALSE
|
665 |
+
using ArrayBuilder::Finish;
|
666 |
+
/// \endcond
|
667 |
+
|
668 |
+
Status Finish(std::shared_ptr<DictionaryArray>* out) { return FinishTyped(out); }
|
669 |
+
|
670 |
+
std::shared_ptr<DataType> type() const override {
|
671 |
+
return ::arrow::dictionary(indices_builder_.type(), null());
|
672 |
+
}
|
673 |
+
|
674 |
+
protected:
|
675 |
+
BuilderType indices_builder_;
|
676 |
+
};
|
677 |
+
|
678 |
+
} // namespace internal
|
679 |
+
|
680 |
+
/// \brief A DictionaryArray builder that uses AdaptiveIntBuilder to return the
|
681 |
+
/// smallest index size that can accommodate the dictionary indices
|
682 |
+
template <typename T>
|
683 |
+
class DictionaryBuilder : public internal::DictionaryBuilderBase<AdaptiveIntBuilder, T> {
|
684 |
+
public:
|
685 |
+
using BASE = internal::DictionaryBuilderBase<AdaptiveIntBuilder, T>;
|
686 |
+
using BASE::BASE;
|
687 |
+
|
688 |
+
/// \brief Append dictionary indices directly without modifying memo
|
689 |
+
///
|
690 |
+
/// NOTE: Experimental API
|
691 |
+
Status AppendIndices(const int64_t* values, int64_t length,
|
692 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
693 |
+
int64_t null_count_before = this->indices_builder_.null_count();
|
694 |
+
ARROW_RETURN_NOT_OK(this->indices_builder_.AppendValues(values, length, valid_bytes));
|
695 |
+
this->capacity_ = this->indices_builder_.capacity();
|
696 |
+
this->length_ += length;
|
697 |
+
this->null_count_ += this->indices_builder_.null_count() - null_count_before;
|
698 |
+
return Status::OK();
|
699 |
+
}
|
700 |
+
};
|
701 |
+
|
702 |
+
/// \brief A DictionaryArray builder that always returns int32 dictionary
|
703 |
+
/// indices so that data cast to dictionary form will have a consistent index
|
704 |
+
/// type, e.g. for creating a ChunkedArray
|
705 |
+
template <typename T>
|
706 |
+
class Dictionary32Builder : public internal::DictionaryBuilderBase<Int32Builder, T> {
|
707 |
+
public:
|
708 |
+
using BASE = internal::DictionaryBuilderBase<Int32Builder, T>;
|
709 |
+
using BASE::BASE;
|
710 |
+
|
711 |
+
/// \brief Append dictionary indices directly without modifying memo
|
712 |
+
///
|
713 |
+
/// NOTE: Experimental API
|
714 |
+
Status AppendIndices(const int32_t* values, int64_t length,
|
715 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
716 |
+
int64_t null_count_before = this->indices_builder_.null_count();
|
717 |
+
ARROW_RETURN_NOT_OK(this->indices_builder_.AppendValues(values, length, valid_bytes));
|
718 |
+
this->capacity_ = this->indices_builder_.capacity();
|
719 |
+
this->length_ += length;
|
720 |
+
this->null_count_ += this->indices_builder_.null_count() - null_count_before;
|
721 |
+
return Status::OK();
|
722 |
+
}
|
723 |
+
};
|
724 |
+
|
725 |
+
// ----------------------------------------------------------------------
|
726 |
+
// Binary / Unicode builders
|
727 |
+
// (compatibility aliases; those used to be derived classes with additional
|
728 |
+
// Append() overloads, but they have been folded into DictionaryBuilderBase)
|
729 |
+
|
730 |
+
using BinaryDictionaryBuilder = DictionaryBuilder<BinaryType>;
|
731 |
+
using StringDictionaryBuilder = DictionaryBuilder<StringType>;
|
732 |
+
using BinaryDictionary32Builder = Dictionary32Builder<BinaryType>;
|
733 |
+
using StringDictionary32Builder = Dictionary32Builder<StringType>;
|
734 |
+
|
735 |
+
/// @}
|
736 |
+
|
737 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_primitive.h
ADDED
@@ -0,0 +1,555 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <memory>
|
22 |
+
#include <vector>
|
23 |
+
|
24 |
+
#include "arrow/array/builder_base.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/result.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/type_traits.h"
|
29 |
+
|
30 |
+
namespace arrow {
|
31 |
+
|
32 |
+
class ARROW_EXPORT NullBuilder : public ArrayBuilder {
|
33 |
+
public:
|
34 |
+
explicit NullBuilder(MemoryPool* pool = default_memory_pool(),
|
35 |
+
int64_t alignment = kDefaultBufferAlignment)
|
36 |
+
: ArrayBuilder(pool) {}
|
37 |
+
explicit NullBuilder(const std::shared_ptr<DataType>& type,
|
38 |
+
MemoryPool* pool = default_memory_pool(),
|
39 |
+
int64_t alignment = kDefaultBufferAlignment)
|
40 |
+
: NullBuilder(pool, alignment) {}
|
41 |
+
|
42 |
+
/// \brief Append the specified number of null elements
|
43 |
+
Status AppendNulls(int64_t length) final {
|
44 |
+
if (length < 0) return Status::Invalid("length must be positive");
|
45 |
+
null_count_ += length;
|
46 |
+
length_ += length;
|
47 |
+
return Status::OK();
|
48 |
+
}
|
49 |
+
|
50 |
+
/// \brief Append a single null element
|
51 |
+
Status AppendNull() final { return AppendNulls(1); }
|
52 |
+
|
53 |
+
Status AppendEmptyValues(int64_t length) final { return AppendNulls(length); }
|
54 |
+
|
55 |
+
Status AppendEmptyValue() final { return AppendEmptyValues(1); }
|
56 |
+
|
57 |
+
Status Append(std::nullptr_t) { return AppendNull(); }
|
58 |
+
|
59 |
+
Status AppendArraySlice(const ArraySpan&, int64_t, int64_t length) override {
|
60 |
+
return AppendNulls(length);
|
61 |
+
}
|
62 |
+
|
63 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
64 |
+
|
65 |
+
/// \cond FALSE
|
66 |
+
using ArrayBuilder::Finish;
|
67 |
+
/// \endcond
|
68 |
+
|
69 |
+
std::shared_ptr<DataType> type() const override { return null(); }
|
70 |
+
|
71 |
+
Status Finish(std::shared_ptr<NullArray>* out) { return FinishTyped(out); }
|
72 |
+
};
|
73 |
+
|
74 |
+
/// \addtogroup numeric-builders
|
75 |
+
///
|
76 |
+
/// @{
|
77 |
+
|
78 |
+
/// Base class for all Builders that emit an Array of a scalar numerical type.
|
79 |
+
template <typename T>
|
80 |
+
class NumericBuilder
|
81 |
+
: public ArrayBuilder,
|
82 |
+
public internal::ArrayBuilderExtraOps<NumericBuilder<T>, typename T::c_type> {
|
83 |
+
public:
|
84 |
+
using TypeClass = T;
|
85 |
+
using value_type = typename T::c_type;
|
86 |
+
using ArrayType = typename TypeTraits<T>::ArrayType;
|
87 |
+
|
88 |
+
template <typename T1 = T>
|
89 |
+
explicit NumericBuilder(
|
90 |
+
enable_if_parameter_free<T1, MemoryPool*> pool = default_memory_pool(),
|
91 |
+
int64_t alignment = kDefaultBufferAlignment)
|
92 |
+
: ArrayBuilder(pool, alignment),
|
93 |
+
type_(TypeTraits<T>::type_singleton()),
|
94 |
+
data_builder_(pool, alignment) {}
|
95 |
+
|
96 |
+
NumericBuilder(const std::shared_ptr<DataType>& type, MemoryPool* pool,
|
97 |
+
int64_t alignment = kDefaultBufferAlignment)
|
98 |
+
: ArrayBuilder(pool, alignment), type_(type), data_builder_(pool, alignment) {}
|
99 |
+
|
100 |
+
/// Append a single scalar and increase the size if necessary.
|
101 |
+
Status Append(const value_type val) {
|
102 |
+
ARROW_RETURN_NOT_OK(ArrayBuilder::Reserve(1));
|
103 |
+
UnsafeAppend(val);
|
104 |
+
return Status::OK();
|
105 |
+
}
|
106 |
+
|
107 |
+
/// Write nulls as uint8_t* (0 value indicates null) into pre-allocated memory
|
108 |
+
/// The memory at the corresponding data slot is set to 0 to prevent
|
109 |
+
/// uninitialized memory access
|
110 |
+
Status AppendNulls(int64_t length) final {
|
111 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
112 |
+
data_builder_.UnsafeAppend(length, value_type{}); // zero
|
113 |
+
UnsafeSetNull(length);
|
114 |
+
return Status::OK();
|
115 |
+
}
|
116 |
+
|
117 |
+
/// \brief Append a single null element
|
118 |
+
Status AppendNull() final {
|
119 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
120 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
121 |
+
UnsafeAppendToBitmap(false);
|
122 |
+
return Status::OK();
|
123 |
+
}
|
124 |
+
|
125 |
+
/// \brief Append a empty element
|
126 |
+
Status AppendEmptyValue() final {
|
127 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
128 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
129 |
+
UnsafeAppendToBitmap(true);
|
130 |
+
return Status::OK();
|
131 |
+
}
|
132 |
+
|
133 |
+
/// \brief Append several empty elements
|
134 |
+
Status AppendEmptyValues(int64_t length) final {
|
135 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
136 |
+
data_builder_.UnsafeAppend(length, value_type{}); // zero
|
137 |
+
UnsafeSetNotNull(length);
|
138 |
+
return Status::OK();
|
139 |
+
}
|
140 |
+
|
141 |
+
value_type GetValue(int64_t index) const { return data_builder_.data()[index]; }
|
142 |
+
|
143 |
+
void Reset() override {
|
144 |
+
data_builder_.Reset();
|
145 |
+
ArrayBuilder::Reset();
|
146 |
+
}
|
147 |
+
|
148 |
+
Status Resize(int64_t capacity) override {
|
149 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
150 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
151 |
+
ARROW_RETURN_NOT_OK(data_builder_.Resize(capacity));
|
152 |
+
return ArrayBuilder::Resize(capacity);
|
153 |
+
}
|
154 |
+
|
155 |
+
value_type operator[](int64_t index) const { return GetValue(index); }
|
156 |
+
|
157 |
+
value_type& operator[](int64_t index) {
|
158 |
+
return reinterpret_cast<value_type*>(data_builder_.mutable_data())[index];
|
159 |
+
}
|
160 |
+
|
161 |
+
/// \brief Append a sequence of elements in one shot
|
162 |
+
/// \param[in] values a contiguous C array of values
|
163 |
+
/// \param[in] length the number of values to append
|
164 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
165 |
+
/// indicates a valid (non-null) value
|
166 |
+
/// \return Status
|
167 |
+
Status AppendValues(const value_type* values, int64_t length,
|
168 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
169 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
170 |
+
data_builder_.UnsafeAppend(values, length);
|
171 |
+
// length_ is update by these
|
172 |
+
ArrayBuilder::UnsafeAppendToBitmap(valid_bytes, length);
|
173 |
+
return Status::OK();
|
174 |
+
}
|
175 |
+
|
176 |
+
/// \brief Append a sequence of elements in one shot
|
177 |
+
/// \param[in] values a contiguous C array of values
|
178 |
+
/// \param[in] length the number of values to append
|
179 |
+
/// \param[in] bitmap a validity bitmap to copy (may be null)
|
180 |
+
/// \param[in] bitmap_offset an offset into the validity bitmap
|
181 |
+
/// \return Status
|
182 |
+
Status AppendValues(const value_type* values, int64_t length, const uint8_t* bitmap,
|
183 |
+
int64_t bitmap_offset) {
|
184 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
185 |
+
data_builder_.UnsafeAppend(values, length);
|
186 |
+
// length_ is update by these
|
187 |
+
ArrayBuilder::UnsafeAppendToBitmap(bitmap, bitmap_offset, length);
|
188 |
+
return Status::OK();
|
189 |
+
}
|
190 |
+
|
191 |
+
/// \brief Append a sequence of elements in one shot
|
192 |
+
/// \param[in] values a contiguous C array of values
|
193 |
+
/// \param[in] length the number of values to append
|
194 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
195 |
+
/// (0). Equal in length to values
|
196 |
+
/// \return Status
|
197 |
+
Status AppendValues(const value_type* values, int64_t length,
|
198 |
+
const std::vector<bool>& is_valid) {
|
199 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
200 |
+
data_builder_.UnsafeAppend(values, length);
|
201 |
+
// length_ is update by these
|
202 |
+
ArrayBuilder::UnsafeAppendToBitmap(is_valid);
|
203 |
+
return Status::OK();
|
204 |
+
}
|
205 |
+
|
206 |
+
/// \brief Append a sequence of elements in one shot
|
207 |
+
/// \param[in] values a std::vector of values
|
208 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
209 |
+
/// (0). Equal in length to values
|
210 |
+
/// \return Status
|
211 |
+
Status AppendValues(const std::vector<value_type>& values,
|
212 |
+
const std::vector<bool>& is_valid) {
|
213 |
+
return AppendValues(values.data(), static_cast<int64_t>(values.size()), is_valid);
|
214 |
+
}
|
215 |
+
|
216 |
+
/// \brief Append a sequence of elements in one shot
|
217 |
+
/// \param[in] values a std::vector of values
|
218 |
+
/// \return Status
|
219 |
+
Status AppendValues(const std::vector<value_type>& values) {
|
220 |
+
return AppendValues(values.data(), static_cast<int64_t>(values.size()));
|
221 |
+
}
|
222 |
+
|
223 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
224 |
+
ARROW_ASSIGN_OR_RAISE(auto null_bitmap,
|
225 |
+
null_bitmap_builder_.FinishWithLength(length_));
|
226 |
+
ARROW_ASSIGN_OR_RAISE(auto data, data_builder_.FinishWithLength(length_));
|
227 |
+
*out = ArrayData::Make(type(), length_, {null_bitmap, data}, null_count_);
|
228 |
+
capacity_ = length_ = null_count_ = 0;
|
229 |
+
return Status::OK();
|
230 |
+
}
|
231 |
+
|
232 |
+
/// \cond FALSE
|
233 |
+
using ArrayBuilder::Finish;
|
234 |
+
/// \endcond
|
235 |
+
|
236 |
+
Status Finish(std::shared_ptr<ArrayType>* out) { return FinishTyped(out); }
|
237 |
+
|
238 |
+
/// \brief Append a sequence of elements in one shot
|
239 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
240 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
241 |
+
/// \return Status
|
242 |
+
template <typename ValuesIter>
|
243 |
+
Status AppendValues(ValuesIter values_begin, ValuesIter values_end) {
|
244 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
245 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
246 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
247 |
+
// this updates the length_
|
248 |
+
UnsafeSetNotNull(length);
|
249 |
+
return Status::OK();
|
250 |
+
}
|
251 |
+
|
252 |
+
/// \brief Append a sequence of elements in one shot, with a specified nullmap
|
253 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
254 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
255 |
+
/// \param[in] valid_begin InputIterator with elements indication valid(1)
|
256 |
+
/// or null(0) values.
|
257 |
+
/// \return Status
|
258 |
+
template <typename ValuesIter, typename ValidIter>
|
259 |
+
enable_if_t<!std::is_pointer<ValidIter>::value, Status> AppendValues(
|
260 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
261 |
+
static_assert(!internal::is_null_pointer<ValidIter>::value,
|
262 |
+
"Don't pass a NULLPTR directly as valid_begin, use the 2-argument "
|
263 |
+
"version instead");
|
264 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
265 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
266 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
267 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
268 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
269 |
+
length_ = null_bitmap_builder_.length();
|
270 |
+
null_count_ = null_bitmap_builder_.false_count();
|
271 |
+
return Status::OK();
|
272 |
+
}
|
273 |
+
|
274 |
+
// Same as above, with a pointer type ValidIter
|
275 |
+
template <typename ValuesIter, typename ValidIter>
|
276 |
+
enable_if_t<std::is_pointer<ValidIter>::value, Status> AppendValues(
|
277 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
278 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
279 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
280 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
281 |
+
// this updates the length_
|
282 |
+
if (valid_begin == NULLPTR) {
|
283 |
+
UnsafeSetNotNull(length);
|
284 |
+
} else {
|
285 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
286 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
287 |
+
length_ = null_bitmap_builder_.length();
|
288 |
+
null_count_ = null_bitmap_builder_.false_count();
|
289 |
+
}
|
290 |
+
|
291 |
+
return Status::OK();
|
292 |
+
}
|
293 |
+
|
294 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
295 |
+
int64_t length) override {
|
296 |
+
return AppendValues(array.GetValues<value_type>(1) + offset, length,
|
297 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
298 |
+
}
|
299 |
+
|
300 |
+
/// Append a single scalar under the assumption that the underlying Buffer is
|
301 |
+
/// large enough.
|
302 |
+
///
|
303 |
+
/// This method does not capacity-check; make sure to call Reserve
|
304 |
+
/// beforehand.
|
305 |
+
void UnsafeAppend(const value_type val) {
|
306 |
+
ArrayBuilder::UnsafeAppendToBitmap(true);
|
307 |
+
data_builder_.UnsafeAppend(val);
|
308 |
+
}
|
309 |
+
|
310 |
+
void UnsafeAppendNull() {
|
311 |
+
ArrayBuilder::UnsafeAppendToBitmap(false);
|
312 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
313 |
+
}
|
314 |
+
|
315 |
+
std::shared_ptr<DataType> type() const override { return type_; }
|
316 |
+
|
317 |
+
protected:
|
318 |
+
std::shared_ptr<DataType> type_;
|
319 |
+
TypedBufferBuilder<value_type> data_builder_;
|
320 |
+
};
|
321 |
+
|
322 |
+
// Builders
|
323 |
+
|
324 |
+
using UInt8Builder = NumericBuilder<UInt8Type>;
|
325 |
+
using UInt16Builder = NumericBuilder<UInt16Type>;
|
326 |
+
using UInt32Builder = NumericBuilder<UInt32Type>;
|
327 |
+
using UInt64Builder = NumericBuilder<UInt64Type>;
|
328 |
+
|
329 |
+
using Int8Builder = NumericBuilder<Int8Type>;
|
330 |
+
using Int16Builder = NumericBuilder<Int16Type>;
|
331 |
+
using Int32Builder = NumericBuilder<Int32Type>;
|
332 |
+
using Int64Builder = NumericBuilder<Int64Type>;
|
333 |
+
|
334 |
+
using HalfFloatBuilder = NumericBuilder<HalfFloatType>;
|
335 |
+
using FloatBuilder = NumericBuilder<FloatType>;
|
336 |
+
using DoubleBuilder = NumericBuilder<DoubleType>;
|
337 |
+
|
338 |
+
/// @}
|
339 |
+
|
340 |
+
/// \addtogroup temporal-builders
|
341 |
+
///
|
342 |
+
/// @{
|
343 |
+
|
344 |
+
using Date32Builder = NumericBuilder<Date32Type>;
|
345 |
+
using Date64Builder = NumericBuilder<Date64Type>;
|
346 |
+
using Time32Builder = NumericBuilder<Time32Type>;
|
347 |
+
using Time64Builder = NumericBuilder<Time64Type>;
|
348 |
+
using TimestampBuilder = NumericBuilder<TimestampType>;
|
349 |
+
using MonthIntervalBuilder = NumericBuilder<MonthIntervalType>;
|
350 |
+
using DurationBuilder = NumericBuilder<DurationType>;
|
351 |
+
|
352 |
+
/// @}
|
353 |
+
|
354 |
+
class ARROW_EXPORT BooleanBuilder
|
355 |
+
: public ArrayBuilder,
|
356 |
+
public internal::ArrayBuilderExtraOps<BooleanBuilder, bool> {
|
357 |
+
public:
|
358 |
+
using TypeClass = BooleanType;
|
359 |
+
using value_type = bool;
|
360 |
+
|
361 |
+
explicit BooleanBuilder(MemoryPool* pool = default_memory_pool(),
|
362 |
+
int64_t alignment = kDefaultBufferAlignment);
|
363 |
+
|
364 |
+
BooleanBuilder(const std::shared_ptr<DataType>& type,
|
365 |
+
MemoryPool* pool = default_memory_pool(),
|
366 |
+
int64_t alignment = kDefaultBufferAlignment);
|
367 |
+
|
368 |
+
/// Write nulls as uint8_t* (0 value indicates null) into pre-allocated memory
|
369 |
+
Status AppendNulls(int64_t length) final {
|
370 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
371 |
+
data_builder_.UnsafeAppend(length, false);
|
372 |
+
UnsafeSetNull(length);
|
373 |
+
return Status::OK();
|
374 |
+
}
|
375 |
+
|
376 |
+
Status AppendNull() final {
|
377 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
378 |
+
UnsafeAppendNull();
|
379 |
+
return Status::OK();
|
380 |
+
}
|
381 |
+
|
382 |
+
Status AppendEmptyValue() final {
|
383 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
384 |
+
data_builder_.UnsafeAppend(false);
|
385 |
+
UnsafeSetNotNull(1);
|
386 |
+
return Status::OK();
|
387 |
+
}
|
388 |
+
|
389 |
+
Status AppendEmptyValues(int64_t length) final {
|
390 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
391 |
+
data_builder_.UnsafeAppend(length, false);
|
392 |
+
UnsafeSetNotNull(length);
|
393 |
+
return Status::OK();
|
394 |
+
}
|
395 |
+
|
396 |
+
/// Scalar append
|
397 |
+
Status Append(const bool val) {
|
398 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
399 |
+
UnsafeAppend(val);
|
400 |
+
return Status::OK();
|
401 |
+
}
|
402 |
+
|
403 |
+
Status Append(const uint8_t val) { return Append(val != 0); }
|
404 |
+
|
405 |
+
/// Scalar append, without checking for capacity
|
406 |
+
void UnsafeAppend(const bool val) {
|
407 |
+
data_builder_.UnsafeAppend(val);
|
408 |
+
UnsafeAppendToBitmap(true);
|
409 |
+
}
|
410 |
+
|
411 |
+
void UnsafeAppendNull() {
|
412 |
+
data_builder_.UnsafeAppend(false);
|
413 |
+
UnsafeAppendToBitmap(false);
|
414 |
+
}
|
415 |
+
|
416 |
+
void UnsafeAppend(const uint8_t val) { UnsafeAppend(val != 0); }
|
417 |
+
|
418 |
+
/// \brief Append a sequence of elements in one shot
|
419 |
+
/// \param[in] values a contiguous array of bytes (non-zero is 1)
|
420 |
+
/// \param[in] length the number of values to append
|
421 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
422 |
+
/// indicates a valid (non-null) value
|
423 |
+
/// \return Status
|
424 |
+
Status AppendValues(const uint8_t* values, int64_t length,
|
425 |
+
const uint8_t* valid_bytes = NULLPTR);
|
426 |
+
|
427 |
+
/// \brief Append a sequence of elements in one shot
|
428 |
+
/// \param[in] values a bitmap of values
|
429 |
+
/// \param[in] length the number of values to append
|
430 |
+
/// \param[in] validity a validity bitmap to copy (may be null)
|
431 |
+
/// \param[in] offset an offset into the values and validity bitmaps
|
432 |
+
/// \return Status
|
433 |
+
Status AppendValues(const uint8_t* values, int64_t length, const uint8_t* validity,
|
434 |
+
int64_t offset);
|
435 |
+
|
436 |
+
/// \brief Append a sequence of elements in one shot
|
437 |
+
/// \param[in] values a contiguous C array of values
|
438 |
+
/// \param[in] length the number of values to append
|
439 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
440 |
+
/// (0). Equal in length to values
|
441 |
+
/// \return Status
|
442 |
+
Status AppendValues(const uint8_t* values, int64_t length,
|
443 |
+
const std::vector<bool>& is_valid);
|
444 |
+
|
445 |
+
/// \brief Append a sequence of elements in one shot
|
446 |
+
/// \param[in] values a std::vector of bytes
|
447 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
448 |
+
/// (0). Equal in length to values
|
449 |
+
/// \return Status
|
450 |
+
Status AppendValues(const std::vector<uint8_t>& values,
|
451 |
+
const std::vector<bool>& is_valid);
|
452 |
+
|
453 |
+
/// \brief Append a sequence of elements in one shot
|
454 |
+
/// \param[in] values a std::vector of bytes
|
455 |
+
/// \return Status
|
456 |
+
Status AppendValues(const std::vector<uint8_t>& values);
|
457 |
+
|
458 |
+
/// \brief Append a sequence of elements in one shot
|
459 |
+
/// \param[in] values an std::vector<bool> indicating true (1) or false
|
460 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
461 |
+
/// (0). Equal in length to values
|
462 |
+
/// \return Status
|
463 |
+
Status AppendValues(const std::vector<bool>& values, const std::vector<bool>& is_valid);
|
464 |
+
|
465 |
+
/// \brief Append a sequence of elements in one shot
|
466 |
+
/// \param[in] values an std::vector<bool> indicating true (1) or false
|
467 |
+
/// \return Status
|
468 |
+
Status AppendValues(const std::vector<bool>& values);
|
469 |
+
|
470 |
+
/// \brief Append a sequence of elements in one shot
|
471 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
472 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
473 |
+
/// or null(0) values
|
474 |
+
/// \return Status
|
475 |
+
template <typename ValuesIter>
|
476 |
+
Status AppendValues(ValuesIter values_begin, ValuesIter values_end) {
|
477 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
478 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
479 |
+
data_builder_.UnsafeAppend<false>(
|
480 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
481 |
+
// this updates length_
|
482 |
+
UnsafeSetNotNull(length);
|
483 |
+
return Status::OK();
|
484 |
+
}
|
485 |
+
|
486 |
+
/// \brief Append a sequence of elements in one shot, with a specified nullmap
|
487 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
488 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
489 |
+
/// \param[in] valid_begin InputIterator with elements indication valid(1)
|
490 |
+
/// or null(0) values
|
491 |
+
/// \return Status
|
492 |
+
template <typename ValuesIter, typename ValidIter>
|
493 |
+
enable_if_t<!std::is_pointer<ValidIter>::value, Status> AppendValues(
|
494 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
495 |
+
static_assert(!internal::is_null_pointer<ValidIter>::value,
|
496 |
+
"Don't pass a NULLPTR directly as valid_begin, use the 2-argument "
|
497 |
+
"version instead");
|
498 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
499 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
500 |
+
|
501 |
+
data_builder_.UnsafeAppend<false>(
|
502 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
503 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
504 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
505 |
+
length_ = null_bitmap_builder_.length();
|
506 |
+
null_count_ = null_bitmap_builder_.false_count();
|
507 |
+
return Status::OK();
|
508 |
+
}
|
509 |
+
|
510 |
+
// Same as above, for a pointer type ValidIter
|
511 |
+
template <typename ValuesIter, typename ValidIter>
|
512 |
+
enable_if_t<std::is_pointer<ValidIter>::value, Status> AppendValues(
|
513 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
514 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
515 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
516 |
+
data_builder_.UnsafeAppend<false>(
|
517 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
518 |
+
|
519 |
+
if (valid_begin == NULLPTR) {
|
520 |
+
UnsafeSetNotNull(length);
|
521 |
+
} else {
|
522 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
523 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
524 |
+
}
|
525 |
+
length_ = null_bitmap_builder_.length();
|
526 |
+
null_count_ = null_bitmap_builder_.false_count();
|
527 |
+
return Status::OK();
|
528 |
+
}
|
529 |
+
|
530 |
+
Status AppendValues(int64_t length, bool value);
|
531 |
+
|
532 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
533 |
+
int64_t length) override {
|
534 |
+
return AppendValues(array.GetValues<uint8_t>(1, 0), length,
|
535 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
536 |
+
}
|
537 |
+
|
538 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
539 |
+
|
540 |
+
/// \cond FALSE
|
541 |
+
using ArrayBuilder::Finish;
|
542 |
+
/// \endcond
|
543 |
+
|
544 |
+
Status Finish(std::shared_ptr<BooleanArray>* out) { return FinishTyped(out); }
|
545 |
+
|
546 |
+
void Reset() override;
|
547 |
+
Status Resize(int64_t capacity) override;
|
548 |
+
|
549 |
+
std::shared_ptr<DataType> type() const override { return boolean(); }
|
550 |
+
|
551 |
+
protected:
|
552 |
+
TypedBufferBuilder<bool> data_builder_;
|
553 |
+
};
|
554 |
+
|
555 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_run_end.h
ADDED
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <limits>
|
22 |
+
#include <memory>
|
23 |
+
#include <utility>
|
24 |
+
#include <vector>
|
25 |
+
|
26 |
+
#include "arrow/array.h"
|
27 |
+
#include "arrow/array/builder_base.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
|
31 |
+
/// \addtogroup run-end-encoded-builders
|
32 |
+
///
|
33 |
+
/// @{
|
34 |
+
|
35 |
+
namespace internal {
|
36 |
+
|
37 |
+
/// \brief An ArrayBuilder that deduplicates repeated values as they are
|
38 |
+
/// appended to the inner-ArrayBuilder and reports the length of the current run
|
39 |
+
/// of identical values.
|
40 |
+
///
|
41 |
+
/// The following sequence of calls
|
42 |
+
///
|
43 |
+
/// Append(2)
|
44 |
+
/// Append(2)
|
45 |
+
/// Append(2)
|
46 |
+
/// Append(7)
|
47 |
+
/// Append(7)
|
48 |
+
/// Append(2)
|
49 |
+
/// FinishInternal()
|
50 |
+
///
|
51 |
+
/// will cause the inner-builder to receive only 3 Append calls
|
52 |
+
///
|
53 |
+
/// Append(2)
|
54 |
+
/// Append(7)
|
55 |
+
/// Append(2)
|
56 |
+
/// FinishInternal()
|
57 |
+
///
|
58 |
+
/// Note that values returned by length(), null_count() and capacity() are
|
59 |
+
/// related to the compressed array built by the inner-ArrayBuilder.
|
60 |
+
class RunCompressorBuilder : public ArrayBuilder {
|
61 |
+
public:
|
62 |
+
RunCompressorBuilder(MemoryPool* pool, std::shared_ptr<ArrayBuilder> inner_builder,
|
63 |
+
std::shared_ptr<DataType> type);
|
64 |
+
|
65 |
+
~RunCompressorBuilder() override;
|
66 |
+
|
67 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(RunCompressorBuilder);
|
68 |
+
|
69 |
+
/// \brief Called right before a run is being closed
|
70 |
+
///
|
71 |
+
/// Subclasses can override this function to perform an additional action when
|
72 |
+
/// a run is closed (i.e. run-length is known and value is appended to the
|
73 |
+
/// inner builder).
|
74 |
+
///
|
75 |
+
/// \param value can be NULLPTR if closing a run of NULLs
|
76 |
+
/// \param length the greater than 0 length of the value run being closed
|
77 |
+
virtual Status WillCloseRun(const std::shared_ptr<const Scalar>& value,
|
78 |
+
int64_t length) {
|
79 |
+
return Status::OK();
|
80 |
+
}
|
81 |
+
|
82 |
+
/// \brief Called right before a run of empty values is being closed
|
83 |
+
///
|
84 |
+
/// Subclasses can override this function to perform an additional action when
|
85 |
+
/// a run of empty values is appended (i.e. run-length is known and a single
|
86 |
+
/// empty value is appended to the inner builder).
|
87 |
+
///
|
88 |
+
/// \param length the greater than 0 length of the value run being closed
|
89 |
+
virtual Status WillCloseRunOfEmptyValues(int64_t length) { return Status::OK(); }
|
90 |
+
|
91 |
+
/// \brief Allocate enough memory for a given number of array elements.
|
92 |
+
///
|
93 |
+
/// NOTE: Conservatively resizing a run-length compressed array for a given
|
94 |
+
/// number of logical elements is not possible, since the physical length will
|
95 |
+
/// vary depending on the values to be appended in the future. But we can
|
96 |
+
/// pessimistically assume that each run will contain a single value and
|
97 |
+
/// allocate that number of runs.
|
98 |
+
Status Resize(int64_t capacity) override { return ResizePhysical(capacity); }
|
99 |
+
|
100 |
+
/// \brief Allocate enough memory for a given number of runs.
|
101 |
+
///
|
102 |
+
/// Like Resize on non-encoded builders, it does not account for variable size
|
103 |
+
/// data.
|
104 |
+
Status ResizePhysical(int64_t capacity);
|
105 |
+
|
106 |
+
Status ReservePhysical(int64_t additional_capacity) {
|
107 |
+
return Reserve(additional_capacity);
|
108 |
+
}
|
109 |
+
|
110 |
+
void Reset() override;
|
111 |
+
|
112 |
+
Status AppendNull() final { return AppendNulls(1); }
|
113 |
+
Status AppendNulls(int64_t length) override;
|
114 |
+
|
115 |
+
Status AppendEmptyValue() final { return AppendEmptyValues(1); }
|
116 |
+
Status AppendEmptyValues(int64_t length) override;
|
117 |
+
|
118 |
+
Status AppendScalar(const Scalar& scalar, int64_t n_repeats) override;
|
119 |
+
Status AppendScalars(const ScalarVector& scalars) override;
|
120 |
+
|
121 |
+
// AppendArraySlice() is not implemented.
|
122 |
+
|
123 |
+
/// \brief Append a slice of an array containing values from already
|
124 |
+
/// compressed runs.
|
125 |
+
///
|
126 |
+
/// NOTE: WillCloseRun() is not called as the length of each run cannot be
|
127 |
+
/// determined at this point. Caller should ensure that !has_open_run() by
|
128 |
+
/// calling FinishCurrentRun() before calling this.
|
129 |
+
///
|
130 |
+
/// Pre-condition: !has_open_run()
|
131 |
+
Status AppendRunCompressedArraySlice(const ArraySpan& array, int64_t offset,
|
132 |
+
int64_t length);
|
133 |
+
|
134 |
+
/// \brief Forces the closing of the current run if one is currently open.
|
135 |
+
///
|
136 |
+
/// This can be called when one wants to ensure the current run will not be
|
137 |
+
/// extended. This may cause identical values to appear close to each other in
|
138 |
+
/// the underlying array (i.e. two runs that could be a single run) if more
|
139 |
+
/// values are appended after this is called.
|
140 |
+
///
|
141 |
+
/// Finish() and FinishInternal() call this automatically.
|
142 |
+
virtual Status FinishCurrentRun();
|
143 |
+
|
144 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
145 |
+
|
146 |
+
ArrayBuilder& inner_builder() const { return *inner_builder_; }
|
147 |
+
|
148 |
+
std::shared_ptr<DataType> type() const override { return inner_builder_->type(); }
|
149 |
+
|
150 |
+
bool has_open_run() const { return current_run_length_ > 0; }
|
151 |
+
int64_t open_run_length() const { return current_run_length_; }
|
152 |
+
|
153 |
+
private:
|
154 |
+
inline void UpdateDimensions() {
|
155 |
+
capacity_ = inner_builder_->capacity();
|
156 |
+
length_ = inner_builder_->length();
|
157 |
+
null_count_ = inner_builder_->null_count();
|
158 |
+
}
|
159 |
+
|
160 |
+
private:
|
161 |
+
std::shared_ptr<ArrayBuilder> inner_builder_;
|
162 |
+
std::shared_ptr<const Scalar> current_value_ = NULLPTR;
|
163 |
+
int64_t current_run_length_ = 0;
|
164 |
+
};
|
165 |
+
|
166 |
+
} // namespace internal
|
167 |
+
|
168 |
+
// ----------------------------------------------------------------------
|
169 |
+
// RunEndEncoded builder
|
170 |
+
|
171 |
+
/// \brief Run-end encoded array builder.
|
172 |
+
///
|
173 |
+
/// NOTE: the value returned by and capacity() is related to the
|
174 |
+
/// compressed array (physical) and not the decoded array (logical) that is
|
175 |
+
/// run-end encoded. null_count() always returns 0. length(), on the other hand,
|
176 |
+
/// returns the logical length of the run-end encoded array.
|
177 |
+
class ARROW_EXPORT RunEndEncodedBuilder : public ArrayBuilder {
|
178 |
+
private:
|
179 |
+
// An internal::RunCompressorBuilder that produces a run-end in the
|
180 |
+
// RunEndEncodedBuilder every time a value-run is closed.
|
181 |
+
class ValueRunBuilder : public internal::RunCompressorBuilder {
|
182 |
+
public:
|
183 |
+
ValueRunBuilder(MemoryPool* pool, const std::shared_ptr<ArrayBuilder>& value_builder,
|
184 |
+
const std::shared_ptr<DataType>& value_type,
|
185 |
+
RunEndEncodedBuilder& ree_builder);
|
186 |
+
|
187 |
+
~ValueRunBuilder() override = default;
|
188 |
+
|
189 |
+
Status WillCloseRun(const std::shared_ptr<const Scalar>&, int64_t length) override {
|
190 |
+
return ree_builder_.CloseRun(length);
|
191 |
+
}
|
192 |
+
|
193 |
+
Status WillCloseRunOfEmptyValues(int64_t length) override {
|
194 |
+
return ree_builder_.CloseRun(length);
|
195 |
+
}
|
196 |
+
|
197 |
+
private:
|
198 |
+
RunEndEncodedBuilder& ree_builder_;
|
199 |
+
};
|
200 |
+
|
201 |
+
public:
|
202 |
+
RunEndEncodedBuilder(MemoryPool* pool,
|
203 |
+
const std::shared_ptr<ArrayBuilder>& run_end_builder,
|
204 |
+
const std::shared_ptr<ArrayBuilder>& value_builder,
|
205 |
+
std::shared_ptr<DataType> type);
|
206 |
+
|
207 |
+
/// \brief Allocate enough memory for a given number of array elements.
|
208 |
+
///
|
209 |
+
/// NOTE: Conservatively resizing an REE for a given number of logical
|
210 |
+
/// elements is not possible, since the physical length will vary depending on
|
211 |
+
/// the values to be appended in the future. But we can pessimistically assume
|
212 |
+
/// that each run will contain a single value and allocate that number of
|
213 |
+
/// runs.
|
214 |
+
Status Resize(int64_t capacity) override { return ResizePhysical(capacity); }
|
215 |
+
|
216 |
+
/// \brief Allocate enough memory for a given number of runs.
|
217 |
+
Status ResizePhysical(int64_t capacity);
|
218 |
+
|
219 |
+
/// \brief Ensure that there is enough space allocated to append the indicated
|
220 |
+
/// number of run without any further reallocation. Overallocation is
|
221 |
+
/// used in order to minimize the impact of incremental ReservePhysical() calls.
|
222 |
+
/// Note that additional_capacity is relative to the current number of elements
|
223 |
+
/// rather than to the current capacity, so calls to Reserve() which are not
|
224 |
+
/// interspersed with addition of new elements may not increase the capacity.
|
225 |
+
///
|
226 |
+
/// \param[in] additional_capacity the number of additional runs
|
227 |
+
/// \return Status
|
228 |
+
Status ReservePhysical(int64_t additional_capacity) {
|
229 |
+
return Reserve(additional_capacity);
|
230 |
+
}
|
231 |
+
|
232 |
+
void Reset() override;
|
233 |
+
|
234 |
+
Status AppendNull() final { return AppendNulls(1); }
|
235 |
+
Status AppendNulls(int64_t length) override;
|
236 |
+
|
237 |
+
Status AppendEmptyValue() final { return AppendEmptyValues(1); }
|
238 |
+
Status AppendEmptyValues(int64_t length) override;
|
239 |
+
Status AppendScalar(const Scalar& scalar, int64_t n_repeats) override;
|
240 |
+
Status AppendScalars(const ScalarVector& scalars) override;
|
241 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
242 |
+
int64_t length) override;
|
243 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
244 |
+
|
245 |
+
/// \cond FALSE
|
246 |
+
using ArrayBuilder::Finish;
|
247 |
+
/// \endcond
|
248 |
+
|
249 |
+
Status Finish(std::shared_ptr<RunEndEncodedArray>* out) { return FinishTyped(out); }
|
250 |
+
|
251 |
+
/// \brief Forces the closing of the current run if one is currently open.
|
252 |
+
///
|
253 |
+
/// This can be called when one wants to ensure the current run will not be
|
254 |
+
/// extended. This may cause identical values to appear close to each other in
|
255 |
+
/// the values array (i.e. two runs that could be a single run) if more
|
256 |
+
/// values are appended after this is called.
|
257 |
+
Status FinishCurrentRun();
|
258 |
+
|
259 |
+
std::shared_ptr<DataType> type() const override;
|
260 |
+
|
261 |
+
private:
|
262 |
+
/// \brief Update physical capacity and logical length
|
263 |
+
///
|
264 |
+
/// \param committed_logical_length number of logical values that have been
|
265 |
+
/// committed to the values array
|
266 |
+
/// \param open_run_length number of logical values in the currently open run if any
|
267 |
+
inline void UpdateDimensions(int64_t committed_logical_length,
|
268 |
+
int64_t open_run_length) {
|
269 |
+
capacity_ = run_end_builder().capacity();
|
270 |
+
length_ = committed_logical_length + open_run_length;
|
271 |
+
committed_logical_length_ = committed_logical_length;
|
272 |
+
}
|
273 |
+
|
274 |
+
// Pre-condition: !value_run_builder_.has_open_run()
|
275 |
+
template <typename RunEndCType>
|
276 |
+
Status DoAppendArraySlice(const ArraySpan& array, int64_t offset, int64_t length);
|
277 |
+
|
278 |
+
template <typename RunEndCType>
|
279 |
+
Status DoAppendRunEnd(int64_t run_end);
|
280 |
+
|
281 |
+
/// \brief Cast run_end to the appropriate type and appends it to the run_ends
|
282 |
+
/// array.
|
283 |
+
Status AppendRunEnd(int64_t run_end);
|
284 |
+
|
285 |
+
/// \brief Close a run by appending a value to the run_ends array and updating
|
286 |
+
/// length_ to reflect the new run.
|
287 |
+
///
|
288 |
+
/// Pre-condition: run_length > 0.
|
289 |
+
[[nodiscard]] Status CloseRun(int64_t run_length);
|
290 |
+
|
291 |
+
ArrayBuilder& run_end_builder();
|
292 |
+
ArrayBuilder& value_builder();
|
293 |
+
|
294 |
+
private:
|
295 |
+
std::shared_ptr<RunEndEncodedType> type_;
|
296 |
+
ValueRunBuilder* value_run_builder_;
|
297 |
+
// The length not counting the current open run in the value_run_builder_
|
298 |
+
int64_t committed_logical_length_ = 0;
|
299 |
+
};
|
300 |
+
|
301 |
+
/// @}
|
302 |
+
|
303 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_time.h
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Contains declarations of time related Arrow builder types.
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <memory>
|
23 |
+
|
24 |
+
#include "arrow/array/builder_base.h"
|
25 |
+
#include "arrow/array/builder_primitive.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
|
29 |
+
/// \addtogroup temporal-builders
|
30 |
+
///
|
31 |
+
/// @{
|
32 |
+
|
33 |
+
// TODO(ARROW-7938): this class is untested
|
34 |
+
|
35 |
+
class ARROW_EXPORT DayTimeIntervalBuilder : public NumericBuilder<DayTimeIntervalType> {
|
36 |
+
public:
|
37 |
+
using DayMilliseconds = DayTimeIntervalType::DayMilliseconds;
|
38 |
+
|
39 |
+
explicit DayTimeIntervalBuilder(MemoryPool* pool = default_memory_pool(),
|
40 |
+
int64_t alignment = kDefaultBufferAlignment)
|
41 |
+
: DayTimeIntervalBuilder(day_time_interval(), pool, alignment) {}
|
42 |
+
|
43 |
+
explicit DayTimeIntervalBuilder(std::shared_ptr<DataType> type,
|
44 |
+
MemoryPool* pool = default_memory_pool(),
|
45 |
+
int64_t alignment = kDefaultBufferAlignment)
|
46 |
+
: NumericBuilder<DayTimeIntervalType>(type, pool, alignment) {}
|
47 |
+
};
|
48 |
+
|
49 |
+
class ARROW_EXPORT MonthDayNanoIntervalBuilder
|
50 |
+
: public NumericBuilder<MonthDayNanoIntervalType> {
|
51 |
+
public:
|
52 |
+
using MonthDayNanos = MonthDayNanoIntervalType::MonthDayNanos;
|
53 |
+
|
54 |
+
explicit MonthDayNanoIntervalBuilder(MemoryPool* pool = default_memory_pool(),
|
55 |
+
int64_t alignment = kDefaultBufferAlignment)
|
56 |
+
: MonthDayNanoIntervalBuilder(month_day_nano_interval(), pool, alignment) {}
|
57 |
+
|
58 |
+
explicit MonthDayNanoIntervalBuilder(std::shared_ptr<DataType> type,
|
59 |
+
MemoryPool* pool = default_memory_pool(),
|
60 |
+
int64_t alignment = kDefaultBufferAlignment)
|
61 |
+
: NumericBuilder<MonthDayNanoIntervalType>(type, pool, alignment) {}
|
62 |
+
};
|
63 |
+
|
64 |
+
/// @}
|
65 |
+
|
66 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_union.h
ADDED
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <string>
|
23 |
+
#include <vector>
|
24 |
+
|
25 |
+
#include "arrow/array/array_nested.h"
|
26 |
+
#include "arrow/array/builder_base.h"
|
27 |
+
#include "arrow/array/data.h"
|
28 |
+
#include "arrow/buffer_builder.h"
|
29 |
+
#include "arrow/memory_pool.h"
|
30 |
+
#include "arrow/status.h"
|
31 |
+
#include "arrow/type.h"
|
32 |
+
#include "arrow/util/visibility.h"
|
33 |
+
|
34 |
+
namespace arrow {
|
35 |
+
|
36 |
+
/// \addtogroup nested-builders
|
37 |
+
///
|
38 |
+
/// @{
|
39 |
+
|
40 |
+
/// \brief Base class for union array builds.
|
41 |
+
///
|
42 |
+
/// Note that while we subclass ArrayBuilder, as union types do not have a
|
43 |
+
/// validity bitmap, the bitmap builder member of ArrayBuilder is not used.
|
44 |
+
class ARROW_EXPORT BasicUnionBuilder : public ArrayBuilder {
|
45 |
+
public:
|
46 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
47 |
+
|
48 |
+
/// \cond FALSE
|
49 |
+
using ArrayBuilder::Finish;
|
50 |
+
/// \endcond
|
51 |
+
|
52 |
+
Status Finish(std::shared_ptr<UnionArray>* out) { return FinishTyped(out); }
|
53 |
+
|
54 |
+
/// \brief Make a new child builder available to the UnionArray
|
55 |
+
///
|
56 |
+
/// \param[in] new_child the child builder
|
57 |
+
/// \param[in] field_name the name of the field in the union array type
|
58 |
+
/// if type inference is used
|
59 |
+
/// \return child index, which is the "type" argument that needs
|
60 |
+
/// to be passed to the "Append" method to add a new element to
|
61 |
+
/// the union array.
|
62 |
+
int8_t AppendChild(const std::shared_ptr<ArrayBuilder>& new_child,
|
63 |
+
const std::string& field_name = "");
|
64 |
+
|
65 |
+
std::shared_ptr<DataType> type() const override;
|
66 |
+
|
67 |
+
int64_t length() const override { return types_builder_.length(); }
|
68 |
+
|
69 |
+
protected:
|
70 |
+
BasicUnionBuilder(MemoryPool* pool, int64_t alignment,
|
71 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
72 |
+
const std::shared_ptr<DataType>& type);
|
73 |
+
|
74 |
+
int8_t NextTypeId();
|
75 |
+
|
76 |
+
std::vector<std::shared_ptr<Field>> child_fields_;
|
77 |
+
std::vector<int8_t> type_codes_;
|
78 |
+
UnionMode::type mode_;
|
79 |
+
|
80 |
+
std::vector<ArrayBuilder*> type_id_to_children_;
|
81 |
+
std::vector<int> type_id_to_child_id_;
|
82 |
+
// for all type_id < dense_type_id_, type_id_to_children_[type_id] != nullptr
|
83 |
+
int8_t dense_type_id_ = 0;
|
84 |
+
TypedBufferBuilder<int8_t> types_builder_;
|
85 |
+
};
|
86 |
+
|
87 |
+
/// \class DenseUnionBuilder
|
88 |
+
///
|
89 |
+
/// This API is EXPERIMENTAL.
|
90 |
+
class ARROW_EXPORT DenseUnionBuilder : public BasicUnionBuilder {
|
91 |
+
public:
|
92 |
+
/// Use this constructor to initialize the UnionBuilder with no child builders,
|
93 |
+
/// allowing type to be inferred. You will need to call AppendChild for each of the
|
94 |
+
/// children builders you want to use.
|
95 |
+
explicit DenseUnionBuilder(MemoryPool* pool,
|
96 |
+
int64_t alignment = kDefaultBufferAlignment)
|
97 |
+
: BasicUnionBuilder(pool, alignment, {}, dense_union(FieldVector{})),
|
98 |
+
offsets_builder_(pool, alignment) {}
|
99 |
+
|
100 |
+
/// Use this constructor to specify the type explicitly.
|
101 |
+
/// You can still add child builders to the union after using this constructor
|
102 |
+
DenseUnionBuilder(MemoryPool* pool,
|
103 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
104 |
+
const std::shared_ptr<DataType>& type,
|
105 |
+
int64_t alignment = kDefaultBufferAlignment)
|
106 |
+
: BasicUnionBuilder(pool, alignment, children, type),
|
107 |
+
offsets_builder_(pool, alignment) {}
|
108 |
+
|
109 |
+
Status AppendNull() final {
|
110 |
+
const int8_t first_child_code = type_codes_[0];
|
111 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
112 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
113 |
+
ARROW_RETURN_NOT_OK(
|
114 |
+
offsets_builder_.Append(static_cast<int32_t>(child_builder->length())));
|
115 |
+
// Append a null arbitrarily to the first child
|
116 |
+
return child_builder->AppendNull();
|
117 |
+
}
|
118 |
+
|
119 |
+
Status AppendNulls(int64_t length) final {
|
120 |
+
const int8_t first_child_code = type_codes_[0];
|
121 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
122 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
123 |
+
ARROW_RETURN_NOT_OK(
|
124 |
+
offsets_builder_.Append(length, static_cast<int32_t>(child_builder->length())));
|
125 |
+
// Append just a single null to the first child
|
126 |
+
return child_builder->AppendNull();
|
127 |
+
}
|
128 |
+
|
129 |
+
Status AppendEmptyValue() final {
|
130 |
+
const int8_t first_child_code = type_codes_[0];
|
131 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
132 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
133 |
+
ARROW_RETURN_NOT_OK(
|
134 |
+
offsets_builder_.Append(static_cast<int32_t>(child_builder->length())));
|
135 |
+
// Append an empty value arbitrarily to the first child
|
136 |
+
return child_builder->AppendEmptyValue();
|
137 |
+
}
|
138 |
+
|
139 |
+
Status AppendEmptyValues(int64_t length) final {
|
140 |
+
const int8_t first_child_code = type_codes_[0];
|
141 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
142 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
143 |
+
ARROW_RETURN_NOT_OK(
|
144 |
+
offsets_builder_.Append(length, static_cast<int32_t>(child_builder->length())));
|
145 |
+
// Append just a single empty value to the first child
|
146 |
+
return child_builder->AppendEmptyValue();
|
147 |
+
}
|
148 |
+
|
149 |
+
/// \brief Append an element to the UnionArray. This must be followed
|
150 |
+
/// by an append to the appropriate child builder.
|
151 |
+
///
|
152 |
+
/// \param[in] next_type type_id of the child to which the next value will be appended.
|
153 |
+
///
|
154 |
+
/// The corresponding child builder must be appended to independently after this method
|
155 |
+
/// is called.
|
156 |
+
Status Append(int8_t next_type) {
|
157 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(next_type));
|
158 |
+
if (type_id_to_children_[next_type]->length() == kListMaximumElements) {
|
159 |
+
return Status::CapacityError(
|
160 |
+
"a dense UnionArray cannot contain more than 2^31 - 1 elements from a single "
|
161 |
+
"child");
|
162 |
+
}
|
163 |
+
auto offset = static_cast<int32_t>(type_id_to_children_[next_type]->length());
|
164 |
+
return offsets_builder_.Append(offset);
|
165 |
+
}
|
166 |
+
|
167 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
168 |
+
int64_t length) override;
|
169 |
+
|
170 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
171 |
+
|
172 |
+
private:
|
173 |
+
TypedBufferBuilder<int32_t> offsets_builder_;
|
174 |
+
};
|
175 |
+
|
176 |
+
/// \class SparseUnionBuilder
|
177 |
+
///
|
178 |
+
/// This API is EXPERIMENTAL.
|
179 |
+
class ARROW_EXPORT SparseUnionBuilder : public BasicUnionBuilder {
|
180 |
+
public:
|
181 |
+
/// Use this constructor to initialize the UnionBuilder with no child builders,
|
182 |
+
/// allowing type to be inferred. You will need to call AppendChild for each of the
|
183 |
+
/// children builders you want to use.
|
184 |
+
explicit SparseUnionBuilder(MemoryPool* pool,
|
185 |
+
int64_t alignment = kDefaultBufferAlignment)
|
186 |
+
: BasicUnionBuilder(pool, alignment, {}, sparse_union(FieldVector{})) {}
|
187 |
+
|
188 |
+
/// Use this constructor to specify the type explicitly.
|
189 |
+
/// You can still add child builders to the union after using this constructor
|
190 |
+
SparseUnionBuilder(MemoryPool* pool,
|
191 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
192 |
+
const std::shared_ptr<DataType>& type,
|
193 |
+
int64_t alignment = kDefaultBufferAlignment)
|
194 |
+
: BasicUnionBuilder(pool, alignment, children, type) {}
|
195 |
+
|
196 |
+
/// \brief Append a null value.
|
197 |
+
///
|
198 |
+
/// A null is appended to the first child, empty values to the other children.
|
199 |
+
Status AppendNull() final {
|
200 |
+
const auto first_child_code = type_codes_[0];
|
201 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
202 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[first_child_code]->AppendNull());
|
203 |
+
for (int i = 1; i < static_cast<int>(type_codes_.size()); ++i) {
|
204 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[type_codes_[i]]->AppendEmptyValue());
|
205 |
+
}
|
206 |
+
return Status::OK();
|
207 |
+
}
|
208 |
+
|
209 |
+
/// \brief Append multiple null values.
|
210 |
+
///
|
211 |
+
/// Nulls are appended to the first child, empty values to the other children.
|
212 |
+
Status AppendNulls(int64_t length) final {
|
213 |
+
const auto first_child_code = type_codes_[0];
|
214 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
215 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[first_child_code]->AppendNulls(length));
|
216 |
+
for (int i = 1; i < static_cast<int>(type_codes_.size()); ++i) {
|
217 |
+
ARROW_RETURN_NOT_OK(
|
218 |
+
type_id_to_children_[type_codes_[i]]->AppendEmptyValues(length));
|
219 |
+
}
|
220 |
+
return Status::OK();
|
221 |
+
}
|
222 |
+
|
223 |
+
Status AppendEmptyValue() final {
|
224 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(type_codes_[0]));
|
225 |
+
for (int8_t code : type_codes_) {
|
226 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[code]->AppendEmptyValue());
|
227 |
+
}
|
228 |
+
return Status::OK();
|
229 |
+
}
|
230 |
+
|
231 |
+
Status AppendEmptyValues(int64_t length) final {
|
232 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, type_codes_[0]));
|
233 |
+
for (int8_t code : type_codes_) {
|
234 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[code]->AppendEmptyValues(length));
|
235 |
+
}
|
236 |
+
return Status::OK();
|
237 |
+
}
|
238 |
+
|
239 |
+
/// \brief Append an element to the UnionArray. This must be followed
|
240 |
+
/// by an append to the appropriate child builder.
|
241 |
+
///
|
242 |
+
/// \param[in] next_type type_id of the child to which the next value will be appended.
|
243 |
+
///
|
244 |
+
/// The corresponding child builder must be appended to independently after this method
|
245 |
+
/// is called, and all other child builders must have null or empty value appended.
|
246 |
+
Status Append(int8_t next_type) { return types_builder_.Append(next_type); }
|
247 |
+
|
248 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
249 |
+
int64_t length) override;
|
250 |
+
};
|
251 |
+
|
252 |
+
/// @}
|
253 |
+
|
254 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/concatenate.h
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
|
22 |
+
#include "arrow/type_fwd.h"
|
23 |
+
#include "arrow/util/macros.h"
|
24 |
+
#include "arrow/util/visibility.h"
|
25 |
+
|
26 |
+
namespace arrow {
|
27 |
+
|
28 |
+
/// \brief Concatenate arrays
|
29 |
+
///
|
30 |
+
/// \param[in] arrays a vector of arrays to be concatenated
|
31 |
+
/// \param[in] pool memory to store the result will be allocated from this memory pool
|
32 |
+
/// \return the concatenated array
|
33 |
+
ARROW_EXPORT
|
34 |
+
Result<std::shared_ptr<Array>> Concatenate(const ArrayVector& arrays,
|
35 |
+
MemoryPool* pool = default_memory_pool());
|
36 |
+
|
37 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/data.h
ADDED
@@ -0,0 +1,634 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <atomic> // IWYU pragma: export
|
21 |
+
#include <cassert>
|
22 |
+
#include <cstdint>
|
23 |
+
#include <memory>
|
24 |
+
#include <utility>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/buffer.h"
|
28 |
+
#include "arrow/result.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/type_fwd.h"
|
31 |
+
#include "arrow/util/bit_util.h"
|
32 |
+
#include "arrow/util/macros.h"
|
33 |
+
#include "arrow/util/span.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
|
36 |
+
namespace arrow {
|
37 |
+
|
38 |
+
namespace internal {
|
39 |
+
// ----------------------------------------------------------------------
|
40 |
+
// Null handling for types without a validity bitmap and the dictionary type
|
41 |
+
|
42 |
+
ARROW_EXPORT bool IsNullSparseUnion(const ArrayData& data, int64_t i);
|
43 |
+
ARROW_EXPORT bool IsNullDenseUnion(const ArrayData& data, int64_t i);
|
44 |
+
ARROW_EXPORT bool IsNullRunEndEncoded(const ArrayData& data, int64_t i);
|
45 |
+
|
46 |
+
ARROW_EXPORT bool UnionMayHaveLogicalNulls(const ArrayData& data);
|
47 |
+
ARROW_EXPORT bool RunEndEncodedMayHaveLogicalNulls(const ArrayData& data);
|
48 |
+
ARROW_EXPORT bool DictionaryMayHaveLogicalNulls(const ArrayData& data);
|
49 |
+
} // namespace internal
|
50 |
+
|
51 |
+
// When slicing, we do not know the null count of the sliced range without
|
52 |
+
// doing some computation. To avoid doing this eagerly, we set the null count
|
53 |
+
// to -1 (any negative number will do). When Array::null_count is called the
|
54 |
+
// first time, the null count will be computed. See ARROW-33
|
55 |
+
constexpr int64_t kUnknownNullCount = -1;
|
56 |
+
|
57 |
+
// ----------------------------------------------------------------------
|
58 |
+
// Generic array data container
|
59 |
+
|
60 |
+
/// \class ArrayData
|
61 |
+
/// \brief Mutable container for generic Arrow array data
|
62 |
+
///
|
63 |
+
/// This data structure is a self-contained representation of the memory and
|
64 |
+
/// metadata inside an Arrow array data structure (called vectors in Java). The
|
65 |
+
/// classes arrow::Array and its subclasses provide strongly-typed accessors
|
66 |
+
/// with support for the visitor pattern and other affordances.
|
67 |
+
///
|
68 |
+
/// This class is designed for easy internal data manipulation, analytical data
|
69 |
+
/// processing, and data transport to and from IPC messages. For example, we
|
70 |
+
/// could cast from int64 to float64 like so:
|
71 |
+
///
|
72 |
+
/// Int64Array arr = GetMyData();
|
73 |
+
/// auto new_data = arr.data()->Copy();
|
74 |
+
/// new_data->type = arrow::float64();
|
75 |
+
/// DoubleArray double_arr(new_data);
|
76 |
+
///
|
77 |
+
/// This object is also useful in an analytics setting where memory may be
|
78 |
+
/// reused. For example, if we had a group of operations all returning doubles,
|
79 |
+
/// say:
|
80 |
+
///
|
81 |
+
/// Log(Sqrt(Expr(arr)))
|
82 |
+
///
|
83 |
+
/// Then the low-level implementations of each of these functions could have
|
84 |
+
/// the signatures
|
85 |
+
///
|
86 |
+
/// void Log(const ArrayData& values, ArrayData* out);
|
87 |
+
///
|
88 |
+
/// As another example a function may consume one or more memory buffers in an
|
89 |
+
/// input array and replace them with newly-allocated data, changing the output
|
90 |
+
/// data type as well.
|
91 |
+
struct ARROW_EXPORT ArrayData {
|
92 |
+
ArrayData() = default;
|
93 |
+
|
94 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
95 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
96 |
+
: type(std::move(type)), length(length), null_count(null_count), offset(offset) {}
|
97 |
+
|
98 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
99 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
100 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
101 |
+
: ArrayData(std::move(type), length, null_count, offset) {
|
102 |
+
this->buffers = std::move(buffers);
|
103 |
+
}
|
104 |
+
|
105 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
106 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
107 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
108 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
109 |
+
: ArrayData(std::move(type), length, null_count, offset) {
|
110 |
+
this->buffers = std::move(buffers);
|
111 |
+
this->child_data = std::move(child_data);
|
112 |
+
}
|
113 |
+
|
114 |
+
static std::shared_ptr<ArrayData> Make(std::shared_ptr<DataType> type, int64_t length,
|
115 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
116 |
+
int64_t null_count = kUnknownNullCount,
|
117 |
+
int64_t offset = 0);
|
118 |
+
|
119 |
+
static std::shared_ptr<ArrayData> Make(
|
120 |
+
std::shared_ptr<DataType> type, int64_t length,
|
121 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
122 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
123 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
124 |
+
|
125 |
+
static std::shared_ptr<ArrayData> Make(
|
126 |
+
std::shared_ptr<DataType> type, int64_t length,
|
127 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
128 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
129 |
+
std::shared_ptr<ArrayData> dictionary, int64_t null_count = kUnknownNullCount,
|
130 |
+
int64_t offset = 0);
|
131 |
+
|
132 |
+
static std::shared_ptr<ArrayData> Make(std::shared_ptr<DataType> type, int64_t length,
|
133 |
+
int64_t null_count = kUnknownNullCount,
|
134 |
+
int64_t offset = 0);
|
135 |
+
|
136 |
+
// Move constructor
|
137 |
+
ArrayData(ArrayData&& other) noexcept
|
138 |
+
: type(std::move(other.type)),
|
139 |
+
length(other.length),
|
140 |
+
offset(other.offset),
|
141 |
+
buffers(std::move(other.buffers)),
|
142 |
+
child_data(std::move(other.child_data)),
|
143 |
+
dictionary(std::move(other.dictionary)) {
|
144 |
+
SetNullCount(other.null_count);
|
145 |
+
}
|
146 |
+
|
147 |
+
// Copy constructor
|
148 |
+
ArrayData(const ArrayData& other) noexcept
|
149 |
+
: type(other.type),
|
150 |
+
length(other.length),
|
151 |
+
offset(other.offset),
|
152 |
+
buffers(other.buffers),
|
153 |
+
child_data(other.child_data),
|
154 |
+
dictionary(other.dictionary) {
|
155 |
+
SetNullCount(other.null_count);
|
156 |
+
}
|
157 |
+
|
158 |
+
// Move assignment
|
159 |
+
ArrayData& operator=(ArrayData&& other) {
|
160 |
+
type = std::move(other.type);
|
161 |
+
length = other.length;
|
162 |
+
SetNullCount(other.null_count);
|
163 |
+
offset = other.offset;
|
164 |
+
buffers = std::move(other.buffers);
|
165 |
+
child_data = std::move(other.child_data);
|
166 |
+
dictionary = std::move(other.dictionary);
|
167 |
+
return *this;
|
168 |
+
}
|
169 |
+
|
170 |
+
// Copy assignment
|
171 |
+
ArrayData& operator=(const ArrayData& other) {
|
172 |
+
type = other.type;
|
173 |
+
length = other.length;
|
174 |
+
SetNullCount(other.null_count);
|
175 |
+
offset = other.offset;
|
176 |
+
buffers = other.buffers;
|
177 |
+
child_data = other.child_data;
|
178 |
+
dictionary = other.dictionary;
|
179 |
+
return *this;
|
180 |
+
}
|
181 |
+
|
182 |
+
std::shared_ptr<ArrayData> Copy() const { return std::make_shared<ArrayData>(*this); }
|
183 |
+
|
184 |
+
/// \brief Copy all buffers and children recursively to destination MemoryManager
|
185 |
+
///
|
186 |
+
/// This utilizes MemoryManager::CopyBuffer to create a new ArrayData object
|
187 |
+
/// recursively copying the buffers and all child buffers to the destination
|
188 |
+
/// memory manager. This includes dictionaries if applicable.
|
189 |
+
Result<std::shared_ptr<ArrayData>> CopyTo(
|
190 |
+
const std::shared_ptr<MemoryManager>& to) const;
|
191 |
+
/// \brief View or Copy this ArrayData to destination memory manager.
|
192 |
+
///
|
193 |
+
/// Tries to view the buffer contents on the given memory manager's device
|
194 |
+
/// if possible (to avoid a copy) but falls back to copying if a no-copy view
|
195 |
+
/// isn't supported.
|
196 |
+
Result<std::shared_ptr<ArrayData>> ViewOrCopyTo(
|
197 |
+
const std::shared_ptr<MemoryManager>& to) const;
|
198 |
+
|
199 |
+
bool IsNull(int64_t i) const { return !IsValid(i); }
|
200 |
+
|
201 |
+
bool IsValid(int64_t i) const {
|
202 |
+
if (buffers[0] != NULLPTR) {
|
203 |
+
return bit_util::GetBit(buffers[0]->data(), i + offset);
|
204 |
+
}
|
205 |
+
const auto type = this->type->id();
|
206 |
+
if (type == Type::SPARSE_UNION) {
|
207 |
+
return !internal::IsNullSparseUnion(*this, i);
|
208 |
+
}
|
209 |
+
if (type == Type::DENSE_UNION) {
|
210 |
+
return !internal::IsNullDenseUnion(*this, i);
|
211 |
+
}
|
212 |
+
if (type == Type::RUN_END_ENCODED) {
|
213 |
+
return !internal::IsNullRunEndEncoded(*this, i);
|
214 |
+
}
|
215 |
+
return null_count.load() != length;
|
216 |
+
}
|
217 |
+
|
218 |
+
// Access a buffer's data as a typed C pointer
|
219 |
+
template <typename T>
|
220 |
+
inline const T* GetValues(int i, int64_t absolute_offset) const {
|
221 |
+
if (buffers[i]) {
|
222 |
+
return reinterpret_cast<const T*>(buffers[i]->data()) + absolute_offset;
|
223 |
+
} else {
|
224 |
+
return NULLPTR;
|
225 |
+
}
|
226 |
+
}
|
227 |
+
|
228 |
+
template <typename T>
|
229 |
+
inline const T* GetValues(int i) const {
|
230 |
+
return GetValues<T>(i, offset);
|
231 |
+
}
|
232 |
+
|
233 |
+
// Like GetValues, but returns NULLPTR instead of aborting if the underlying
|
234 |
+
// buffer is not a CPU buffer.
|
235 |
+
template <typename T>
|
236 |
+
inline const T* GetValuesSafe(int i, int64_t absolute_offset) const {
|
237 |
+
if (buffers[i] && buffers[i]->is_cpu()) {
|
238 |
+
return reinterpret_cast<const T*>(buffers[i]->data()) + absolute_offset;
|
239 |
+
} else {
|
240 |
+
return NULLPTR;
|
241 |
+
}
|
242 |
+
}
|
243 |
+
|
244 |
+
template <typename T>
|
245 |
+
inline const T* GetValuesSafe(int i) const {
|
246 |
+
return GetValuesSafe<T>(i, offset);
|
247 |
+
}
|
248 |
+
|
249 |
+
// Access a buffer's data as a typed C pointer
|
250 |
+
template <typename T>
|
251 |
+
inline T* GetMutableValues(int i, int64_t absolute_offset) {
|
252 |
+
if (buffers[i]) {
|
253 |
+
return reinterpret_cast<T*>(buffers[i]->mutable_data()) + absolute_offset;
|
254 |
+
} else {
|
255 |
+
return NULLPTR;
|
256 |
+
}
|
257 |
+
}
|
258 |
+
|
259 |
+
template <typename T>
|
260 |
+
inline T* GetMutableValues(int i) {
|
261 |
+
return GetMutableValues<T>(i, offset);
|
262 |
+
}
|
263 |
+
|
264 |
+
/// \brief Construct a zero-copy slice of the data with the given offset and length
|
265 |
+
std::shared_ptr<ArrayData> Slice(int64_t offset, int64_t length) const;
|
266 |
+
|
267 |
+
/// \brief Input-checking variant of Slice
|
268 |
+
///
|
269 |
+
/// An Invalid Status is returned if the requested slice falls out of bounds.
|
270 |
+
/// Note that unlike Slice, `length` isn't clamped to the available buffer size.
|
271 |
+
Result<std::shared_ptr<ArrayData>> SliceSafe(int64_t offset, int64_t length) const;
|
272 |
+
|
273 |
+
void SetNullCount(int64_t v) { null_count.store(v); }
|
274 |
+
|
275 |
+
/// \brief Return physical null count, or compute and set it if it's not known
|
276 |
+
int64_t GetNullCount() const;
|
277 |
+
|
278 |
+
/// \brief Return true if the data has a validity bitmap and the physical null
|
279 |
+
/// count is known to be non-zero or not yet known.
|
280 |
+
///
|
281 |
+
/// Note that this is not the same as MayHaveLogicalNulls, which also checks
|
282 |
+
/// for the presence of nulls in child data for types like unions and run-end
|
283 |
+
/// encoded types.
|
284 |
+
///
|
285 |
+
/// \see HasValidityBitmap
|
286 |
+
/// \see MayHaveLogicalNulls
|
287 |
+
bool MayHaveNulls() const {
|
288 |
+
// If an ArrayData is slightly malformed it may have kUnknownNullCount set
|
289 |
+
// but no buffer
|
290 |
+
return null_count.load() != 0 && buffers[0] != NULLPTR;
|
291 |
+
}
|
292 |
+
|
293 |
+
/// \brief Return true if the data has a validity bitmap
|
294 |
+
bool HasValidityBitmap() const { return buffers[0] != NULLPTR; }
|
295 |
+
|
296 |
+
/// \brief Return true if the validity bitmap may have 0's in it, or if the
|
297 |
+
/// child arrays (in the case of types without a validity bitmap) may have
|
298 |
+
/// nulls, or if the dictionary of dictionay array may have nulls.
|
299 |
+
///
|
300 |
+
/// This is not a drop-in replacement for MayHaveNulls, as historically
|
301 |
+
/// MayHaveNulls() has been used to check for the presence of a validity
|
302 |
+
/// bitmap that needs to be checked.
|
303 |
+
///
|
304 |
+
/// Code that previously used MayHaveNulls() and then dealt with the validity
|
305 |
+
/// bitmap directly can be fixed to handle all types correctly without
|
306 |
+
/// performance degradation when handling most types by adopting
|
307 |
+
/// HasValidityBitmap and MayHaveLogicalNulls.
|
308 |
+
///
|
309 |
+
/// Before:
|
310 |
+
///
|
311 |
+
/// uint8_t* validity = array.MayHaveNulls() ? array.buffers[0].data : NULLPTR;
|
312 |
+
/// for (int64_t i = 0; i < array.length; ++i) {
|
313 |
+
/// if (validity && !bit_util::GetBit(validity, i)) {
|
314 |
+
/// continue; // skip a NULL
|
315 |
+
/// }
|
316 |
+
/// ...
|
317 |
+
/// }
|
318 |
+
///
|
319 |
+
/// After:
|
320 |
+
///
|
321 |
+
/// bool all_valid = !array.MayHaveLogicalNulls();
|
322 |
+
/// uint8_t* validity = array.HasValidityBitmap() ? array.buffers[0].data : NULLPTR;
|
323 |
+
/// for (int64_t i = 0; i < array.length; ++i) {
|
324 |
+
/// bool is_valid = all_valid ||
|
325 |
+
/// (validity && bit_util::GetBit(validity, i)) ||
|
326 |
+
/// array.IsValid(i);
|
327 |
+
/// if (!is_valid) {
|
328 |
+
/// continue; // skip a NULL
|
329 |
+
/// }
|
330 |
+
/// ...
|
331 |
+
/// }
|
332 |
+
bool MayHaveLogicalNulls() const {
|
333 |
+
if (buffers[0] != NULLPTR) {
|
334 |
+
return null_count.load() != 0;
|
335 |
+
}
|
336 |
+
const auto t = type->id();
|
337 |
+
if (t == Type::SPARSE_UNION || t == Type::DENSE_UNION) {
|
338 |
+
return internal::UnionMayHaveLogicalNulls(*this);
|
339 |
+
}
|
340 |
+
if (t == Type::RUN_END_ENCODED) {
|
341 |
+
return internal::RunEndEncodedMayHaveLogicalNulls(*this);
|
342 |
+
}
|
343 |
+
if (t == Type::DICTIONARY) {
|
344 |
+
return internal::DictionaryMayHaveLogicalNulls(*this);
|
345 |
+
}
|
346 |
+
return null_count.load() != 0;
|
347 |
+
}
|
348 |
+
|
349 |
+
/// \brief Computes the logical null count for arrays of all types including
|
350 |
+
/// those that do not have a validity bitmap like union and run-end encoded
|
351 |
+
/// arrays
|
352 |
+
///
|
353 |
+
/// If the array has a validity bitmap, this function behaves the same as
|
354 |
+
/// GetNullCount. For types that have no validity bitmap, this function will
|
355 |
+
/// recompute the null count every time it is called.
|
356 |
+
///
|
357 |
+
/// \see GetNullCount
|
358 |
+
int64_t ComputeLogicalNullCount() const;
|
359 |
+
|
360 |
+
std::shared_ptr<DataType> type;
|
361 |
+
int64_t length = 0;
|
362 |
+
mutable std::atomic<int64_t> null_count{0};
|
363 |
+
// The logical start point into the physical buffers (in values, not bytes).
|
364 |
+
// Note that, for child data, this must be *added* to the child data's own offset.
|
365 |
+
int64_t offset = 0;
|
366 |
+
std::vector<std::shared_ptr<Buffer>> buffers;
|
367 |
+
std::vector<std::shared_ptr<ArrayData>> child_data;
|
368 |
+
|
369 |
+
// The dictionary for this Array, if any. Only used for dictionary type
|
370 |
+
std::shared_ptr<ArrayData> dictionary;
|
371 |
+
};
|
372 |
+
|
373 |
+
/// \brief A non-owning Buffer reference
|
374 |
+
struct ARROW_EXPORT BufferSpan {
|
375 |
+
// It is the user of this class's responsibility to ensure that
|
376 |
+
// buffers that were const originally are not written to
|
377 |
+
// accidentally.
|
378 |
+
uint8_t* data = NULLPTR;
|
379 |
+
int64_t size = 0;
|
380 |
+
// Pointer back to buffer that owns this memory
|
381 |
+
const std::shared_ptr<Buffer>* owner = NULLPTR;
|
382 |
+
|
383 |
+
template <typename T>
|
384 |
+
const T* data_as() const {
|
385 |
+
return reinterpret_cast<const T*>(data);
|
386 |
+
}
|
387 |
+
template <typename T>
|
388 |
+
T* mutable_data_as() {
|
389 |
+
return reinterpret_cast<T*>(data);
|
390 |
+
}
|
391 |
+
};
|
392 |
+
|
393 |
+
/// \brief EXPERIMENTAL: A non-owning ArrayData reference that is cheaply
|
394 |
+
/// copyable and does not contain any shared_ptr objects. Do not use in public
|
395 |
+
/// APIs aside from compute kernels for now
|
396 |
+
struct ARROW_EXPORT ArraySpan {
|
397 |
+
const DataType* type = NULLPTR;
|
398 |
+
int64_t length = 0;
|
399 |
+
mutable int64_t null_count = kUnknownNullCount;
|
400 |
+
int64_t offset = 0;
|
401 |
+
BufferSpan buffers[3];
|
402 |
+
|
403 |
+
ArraySpan() = default;
|
404 |
+
|
405 |
+
explicit ArraySpan(const DataType* type, int64_t length) : type(type), length(length) {}
|
406 |
+
|
407 |
+
ArraySpan(const ArrayData& data) { // NOLINT implicit conversion
|
408 |
+
SetMembers(data);
|
409 |
+
}
|
410 |
+
explicit ArraySpan(const Scalar& data) { FillFromScalar(data); }
|
411 |
+
|
412 |
+
/// If dictionary-encoded, put dictionary in the first entry
|
413 |
+
std::vector<ArraySpan> child_data;
|
414 |
+
|
415 |
+
/// \brief Populate ArraySpan to look like an array of length 1 pointing at
|
416 |
+
/// the data members of a Scalar value
|
417 |
+
void FillFromScalar(const Scalar& value);
|
418 |
+
|
419 |
+
void SetMembers(const ArrayData& data);
|
420 |
+
|
421 |
+
void SetBuffer(int index, const std::shared_ptr<Buffer>& buffer) {
|
422 |
+
this->buffers[index].data = const_cast<uint8_t*>(buffer->data());
|
423 |
+
this->buffers[index].size = buffer->size();
|
424 |
+
this->buffers[index].owner = &buffer;
|
425 |
+
}
|
426 |
+
|
427 |
+
const ArraySpan& dictionary() const { return child_data[0]; }
|
428 |
+
|
429 |
+
/// \brief Return the number of buffers (out of 3) that are used to
|
430 |
+
/// constitute this array
|
431 |
+
int num_buffers() const;
|
432 |
+
|
433 |
+
// Access a buffer's data as a typed C pointer
|
434 |
+
template <typename T>
|
435 |
+
inline T* GetValues(int i, int64_t absolute_offset) {
|
436 |
+
return reinterpret_cast<T*>(buffers[i].data) + absolute_offset;
|
437 |
+
}
|
438 |
+
|
439 |
+
template <typename T>
|
440 |
+
inline T* GetValues(int i) {
|
441 |
+
return GetValues<T>(i, this->offset);
|
442 |
+
}
|
443 |
+
|
444 |
+
// Access a buffer's data as a typed C pointer
|
445 |
+
template <typename T>
|
446 |
+
inline const T* GetValues(int i, int64_t absolute_offset) const {
|
447 |
+
return reinterpret_cast<const T*>(buffers[i].data) + absolute_offset;
|
448 |
+
}
|
449 |
+
|
450 |
+
template <typename T>
|
451 |
+
inline const T* GetValues(int i) const {
|
452 |
+
return GetValues<T>(i, this->offset);
|
453 |
+
}
|
454 |
+
|
455 |
+
/// \brief Access a buffer's data as a span
|
456 |
+
///
|
457 |
+
/// \param i The buffer index
|
458 |
+
/// \param length The required length (in number of typed values) of the requested span
|
459 |
+
/// \pre i > 0
|
460 |
+
/// \pre length <= the length of the buffer (in number of values) that's expected for
|
461 |
+
/// this array type
|
462 |
+
/// \return A span<const T> of the requested length
|
463 |
+
template <typename T>
|
464 |
+
util::span<const T> GetSpan(int i, int64_t length) const {
|
465 |
+
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
|
466 |
+
assert(i > 0 && length + offset <= buffer_length);
|
467 |
+
ARROW_UNUSED(buffer_length);
|
468 |
+
return util::span<const T>(buffers[i].data_as<T>() + this->offset, length);
|
469 |
+
}
|
470 |
+
|
471 |
+
/// \brief Access a buffer's data as a span
|
472 |
+
///
|
473 |
+
/// \param i The buffer index
|
474 |
+
/// \param length The required length (in number of typed values) of the requested span
|
475 |
+
/// \pre i > 0
|
476 |
+
/// \pre length <= the length of the buffer (in number of values) that's expected for
|
477 |
+
/// this array type
|
478 |
+
/// \return A span<T> of the requested length
|
479 |
+
template <typename T>
|
480 |
+
util::span<T> GetSpan(int i, int64_t length) {
|
481 |
+
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
|
482 |
+
assert(i > 0 && length + offset <= buffer_length);
|
483 |
+
ARROW_UNUSED(buffer_length);
|
484 |
+
return util::span<T>(buffers[i].mutable_data_as<T>() + this->offset, length);
|
485 |
+
}
|
486 |
+
|
487 |
+
inline bool IsNull(int64_t i) const { return !IsValid(i); }
|
488 |
+
|
489 |
+
inline bool IsValid(int64_t i) const {
|
490 |
+
if (this->buffers[0].data != NULLPTR) {
|
491 |
+
return bit_util::GetBit(this->buffers[0].data, i + this->offset);
|
492 |
+
} else {
|
493 |
+
const auto type = this->type->id();
|
494 |
+
if (type == Type::SPARSE_UNION) {
|
495 |
+
return !IsNullSparseUnion(i);
|
496 |
+
}
|
497 |
+
if (type == Type::DENSE_UNION) {
|
498 |
+
return !IsNullDenseUnion(i);
|
499 |
+
}
|
500 |
+
if (type == Type::RUN_END_ENCODED) {
|
501 |
+
return !IsNullRunEndEncoded(i);
|
502 |
+
}
|
503 |
+
return this->null_count != this->length;
|
504 |
+
}
|
505 |
+
}
|
506 |
+
|
507 |
+
std::shared_ptr<ArrayData> ToArrayData() const;
|
508 |
+
|
509 |
+
std::shared_ptr<Array> ToArray() const;
|
510 |
+
|
511 |
+
std::shared_ptr<Buffer> GetBuffer(int index) const {
|
512 |
+
const BufferSpan& buf = this->buffers[index];
|
513 |
+
if (buf.owner) {
|
514 |
+
return *buf.owner;
|
515 |
+
} else if (buf.data != NULLPTR) {
|
516 |
+
// Buffer points to some memory without an owning buffer
|
517 |
+
return std::make_shared<Buffer>(buf.data, buf.size);
|
518 |
+
} else {
|
519 |
+
return NULLPTR;
|
520 |
+
}
|
521 |
+
}
|
522 |
+
|
523 |
+
void SetSlice(int64_t offset, int64_t length) {
|
524 |
+
this->offset = offset;
|
525 |
+
this->length = length;
|
526 |
+
if (this->type->id() == Type::NA) {
|
527 |
+
this->null_count = this->length;
|
528 |
+
} else if (this->MayHaveNulls()) {
|
529 |
+
this->null_count = kUnknownNullCount;
|
530 |
+
} else {
|
531 |
+
this->null_count = 0;
|
532 |
+
}
|
533 |
+
}
|
534 |
+
|
535 |
+
/// \brief Return physical null count, or compute and set it if it's not known
|
536 |
+
int64_t GetNullCount() const;
|
537 |
+
|
538 |
+
/// \brief Return true if the array has a validity bitmap and the physical null
|
539 |
+
/// count is known to be non-zero or not yet known
|
540 |
+
///
|
541 |
+
/// Note that this is not the same as MayHaveLogicalNulls, which also checks
|
542 |
+
/// for the presence of nulls in child data for types like unions and run-end
|
543 |
+
/// encoded types.
|
544 |
+
///
|
545 |
+
/// \see HasValidityBitmap
|
546 |
+
/// \see MayHaveLogicalNulls
|
547 |
+
bool MayHaveNulls() const {
|
548 |
+
// If an ArrayData is slightly malformed it may have kUnknownNullCount set
|
549 |
+
// but no buffer
|
550 |
+
return null_count != 0 && buffers[0].data != NULLPTR;
|
551 |
+
}
|
552 |
+
|
553 |
+
/// \brief Return true if the array has a validity bitmap
|
554 |
+
bool HasValidityBitmap() const { return buffers[0].data != NULLPTR; }
|
555 |
+
|
556 |
+
/// \brief Return true if the validity bitmap may have 0's in it, or if the
|
557 |
+
/// child arrays (in the case of types without a validity bitmap) may have
|
558 |
+
/// nulls, or if the dictionary of dictionay array may have nulls.
|
559 |
+
///
|
560 |
+
/// \see ArrayData::MayHaveLogicalNulls
|
561 |
+
bool MayHaveLogicalNulls() const {
|
562 |
+
if (buffers[0].data != NULLPTR) {
|
563 |
+
return null_count != 0;
|
564 |
+
}
|
565 |
+
const auto t = type->id();
|
566 |
+
if (t == Type::SPARSE_UNION || t == Type::DENSE_UNION) {
|
567 |
+
return UnionMayHaveLogicalNulls();
|
568 |
+
}
|
569 |
+
if (t == Type::RUN_END_ENCODED) {
|
570 |
+
return RunEndEncodedMayHaveLogicalNulls();
|
571 |
+
}
|
572 |
+
if (t == Type::DICTIONARY) {
|
573 |
+
return DictionaryMayHaveLogicalNulls();
|
574 |
+
}
|
575 |
+
return null_count != 0;
|
576 |
+
}
|
577 |
+
|
578 |
+
/// \brief Compute the logical null count for arrays of all types including
|
579 |
+
/// those that do not have a validity bitmap like union and run-end encoded
|
580 |
+
/// arrays
|
581 |
+
///
|
582 |
+
/// If the array has a validity bitmap, this function behaves the same as
|
583 |
+
/// GetNullCount. For types that have no validity bitmap, this function will
|
584 |
+
/// recompute the logical null count every time it is called.
|
585 |
+
///
|
586 |
+
/// \see GetNullCount
|
587 |
+
int64_t ComputeLogicalNullCount() const;
|
588 |
+
|
589 |
+
/// Some DataTypes (StringView, BinaryView) may have an arbitrary number of variadic
|
590 |
+
/// buffers. Since ArraySpan only has 3 buffers, we pack the variadic buffers into
|
591 |
+
/// buffers[2]; IE buffers[2].data points to the first shared_ptr<Buffer> of the
|
592 |
+
/// variadic set and buffers[2].size is the number of variadic buffers times
|
593 |
+
/// sizeof(shared_ptr<Buffer>).
|
594 |
+
///
|
595 |
+
/// \see HasVariadicBuffers
|
596 |
+
util::span<const std::shared_ptr<Buffer>> GetVariadicBuffers() const;
|
597 |
+
bool HasVariadicBuffers() const;
|
598 |
+
|
599 |
+
private:
|
600 |
+
ARROW_FRIEND_EXPORT friend bool internal::IsNullRunEndEncoded(const ArrayData& span,
|
601 |
+
int64_t i);
|
602 |
+
|
603 |
+
bool IsNullSparseUnion(int64_t i) const;
|
604 |
+
bool IsNullDenseUnion(int64_t i) const;
|
605 |
+
|
606 |
+
/// \brief Return true if the value at logical index i is null
|
607 |
+
///
|
608 |
+
/// This function uses binary-search, so it has a O(log N) cost.
|
609 |
+
/// Iterating over the whole array and calling IsNull is O(N log N), so
|
610 |
+
/// for better performance it is recommended to use a
|
611 |
+
/// ree_util::RunEndEncodedArraySpan to iterate run by run instead.
|
612 |
+
bool IsNullRunEndEncoded(int64_t i) const;
|
613 |
+
|
614 |
+
bool UnionMayHaveLogicalNulls() const;
|
615 |
+
bool RunEndEncodedMayHaveLogicalNulls() const;
|
616 |
+
bool DictionaryMayHaveLogicalNulls() const;
|
617 |
+
};
|
618 |
+
|
619 |
+
namespace internal {
|
620 |
+
|
621 |
+
void FillZeroLengthArray(const DataType* type, ArraySpan* span);
|
622 |
+
|
623 |
+
/// Construct a zero-copy view of this ArrayData with the given type.
|
624 |
+
///
|
625 |
+
/// This method checks if the types are layout-compatible.
|
626 |
+
/// Nested types are traversed in depth-first order. Data buffers must have
|
627 |
+
/// the same item sizes, even though the logical types may be different.
|
628 |
+
/// An error is returned if the types are not layout-compatible.
|
629 |
+
ARROW_EXPORT
|
630 |
+
Result<std::shared_ptr<ArrayData>> GetArrayView(const std::shared_ptr<ArrayData>& data,
|
631 |
+
const std::shared_ptr<DataType>& type);
|
632 |
+
|
633 |
+
} // namespace internal
|
634 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/diff.h
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <functional>
|
22 |
+
#include <iosfwd>
|
23 |
+
#include <memory>
|
24 |
+
|
25 |
+
#include "arrow/array/array_base.h"
|
26 |
+
#include "arrow/array/array_nested.h"
|
27 |
+
#include "arrow/result.h"
|
28 |
+
#include "arrow/status.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/util/visibility.h"
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
|
34 |
+
/// \brief Compare two arrays, returning an edit script which expresses the difference
|
35 |
+
/// between them
|
36 |
+
///
|
37 |
+
/// An edit script is an array of struct(insert: bool, run_length: int64_t).
|
38 |
+
/// Each element of "insert" determines whether an element was inserted into (true)
|
39 |
+
/// or deleted from (false) base. Each insertion or deletion is followed by a run of
|
40 |
+
/// elements which are unchanged from base to target; the length of this run is stored
|
41 |
+
/// in "run_length". (Note that the edit script begins and ends with a run of shared
|
42 |
+
/// elements but both fields of the struct must have the same length. To accommodate this
|
43 |
+
/// the first element of "insert" should be ignored.)
|
44 |
+
///
|
45 |
+
/// For example for base "hlloo" and target "hello", the edit script would be
|
46 |
+
/// [
|
47 |
+
/// {"insert": false, "run_length": 1}, // leading run of length 1 ("h")
|
48 |
+
/// {"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
|
49 |
+
/// {"insert": false, "run_length": 0} // delete("o") then an empty run
|
50 |
+
/// ]
|
51 |
+
///
|
52 |
+
/// Diffing arrays containing nulls is not currently supported.
|
53 |
+
///
|
54 |
+
/// \param[in] base baseline for comparison
|
55 |
+
/// \param[in] target an array of identical type to base whose elements differ from base's
|
56 |
+
/// \param[in] pool memory to store the result will be allocated from this memory pool
|
57 |
+
/// \return an edit script array which can be applied to base to produce target
|
58 |
+
ARROW_EXPORT
|
59 |
+
Result<std::shared_ptr<StructArray>> Diff(const Array& base, const Array& target,
|
60 |
+
MemoryPool* pool = default_memory_pool());
|
61 |
+
|
62 |
+
/// \brief visitor interface for easy traversal of an edit script
|
63 |
+
///
|
64 |
+
/// visitor will be called for each hunk of insertions and deletions.
|
65 |
+
ARROW_EXPORT Status VisitEditScript(
|
66 |
+
const Array& edits,
|
67 |
+
const std::function<Status(int64_t delete_begin, int64_t delete_end,
|
68 |
+
int64_t insert_begin, int64_t insert_end)>& visitor);
|
69 |
+
|
70 |
+
/// \brief return a function which will format an edit script in unified
|
71 |
+
/// diff format to os, given base and target arrays of type
|
72 |
+
ARROW_EXPORT Result<
|
73 |
+
std::function<Status(const Array& edits, const Array& base, const Array& target)>>
|
74 |
+
MakeUnifiedDiffFormatter(const DataType& type, std::ostream* os);
|
75 |
+
|
76 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/util.h
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <vector>
|
23 |
+
|
24 |
+
#include "arrow/array/data.h"
|
25 |
+
#include "arrow/compare.h"
|
26 |
+
#include "arrow/result.h"
|
27 |
+
#include "arrow/status.h"
|
28 |
+
#include "arrow/type.h"
|
29 |
+
#include "arrow/util/macros.h"
|
30 |
+
#include "arrow/util/visibility.h"
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
|
34 |
+
/// \defgroup array-factories Array factory functions
|
35 |
+
///
|
36 |
+
/// @{
|
37 |
+
|
38 |
+
/// \brief Create a strongly-typed Array instance from generic ArrayData
|
39 |
+
/// \param[in] data the array contents
|
40 |
+
/// \return the resulting Array instance
|
41 |
+
ARROW_EXPORT
|
42 |
+
std::shared_ptr<Array> MakeArray(const std::shared_ptr<ArrayData>& data);
|
43 |
+
|
44 |
+
/// \brief Create a strongly-typed Array instance with all elements null
|
45 |
+
/// \param[in] type the array type
|
46 |
+
/// \param[in] length the array length
|
47 |
+
/// \param[in] pool the memory pool to allocate memory from
|
48 |
+
ARROW_EXPORT
|
49 |
+
Result<std::shared_ptr<Array>> MakeArrayOfNull(const std::shared_ptr<DataType>& type,
|
50 |
+
int64_t length,
|
51 |
+
MemoryPool* pool = default_memory_pool());
|
52 |
+
|
53 |
+
/// \brief Create an Array instance whose slots are the given scalar
|
54 |
+
/// \param[in] scalar the value with which to fill the array
|
55 |
+
/// \param[in] length the array length
|
56 |
+
/// \param[in] pool the memory pool to allocate memory from
|
57 |
+
ARROW_EXPORT
|
58 |
+
Result<std::shared_ptr<Array>> MakeArrayFromScalar(
|
59 |
+
const Scalar& scalar, int64_t length, MemoryPool* pool = default_memory_pool());
|
60 |
+
|
61 |
+
/// \brief Create an empty Array of a given type
|
62 |
+
///
|
63 |
+
/// The output Array will be of the given type.
|
64 |
+
///
|
65 |
+
/// \param[in] type the data type of the empty Array
|
66 |
+
/// \param[in] pool the memory pool to allocate memory from
|
67 |
+
/// \return the resulting Array
|
68 |
+
ARROW_EXPORT
|
69 |
+
Result<std::shared_ptr<Array>> MakeEmptyArray(std::shared_ptr<DataType> type,
|
70 |
+
MemoryPool* pool = default_memory_pool());
|
71 |
+
|
72 |
+
/// @}
|
73 |
+
|
74 |
+
namespace internal {
|
75 |
+
|
76 |
+
/// \brief Swap endian of each element in a generic ArrayData
|
77 |
+
///
|
78 |
+
/// As dictionaries are often shared between different arrays, dictionaries
|
79 |
+
/// are not swapped by this function and should be handled separately.
|
80 |
+
///
|
81 |
+
/// \param[in] data the array contents
|
82 |
+
/// \param[in] pool the memory pool to allocate memory from
|
83 |
+
/// \return the resulting ArrayData whose elements were swapped
|
84 |
+
ARROW_EXPORT
|
85 |
+
Result<std::shared_ptr<ArrayData>> SwapEndianArrayData(
|
86 |
+
const std::shared_ptr<ArrayData>& data, MemoryPool* pool = default_memory_pool());
|
87 |
+
|
88 |
+
/// Given a number of ArrayVectors, treat each ArrayVector as the
|
89 |
+
/// chunks of a chunked array. Then rechunk each ArrayVector such that
|
90 |
+
/// all ArrayVectors are chunked identically. It is mandatory that
|
91 |
+
/// all ArrayVectors contain the same total number of elements.
|
92 |
+
ARROW_EXPORT
|
93 |
+
std::vector<ArrayVector> RechunkArraysConsistently(const std::vector<ArrayVector>&);
|
94 |
+
|
95 |
+
} // namespace internal
|
96 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/array/validate.h
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include "arrow/status.h"
|
21 |
+
#include "arrow/type_fwd.h"
|
22 |
+
#include "arrow/util/visibility.h"
|
23 |
+
|
24 |
+
namespace arrow {
|
25 |
+
namespace internal {
|
26 |
+
|
27 |
+
// Internal functions implementing Array::Validate() and friends.
|
28 |
+
|
29 |
+
// O(1) array metadata validation
|
30 |
+
|
31 |
+
ARROW_EXPORT
|
32 |
+
Status ValidateArray(const Array& array);
|
33 |
+
|
34 |
+
ARROW_EXPORT
|
35 |
+
Status ValidateArray(const ArrayData& data);
|
36 |
+
|
37 |
+
// O(N) array data validation.
|
38 |
+
// Note that, starting from 7.0.0, "full" routines also validate metadata.
|
39 |
+
// Before, ValidateArray() needed to be called before ValidateArrayFull()
|
40 |
+
// to ensure metadata correctness, otherwise invalid memory accesses
|
41 |
+
// may occur.
|
42 |
+
|
43 |
+
ARROW_EXPORT
|
44 |
+
Status ValidateArrayFull(const Array& array);
|
45 |
+
|
46 |
+
ARROW_EXPORT
|
47 |
+
Status ValidateArrayFull(const ArrayData& data);
|
48 |
+
|
49 |
+
ARROW_EXPORT
|
50 |
+
Status ValidateUTF8(const Array& array);
|
51 |
+
|
52 |
+
ARROW_EXPORT
|
53 |
+
Status ValidateUTF8(const ArrayData& data);
|
54 |
+
|
55 |
+
} // namespace internal
|
56 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/client_cookie_middleware.h
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Middleware implementation for sending and receiving HTTP cookies.
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <memory>
|
23 |
+
|
24 |
+
#include "arrow/flight/client_middleware.h"
|
25 |
+
|
26 |
+
namespace arrow {
|
27 |
+
namespace flight {
|
28 |
+
|
29 |
+
/// \brief Returns a ClientMiddlewareFactory that handles sending and receiving cookies.
|
30 |
+
ARROW_FLIGHT_EXPORT std::shared_ptr<ClientMiddlewareFactory> GetCookieFactory();
|
31 |
+
|
32 |
+
} // namespace flight
|
33 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/client_middleware.h
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Interfaces for defining middleware for Flight clients. Currently
|
19 |
+
// experimental.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <memory>
|
24 |
+
|
25 |
+
#include "arrow/flight/middleware.h"
|
26 |
+
#include "arrow/flight/visibility.h" // IWYU pragma: keep
|
27 |
+
#include "arrow/status.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
namespace flight {
|
31 |
+
|
32 |
+
/// \brief Client-side middleware for a call, instantiated per RPC.
|
33 |
+
///
|
34 |
+
/// Middleware should be fast and must be infallible: there is no way
|
35 |
+
/// to reject the call or report errors from the middleware instance.
|
36 |
+
class ARROW_FLIGHT_EXPORT ClientMiddleware {
|
37 |
+
public:
|
38 |
+
virtual ~ClientMiddleware() = default;
|
39 |
+
|
40 |
+
/// \brief A callback before headers are sent. Extra headers can be
|
41 |
+
/// added, but existing ones cannot be read.
|
42 |
+
virtual void SendingHeaders(AddCallHeaders* outgoing_headers) = 0;
|
43 |
+
|
44 |
+
/// \brief A callback when headers are received from the server.
|
45 |
+
///
|
46 |
+
/// This may be called more than once, since servers send both
|
47 |
+
/// headers and trailers. Some implementations (e.g. gRPC-Java, and
|
48 |
+
/// hence Arrow Flight in Java) may consolidate headers into
|
49 |
+
/// trailers if the RPC errored.
|
50 |
+
virtual void ReceivedHeaders(const CallHeaders& incoming_headers) = 0;
|
51 |
+
|
52 |
+
/// \brief A callback after the call has completed.
|
53 |
+
virtual void CallCompleted(const Status& status) = 0;
|
54 |
+
};
|
55 |
+
|
56 |
+
/// \brief A factory for new middleware instances.
|
57 |
+
///
|
58 |
+
/// If added to a client, this will be called for each RPC (including
|
59 |
+
/// Handshake) to give the opportunity to intercept the call.
|
60 |
+
///
|
61 |
+
/// It is guaranteed that all client middleware methods are called
|
62 |
+
/// from the same thread that calls the RPC method implementation.
|
63 |
+
class ARROW_FLIGHT_EXPORT ClientMiddlewareFactory {
|
64 |
+
public:
|
65 |
+
virtual ~ClientMiddlewareFactory() = default;
|
66 |
+
|
67 |
+
/// \brief A callback for the start of a new call.
|
68 |
+
///
|
69 |
+
/// \param info Information about the call.
|
70 |
+
/// \param[out] middleware The middleware instance for this call. If
|
71 |
+
/// unset, will not add middleware to this call instance from
|
72 |
+
/// this factory.
|
73 |
+
virtual void StartCall(const CallInfo& info,
|
74 |
+
std::unique_ptr<ClientMiddleware>* middleware) = 0;
|
75 |
+
};
|
76 |
+
|
77 |
+
} // namespace flight
|
78 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/pch.h
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Often-used headers, for precompiling.
|
19 |
+
// If updating this header, please make sure you check compilation speed
|
20 |
+
// before checking in. Adding headers which are not used extremely often
|
21 |
+
// may incur a slowdown, since it makes the precompiled header heavier to load.
|
22 |
+
|
23 |
+
#include "arrow/flight/client.h"
|
24 |
+
#include "arrow/flight/server.h"
|
25 |
+
#include "arrow/flight/types.h"
|
26 |
+
#include "arrow/pch.h"
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server.h
ADDED
@@ -0,0 +1,328 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Interfaces to use for defining Flight RPC servers. API should be considered
|
19 |
+
// experimental for now
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <chrono>
|
24 |
+
#include <functional>
|
25 |
+
#include <memory>
|
26 |
+
#include <string>
|
27 |
+
#include <utility>
|
28 |
+
#include <vector>
|
29 |
+
|
30 |
+
#include "arrow/flight/server_auth.h"
|
31 |
+
#include "arrow/flight/type_fwd.h"
|
32 |
+
#include "arrow/flight/types.h" // IWYU pragma: keep
|
33 |
+
#include "arrow/flight/visibility.h" // IWYU pragma: keep
|
34 |
+
#include "arrow/ipc/dictionary.h"
|
35 |
+
#include "arrow/ipc/options.h"
|
36 |
+
#include "arrow/record_batch.h"
|
37 |
+
|
38 |
+
namespace arrow {
|
39 |
+
|
40 |
+
class Schema;
|
41 |
+
class Status;
|
42 |
+
|
43 |
+
namespace flight {
|
44 |
+
|
45 |
+
/// \brief Interface that produces a sequence of IPC payloads to be sent in
|
46 |
+
/// FlightData protobuf messages
|
47 |
+
class ARROW_FLIGHT_EXPORT FlightDataStream {
|
48 |
+
public:
|
49 |
+
virtual ~FlightDataStream();
|
50 |
+
|
51 |
+
virtual std::shared_ptr<Schema> schema() = 0;
|
52 |
+
|
53 |
+
/// \brief Compute FlightPayload containing serialized RecordBatch schema
|
54 |
+
virtual arrow::Result<FlightPayload> GetSchemaPayload() = 0;
|
55 |
+
|
56 |
+
// When the stream is completed, the last payload written will have null
|
57 |
+
// metadata
|
58 |
+
virtual arrow::Result<FlightPayload> Next() = 0;
|
59 |
+
|
60 |
+
virtual Status Close();
|
61 |
+
};
|
62 |
+
|
63 |
+
/// \brief A basic implementation of FlightDataStream that will provide
|
64 |
+
/// a sequence of FlightData messages to be written to a stream
|
65 |
+
class ARROW_FLIGHT_EXPORT RecordBatchStream : public FlightDataStream {
|
66 |
+
public:
|
67 |
+
/// \param[in] reader produces a sequence of record batches
|
68 |
+
/// \param[in] options IPC options for writing
|
69 |
+
explicit RecordBatchStream(
|
70 |
+
const std::shared_ptr<RecordBatchReader>& reader,
|
71 |
+
const ipc::IpcWriteOptions& options = ipc::IpcWriteOptions::Defaults());
|
72 |
+
~RecordBatchStream() override;
|
73 |
+
|
74 |
+
// inherit deprecated API
|
75 |
+
using FlightDataStream::GetSchemaPayload;
|
76 |
+
using FlightDataStream::Next;
|
77 |
+
|
78 |
+
std::shared_ptr<Schema> schema() override;
|
79 |
+
arrow::Result<FlightPayload> GetSchemaPayload() override;
|
80 |
+
|
81 |
+
arrow::Result<FlightPayload> Next() override;
|
82 |
+
Status Close() override;
|
83 |
+
|
84 |
+
private:
|
85 |
+
class RecordBatchStreamImpl;
|
86 |
+
std::unique_ptr<RecordBatchStreamImpl> impl_;
|
87 |
+
};
|
88 |
+
|
89 |
+
/// \brief A reader for IPC payloads uploaded by a client. Also allows
|
90 |
+
/// reading application-defined metadata via the Flight protocol.
|
91 |
+
class ARROW_FLIGHT_EXPORT FlightMessageReader : public MetadataRecordBatchReader {
|
92 |
+
public:
|
93 |
+
/// \brief Get the descriptor for this upload.
|
94 |
+
virtual const FlightDescriptor& descriptor() const = 0;
|
95 |
+
};
|
96 |
+
|
97 |
+
/// \brief A writer for application-specific metadata sent back to the
|
98 |
+
/// client during an upload.
|
99 |
+
class ARROW_FLIGHT_EXPORT FlightMetadataWriter {
|
100 |
+
public:
|
101 |
+
virtual ~FlightMetadataWriter();
|
102 |
+
/// \brief Send a message to the client.
|
103 |
+
virtual Status WriteMetadata(const Buffer& app_metadata) = 0;
|
104 |
+
};
|
105 |
+
|
106 |
+
/// \brief A writer for IPC payloads to a client. Also allows sending
|
107 |
+
/// application-defined metadata via the Flight protocol.
|
108 |
+
///
|
109 |
+
/// This class offers more control compared to FlightDataStream,
|
110 |
+
/// including the option to write metadata without data and the
|
111 |
+
/// ability to interleave reading and writing.
|
112 |
+
class ARROW_FLIGHT_EXPORT FlightMessageWriter : public MetadataRecordBatchWriter {
|
113 |
+
public:
|
114 |
+
virtual ~FlightMessageWriter() = default;
|
115 |
+
};
|
116 |
+
|
117 |
+
/// \brief Call state/contextual data.
|
118 |
+
class ARROW_FLIGHT_EXPORT ServerCallContext {
|
119 |
+
public:
|
120 |
+
virtual ~ServerCallContext() = default;
|
121 |
+
/// \brief The name of the authenticated peer (may be the empty string)
|
122 |
+
virtual const std::string& peer_identity() const = 0;
|
123 |
+
/// \brief The peer address (not validated)
|
124 |
+
virtual const std::string& peer() const = 0;
|
125 |
+
/// \brief Add a response header. This is only valid before the server
|
126 |
+
/// starts sending the response; generally this isn't an issue unless you
|
127 |
+
/// are implementing FlightDataStream, ResultStream, or similar interfaces
|
128 |
+
/// yourself, or during a DoExchange or DoPut.
|
129 |
+
virtual void AddHeader(const std::string& key, const std::string& value) const = 0;
|
130 |
+
/// \brief Add a response trailer. This is only valid before the server
|
131 |
+
/// sends the final status; generally this isn't an issue unless your RPC
|
132 |
+
/// handler launches a thread or similar.
|
133 |
+
virtual void AddTrailer(const std::string& key, const std::string& value) const = 0;
|
134 |
+
/// \brief Look up a middleware by key. Do not maintain a reference
|
135 |
+
/// to the object beyond the request body.
|
136 |
+
/// \return The middleware, or nullptr if not found.
|
137 |
+
virtual ServerMiddleware* GetMiddleware(const std::string& key) const = 0;
|
138 |
+
/// \brief Check if the current RPC has been cancelled (by the client, by
|
139 |
+
/// a network error, etc.).
|
140 |
+
virtual bool is_cancelled() const = 0;
|
141 |
+
/// \brief The headers sent by the client for this call.
|
142 |
+
virtual const CallHeaders& incoming_headers() const = 0;
|
143 |
+
};
|
144 |
+
|
145 |
+
class ARROW_FLIGHT_EXPORT FlightServerOptions {
|
146 |
+
public:
|
147 |
+
explicit FlightServerOptions(const Location& location_);
|
148 |
+
|
149 |
+
~FlightServerOptions();
|
150 |
+
|
151 |
+
/// \brief The host & port (or domain socket path) to listen on.
|
152 |
+
/// Use port 0 to bind to an available port.
|
153 |
+
Location location;
|
154 |
+
/// \brief The authentication handler to use.
|
155 |
+
std::shared_ptr<ServerAuthHandler> auth_handler;
|
156 |
+
/// \brief A list of TLS certificate+key pairs to use.
|
157 |
+
std::vector<CertKeyPair> tls_certificates;
|
158 |
+
/// \brief Enable mTLS and require that the client present a certificate.
|
159 |
+
bool verify_client;
|
160 |
+
/// \brief If using mTLS, the PEM-encoded root certificate to use.
|
161 |
+
std::string root_certificates;
|
162 |
+
/// \brief A list of server middleware to apply, along with a key to
|
163 |
+
/// identify them by.
|
164 |
+
///
|
165 |
+
/// Middleware are always applied in the order provided. Duplicate
|
166 |
+
/// keys are an error.
|
167 |
+
std::vector<std::pair<std::string, std::shared_ptr<ServerMiddlewareFactory>>>
|
168 |
+
middleware;
|
169 |
+
|
170 |
+
/// \brief An optional memory manager to control where to allocate incoming data.
|
171 |
+
std::shared_ptr<MemoryManager> memory_manager;
|
172 |
+
|
173 |
+
/// \brief A Flight implementation-specific callback to customize
|
174 |
+
/// transport-specific options.
|
175 |
+
///
|
176 |
+
/// Not guaranteed to be called. The type of the parameter is
|
177 |
+
/// specific to the Flight implementation. Users should take care to
|
178 |
+
/// link to the same transport implementation as Flight to avoid
|
179 |
+
/// runtime problems. See "Using Arrow C++ in your own project" in
|
180 |
+
/// the documentation for more details.
|
181 |
+
std::function<void(void*)> builder_hook;
|
182 |
+
};
|
183 |
+
|
184 |
+
/// \brief Skeleton RPC server implementation which can be used to create
|
185 |
+
/// custom servers by implementing its abstract methods
|
186 |
+
class ARROW_FLIGHT_EXPORT FlightServerBase {
|
187 |
+
public:
|
188 |
+
FlightServerBase();
|
189 |
+
virtual ~FlightServerBase();
|
190 |
+
|
191 |
+
// Lifecycle methods.
|
192 |
+
|
193 |
+
/// \brief Initialize a Flight server listening at the given location.
|
194 |
+
/// This method must be called before any other method.
|
195 |
+
/// \param[in] options The configuration for this server.
|
196 |
+
Status Init(const FlightServerOptions& options);
|
197 |
+
|
198 |
+
/// \brief Get the port that the Flight server is listening on.
|
199 |
+
/// This method must only be called after Init(). Will return a
|
200 |
+
/// non-positive value if no port exists (e.g. when listening on a
|
201 |
+
/// domain socket).
|
202 |
+
int port() const;
|
203 |
+
|
204 |
+
/// \brief Get the address that the Flight server is listening on.
|
205 |
+
/// This method must only be called after Init().
|
206 |
+
Location location() const;
|
207 |
+
|
208 |
+
/// \brief Set the server to stop when receiving any of the given signal
|
209 |
+
/// numbers.
|
210 |
+
/// This method must be called before Serve().
|
211 |
+
Status SetShutdownOnSignals(const std::vector<int> sigs);
|
212 |
+
|
213 |
+
/// \brief Start serving.
|
214 |
+
/// This method blocks until the server shuts down.
|
215 |
+
///
|
216 |
+
/// The server will start to shut down when either Shutdown() is called
|
217 |
+
/// or one of the signals registered in SetShutdownOnSignals() is received.
|
218 |
+
Status Serve();
|
219 |
+
|
220 |
+
/// \brief Query whether Serve() was interrupted by a signal.
|
221 |
+
/// This method must be called after Serve() has returned.
|
222 |
+
///
|
223 |
+
/// \return int the signal number that interrupted Serve(), if any, otherwise 0
|
224 |
+
int GotSignal() const;
|
225 |
+
|
226 |
+
/// \brief Shut down the server, blocking until current requests finish.
|
227 |
+
///
|
228 |
+
/// Can be called from a signal handler or another thread while Serve()
|
229 |
+
/// blocks. Optionally a deadline can be set. Once the deadline expires
|
230 |
+
/// server will wait until remaining running calls complete.
|
231 |
+
///
|
232 |
+
/// Should only be called once.
|
233 |
+
Status Shutdown(const std::chrono::system_clock::time_point* deadline = NULLPTR);
|
234 |
+
|
235 |
+
/// \brief Block until server shuts down with Shutdown.
|
236 |
+
///
|
237 |
+
/// Does not respond to signals like Serve().
|
238 |
+
Status Wait();
|
239 |
+
|
240 |
+
// Implement these methods to create your own server. The default
|
241 |
+
// implementations will return a not-implemented result to the client
|
242 |
+
|
243 |
+
/// \brief Retrieve a list of available fields given an optional opaque
|
244 |
+
/// criteria
|
245 |
+
/// \param[in] context The call context.
|
246 |
+
/// \param[in] criteria may be null
|
247 |
+
/// \param[out] listings the returned listings iterator
|
248 |
+
/// \return Status
|
249 |
+
virtual Status ListFlights(const ServerCallContext& context, const Criteria* criteria,
|
250 |
+
std::unique_ptr<FlightListing>* listings);
|
251 |
+
|
252 |
+
/// \brief Retrieve the schema and an access plan for the indicated
|
253 |
+
/// descriptor
|
254 |
+
/// \param[in] context The call context.
|
255 |
+
/// \param[in] request the dataset request, whether a named dataset or command
|
256 |
+
/// \param[out] info the returned flight info provider
|
257 |
+
/// \return Status
|
258 |
+
virtual Status GetFlightInfo(const ServerCallContext& context,
|
259 |
+
const FlightDescriptor& request,
|
260 |
+
std::unique_ptr<FlightInfo>* info);
|
261 |
+
|
262 |
+
/// \brief Retrieve the current status of the target query
|
263 |
+
/// \param[in] context The call context.
|
264 |
+
/// \param[in] request the dataset request or a descriptor returned by a
|
265 |
+
/// prior PollFlightInfo call
|
266 |
+
/// \param[out] info the returned retry info provider
|
267 |
+
/// \return Status
|
268 |
+
virtual Status PollFlightInfo(const ServerCallContext& context,
|
269 |
+
const FlightDescriptor& request,
|
270 |
+
std::unique_ptr<PollInfo>* info);
|
271 |
+
|
272 |
+
/// \brief Retrieve the schema for the indicated descriptor
|
273 |
+
/// \param[in] context The call context.
|
274 |
+
/// \param[in] request the dataset request, whether a named dataset or command
|
275 |
+
/// \param[out] schema the returned flight schema provider
|
276 |
+
/// \return Status
|
277 |
+
virtual Status GetSchema(const ServerCallContext& context,
|
278 |
+
const FlightDescriptor& request,
|
279 |
+
std::unique_ptr<SchemaResult>* schema);
|
280 |
+
|
281 |
+
/// \brief Get a stream of IPC payloads to put on the wire
|
282 |
+
/// \param[in] context The call context.
|
283 |
+
/// \param[in] request an opaque ticket
|
284 |
+
/// \param[out] stream the returned stream provider
|
285 |
+
/// \return Status
|
286 |
+
virtual Status DoGet(const ServerCallContext& context, const Ticket& request,
|
287 |
+
std::unique_ptr<FlightDataStream>* stream);
|
288 |
+
|
289 |
+
/// \brief Process a stream of IPC payloads sent from a client
|
290 |
+
/// \param[in] context The call context.
|
291 |
+
/// \param[in] reader a sequence of uploaded record batches
|
292 |
+
/// \param[in] writer send metadata back to the client
|
293 |
+
/// \return Status
|
294 |
+
virtual Status DoPut(const ServerCallContext& context,
|
295 |
+
std::unique_ptr<FlightMessageReader> reader,
|
296 |
+
std::unique_ptr<FlightMetadataWriter> writer);
|
297 |
+
|
298 |
+
/// \brief Process a bidirectional stream of IPC payloads
|
299 |
+
/// \param[in] context The call context.
|
300 |
+
/// \param[in] reader a sequence of uploaded record batches
|
301 |
+
/// \param[in] writer send data back to the client
|
302 |
+
/// \return Status
|
303 |
+
virtual Status DoExchange(const ServerCallContext& context,
|
304 |
+
std::unique_ptr<FlightMessageReader> reader,
|
305 |
+
std::unique_ptr<FlightMessageWriter> writer);
|
306 |
+
|
307 |
+
/// \brief Execute an action, return stream of zero or more results
|
308 |
+
/// \param[in] context The call context.
|
309 |
+
/// \param[in] action the action to execute, with type and body
|
310 |
+
/// \param[out] result the result iterator
|
311 |
+
/// \return Status
|
312 |
+
virtual Status DoAction(const ServerCallContext& context, const Action& action,
|
313 |
+
std::unique_ptr<ResultStream>* result);
|
314 |
+
|
315 |
+
/// \brief Retrieve the list of available actions
|
316 |
+
/// \param[in] context The call context.
|
317 |
+
/// \param[out] actions a vector of available action types
|
318 |
+
/// \return Status
|
319 |
+
virtual Status ListActions(const ServerCallContext& context,
|
320 |
+
std::vector<ActionType>* actions);
|
321 |
+
|
322 |
+
private:
|
323 |
+
struct Impl;
|
324 |
+
std::unique_ptr<Impl> impl_;
|
325 |
+
};
|
326 |
+
|
327 |
+
} // namespace flight
|
328 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server_middleware.h
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Interfaces for defining middleware for Flight servers. Currently
|
19 |
+
// experimental.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <memory>
|
24 |
+
#include <string>
|
25 |
+
|
26 |
+
#include "arrow/flight/middleware.h"
|
27 |
+
#include "arrow/flight/type_fwd.h"
|
28 |
+
#include "arrow/flight/visibility.h" // IWYU pragma: keep
|
29 |
+
#include "arrow/status.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
namespace flight {
|
33 |
+
|
34 |
+
/// \brief Server-side middleware for a call, instantiated per RPC.
|
35 |
+
///
|
36 |
+
/// Middleware should be fast and must be infallible: there is no way
|
37 |
+
/// to reject the call or report errors from the middleware instance.
|
38 |
+
class ARROW_FLIGHT_EXPORT ServerMiddleware {
|
39 |
+
public:
|
40 |
+
virtual ~ServerMiddleware() = default;
|
41 |
+
|
42 |
+
/// \brief Unique name of middleware, used as alternative to RTTI
|
43 |
+
/// \return the string name of the middleware
|
44 |
+
virtual std::string name() const = 0;
|
45 |
+
|
46 |
+
/// \brief A callback before headers are sent. Extra headers can be
|
47 |
+
/// added, but existing ones cannot be read.
|
48 |
+
virtual void SendingHeaders(AddCallHeaders* outgoing_headers) = 0;
|
49 |
+
|
50 |
+
/// \brief A callback after the call has completed.
|
51 |
+
virtual void CallCompleted(const Status& status) = 0;
|
52 |
+
};
|
53 |
+
|
54 |
+
/// \brief A factory for new middleware instances.
|
55 |
+
///
|
56 |
+
/// If added to a server, this will be called for each RPC (including
|
57 |
+
/// Handshake) to give the opportunity to intercept the call.
|
58 |
+
///
|
59 |
+
/// It is guaranteed that all server middleware methods are called
|
60 |
+
/// from the same thread that calls the RPC method implementation.
|
61 |
+
class ARROW_FLIGHT_EXPORT ServerMiddlewareFactory {
|
62 |
+
public:
|
63 |
+
virtual ~ServerMiddlewareFactory() = default;
|
64 |
+
|
65 |
+
/// \brief A callback for the start of a new call.
|
66 |
+
///
|
67 |
+
/// Return a non-OK status to reject the call with the given status.
|
68 |
+
///
|
69 |
+
/// \param[in] info Information about the call.
|
70 |
+
/// \param[in] context The call context.
|
71 |
+
/// \param[out] middleware The middleware instance for this call. If
|
72 |
+
/// null, no middleware will be added to this call instance from
|
73 |
+
/// this factory.
|
74 |
+
/// \return Status A non-OK status will reject the call with the
|
75 |
+
/// given status. Middleware previously in the chain will have
|
76 |
+
/// their CallCompleted callback called. Other middleware
|
77 |
+
/// factories will not be called.
|
78 |
+
virtual Status StartCall(const CallInfo& info, const ServerCallContext& context,
|
79 |
+
std::shared_ptr<ServerMiddleware>* middleware);
|
80 |
+
|
81 |
+
/// \brief A callback for the start of a new call.
|
82 |
+
///
|
83 |
+
/// Return a non-OK status to reject the call with the given status.
|
84 |
+
///
|
85 |
+
/// \param info Information about the call.
|
86 |
+
/// \param incoming_headers Headers sent by the client for this call.
|
87 |
+
/// Do not retain a reference to this object.
|
88 |
+
/// \param[out] middleware The middleware instance for this call. If
|
89 |
+
/// null, no middleware will be added to this call instance from
|
90 |
+
/// this factory.
|
91 |
+
/// \return Status A non-OK status will reject the call with the
|
92 |
+
/// given status. Middleware previously in the chain will have
|
93 |
+
/// their CallCompleted callback called. Other middleware
|
94 |
+
/// factories will not be called.
|
95 |
+
/// \deprecated Deprecated in 13.0.0. Implement the StartCall()
|
96 |
+
/// with ServerCallContext version instead.
|
97 |
+
ARROW_DEPRECATED("Deprecated in 13.0.0. Use ServerCallContext overload instead.")
|
98 |
+
virtual Status StartCall(const CallInfo& info, const CallHeaders& incoming_headers,
|
99 |
+
std::shared_ptr<ServerMiddleware>* middleware) {
|
100 |
+
return Status::NotImplemented(typeid(this).name(), "::StartCall() isn't implemented");
|
101 |
+
}
|
102 |
+
};
|
103 |
+
|
104 |
+
} // namespace flight
|
105 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/server_tracing_middleware.h
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Middleware implementation for propagating OpenTelemetry spans.
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <memory>
|
23 |
+
#include <string>
|
24 |
+
#include <vector>
|
25 |
+
|
26 |
+
#include "arrow/flight/server_middleware.h"
|
27 |
+
#include "arrow/flight/visibility.h"
|
28 |
+
#include "arrow/status.h"
|
29 |
+
|
30 |
+
namespace arrow {
|
31 |
+
namespace flight {
|
32 |
+
|
33 |
+
/// \brief Returns a ServerMiddlewareFactory that handles receiving OpenTelemetry spans.
|
34 |
+
ARROW_FLIGHT_EXPORT std::shared_ptr<ServerMiddlewareFactory>
|
35 |
+
MakeTracingServerMiddlewareFactory();
|
36 |
+
|
37 |
+
/// \brief A server middleware that provides access to the
|
38 |
+
/// OpenTelemetry context, if present.
|
39 |
+
///
|
40 |
+
/// Used to make the OpenTelemetry span available in Python.
|
41 |
+
class ARROW_FLIGHT_EXPORT TracingServerMiddleware : public ServerMiddleware {
|
42 |
+
public:
|
43 |
+
~TracingServerMiddleware();
|
44 |
+
|
45 |
+
static constexpr char const kMiddlewareName[] =
|
46 |
+
"arrow::flight::TracingServerMiddleware";
|
47 |
+
|
48 |
+
std::string name() const override { return kMiddlewareName; }
|
49 |
+
void SendingHeaders(AddCallHeaders*) override;
|
50 |
+
void CallCompleted(const Status&) override;
|
51 |
+
|
52 |
+
struct TraceKey {
|
53 |
+
std::string key;
|
54 |
+
std::string value;
|
55 |
+
};
|
56 |
+
/// \brief Get the trace context.
|
57 |
+
std::vector<TraceKey> GetTraceContext() const;
|
58 |
+
|
59 |
+
private:
|
60 |
+
class Impl;
|
61 |
+
friend class TracingServerMiddlewareFactory;
|
62 |
+
|
63 |
+
explicit TracingServerMiddleware(std::unique_ptr<Impl> impl);
|
64 |
+
std::unique_ptr<Impl> impl_;
|
65 |
+
};
|
66 |
+
|
67 |
+
} // namespace flight
|
68 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/test_definitions.h
ADDED
@@ -0,0 +1,317 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Common test definitions for Flight. Individual transport
|
19 |
+
// implementations can instantiate these tests.
|
20 |
+
//
|
21 |
+
// While Googletest's value-parameterized tests would be a more
|
22 |
+
// natural way to do this, they cause runtime issues on MinGW/MSVC
|
23 |
+
// (Googletest thinks the test suite has been defined twice).
|
24 |
+
|
25 |
+
#pragma once
|
26 |
+
|
27 |
+
#include <functional>
|
28 |
+
#include <memory>
|
29 |
+
#include <string>
|
30 |
+
#include <type_traits>
|
31 |
+
#include <vector>
|
32 |
+
|
33 |
+
#include "arrow/flight/server.h"
|
34 |
+
#include "arrow/flight/types.h"
|
35 |
+
#include "arrow/util/macros.h"
|
36 |
+
|
37 |
+
namespace arrow {
|
38 |
+
namespace flight {
|
39 |
+
|
40 |
+
class ARROW_FLIGHT_EXPORT FlightTest {
|
41 |
+
protected:
|
42 |
+
virtual std::string transport() const = 0;
|
43 |
+
virtual bool supports_async() const { return false; }
|
44 |
+
virtual void SetUpTest() {}
|
45 |
+
virtual void TearDownTest() {}
|
46 |
+
};
|
47 |
+
|
48 |
+
/// Common tests of startup/shutdown
|
49 |
+
class ARROW_FLIGHT_EXPORT ConnectivityTest : public FlightTest {
|
50 |
+
public:
|
51 |
+
// Test methods
|
52 |
+
void TestGetPort();
|
53 |
+
void TestBuilderHook();
|
54 |
+
void TestShutdown();
|
55 |
+
void TestShutdownWithDeadline();
|
56 |
+
void TestBrokenConnection();
|
57 |
+
};
|
58 |
+
|
59 |
+
#define ARROW_FLIGHT_TEST_CONNECTIVITY(FIXTURE) \
|
60 |
+
static_assert(std::is_base_of<ConnectivityTest, FIXTURE>::value, \
|
61 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from ConnectivityTest"); \
|
62 |
+
TEST_F(FIXTURE, GetPort) { TestGetPort(); } \
|
63 |
+
TEST_F(FIXTURE, BuilderHook) { TestBuilderHook(); } \
|
64 |
+
TEST_F(FIXTURE, Shutdown) { TestShutdown(); } \
|
65 |
+
TEST_F(FIXTURE, ShutdownWithDeadline) { TestShutdownWithDeadline(); } \
|
66 |
+
TEST_F(FIXTURE, BrokenConnection) { TestBrokenConnection(); }
|
67 |
+
|
68 |
+
/// Common tests of data plane methods
|
69 |
+
class ARROW_FLIGHT_EXPORT DataTest : public FlightTest {
|
70 |
+
public:
|
71 |
+
void SetUpTest() override;
|
72 |
+
void TearDownTest() override;
|
73 |
+
Status ConnectClient();
|
74 |
+
|
75 |
+
// Test methods
|
76 |
+
void TestDoGetInts();
|
77 |
+
void TestDoGetFloats();
|
78 |
+
void TestDoGetDicts();
|
79 |
+
void TestDoGetLargeBatch();
|
80 |
+
void TestFlightDataStreamError();
|
81 |
+
void TestOverflowServerBatch();
|
82 |
+
void TestOverflowClientBatch();
|
83 |
+
void TestDoExchange();
|
84 |
+
void TestDoExchangeNoData();
|
85 |
+
void TestDoExchangeWriteOnlySchema();
|
86 |
+
void TestDoExchangeGet();
|
87 |
+
void TestDoExchangePut();
|
88 |
+
void TestDoExchangeEcho();
|
89 |
+
void TestDoExchangeTotal();
|
90 |
+
void TestDoExchangeError();
|
91 |
+
void TestDoExchangeConcurrency();
|
92 |
+
void TestDoExchangeUndrained();
|
93 |
+
void TestIssue5095();
|
94 |
+
|
95 |
+
private:
|
96 |
+
void CheckDoGet(
|
97 |
+
const FlightDescriptor& descr, const RecordBatchVector& expected_batches,
|
98 |
+
std::function<void(const std::vector<FlightEndpoint>&)> check_endpoints);
|
99 |
+
void CheckDoGet(const Ticket& ticket, const RecordBatchVector& expected_batches);
|
100 |
+
|
101 |
+
std::unique_ptr<FlightClient> client_;
|
102 |
+
std::unique_ptr<FlightServerBase> server_;
|
103 |
+
};
|
104 |
+
|
105 |
+
#define ARROW_FLIGHT_TEST_DATA(FIXTURE) \
|
106 |
+
static_assert(std::is_base_of<DataTest, FIXTURE>::value, \
|
107 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from DataTest"); \
|
108 |
+
TEST_F(FIXTURE, TestDoGetInts) { TestDoGetInts(); } \
|
109 |
+
TEST_F(FIXTURE, TestDoGetFloats) { TestDoGetFloats(); } \
|
110 |
+
TEST_F(FIXTURE, TestDoGetDicts) { TestDoGetDicts(); } \
|
111 |
+
TEST_F(FIXTURE, TestDoGetLargeBatch) { TestDoGetLargeBatch(); } \
|
112 |
+
TEST_F(FIXTURE, TestFlightDataStreamError) { TestFlightDataStreamError(); } \
|
113 |
+
TEST_F(FIXTURE, TestOverflowServerBatch) { TestOverflowServerBatch(); } \
|
114 |
+
TEST_F(FIXTURE, TestOverflowClientBatch) { TestOverflowClientBatch(); } \
|
115 |
+
TEST_F(FIXTURE, TestDoExchange) { TestDoExchange(); } \
|
116 |
+
TEST_F(FIXTURE, TestDoExchangeNoData) { TestDoExchangeNoData(); } \
|
117 |
+
TEST_F(FIXTURE, TestDoExchangeWriteOnlySchema) { TestDoExchangeWriteOnlySchema(); } \
|
118 |
+
TEST_F(FIXTURE, TestDoExchangeGet) { TestDoExchangeGet(); } \
|
119 |
+
TEST_F(FIXTURE, TestDoExchangePut) { TestDoExchangePut(); } \
|
120 |
+
TEST_F(FIXTURE, TestDoExchangeEcho) { TestDoExchangeEcho(); } \
|
121 |
+
TEST_F(FIXTURE, TestDoExchangeTotal) { TestDoExchangeTotal(); } \
|
122 |
+
TEST_F(FIXTURE, TestDoExchangeError) { TestDoExchangeError(); } \
|
123 |
+
TEST_F(FIXTURE, TestDoExchangeConcurrency) { TestDoExchangeConcurrency(); } \
|
124 |
+
TEST_F(FIXTURE, TestDoExchangeUndrained) { TestDoExchangeUndrained(); } \
|
125 |
+
TEST_F(FIXTURE, TestIssue5095) { TestIssue5095(); }
|
126 |
+
|
127 |
+
/// \brief Specific tests of DoPut.
|
128 |
+
class ARROW_FLIGHT_EXPORT DoPutTest : public FlightTest {
|
129 |
+
public:
|
130 |
+
void SetUpTest() override;
|
131 |
+
void TearDownTest() override;
|
132 |
+
void CheckBatches(const FlightDescriptor& expected_descriptor,
|
133 |
+
const RecordBatchVector& expected_batches);
|
134 |
+
void CheckDoPut(const FlightDescriptor& descr, const std::shared_ptr<Schema>& schema,
|
135 |
+
const RecordBatchVector& batches);
|
136 |
+
|
137 |
+
// Test methods
|
138 |
+
void TestInts();
|
139 |
+
void TestFloats();
|
140 |
+
void TestEmptyBatch();
|
141 |
+
void TestDicts();
|
142 |
+
void TestLargeBatch();
|
143 |
+
void TestSizeLimit();
|
144 |
+
void TestUndrained();
|
145 |
+
|
146 |
+
private:
|
147 |
+
std::unique_ptr<FlightClient> client_;
|
148 |
+
std::unique_ptr<FlightServerBase> server_;
|
149 |
+
};
|
150 |
+
|
151 |
+
#define ARROW_FLIGHT_TEST_DO_PUT(FIXTURE) \
|
152 |
+
static_assert(std::is_base_of<DoPutTest, FIXTURE>::value, \
|
153 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from DoPutTest"); \
|
154 |
+
TEST_F(FIXTURE, TestInts) { TestInts(); } \
|
155 |
+
TEST_F(FIXTURE, TestFloats) { TestFloats(); } \
|
156 |
+
TEST_F(FIXTURE, TestEmptyBatch) { TestEmptyBatch(); } \
|
157 |
+
TEST_F(FIXTURE, TestDicts) { TestDicts(); } \
|
158 |
+
TEST_F(FIXTURE, TestLargeBatch) { TestLargeBatch(); } \
|
159 |
+
TEST_F(FIXTURE, TestSizeLimit) { TestSizeLimit(); } \
|
160 |
+
TEST_F(FIXTURE, TestUndrained) { TestUndrained(); }
|
161 |
+
|
162 |
+
class ARROW_FLIGHT_EXPORT AppMetadataTestServer : public FlightServerBase {
|
163 |
+
public:
|
164 |
+
virtual ~AppMetadataTestServer() = default;
|
165 |
+
|
166 |
+
Status DoGet(const ServerCallContext& context, const Ticket& request,
|
167 |
+
std::unique_ptr<FlightDataStream>* data_stream) override;
|
168 |
+
|
169 |
+
Status DoPut(const ServerCallContext& context,
|
170 |
+
std::unique_ptr<FlightMessageReader> reader,
|
171 |
+
std::unique_ptr<FlightMetadataWriter> writer) override;
|
172 |
+
};
|
173 |
+
|
174 |
+
/// \brief Tests of app_metadata in data plane methods.
|
175 |
+
class ARROW_FLIGHT_EXPORT AppMetadataTest : public FlightTest {
|
176 |
+
public:
|
177 |
+
void SetUpTest() override;
|
178 |
+
void TearDownTest() override;
|
179 |
+
|
180 |
+
// Test methods
|
181 |
+
void TestDoGet();
|
182 |
+
void TestDoGetDictionaries();
|
183 |
+
void TestDoPut();
|
184 |
+
void TestDoPutDictionaries();
|
185 |
+
void TestDoPutReadMetadata();
|
186 |
+
|
187 |
+
private:
|
188 |
+
std::unique_ptr<FlightClient> client_;
|
189 |
+
std::unique_ptr<FlightServerBase> server_;
|
190 |
+
};
|
191 |
+
|
192 |
+
#define ARROW_FLIGHT_TEST_APP_METADATA(FIXTURE) \
|
193 |
+
static_assert(std::is_base_of<AppMetadataTest, FIXTURE>::value, \
|
194 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from AppMetadataTest"); \
|
195 |
+
TEST_F(FIXTURE, TestDoGet) { TestDoGet(); } \
|
196 |
+
TEST_F(FIXTURE, TestDoGetDictionaries) { TestDoGetDictionaries(); } \
|
197 |
+
TEST_F(FIXTURE, TestDoPut) { TestDoPut(); } \
|
198 |
+
TEST_F(FIXTURE, TestDoPutDictionaries) { TestDoPutDictionaries(); } \
|
199 |
+
TEST_F(FIXTURE, TestDoPutReadMetadata) { TestDoPutReadMetadata(); }
|
200 |
+
|
201 |
+
/// \brief Tests of IPC options in data plane methods.
|
202 |
+
class ARROW_FLIGHT_EXPORT IpcOptionsTest : public FlightTest {
|
203 |
+
public:
|
204 |
+
void SetUpTest() override;
|
205 |
+
void TearDownTest() override;
|
206 |
+
|
207 |
+
// Test methods
|
208 |
+
void TestDoGetReadOptions();
|
209 |
+
void TestDoPutWriteOptions();
|
210 |
+
void TestDoExchangeClientWriteOptions();
|
211 |
+
void TestDoExchangeClientWriteOptionsBegin();
|
212 |
+
void TestDoExchangeServerWriteOptions();
|
213 |
+
|
214 |
+
private:
|
215 |
+
std::unique_ptr<FlightClient> client_;
|
216 |
+
std::unique_ptr<FlightServerBase> server_;
|
217 |
+
};
|
218 |
+
|
219 |
+
#define ARROW_FLIGHT_TEST_IPC_OPTIONS(FIXTURE) \
|
220 |
+
static_assert(std::is_base_of<IpcOptionsTest, FIXTURE>::value, \
|
221 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from IpcOptionsTest"); \
|
222 |
+
TEST_F(FIXTURE, TestDoGetReadOptions) { TestDoGetReadOptions(); } \
|
223 |
+
TEST_F(FIXTURE, TestDoPutWriteOptions) { TestDoPutWriteOptions(); } \
|
224 |
+
TEST_F(FIXTURE, TestDoExchangeClientWriteOptions) { \
|
225 |
+
TestDoExchangeClientWriteOptions(); \
|
226 |
+
} \
|
227 |
+
TEST_F(FIXTURE, TestDoExchangeClientWriteOptionsBegin) { \
|
228 |
+
TestDoExchangeClientWriteOptionsBegin(); \
|
229 |
+
} \
|
230 |
+
TEST_F(FIXTURE, TestDoExchangeServerWriteOptions) { \
|
231 |
+
TestDoExchangeServerWriteOptions(); \
|
232 |
+
}
|
233 |
+
|
234 |
+
/// \brief Tests of data plane methods with CUDA memory.
|
235 |
+
///
|
236 |
+
/// If not built with ARROW_CUDA, tests are no-ops.
|
237 |
+
class ARROW_FLIGHT_EXPORT CudaDataTest : public FlightTest {
|
238 |
+
public:
|
239 |
+
void SetUpTest() override;
|
240 |
+
void TearDownTest() override;
|
241 |
+
|
242 |
+
// Test methods
|
243 |
+
void TestDoGet();
|
244 |
+
void TestDoPut();
|
245 |
+
void TestDoExchange();
|
246 |
+
|
247 |
+
private:
|
248 |
+
class Impl;
|
249 |
+
std::unique_ptr<FlightClient> client_;
|
250 |
+
std::unique_ptr<FlightServerBase> server_;
|
251 |
+
std::shared_ptr<Impl> impl_;
|
252 |
+
};
|
253 |
+
|
254 |
+
#define ARROW_FLIGHT_TEST_CUDA_DATA(FIXTURE) \
|
255 |
+
static_assert(std::is_base_of<CudaDataTest, FIXTURE>::value, \
|
256 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from CudaDataTest"); \
|
257 |
+
TEST_F(FIXTURE, TestDoGet) { TestDoGet(); } \
|
258 |
+
TEST_F(FIXTURE, TestDoPut) { TestDoPut(); } \
|
259 |
+
TEST_F(FIXTURE, TestDoExchange) { TestDoExchange(); }
|
260 |
+
|
261 |
+
/// \brief Tests of error handling.
|
262 |
+
class ARROW_FLIGHT_EXPORT ErrorHandlingTest : public FlightTest {
|
263 |
+
public:
|
264 |
+
void SetUpTest() override;
|
265 |
+
void TearDownTest() override;
|
266 |
+
|
267 |
+
// Test methods
|
268 |
+
void TestGetFlightInfo();
|
269 |
+
void TestGetFlightInfoMetadata();
|
270 |
+
void TestAsyncGetFlightInfo();
|
271 |
+
void TestDoPut();
|
272 |
+
void TestDoExchange();
|
273 |
+
|
274 |
+
protected:
|
275 |
+
struct Impl;
|
276 |
+
|
277 |
+
std::vector<std::pair<std::string, std::string>> GetHeaders();
|
278 |
+
|
279 |
+
std::shared_ptr<Impl> impl_;
|
280 |
+
std::unique_ptr<FlightClient> client_;
|
281 |
+
std::unique_ptr<FlightServerBase> server_;
|
282 |
+
};
|
283 |
+
|
284 |
+
#define ARROW_FLIGHT_TEST_ERROR_HANDLING(FIXTURE) \
|
285 |
+
static_assert(std::is_base_of<ErrorHandlingTest, FIXTURE>::value, \
|
286 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from ErrorHandlingTest"); \
|
287 |
+
TEST_F(FIXTURE, TestAsyncGetFlightInfo) { TestAsyncGetFlightInfo(); } \
|
288 |
+
TEST_F(FIXTURE, TestGetFlightInfo) { TestGetFlightInfo(); } \
|
289 |
+
TEST_F(FIXTURE, TestGetFlightInfoMetadata) { TestGetFlightInfoMetadata(); } \
|
290 |
+
TEST_F(FIXTURE, TestDoPut) { TestDoPut(); } \
|
291 |
+
TEST_F(FIXTURE, TestDoExchange) { TestDoExchange(); }
|
292 |
+
|
293 |
+
/// \brief Tests of the async client.
|
294 |
+
class ARROW_FLIGHT_EXPORT AsyncClientTest : public FlightTest {
|
295 |
+
public:
|
296 |
+
void SetUpTest() override;
|
297 |
+
void TearDownTest() override;
|
298 |
+
|
299 |
+
// Test methods
|
300 |
+
void TestGetFlightInfo();
|
301 |
+
void TestGetFlightInfoFuture();
|
302 |
+
void TestListenerLifetime();
|
303 |
+
|
304 |
+
private:
|
305 |
+
std::unique_ptr<FlightClient> client_;
|
306 |
+
std::unique_ptr<FlightServerBase> server_;
|
307 |
+
};
|
308 |
+
|
309 |
+
#define ARROW_FLIGHT_TEST_ASYNC_CLIENT(FIXTURE) \
|
310 |
+
static_assert(std::is_base_of<AsyncClientTest, FIXTURE>::value, \
|
311 |
+
ARROW_STRINGIFY(FIXTURE) " must inherit from AsyncClientTest"); \
|
312 |
+
TEST_F(FIXTURE, TestGetFlightInfo) { TestGetFlightInfo(); } \
|
313 |
+
TEST_F(FIXTURE, TestGetFlightInfoFuture) { TestGetFlightInfoFuture(); } \
|
314 |
+
TEST_F(FIXTURE, TestListenerLifetime) { TestListenerLifetime(); }
|
315 |
+
|
316 |
+
} // namespace flight
|
317 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/test_util.h
ADDED
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <gmock/gmock.h>
|
21 |
+
#include <gtest/gtest.h>
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
#include <string>
|
26 |
+
#include <thread>
|
27 |
+
#include <utility>
|
28 |
+
#include <vector>
|
29 |
+
|
30 |
+
#include "arrow/status.h"
|
31 |
+
#include "arrow/testing/gtest_util.h"
|
32 |
+
#include "arrow/testing/util.h"
|
33 |
+
|
34 |
+
#include "arrow/flight/client.h"
|
35 |
+
#include "arrow/flight/client_auth.h"
|
36 |
+
#include "arrow/flight/server.h"
|
37 |
+
#include "arrow/flight/server_auth.h"
|
38 |
+
#include "arrow/flight/types.h"
|
39 |
+
#include "arrow/flight/visibility.h"
|
40 |
+
|
41 |
+
namespace boost {
|
42 |
+
namespace process {
|
43 |
+
|
44 |
+
class child;
|
45 |
+
|
46 |
+
} // namespace process
|
47 |
+
} // namespace boost
|
48 |
+
|
49 |
+
namespace arrow {
|
50 |
+
namespace flight {
|
51 |
+
|
52 |
+
// ----------------------------------------------------------------------
|
53 |
+
// Helpers to compare values for equality
|
54 |
+
|
55 |
+
inline void AssertEqual(const FlightInfo& expected, const FlightInfo& actual) {
|
56 |
+
ipc::DictionaryMemo expected_memo;
|
57 |
+
ipc::DictionaryMemo actual_memo;
|
58 |
+
ASSERT_OK_AND_ASSIGN(auto ex_schema, expected.GetSchema(&expected_memo));
|
59 |
+
ASSERT_OK_AND_ASSIGN(auto actual_schema, actual.GetSchema(&actual_memo));
|
60 |
+
|
61 |
+
AssertSchemaEqual(*ex_schema, *actual_schema);
|
62 |
+
ASSERT_EQ(expected.total_records(), actual.total_records());
|
63 |
+
ASSERT_EQ(expected.total_bytes(), actual.total_bytes());
|
64 |
+
|
65 |
+
ASSERT_EQ(expected.descriptor(), actual.descriptor());
|
66 |
+
ASSERT_THAT(actual.endpoints(), ::testing::ContainerEq(expected.endpoints()));
|
67 |
+
}
|
68 |
+
|
69 |
+
// ----------------------------------------------------------------------
|
70 |
+
// Fixture to use for running test servers
|
71 |
+
|
72 |
+
class ARROW_FLIGHT_EXPORT TestServer {
|
73 |
+
public:
|
74 |
+
explicit TestServer(const std::string& executable_name)
|
75 |
+
: executable_name_(executable_name), port_(::arrow::GetListenPort()) {}
|
76 |
+
TestServer(const std::string& executable_name, int port)
|
77 |
+
: executable_name_(executable_name), port_(port) {}
|
78 |
+
TestServer(const std::string& executable_name, const std::string& unix_sock)
|
79 |
+
: executable_name_(executable_name), unix_sock_(unix_sock) {}
|
80 |
+
|
81 |
+
void Start(const std::vector<std::string>& extra_args);
|
82 |
+
void Start() { Start({}); }
|
83 |
+
|
84 |
+
int Stop();
|
85 |
+
|
86 |
+
bool IsRunning();
|
87 |
+
|
88 |
+
int port() const;
|
89 |
+
const std::string& unix_sock() const;
|
90 |
+
|
91 |
+
private:
|
92 |
+
std::string executable_name_;
|
93 |
+
int port_;
|
94 |
+
std::string unix_sock_;
|
95 |
+
std::shared_ptr<::boost::process::child> server_process_;
|
96 |
+
};
|
97 |
+
|
98 |
+
/// \brief Create a simple Flight server for testing
|
99 |
+
ARROW_FLIGHT_EXPORT
|
100 |
+
std::unique_ptr<FlightServerBase> ExampleTestServer();
|
101 |
+
|
102 |
+
// Helper to initialize a server and matching client with callbacks to
|
103 |
+
// populate options.
|
104 |
+
template <typename T, typename... Args>
|
105 |
+
Status MakeServer(const Location& location, std::unique_ptr<FlightServerBase>* server,
|
106 |
+
std::unique_ptr<FlightClient>* client,
|
107 |
+
std::function<Status(FlightServerOptions*)> make_server_options,
|
108 |
+
std::function<Status(FlightClientOptions*)> make_client_options,
|
109 |
+
Args&&... server_args) {
|
110 |
+
*server = std::make_unique<T>(std::forward<Args>(server_args)...);
|
111 |
+
FlightServerOptions server_options(location);
|
112 |
+
RETURN_NOT_OK(make_server_options(&server_options));
|
113 |
+
RETURN_NOT_OK((*server)->Init(server_options));
|
114 |
+
std::string uri =
|
115 |
+
location.scheme() + "://127.0.0.1:" + std::to_string((*server)->port());
|
116 |
+
ARROW_ASSIGN_OR_RAISE(auto real_location, Location::Parse(uri));
|
117 |
+
FlightClientOptions client_options = FlightClientOptions::Defaults();
|
118 |
+
RETURN_NOT_OK(make_client_options(&client_options));
|
119 |
+
return FlightClient::Connect(real_location, client_options).Value(client);
|
120 |
+
}
|
121 |
+
|
122 |
+
// Helper to initialize a server and matching client with callbacks to
|
123 |
+
// populate options.
|
124 |
+
template <typename T, typename... Args>
|
125 |
+
Status MakeServer(std::unique_ptr<FlightServerBase>* server,
|
126 |
+
std::unique_ptr<FlightClient>* client,
|
127 |
+
std::function<Status(FlightServerOptions*)> make_server_options,
|
128 |
+
std::function<Status(FlightClientOptions*)> make_client_options,
|
129 |
+
Args&&... server_args) {
|
130 |
+
ARROW_ASSIGN_OR_RAISE(auto location, Location::ForGrpcTcp("localhost", 0));
|
131 |
+
return MakeServer<T>(location, server, client, std::move(make_server_options),
|
132 |
+
std::move(make_client_options),
|
133 |
+
std::forward<Args>(server_args)...);
|
134 |
+
}
|
135 |
+
|
136 |
+
// ----------------------------------------------------------------------
|
137 |
+
// A FlightDataStream that numbers the record batches
|
138 |
+
/// \brief A basic implementation of FlightDataStream that will provide
|
139 |
+
/// a sequence of FlightData messages to be written to a stream
|
140 |
+
class ARROW_FLIGHT_EXPORT NumberingStream : public FlightDataStream {
|
141 |
+
public:
|
142 |
+
explicit NumberingStream(std::unique_ptr<FlightDataStream> stream);
|
143 |
+
|
144 |
+
std::shared_ptr<Schema> schema() override;
|
145 |
+
arrow::Result<FlightPayload> GetSchemaPayload() override;
|
146 |
+
arrow::Result<FlightPayload> Next() override;
|
147 |
+
|
148 |
+
private:
|
149 |
+
int counter_;
|
150 |
+
std::shared_ptr<FlightDataStream> stream_;
|
151 |
+
};
|
152 |
+
|
153 |
+
// ----------------------------------------------------------------------
|
154 |
+
// Example data for test-server and unit tests
|
155 |
+
|
156 |
+
ARROW_FLIGHT_EXPORT
|
157 |
+
std::shared_ptr<Schema> ExampleIntSchema();
|
158 |
+
|
159 |
+
ARROW_FLIGHT_EXPORT
|
160 |
+
std::shared_ptr<Schema> ExampleStringSchema();
|
161 |
+
|
162 |
+
ARROW_FLIGHT_EXPORT
|
163 |
+
std::shared_ptr<Schema> ExampleDictSchema();
|
164 |
+
|
165 |
+
ARROW_FLIGHT_EXPORT
|
166 |
+
std::shared_ptr<Schema> ExampleLargeSchema();
|
167 |
+
|
168 |
+
ARROW_FLIGHT_EXPORT
|
169 |
+
Status ExampleIntBatches(RecordBatchVector* out);
|
170 |
+
|
171 |
+
ARROW_FLIGHT_EXPORT
|
172 |
+
Status ExampleFloatBatches(RecordBatchVector* out);
|
173 |
+
|
174 |
+
ARROW_FLIGHT_EXPORT
|
175 |
+
Status ExampleDictBatches(RecordBatchVector* out);
|
176 |
+
|
177 |
+
ARROW_FLIGHT_EXPORT
|
178 |
+
Status ExampleNestedBatches(RecordBatchVector* out);
|
179 |
+
|
180 |
+
ARROW_FLIGHT_EXPORT
|
181 |
+
Status ExampleLargeBatches(RecordBatchVector* out);
|
182 |
+
|
183 |
+
ARROW_FLIGHT_EXPORT
|
184 |
+
arrow::Result<std::shared_ptr<RecordBatch>> VeryLargeBatch();
|
185 |
+
|
186 |
+
ARROW_FLIGHT_EXPORT
|
187 |
+
std::vector<FlightInfo> ExampleFlightInfo();
|
188 |
+
|
189 |
+
ARROW_FLIGHT_EXPORT
|
190 |
+
std::vector<ActionType> ExampleActionTypes();
|
191 |
+
|
192 |
+
ARROW_FLIGHT_EXPORT
|
193 |
+
FlightInfo MakeFlightInfo(const Schema& schema, const FlightDescriptor& descriptor,
|
194 |
+
const std::vector<FlightEndpoint>& endpoints,
|
195 |
+
int64_t total_records, int64_t total_bytes, bool ordered,
|
196 |
+
std::string app_metadata);
|
197 |
+
|
198 |
+
// ----------------------------------------------------------------------
|
199 |
+
// A pair of authentication handlers that check for a predefined password
|
200 |
+
// and set the peer identity to a predefined username.
|
201 |
+
|
202 |
+
class ARROW_FLIGHT_EXPORT TestServerAuthHandler : public ServerAuthHandler {
|
203 |
+
public:
|
204 |
+
explicit TestServerAuthHandler(const std::string& username,
|
205 |
+
const std::string& password);
|
206 |
+
~TestServerAuthHandler() override;
|
207 |
+
Status Authenticate(const ServerCallContext& context, ServerAuthSender* outgoing,
|
208 |
+
ServerAuthReader* incoming) override;
|
209 |
+
Status IsValid(const ServerCallContext& context, const std::string& token,
|
210 |
+
std::string* peer_identity) override;
|
211 |
+
|
212 |
+
private:
|
213 |
+
std::string username_;
|
214 |
+
std::string password_;
|
215 |
+
};
|
216 |
+
|
217 |
+
class ARROW_FLIGHT_EXPORT TestServerBasicAuthHandler : public ServerAuthHandler {
|
218 |
+
public:
|
219 |
+
explicit TestServerBasicAuthHandler(const std::string& username,
|
220 |
+
const std::string& password);
|
221 |
+
~TestServerBasicAuthHandler() override;
|
222 |
+
Status Authenticate(const ServerCallContext& context, ServerAuthSender* outgoing,
|
223 |
+
ServerAuthReader* incoming) override;
|
224 |
+
Status IsValid(const ServerCallContext& context, const std::string& token,
|
225 |
+
std::string* peer_identity) override;
|
226 |
+
|
227 |
+
private:
|
228 |
+
BasicAuth basic_auth_;
|
229 |
+
};
|
230 |
+
|
231 |
+
class ARROW_FLIGHT_EXPORT TestClientAuthHandler : public ClientAuthHandler {
|
232 |
+
public:
|
233 |
+
explicit TestClientAuthHandler(const std::string& username,
|
234 |
+
const std::string& password);
|
235 |
+
~TestClientAuthHandler() override;
|
236 |
+
Status Authenticate(ClientAuthSender* outgoing, ClientAuthReader* incoming) override;
|
237 |
+
Status GetToken(std::string* token) override;
|
238 |
+
|
239 |
+
private:
|
240 |
+
std::string username_;
|
241 |
+
std::string password_;
|
242 |
+
};
|
243 |
+
|
244 |
+
class ARROW_FLIGHT_EXPORT TestClientBasicAuthHandler : public ClientAuthHandler {
|
245 |
+
public:
|
246 |
+
explicit TestClientBasicAuthHandler(const std::string& username,
|
247 |
+
const std::string& password);
|
248 |
+
~TestClientBasicAuthHandler() override;
|
249 |
+
Status Authenticate(ClientAuthSender* outgoing, ClientAuthReader* incoming) override;
|
250 |
+
Status GetToken(std::string* token) override;
|
251 |
+
|
252 |
+
private:
|
253 |
+
BasicAuth basic_auth_;
|
254 |
+
std::string token_;
|
255 |
+
};
|
256 |
+
|
257 |
+
ARROW_FLIGHT_EXPORT
|
258 |
+
Status ExampleTlsCertificates(std::vector<CertKeyPair>* out);
|
259 |
+
|
260 |
+
ARROW_FLIGHT_EXPORT
|
261 |
+
Status ExampleTlsCertificateRoot(CertKeyPair* out);
|
262 |
+
|
263 |
+
} // namespace flight
|
264 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/types.h
ADDED
@@ -0,0 +1,1147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Data structure for Flight RPC. API should be considered experimental for now
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <chrono>
|
23 |
+
#include <cstddef>
|
24 |
+
#include <cstdint>
|
25 |
+
#include <map>
|
26 |
+
#include <memory>
|
27 |
+
#include <optional>
|
28 |
+
#include <string>
|
29 |
+
#include <string_view>
|
30 |
+
#include <utility>
|
31 |
+
#include <variant>
|
32 |
+
#include <vector>
|
33 |
+
|
34 |
+
#include "arrow/flight/type_fwd.h"
|
35 |
+
#include "arrow/flight/visibility.h"
|
36 |
+
#include "arrow/ipc/options.h"
|
37 |
+
#include "arrow/ipc/writer.h"
|
38 |
+
#include "arrow/result.h"
|
39 |
+
#include "arrow/status.h"
|
40 |
+
|
41 |
+
namespace arrow {
|
42 |
+
|
43 |
+
class Buffer;
|
44 |
+
class RecordBatch;
|
45 |
+
class Schema;
|
46 |
+
class Status;
|
47 |
+
class Table;
|
48 |
+
|
49 |
+
namespace ipc {
|
50 |
+
|
51 |
+
class DictionaryMemo;
|
52 |
+
|
53 |
+
} // namespace ipc
|
54 |
+
|
55 |
+
namespace util {
|
56 |
+
|
57 |
+
class Uri;
|
58 |
+
|
59 |
+
} // namespace util
|
60 |
+
|
61 |
+
namespace flight {
|
62 |
+
|
63 |
+
/// \brief A timestamp compatible with Protocol Buffer's
|
64 |
+
/// google.protobuf.Timestamp:
|
65 |
+
///
|
66 |
+
/// https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp
|
67 |
+
///
|
68 |
+
/// > A Timestamp represents a point in time independent of any time
|
69 |
+
/// > zone or calendar, represented as seconds and fractions of
|
70 |
+
/// > seconds at nanosecond resolution in UTC Epoch time. It is
|
71 |
+
/// > encoded using the Proleptic Gregorian Calendar which extends the
|
72 |
+
/// > Gregorian calendar backwards to year one. It is encoded assuming
|
73 |
+
/// > all minutes are 60 seconds long, i.e. leap seconds are "smeared"
|
74 |
+
/// > so that no leap second table is needed for interpretation. Range
|
75 |
+
/// > is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
|
76 |
+
using Timestamp = std::chrono::system_clock::time_point;
|
77 |
+
|
78 |
+
/// \brief A Flight-specific status code. Used to encode some
|
79 |
+
/// additional status codes into an Arrow Status.
|
80 |
+
enum class FlightStatusCode : int8_t {
|
81 |
+
/// An implementation error has occurred.
|
82 |
+
Internal,
|
83 |
+
/// A request timed out.
|
84 |
+
TimedOut,
|
85 |
+
/// A request was cancelled.
|
86 |
+
Cancelled,
|
87 |
+
/// We are not authenticated to the remote service.
|
88 |
+
Unauthenticated,
|
89 |
+
/// We do not have permission to make this request.
|
90 |
+
Unauthorized,
|
91 |
+
/// The remote service cannot handle this request at the moment.
|
92 |
+
Unavailable,
|
93 |
+
/// A request failed for some other reason
|
94 |
+
Failed
|
95 |
+
};
|
96 |
+
|
97 |
+
// Silence warning
|
98 |
+
// "non dll-interface class RecordBatchReader used as base for dll-interface class"
|
99 |
+
#ifdef _MSC_VER
|
100 |
+
#pragma warning(push)
|
101 |
+
#pragma warning(disable : 4275)
|
102 |
+
#endif
|
103 |
+
|
104 |
+
/// \brief Flight-specific error information in a Status.
|
105 |
+
class ARROW_FLIGHT_EXPORT FlightStatusDetail : public arrow::StatusDetail {
|
106 |
+
public:
|
107 |
+
explicit FlightStatusDetail(FlightStatusCode code) : code_{code} {}
|
108 |
+
explicit FlightStatusDetail(FlightStatusCode code, std::string extra_info)
|
109 |
+
: code_{code}, extra_info_(std::move(extra_info)) {}
|
110 |
+
const char* type_id() const override;
|
111 |
+
std::string ToString() const override;
|
112 |
+
|
113 |
+
/// \brief Get the Flight status code.
|
114 |
+
FlightStatusCode code() const;
|
115 |
+
/// \brief Get the extra error info
|
116 |
+
std::string extra_info() const;
|
117 |
+
/// \brief Get the human-readable name of the status code.
|
118 |
+
std::string CodeAsString() const;
|
119 |
+
/// \brief Set the extra error info
|
120 |
+
void set_extra_info(std::string extra_info);
|
121 |
+
|
122 |
+
/// \brief Try to extract a \a FlightStatusDetail from any Arrow
|
123 |
+
/// status.
|
124 |
+
///
|
125 |
+
/// \return a \a FlightStatusDetail if it could be unwrapped, \a
|
126 |
+
/// nullptr otherwise
|
127 |
+
static std::shared_ptr<FlightStatusDetail> UnwrapStatus(const arrow::Status& status);
|
128 |
+
|
129 |
+
private:
|
130 |
+
FlightStatusCode code_;
|
131 |
+
std::string extra_info_;
|
132 |
+
};
|
133 |
+
|
134 |
+
#ifdef _MSC_VER
|
135 |
+
#pragma warning(pop)
|
136 |
+
#endif
|
137 |
+
|
138 |
+
/// \brief Make an appropriate Arrow status for the given
|
139 |
+
/// Flight-specific status.
|
140 |
+
///
|
141 |
+
/// \param code The Flight status code.
|
142 |
+
/// \param message The message for the error.
|
143 |
+
/// \param extra_info Optional extra binary info for the error (eg protobuf)
|
144 |
+
ARROW_FLIGHT_EXPORT
|
145 |
+
Status MakeFlightError(FlightStatusCode code, std::string message,
|
146 |
+
std::string extra_info = {});
|
147 |
+
|
148 |
+
/// \brief Headers sent from the client or server.
|
149 |
+
///
|
150 |
+
/// Header values are ordered.
|
151 |
+
using CallHeaders = std::multimap<std::string_view, std::string_view>;
|
152 |
+
|
153 |
+
/// \brief A TLS certificate plus key.
|
154 |
+
struct ARROW_FLIGHT_EXPORT CertKeyPair {
|
155 |
+
/// \brief The certificate in PEM format.
|
156 |
+
std::string pem_cert;
|
157 |
+
|
158 |
+
/// \brief The key in PEM format.
|
159 |
+
std::string pem_key;
|
160 |
+
};
|
161 |
+
|
162 |
+
/// \brief A type of action that can be performed with the DoAction RPC.
|
163 |
+
struct ARROW_FLIGHT_EXPORT ActionType {
|
164 |
+
/// \brief The name of the action.
|
165 |
+
std::string type;
|
166 |
+
|
167 |
+
/// \brief A human-readable description of the action.
|
168 |
+
std::string description;
|
169 |
+
|
170 |
+
std::string ToString() const;
|
171 |
+
bool Equals(const ActionType& other) const;
|
172 |
+
|
173 |
+
friend bool operator==(const ActionType& left, const ActionType& right) {
|
174 |
+
return left.Equals(right);
|
175 |
+
}
|
176 |
+
friend bool operator!=(const ActionType& left, const ActionType& right) {
|
177 |
+
return !(left == right);
|
178 |
+
}
|
179 |
+
|
180 |
+
/// \brief Serialize this message to its wire-format representation.
|
181 |
+
arrow::Result<std::string> SerializeToString() const;
|
182 |
+
|
183 |
+
/// \brief Deserialize this message from its wire-format representation.
|
184 |
+
static arrow::Result<ActionType> Deserialize(std::string_view serialized);
|
185 |
+
|
186 |
+
static const ActionType kCancelFlightInfo;
|
187 |
+
static const ActionType kRenewFlightEndpoint;
|
188 |
+
static const ActionType kSetSessionOptions;
|
189 |
+
static const ActionType kGetSessionOptions;
|
190 |
+
static const ActionType kCloseSession;
|
191 |
+
};
|
192 |
+
|
193 |
+
/// \brief Opaque selection criteria for ListFlights RPC
|
194 |
+
struct ARROW_FLIGHT_EXPORT Criteria {
|
195 |
+
/// Opaque criteria expression, dependent on server implementation
|
196 |
+
std::string expression;
|
197 |
+
|
198 |
+
std::string ToString() const;
|
199 |
+
bool Equals(const Criteria& other) const;
|
200 |
+
|
201 |
+
friend bool operator==(const Criteria& left, const Criteria& right) {
|
202 |
+
return left.Equals(right);
|
203 |
+
}
|
204 |
+
friend bool operator!=(const Criteria& left, const Criteria& right) {
|
205 |
+
return !(left == right);
|
206 |
+
}
|
207 |
+
|
208 |
+
/// \brief Serialize this message to its wire-format representation.
|
209 |
+
arrow::Result<std::string> SerializeToString() const;
|
210 |
+
|
211 |
+
/// \brief Deserialize this message from its wire-format representation.
|
212 |
+
static arrow::Result<Criteria> Deserialize(std::string_view serialized);
|
213 |
+
};
|
214 |
+
|
215 |
+
/// \brief An action to perform with the DoAction RPC
|
216 |
+
struct ARROW_FLIGHT_EXPORT Action {
|
217 |
+
/// The action type
|
218 |
+
std::string type;
|
219 |
+
|
220 |
+
/// The action content as a Buffer
|
221 |
+
std::shared_ptr<Buffer> body;
|
222 |
+
|
223 |
+
std::string ToString() const;
|
224 |
+
bool Equals(const Action& other) const;
|
225 |
+
|
226 |
+
friend bool operator==(const Action& left, const Action& right) {
|
227 |
+
return left.Equals(right);
|
228 |
+
}
|
229 |
+
friend bool operator!=(const Action& left, const Action& right) {
|
230 |
+
return !(left == right);
|
231 |
+
}
|
232 |
+
|
233 |
+
/// \brief Serialize this message to its wire-format representation.
|
234 |
+
arrow::Result<std::string> SerializeToString() const;
|
235 |
+
|
236 |
+
/// \brief Deserialize this message from its wire-format representation.
|
237 |
+
static arrow::Result<Action> Deserialize(std::string_view serialized);
|
238 |
+
};
|
239 |
+
|
240 |
+
/// \brief Opaque result returned after executing an action
|
241 |
+
struct ARROW_FLIGHT_EXPORT Result {
|
242 |
+
std::shared_ptr<Buffer> body;
|
243 |
+
|
244 |
+
std::string ToString() const;
|
245 |
+
bool Equals(const Result& other) const;
|
246 |
+
|
247 |
+
friend bool operator==(const Result& left, const Result& right) {
|
248 |
+
return left.Equals(right);
|
249 |
+
}
|
250 |
+
friend bool operator!=(const Result& left, const Result& right) {
|
251 |
+
return !(left == right);
|
252 |
+
}
|
253 |
+
|
254 |
+
/// \brief Serialize this message to its wire-format representation.
|
255 |
+
arrow::Result<std::string> SerializeToString() const;
|
256 |
+
|
257 |
+
/// \brief Deserialize this message from its wire-format representation.
|
258 |
+
static arrow::Result<Result> Deserialize(std::string_view serialized);
|
259 |
+
};
|
260 |
+
|
261 |
+
enum class CancelStatus {
|
262 |
+
/// The cancellation status is unknown. Servers should avoid using
|
263 |
+
/// this value (send a kNotCancellable if the requested FlightInfo
|
264 |
+
/// is not known). Clients can retry the request.
|
265 |
+
kUnspecified = 0,
|
266 |
+
/// The cancellation request is complete. Subsequent requests with
|
267 |
+
/// the same payload may return kCancelled or a kNotCancellable error.
|
268 |
+
kCancelled = 1,
|
269 |
+
/// The cancellation request is in progress. The client may retry
|
270 |
+
/// the cancellation request.
|
271 |
+
kCancelling = 2,
|
272 |
+
// The FlightInfo is not cancellable. The client should not retry the
|
273 |
+
// cancellation request.
|
274 |
+
kNotCancellable = 3,
|
275 |
+
};
|
276 |
+
|
277 |
+
/// \brief The result of the CancelFlightInfo action.
|
278 |
+
struct ARROW_FLIGHT_EXPORT CancelFlightInfoResult {
|
279 |
+
CancelStatus status;
|
280 |
+
|
281 |
+
std::string ToString() const;
|
282 |
+
bool Equals(const CancelFlightInfoResult& other) const;
|
283 |
+
|
284 |
+
friend bool operator==(const CancelFlightInfoResult& left,
|
285 |
+
const CancelFlightInfoResult& right) {
|
286 |
+
return left.Equals(right);
|
287 |
+
}
|
288 |
+
friend bool operator!=(const CancelFlightInfoResult& left,
|
289 |
+
const CancelFlightInfoResult& right) {
|
290 |
+
return !(left == right);
|
291 |
+
}
|
292 |
+
|
293 |
+
/// \brief Serialize this message to its wire-format representation.
|
294 |
+
arrow::Result<std::string> SerializeToString() const;
|
295 |
+
|
296 |
+
/// \brief Deserialize this message from its wire-format representation.
|
297 |
+
static arrow::Result<CancelFlightInfoResult> Deserialize(std::string_view serialized);
|
298 |
+
};
|
299 |
+
|
300 |
+
ARROW_FLIGHT_EXPORT
|
301 |
+
std::ostream& operator<<(std::ostream& os, CancelStatus status);
|
302 |
+
|
303 |
+
/// \brief message for simple auth
|
304 |
+
struct ARROW_FLIGHT_EXPORT BasicAuth {
|
305 |
+
std::string username;
|
306 |
+
std::string password;
|
307 |
+
|
308 |
+
std::string ToString() const;
|
309 |
+
bool Equals(const BasicAuth& other) const;
|
310 |
+
|
311 |
+
friend bool operator==(const BasicAuth& left, const BasicAuth& right) {
|
312 |
+
return left.Equals(right);
|
313 |
+
}
|
314 |
+
friend bool operator!=(const BasicAuth& left, const BasicAuth& right) {
|
315 |
+
return !(left == right);
|
316 |
+
}
|
317 |
+
|
318 |
+
/// \brief Deserialize this message from its wire-format representation.
|
319 |
+
static arrow::Result<BasicAuth> Deserialize(std::string_view serialized);
|
320 |
+
/// \brief Serialize this message to its wire-format representation.
|
321 |
+
arrow::Result<std::string> SerializeToString() const;
|
322 |
+
};
|
323 |
+
|
324 |
+
/// \brief A request to retrieve or generate a dataset
|
325 |
+
struct ARROW_FLIGHT_EXPORT FlightDescriptor {
|
326 |
+
enum DescriptorType {
|
327 |
+
UNKNOWN = 0, /// Unused
|
328 |
+
PATH = 1, /// Named path identifying a dataset
|
329 |
+
CMD = 2 /// Opaque command to generate a dataset
|
330 |
+
};
|
331 |
+
|
332 |
+
/// The descriptor type
|
333 |
+
DescriptorType type;
|
334 |
+
|
335 |
+
/// Opaque value used to express a command. Should only be defined when type
|
336 |
+
/// is CMD
|
337 |
+
std::string cmd;
|
338 |
+
|
339 |
+
/// List of strings identifying a particular dataset. Should only be defined
|
340 |
+
/// when type is PATH
|
341 |
+
std::vector<std::string> path;
|
342 |
+
|
343 |
+
bool Equals(const FlightDescriptor& other) const;
|
344 |
+
|
345 |
+
/// \brief Get a human-readable form of this descriptor.
|
346 |
+
std::string ToString() const;
|
347 |
+
|
348 |
+
/// \brief Get the wire-format representation of this type.
|
349 |
+
///
|
350 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
351 |
+
/// services) that may want to return Flight types.
|
352 |
+
arrow::Result<std::string> SerializeToString() const;
|
353 |
+
|
354 |
+
/// \brief Parse the wire-format representation of this type.
|
355 |
+
///
|
356 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
357 |
+
/// services) that may want to return Flight types.
|
358 |
+
static arrow::Result<FlightDescriptor> Deserialize(std::string_view serialized);
|
359 |
+
|
360 |
+
// Convenience factory functions
|
361 |
+
|
362 |
+
static FlightDescriptor Command(const std::string& c) {
|
363 |
+
return FlightDescriptor{CMD, c, {}};
|
364 |
+
}
|
365 |
+
|
366 |
+
static FlightDescriptor Path(const std::vector<std::string>& p) {
|
367 |
+
return FlightDescriptor{PATH, "", p};
|
368 |
+
}
|
369 |
+
|
370 |
+
friend bool operator==(const FlightDescriptor& left, const FlightDescriptor& right) {
|
371 |
+
return left.Equals(right);
|
372 |
+
}
|
373 |
+
friend bool operator!=(const FlightDescriptor& left, const FlightDescriptor& right) {
|
374 |
+
return !(left == right);
|
375 |
+
}
|
376 |
+
};
|
377 |
+
|
378 |
+
/// \brief Data structure providing an opaque identifier or credential to use
|
379 |
+
/// when requesting a data stream with the DoGet RPC
|
380 |
+
struct ARROW_FLIGHT_EXPORT Ticket {
|
381 |
+
std::string ticket;
|
382 |
+
|
383 |
+
std::string ToString() const;
|
384 |
+
bool Equals(const Ticket& other) const;
|
385 |
+
|
386 |
+
friend bool operator==(const Ticket& left, const Ticket& right) {
|
387 |
+
return left.Equals(right);
|
388 |
+
}
|
389 |
+
friend bool operator!=(const Ticket& left, const Ticket& right) {
|
390 |
+
return !(left == right);
|
391 |
+
}
|
392 |
+
|
393 |
+
/// \brief Get the wire-format representation of this type.
|
394 |
+
///
|
395 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
396 |
+
/// services) that may want to return Flight types.
|
397 |
+
arrow::Result<std::string> SerializeToString() const;
|
398 |
+
|
399 |
+
/// \brief Parse the wire-format representation of this type.
|
400 |
+
///
|
401 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
402 |
+
/// services) that may want to return Flight types.
|
403 |
+
static arrow::Result<Ticket> Deserialize(std::string_view serialized);
|
404 |
+
};
|
405 |
+
|
406 |
+
class FlightClient;
|
407 |
+
class FlightServerBase;
|
408 |
+
|
409 |
+
ARROW_FLIGHT_EXPORT
|
410 |
+
extern const char* kSchemeGrpc;
|
411 |
+
ARROW_FLIGHT_EXPORT
|
412 |
+
extern const char* kSchemeGrpcTcp;
|
413 |
+
ARROW_FLIGHT_EXPORT
|
414 |
+
extern const char* kSchemeGrpcUnix;
|
415 |
+
ARROW_FLIGHT_EXPORT
|
416 |
+
extern const char* kSchemeGrpcTls;
|
417 |
+
|
418 |
+
/// \brief A host location (a URI)
|
419 |
+
struct ARROW_FLIGHT_EXPORT Location {
|
420 |
+
public:
|
421 |
+
/// \brief Initialize a blank location.
|
422 |
+
Location();
|
423 |
+
|
424 |
+
/// \brief Initialize a location by parsing a URI string
|
425 |
+
static arrow::Result<Location> Parse(const std::string& uri_string);
|
426 |
+
|
427 |
+
/// \brief Get the fallback URI.
|
428 |
+
///
|
429 |
+
/// arrow-flight-reuse-connection://? means that a client may attempt to
|
430 |
+
/// reuse an existing connection to a Flight service to fetch data instead
|
431 |
+
/// of creating a new connection to one of the other locations listed in a
|
432 |
+
/// FlightEndpoint response.
|
433 |
+
static const Location& ReuseConnection();
|
434 |
+
|
435 |
+
/// \brief Initialize a location for a non-TLS, gRPC-based Flight
|
436 |
+
/// service from a host and port
|
437 |
+
/// \param[in] host The hostname to connect to
|
438 |
+
/// \param[in] port The port
|
439 |
+
/// \return Arrow result with the resulting location
|
440 |
+
static arrow::Result<Location> ForGrpcTcp(const std::string& host, const int port);
|
441 |
+
|
442 |
+
/// \brief Initialize a location for a TLS-enabled, gRPC-based Flight
|
443 |
+
/// service from a host and port
|
444 |
+
/// \param[in] host The hostname to connect to
|
445 |
+
/// \param[in] port The port
|
446 |
+
/// \return Arrow result with the resulting location
|
447 |
+
static arrow::Result<Location> ForGrpcTls(const std::string& host, const int port);
|
448 |
+
|
449 |
+
/// \brief Initialize a location for a domain socket-based Flight
|
450 |
+
/// service
|
451 |
+
/// \param[in] path The path to the domain socket
|
452 |
+
/// \return Arrow result with the resulting location
|
453 |
+
static arrow::Result<Location> ForGrpcUnix(const std::string& path);
|
454 |
+
|
455 |
+
/// \brief Initialize a location based on a URI scheme
|
456 |
+
static arrow::Result<Location> ForScheme(const std::string& scheme,
|
457 |
+
const std::string& host, const int port);
|
458 |
+
|
459 |
+
/// \brief Get a representation of this URI as a string.
|
460 |
+
std::string ToString() const;
|
461 |
+
|
462 |
+
/// \brief Get the scheme of this URI.
|
463 |
+
std::string scheme() const;
|
464 |
+
|
465 |
+
bool Equals(const Location& other) const;
|
466 |
+
|
467 |
+
friend bool operator==(const Location& left, const Location& right) {
|
468 |
+
return left.Equals(right);
|
469 |
+
}
|
470 |
+
friend bool operator!=(const Location& left, const Location& right) {
|
471 |
+
return !(left == right);
|
472 |
+
}
|
473 |
+
|
474 |
+
private:
|
475 |
+
friend class FlightClient;
|
476 |
+
friend class FlightServerBase;
|
477 |
+
std::shared_ptr<arrow::util::Uri> uri_;
|
478 |
+
};
|
479 |
+
|
480 |
+
/// \brief A flight ticket and list of locations where the ticket can be
|
481 |
+
/// redeemed
|
482 |
+
struct ARROW_FLIGHT_EXPORT FlightEndpoint {
|
483 |
+
/// Opaque ticket identify; use with DoGet RPC
|
484 |
+
Ticket ticket;
|
485 |
+
|
486 |
+
/// List of locations where ticket can be redeemed. If the list is empty, the
|
487 |
+
/// ticket can only be redeemed on the current service where the ticket was
|
488 |
+
/// generated
|
489 |
+
std::vector<Location> locations;
|
490 |
+
|
491 |
+
/// Expiration time of this stream. If present, clients may assume
|
492 |
+
/// they can retry DoGet requests. Otherwise, clients should avoid
|
493 |
+
/// retrying DoGet requests.
|
494 |
+
std::optional<Timestamp> expiration_time;
|
495 |
+
|
496 |
+
/// Opaque Application-defined metadata
|
497 |
+
std::string app_metadata;
|
498 |
+
|
499 |
+
std::string ToString() const;
|
500 |
+
bool Equals(const FlightEndpoint& other) const;
|
501 |
+
|
502 |
+
friend bool operator==(const FlightEndpoint& left, const FlightEndpoint& right) {
|
503 |
+
return left.Equals(right);
|
504 |
+
}
|
505 |
+
friend bool operator!=(const FlightEndpoint& left, const FlightEndpoint& right) {
|
506 |
+
return !(left == right);
|
507 |
+
}
|
508 |
+
|
509 |
+
/// \brief Serialize this message to its wire-format representation.
|
510 |
+
arrow::Result<std::string> SerializeToString() const;
|
511 |
+
|
512 |
+
/// \brief Deserialize this message from its wire-format representation.
|
513 |
+
static arrow::Result<FlightEndpoint> Deserialize(std::string_view serialized);
|
514 |
+
};
|
515 |
+
|
516 |
+
/// \brief The request of the RenewFlightEndpoint action.
|
517 |
+
struct ARROW_FLIGHT_EXPORT RenewFlightEndpointRequest {
|
518 |
+
FlightEndpoint endpoint;
|
519 |
+
|
520 |
+
std::string ToString() const;
|
521 |
+
bool Equals(const RenewFlightEndpointRequest& other) const;
|
522 |
+
|
523 |
+
friend bool operator==(const RenewFlightEndpointRequest& left,
|
524 |
+
const RenewFlightEndpointRequest& right) {
|
525 |
+
return left.Equals(right);
|
526 |
+
}
|
527 |
+
friend bool operator!=(const RenewFlightEndpointRequest& left,
|
528 |
+
const RenewFlightEndpointRequest& right) {
|
529 |
+
return !(left == right);
|
530 |
+
}
|
531 |
+
|
532 |
+
/// \brief Serialize this message to its wire-format representation.
|
533 |
+
arrow::Result<std::string> SerializeToString() const;
|
534 |
+
|
535 |
+
/// \brief Deserialize this message from its wire-format representation.
|
536 |
+
static arrow::Result<RenewFlightEndpointRequest> Deserialize(
|
537 |
+
std::string_view serialized);
|
538 |
+
};
|
539 |
+
|
540 |
+
/// \brief Staging data structure for messages about to be put on the wire
|
541 |
+
///
|
542 |
+
/// This structure corresponds to FlightData in the protocol.
|
543 |
+
struct ARROW_FLIGHT_EXPORT FlightPayload {
|
544 |
+
std::shared_ptr<Buffer> descriptor;
|
545 |
+
std::shared_ptr<Buffer> app_metadata;
|
546 |
+
ipc::IpcPayload ipc_message;
|
547 |
+
|
548 |
+
/// \brief Check that the payload can be written to the wire.
|
549 |
+
Status Validate() const;
|
550 |
+
};
|
551 |
+
|
552 |
+
/// \brief Schema result returned after a schema request RPC
|
553 |
+
struct ARROW_FLIGHT_EXPORT SchemaResult {
|
554 |
+
public:
|
555 |
+
SchemaResult() = default;
|
556 |
+
explicit SchemaResult(std::string schema) : raw_schema_(std::move(schema)) {}
|
557 |
+
|
558 |
+
/// \brief Factory method to construct a SchemaResult.
|
559 |
+
static arrow::Result<std::unique_ptr<SchemaResult>> Make(const Schema& schema);
|
560 |
+
|
561 |
+
/// \brief return schema
|
562 |
+
/// \param[in,out] dictionary_memo for dictionary bookkeeping, will
|
563 |
+
/// be modified
|
564 |
+
/// \return Arrow result with the reconstructed Schema
|
565 |
+
arrow::Result<std::shared_ptr<Schema>> GetSchema(
|
566 |
+
ipc::DictionaryMemo* dictionary_memo) const;
|
567 |
+
|
568 |
+
const std::string& serialized_schema() const { return raw_schema_; }
|
569 |
+
|
570 |
+
std::string ToString() const;
|
571 |
+
bool Equals(const SchemaResult& other) const;
|
572 |
+
|
573 |
+
friend bool operator==(const SchemaResult& left, const SchemaResult& right) {
|
574 |
+
return left.Equals(right);
|
575 |
+
}
|
576 |
+
friend bool operator!=(const SchemaResult& left, const SchemaResult& right) {
|
577 |
+
return !(left == right);
|
578 |
+
}
|
579 |
+
|
580 |
+
/// \brief Serialize this message to its wire-format representation.
|
581 |
+
arrow::Result<std::string> SerializeToString() const;
|
582 |
+
|
583 |
+
/// \brief Deserialize this message from its wire-format representation.
|
584 |
+
static arrow::Result<SchemaResult> Deserialize(std::string_view serialized);
|
585 |
+
|
586 |
+
private:
|
587 |
+
std::string raw_schema_;
|
588 |
+
};
|
589 |
+
|
590 |
+
/// \brief The access coordinates for retrieval of a dataset, returned by
|
591 |
+
/// GetFlightInfo
|
592 |
+
class ARROW_FLIGHT_EXPORT FlightInfo {
|
593 |
+
public:
|
594 |
+
struct Data {
|
595 |
+
std::string schema;
|
596 |
+
FlightDescriptor descriptor;
|
597 |
+
std::vector<FlightEndpoint> endpoints;
|
598 |
+
int64_t total_records = -1;
|
599 |
+
int64_t total_bytes = -1;
|
600 |
+
bool ordered = false;
|
601 |
+
std::string app_metadata;
|
602 |
+
};
|
603 |
+
|
604 |
+
explicit FlightInfo(Data data) : data_(std::move(data)), reconstructed_schema_(false) {}
|
605 |
+
|
606 |
+
/// \brief Factory method to construct a FlightInfo.
|
607 |
+
static arrow::Result<FlightInfo> Make(const Schema& schema,
|
608 |
+
const FlightDescriptor& descriptor,
|
609 |
+
const std::vector<FlightEndpoint>& endpoints,
|
610 |
+
int64_t total_records, int64_t total_bytes,
|
611 |
+
bool ordered = false,
|
612 |
+
std::string app_metadata = "");
|
613 |
+
|
614 |
+
/// \brief Deserialize the Arrow schema of the dataset. Populate any
|
615 |
+
/// dictionary encoded fields into a DictionaryMemo for
|
616 |
+
/// bookkeeping
|
617 |
+
/// \param[in,out] dictionary_memo for dictionary bookkeeping, will
|
618 |
+
/// be modified
|
619 |
+
/// \return Arrow result with the reconstructed Schema
|
620 |
+
arrow::Result<std::shared_ptr<Schema>> GetSchema(
|
621 |
+
ipc::DictionaryMemo* dictionary_memo) const;
|
622 |
+
|
623 |
+
const std::string& serialized_schema() const { return data_.schema; }
|
624 |
+
|
625 |
+
/// The descriptor associated with this flight, may not be set
|
626 |
+
const FlightDescriptor& descriptor() const { return data_.descriptor; }
|
627 |
+
|
628 |
+
/// A list of endpoints associated with the flight (dataset). To consume the
|
629 |
+
/// whole flight, all endpoints must be consumed
|
630 |
+
const std::vector<FlightEndpoint>& endpoints() const { return data_.endpoints; }
|
631 |
+
|
632 |
+
/// The total number of records (rows) in the dataset. If unknown, set to -1
|
633 |
+
int64_t total_records() const { return data_.total_records; }
|
634 |
+
|
635 |
+
/// The total number of bytes in the dataset. If unknown, set to -1
|
636 |
+
int64_t total_bytes() const { return data_.total_bytes; }
|
637 |
+
|
638 |
+
/// Whether endpoints are in the same order as the data.
|
639 |
+
bool ordered() const { return data_.ordered; }
|
640 |
+
|
641 |
+
/// Application-defined opaque metadata
|
642 |
+
const std::string& app_metadata() const { return data_.app_metadata; }
|
643 |
+
|
644 |
+
/// \brief Get the wire-format representation of this type.
|
645 |
+
///
|
646 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
647 |
+
/// services) that may want to return Flight types.
|
648 |
+
arrow::Result<std::string> SerializeToString() const;
|
649 |
+
|
650 |
+
/// \brief Parse the wire-format representation of this type.
|
651 |
+
///
|
652 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
653 |
+
/// services) that may want to return Flight types.
|
654 |
+
static arrow::Result<std::unique_ptr<FlightInfo>> Deserialize(
|
655 |
+
std::string_view serialized);
|
656 |
+
|
657 |
+
std::string ToString() const;
|
658 |
+
|
659 |
+
/// Compare two FlightInfo for equality. This will compare the
|
660 |
+
/// serialized schema representations, NOT the logical equality of
|
661 |
+
/// the schemas.
|
662 |
+
bool Equals(const FlightInfo& other) const;
|
663 |
+
|
664 |
+
friend bool operator==(const FlightInfo& left, const FlightInfo& right) {
|
665 |
+
return left.Equals(right);
|
666 |
+
}
|
667 |
+
friend bool operator!=(const FlightInfo& left, const FlightInfo& right) {
|
668 |
+
return !(left == right);
|
669 |
+
}
|
670 |
+
|
671 |
+
private:
|
672 |
+
Data data_;
|
673 |
+
mutable std::shared_ptr<Schema> schema_;
|
674 |
+
mutable bool reconstructed_schema_;
|
675 |
+
};
|
676 |
+
|
677 |
+
/// \brief The information to process a long-running query.
|
678 |
+
class ARROW_FLIGHT_EXPORT PollInfo {
|
679 |
+
public:
|
680 |
+
/// The currently available results so far.
|
681 |
+
std::unique_ptr<FlightInfo> info = NULLPTR;
|
682 |
+
/// The descriptor the client should use on the next try. If unset,
|
683 |
+
/// the query is complete.
|
684 |
+
std::optional<FlightDescriptor> descriptor = std::nullopt;
|
685 |
+
/// Query progress. Must be in [0.0, 1.0] but need not be
|
686 |
+
/// monotonic or nondecreasing. If unknown, do not set.
|
687 |
+
std::optional<double> progress = std::nullopt;
|
688 |
+
/// Expiration time for this request. After this passes, the server
|
689 |
+
/// might not accept the poll descriptor anymore (and the query may
|
690 |
+
/// be cancelled). This may be updated on a call to PollFlightInfo.
|
691 |
+
std::optional<Timestamp> expiration_time = std::nullopt;
|
692 |
+
|
693 |
+
PollInfo()
|
694 |
+
: info(NULLPTR),
|
695 |
+
descriptor(std::nullopt),
|
696 |
+
progress(std::nullopt),
|
697 |
+
expiration_time(std::nullopt) {}
|
698 |
+
|
699 |
+
explicit PollInfo(std::unique_ptr<FlightInfo> info,
|
700 |
+
std::optional<FlightDescriptor> descriptor,
|
701 |
+
std::optional<double> progress,
|
702 |
+
std::optional<Timestamp> expiration_time)
|
703 |
+
: info(std::move(info)),
|
704 |
+
descriptor(std::move(descriptor)),
|
705 |
+
progress(progress),
|
706 |
+
expiration_time(expiration_time) {}
|
707 |
+
|
708 |
+
// Must not be explicit; to declare one we must declare all ("rule of five")
|
709 |
+
PollInfo(const PollInfo& other) // NOLINT(runtime/explicit)
|
710 |
+
: info(other.info ? std::make_unique<FlightInfo>(*other.info) : NULLPTR),
|
711 |
+
descriptor(other.descriptor),
|
712 |
+
progress(other.progress),
|
713 |
+
expiration_time(other.expiration_time) {}
|
714 |
+
PollInfo(PollInfo&& other) noexcept = default; // NOLINT(runtime/explicit)
|
715 |
+
~PollInfo() = default;
|
716 |
+
PollInfo& operator=(const PollInfo& other) {
|
717 |
+
info = other.info ? std::make_unique<FlightInfo>(*other.info) : NULLPTR;
|
718 |
+
descriptor = other.descriptor;
|
719 |
+
progress = other.progress;
|
720 |
+
expiration_time = other.expiration_time;
|
721 |
+
return *this;
|
722 |
+
}
|
723 |
+
PollInfo& operator=(PollInfo&& other) = default;
|
724 |
+
|
725 |
+
/// \brief Get the wire-format representation of this type.
|
726 |
+
///
|
727 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
728 |
+
/// services) that may want to return Flight types.
|
729 |
+
arrow::Result<std::string> SerializeToString() const;
|
730 |
+
|
731 |
+
/// \brief Parse the wire-format representation of this type.
|
732 |
+
///
|
733 |
+
/// Useful when interoperating with non-Flight systems (e.g. REST
|
734 |
+
/// services) that may want to return Flight types.
|
735 |
+
static arrow::Result<std::unique_ptr<PollInfo>> Deserialize(
|
736 |
+
std::string_view serialized);
|
737 |
+
|
738 |
+
std::string ToString() const;
|
739 |
+
|
740 |
+
/// Compare two PollInfo for equality. This will compare the
|
741 |
+
/// serialized schema representations, NOT the logical equality of
|
742 |
+
/// the schemas.
|
743 |
+
bool Equals(const PollInfo& other) const;
|
744 |
+
|
745 |
+
friend bool operator==(const PollInfo& left, const PollInfo& right) {
|
746 |
+
return left.Equals(right);
|
747 |
+
}
|
748 |
+
friend bool operator!=(const PollInfo& left, const PollInfo& right) {
|
749 |
+
return !(left == right);
|
750 |
+
}
|
751 |
+
};
|
752 |
+
|
753 |
+
/// \brief The request of the CancelFlightInfoRequest action.
|
754 |
+
struct ARROW_FLIGHT_EXPORT CancelFlightInfoRequest {
|
755 |
+
std::unique_ptr<FlightInfo> info;
|
756 |
+
|
757 |
+
std::string ToString() const;
|
758 |
+
bool Equals(const CancelFlightInfoRequest& other) const;
|
759 |
+
|
760 |
+
friend bool operator==(const CancelFlightInfoRequest& left,
|
761 |
+
const CancelFlightInfoRequest& right) {
|
762 |
+
return left.Equals(right);
|
763 |
+
}
|
764 |
+
friend bool operator!=(const CancelFlightInfoRequest& left,
|
765 |
+
const CancelFlightInfoRequest& right) {
|
766 |
+
return !(left == right);
|
767 |
+
}
|
768 |
+
|
769 |
+
/// \brief Serialize this message to its wire-format representation.
|
770 |
+
arrow::Result<std::string> SerializeToString() const;
|
771 |
+
|
772 |
+
/// \brief Deserialize this message from its wire-format representation.
|
773 |
+
static arrow::Result<CancelFlightInfoRequest> Deserialize(std::string_view serialized);
|
774 |
+
};
|
775 |
+
|
776 |
+
/// \brief Variant supporting all possible value types for {Set,Get}SessionOptions
|
777 |
+
///
|
778 |
+
/// By convention, an attempt to set a valueless (std::monostate) SessionOptionValue
|
779 |
+
/// should attempt to unset or clear the named option value on the server.
|
780 |
+
using SessionOptionValue = std::variant<std::monostate, std::string, bool, int64_t,
|
781 |
+
double, std::vector<std::string>>;
|
782 |
+
|
783 |
+
/// \brief The result of setting a session option.
|
784 |
+
enum class SetSessionOptionErrorValue : int8_t {
|
785 |
+
/// \brief The status of setting the option is unknown.
|
786 |
+
///
|
787 |
+
/// Servers should avoid using this value (send a NOT_FOUND error if the requested
|
788 |
+
/// session is not known). Clients can retry the request.
|
789 |
+
kUnspecified,
|
790 |
+
/// \brief The given session option name is invalid.
|
791 |
+
kInvalidName,
|
792 |
+
/// \brief The session option value or type is invalid.
|
793 |
+
kInvalidValue,
|
794 |
+
/// \brief The session option cannot be set.
|
795 |
+
kError
|
796 |
+
};
|
797 |
+
std::string ToString(const SetSessionOptionErrorValue& error_value);
|
798 |
+
std::ostream& operator<<(std::ostream& os, const SetSessionOptionErrorValue& error_value);
|
799 |
+
|
800 |
+
/// \brief The result of closing a session.
|
801 |
+
enum class CloseSessionStatus : int8_t {
|
802 |
+
// \brief The session close status is unknown.
|
803 |
+
//
|
804 |
+
// Servers should avoid using this value (send a NOT_FOUND error if the requested
|
805 |
+
// session is not known). Clients can retry the request.
|
806 |
+
kUnspecified,
|
807 |
+
// \brief The session close request is complete.
|
808 |
+
//
|
809 |
+
// Subsequent requests with the same session produce a NOT_FOUND error.
|
810 |
+
kClosed,
|
811 |
+
// \brief The session close request is in progress.
|
812 |
+
//
|
813 |
+
// The client may retry the request.
|
814 |
+
kClosing,
|
815 |
+
// \brief The session is not closeable.
|
816 |
+
//
|
817 |
+
// The client should not retry the request.
|
818 |
+
kNotClosable
|
819 |
+
};
|
820 |
+
std::string ToString(const CloseSessionStatus& status);
|
821 |
+
std::ostream& operator<<(std::ostream& os, const CloseSessionStatus& status);
|
822 |
+
|
823 |
+
/// \brief A request to set a set of session options by name/value.
|
824 |
+
struct ARROW_FLIGHT_EXPORT SetSessionOptionsRequest {
|
825 |
+
std::map<std::string, SessionOptionValue> session_options;
|
826 |
+
|
827 |
+
std::string ToString() const;
|
828 |
+
bool Equals(const SetSessionOptionsRequest& other) const;
|
829 |
+
|
830 |
+
friend bool operator==(const SetSessionOptionsRequest& left,
|
831 |
+
const SetSessionOptionsRequest& right) {
|
832 |
+
return left.Equals(right);
|
833 |
+
}
|
834 |
+
friend bool operator!=(const SetSessionOptionsRequest& left,
|
835 |
+
const SetSessionOptionsRequest& right) {
|
836 |
+
return !(left == right);
|
837 |
+
}
|
838 |
+
|
839 |
+
/// \brief Serialize this message to its wire-format representation.
|
840 |
+
arrow::Result<std::string> SerializeToString() const;
|
841 |
+
|
842 |
+
/// \brief Deserialize this message from its wire-format representation.
|
843 |
+
static arrow::Result<SetSessionOptionsRequest> Deserialize(std::string_view serialized);
|
844 |
+
};
|
845 |
+
|
846 |
+
/// \brief The result(s) of setting session option(s).
|
847 |
+
struct ARROW_FLIGHT_EXPORT SetSessionOptionsResult {
|
848 |
+
struct Error {
|
849 |
+
SetSessionOptionErrorValue value;
|
850 |
+
|
851 |
+
bool Equals(const Error& other) const { return value == other.value; }
|
852 |
+
friend bool operator==(const Error& left, const Error& right) {
|
853 |
+
return left.Equals(right);
|
854 |
+
}
|
855 |
+
friend bool operator!=(const Error& left, const Error& right) {
|
856 |
+
return !(left == right);
|
857 |
+
}
|
858 |
+
};
|
859 |
+
|
860 |
+
std::map<std::string, Error> errors;
|
861 |
+
|
862 |
+
std::string ToString() const;
|
863 |
+
bool Equals(const SetSessionOptionsResult& other) const;
|
864 |
+
|
865 |
+
friend bool operator==(const SetSessionOptionsResult& left,
|
866 |
+
const SetSessionOptionsResult& right) {
|
867 |
+
return left.Equals(right);
|
868 |
+
}
|
869 |
+
friend bool operator!=(const SetSessionOptionsResult& left,
|
870 |
+
const SetSessionOptionsResult& right) {
|
871 |
+
return !(left == right);
|
872 |
+
}
|
873 |
+
|
874 |
+
/// \brief Serialize this message to its wire-format representation.
|
875 |
+
arrow::Result<std::string> SerializeToString() const;
|
876 |
+
|
877 |
+
/// \brief Deserialize this message from its wire-format representation.
|
878 |
+
static arrow::Result<SetSessionOptionsResult> Deserialize(std::string_view serialized);
|
879 |
+
};
|
880 |
+
|
881 |
+
/// \brief A request to get current session options.
|
882 |
+
struct ARROW_FLIGHT_EXPORT GetSessionOptionsRequest {
|
883 |
+
std::string ToString() const;
|
884 |
+
bool Equals(const GetSessionOptionsRequest& other) const;
|
885 |
+
|
886 |
+
friend bool operator==(const GetSessionOptionsRequest& left,
|
887 |
+
const GetSessionOptionsRequest& right) {
|
888 |
+
return left.Equals(right);
|
889 |
+
}
|
890 |
+
friend bool operator!=(const GetSessionOptionsRequest& left,
|
891 |
+
const GetSessionOptionsRequest& right) {
|
892 |
+
return !(left == right);
|
893 |
+
}
|
894 |
+
|
895 |
+
/// \brief Serialize this message to its wire-format representation.
|
896 |
+
arrow::Result<std::string> SerializeToString() const;
|
897 |
+
|
898 |
+
/// \brief Deserialize this message from its wire-format representation.
|
899 |
+
static arrow::Result<GetSessionOptionsRequest> Deserialize(std::string_view serialized);
|
900 |
+
};
|
901 |
+
|
902 |
+
/// \brief The current session options.
|
903 |
+
struct ARROW_FLIGHT_EXPORT GetSessionOptionsResult {
|
904 |
+
std::map<std::string, SessionOptionValue> session_options;
|
905 |
+
|
906 |
+
std::string ToString() const;
|
907 |
+
bool Equals(const GetSessionOptionsResult& other) const;
|
908 |
+
|
909 |
+
friend bool operator==(const GetSessionOptionsResult& left,
|
910 |
+
const GetSessionOptionsResult& right) {
|
911 |
+
return left.Equals(right);
|
912 |
+
}
|
913 |
+
friend bool operator!=(const GetSessionOptionsResult& left,
|
914 |
+
const GetSessionOptionsResult& right) {
|
915 |
+
return !(left == right);
|
916 |
+
}
|
917 |
+
|
918 |
+
/// \brief Serialize this message to its wire-format representation.
|
919 |
+
arrow::Result<std::string> SerializeToString() const;
|
920 |
+
|
921 |
+
/// \brief Deserialize this message from its wire-format representation.
|
922 |
+
static arrow::Result<GetSessionOptionsResult> Deserialize(std::string_view serialized);
|
923 |
+
};
|
924 |
+
|
925 |
+
/// \brief A request to close the open client session.
|
926 |
+
struct ARROW_FLIGHT_EXPORT CloseSessionRequest {
|
927 |
+
std::string ToString() const;
|
928 |
+
bool Equals(const CloseSessionRequest& other) const;
|
929 |
+
|
930 |
+
friend bool operator==(const CloseSessionRequest& left,
|
931 |
+
const CloseSessionRequest& right) {
|
932 |
+
return left.Equals(right);
|
933 |
+
}
|
934 |
+
friend bool operator!=(const CloseSessionRequest& left,
|
935 |
+
const CloseSessionRequest& right) {
|
936 |
+
return !(left == right);
|
937 |
+
}
|
938 |
+
|
939 |
+
/// \brief Serialize this message to its wire-format representation.
|
940 |
+
arrow::Result<std::string> SerializeToString() const;
|
941 |
+
|
942 |
+
/// \brief Deserialize this message from its wire-format representation.
|
943 |
+
static arrow::Result<CloseSessionRequest> Deserialize(std::string_view serialized);
|
944 |
+
};
|
945 |
+
|
946 |
+
/// \brief The result of attempting to close the client session.
|
947 |
+
struct ARROW_FLIGHT_EXPORT CloseSessionResult {
|
948 |
+
CloseSessionStatus status;
|
949 |
+
|
950 |
+
std::string ToString() const;
|
951 |
+
bool Equals(const CloseSessionResult& other) const;
|
952 |
+
|
953 |
+
friend bool operator==(const CloseSessionResult& left,
|
954 |
+
const CloseSessionResult& right) {
|
955 |
+
return left.Equals(right);
|
956 |
+
}
|
957 |
+
friend bool operator!=(const CloseSessionResult& left,
|
958 |
+
const CloseSessionResult& right) {
|
959 |
+
return !(left == right);
|
960 |
+
}
|
961 |
+
|
962 |
+
/// \brief Serialize this message to its wire-format representation.
|
963 |
+
arrow::Result<std::string> SerializeToString() const;
|
964 |
+
|
965 |
+
/// \brief Deserialize this message from its wire-format representation.
|
966 |
+
static arrow::Result<CloseSessionResult> Deserialize(std::string_view serialized);
|
967 |
+
};
|
968 |
+
|
969 |
+
/// \brief An iterator to FlightInfo instances returned by ListFlights.
|
970 |
+
class ARROW_FLIGHT_EXPORT FlightListing {
|
971 |
+
public:
|
972 |
+
virtual ~FlightListing() = default;
|
973 |
+
|
974 |
+
/// \brief Retrieve the next FlightInfo from the iterator.
|
975 |
+
/// \return Arrow result with a single FlightInfo. Set to \a nullptr if there
|
976 |
+
/// are none left.
|
977 |
+
virtual arrow::Result<std::unique_ptr<FlightInfo>> Next() = 0;
|
978 |
+
};
|
979 |
+
|
980 |
+
/// \brief An iterator to Result instances returned by DoAction.
|
981 |
+
class ARROW_FLIGHT_EXPORT ResultStream {
|
982 |
+
public:
|
983 |
+
virtual ~ResultStream() = default;
|
984 |
+
|
985 |
+
/// \brief Retrieve the next Result from the iterator.
|
986 |
+
/// \return Arrow result with a single Result. Set to \a nullptr if there are none left.
|
987 |
+
virtual arrow::Result<std::unique_ptr<Result>> Next() = 0;
|
988 |
+
|
989 |
+
/// \brief Read and drop the remaining messages to get the error (if any) from a server.
|
990 |
+
/// \return Status OK if this is no error from a server, any other status if a
|
991 |
+
/// server returns an error.
|
992 |
+
Status Drain();
|
993 |
+
};
|
994 |
+
|
995 |
+
/// \brief A holder for a RecordBatch with associated Flight metadata.
|
996 |
+
struct ARROW_FLIGHT_EXPORT FlightStreamChunk {
|
997 |
+
public:
|
998 |
+
std::shared_ptr<RecordBatch> data;
|
999 |
+
std::shared_ptr<Buffer> app_metadata;
|
1000 |
+
};
|
1001 |
+
|
1002 |
+
/// \brief An interface to read Flight data with metadata.
|
1003 |
+
class ARROW_FLIGHT_EXPORT MetadataRecordBatchReader {
|
1004 |
+
public:
|
1005 |
+
virtual ~MetadataRecordBatchReader() = default;
|
1006 |
+
|
1007 |
+
/// \brief Get the schema for this stream.
|
1008 |
+
virtual arrow::Result<std::shared_ptr<Schema>> GetSchema() = 0;
|
1009 |
+
|
1010 |
+
/// \brief Get the next message from Flight. If the stream is
|
1011 |
+
/// finished, then the members of \a FlightStreamChunk will be
|
1012 |
+
/// nullptr.
|
1013 |
+
virtual arrow::Result<FlightStreamChunk> Next() = 0;
|
1014 |
+
|
1015 |
+
/// \brief Consume entire stream as a vector of record batches
|
1016 |
+
virtual arrow::Result<std::vector<std::shared_ptr<RecordBatch>>> ToRecordBatches();
|
1017 |
+
|
1018 |
+
/// \brief Consume entire stream as a Table
|
1019 |
+
virtual arrow::Result<std::shared_ptr<Table>> ToTable();
|
1020 |
+
};
|
1021 |
+
|
1022 |
+
/// \brief Convert a MetadataRecordBatchReader to a regular RecordBatchReader.
|
1023 |
+
ARROW_FLIGHT_EXPORT
|
1024 |
+
arrow::Result<std::shared_ptr<RecordBatchReader>> MakeRecordBatchReader(
|
1025 |
+
std::shared_ptr<MetadataRecordBatchReader> reader);
|
1026 |
+
|
1027 |
+
/// \brief An interface to write IPC payloads with metadata.
|
1028 |
+
class ARROW_FLIGHT_EXPORT MetadataRecordBatchWriter : public ipc::RecordBatchWriter {
|
1029 |
+
public:
|
1030 |
+
virtual ~MetadataRecordBatchWriter() = default;
|
1031 |
+
/// \brief Begin writing data with the given schema. Only used with \a DoExchange.
|
1032 |
+
virtual Status Begin(const std::shared_ptr<Schema>& schema,
|
1033 |
+
const ipc::IpcWriteOptions& options) = 0;
|
1034 |
+
virtual Status Begin(const std::shared_ptr<Schema>& schema);
|
1035 |
+
virtual Status WriteMetadata(std::shared_ptr<Buffer> app_metadata) = 0;
|
1036 |
+
virtual Status WriteWithMetadata(const RecordBatch& batch,
|
1037 |
+
std::shared_ptr<Buffer> app_metadata) = 0;
|
1038 |
+
};
|
1039 |
+
|
1040 |
+
/// \brief A FlightListing implementation based on a vector of
|
1041 |
+
/// FlightInfo objects.
|
1042 |
+
///
|
1043 |
+
/// This can be iterated once, then it is consumed.
|
1044 |
+
class ARROW_FLIGHT_EXPORT SimpleFlightListing : public FlightListing {
|
1045 |
+
public:
|
1046 |
+
explicit SimpleFlightListing(const std::vector<FlightInfo>& flights);
|
1047 |
+
explicit SimpleFlightListing(std::vector<FlightInfo>&& flights);
|
1048 |
+
|
1049 |
+
arrow::Result<std::unique_ptr<FlightInfo>> Next() override;
|
1050 |
+
|
1051 |
+
private:
|
1052 |
+
int position_;
|
1053 |
+
std::vector<FlightInfo> flights_;
|
1054 |
+
};
|
1055 |
+
|
1056 |
+
/// \brief A ResultStream implementation based on a vector of
|
1057 |
+
/// Result objects.
|
1058 |
+
///
|
1059 |
+
/// This can be iterated once, then it is consumed.
|
1060 |
+
class ARROW_FLIGHT_EXPORT SimpleResultStream : public ResultStream {
|
1061 |
+
public:
|
1062 |
+
explicit SimpleResultStream(std::vector<Result>&& results);
|
1063 |
+
arrow::Result<std::unique_ptr<Result>> Next() override;
|
1064 |
+
|
1065 |
+
private:
|
1066 |
+
std::vector<Result> results_;
|
1067 |
+
size_t position_;
|
1068 |
+
};
|
1069 |
+
|
1070 |
+
/// \defgroup flight-error Error Handling
|
1071 |
+
/// Types for handling errors from RPCs. Flight uses a set of status
|
1072 |
+
/// codes standardized across Flight implementations, so these types
|
1073 |
+
/// let applications work directly with those codes instead of having
|
1074 |
+
/// to translate to and from Arrow Status.
|
1075 |
+
/// @{
|
1076 |
+
|
1077 |
+
/// \brief Abstract status code for an RPC as per the Flight
|
1078 |
+
/// specification.
|
1079 |
+
enum class TransportStatusCode {
|
1080 |
+
/// \brief No error.
|
1081 |
+
kOk = 0,
|
1082 |
+
/// \brief An unknown error occurred.
|
1083 |
+
kUnknown = 1,
|
1084 |
+
/// \brief An error occurred in the transport implementation, or an
|
1085 |
+
/// error internal to the service implementation occurred.
|
1086 |
+
kInternal = 2,
|
1087 |
+
/// \brief An argument is invalid.
|
1088 |
+
kInvalidArgument = 3,
|
1089 |
+
/// \brief The request timed out.
|
1090 |
+
kTimedOut = 4,
|
1091 |
+
/// \brief An argument is not necessarily invalid, but references
|
1092 |
+
/// some resource that does not exist. Prefer over
|
1093 |
+
/// kInvalidArgument where applicable.
|
1094 |
+
kNotFound = 5,
|
1095 |
+
/// \brief The request attempted to create some resource that does
|
1096 |
+
/// not exist.
|
1097 |
+
kAlreadyExists = 6,
|
1098 |
+
/// \brief The request was explicitly cancelled.
|
1099 |
+
kCancelled = 7,
|
1100 |
+
/// \brief The client is not authenticated.
|
1101 |
+
kUnauthenticated = 8,
|
1102 |
+
/// \brief The client is not authorized to perform this request.
|
1103 |
+
kUnauthorized = 9,
|
1104 |
+
/// \brief The request is not implemented
|
1105 |
+
kUnimplemented = 10,
|
1106 |
+
/// \brief There is a network connectivity error, or some resource
|
1107 |
+
/// is otherwise unavailable. Most likely a temporary condition.
|
1108 |
+
kUnavailable = 11,
|
1109 |
+
};
|
1110 |
+
|
1111 |
+
/// \brief Convert a code to a string.
|
1112 |
+
std::string ToString(TransportStatusCode code);
|
1113 |
+
|
1114 |
+
/// \brief An error from an RPC call, using Flight error codes directly
|
1115 |
+
/// instead of trying to translate to Arrow Status.
|
1116 |
+
///
|
1117 |
+
/// Currently, only attached to the Status passed to AsyncListener::OnFinish.
|
1118 |
+
///
|
1119 |
+
/// This API is EXPERIMENTAL.
|
1120 |
+
class ARROW_FLIGHT_EXPORT TransportStatusDetail : public StatusDetail {
|
1121 |
+
public:
|
1122 |
+
constexpr static const char* kTypeId = "flight::TransportStatusDetail";
|
1123 |
+
explicit TransportStatusDetail(TransportStatusCode code, std::string message,
|
1124 |
+
std::vector<std::pair<std::string, std::string>> details)
|
1125 |
+
: code_(code), message_(std::move(message)), details_(std::move(details)) {}
|
1126 |
+
const char* type_id() const override { return kTypeId; }
|
1127 |
+
std::string ToString() const override;
|
1128 |
+
|
1129 |
+
static std::optional<std::reference_wrapper<const TransportStatusDetail>> Unwrap(
|
1130 |
+
const Status& status);
|
1131 |
+
|
1132 |
+
TransportStatusCode code() const { return code_; }
|
1133 |
+
std::string_view message() const { return message_; }
|
1134 |
+
const std::vector<std::pair<std::string, std::string>>& details() const {
|
1135 |
+
return details_;
|
1136 |
+
}
|
1137 |
+
|
1138 |
+
private:
|
1139 |
+
TransportStatusCode code_;
|
1140 |
+
std::string message_;
|
1141 |
+
std::vector<std::pair<std::string, std::string>> details_;
|
1142 |
+
};
|
1143 |
+
|
1144 |
+
/// @}
|
1145 |
+
|
1146 |
+
} // namespace flight
|
1147 |
+
} // namespace arrow
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/flight/visibility.h
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#if defined(_WIN32) || defined(__CYGWIN__)
|
21 |
+
#if defined(_MSC_VER)
|
22 |
+
#pragma warning(push)
|
23 |
+
#pragma warning(disable : 4251)
|
24 |
+
#else
|
25 |
+
#pragma GCC diagnostic ignored "-Wattributes"
|
26 |
+
#endif
|
27 |
+
|
28 |
+
#ifdef ARROW_FLIGHT_STATIC
|
29 |
+
#define ARROW_FLIGHT_EXPORT
|
30 |
+
#elif defined(ARROW_FLIGHT_EXPORTING)
|
31 |
+
#define ARROW_FLIGHT_EXPORT __declspec(dllexport)
|
32 |
+
#else
|
33 |
+
#define ARROW_FLIGHT_EXPORT __declspec(dllimport)
|
34 |
+
#endif
|
35 |
+
|
36 |
+
#define ARROW_FLIGHT_NO_EXPORT
|
37 |
+
#else // Not Windows
|
38 |
+
#ifndef ARROW_FLIGHT_EXPORT
|
39 |
+
#define ARROW_FLIGHT_EXPORT __attribute__((visibility("default")))
|
40 |
+
#endif
|
41 |
+
#ifndef ARROW_FLIGHT_NO_EXPORT
|
42 |
+
#define ARROW_FLIGHT_NO_EXPORT __attribute__((visibility("hidden")))
|
43 |
+
#endif
|
44 |
+
#endif // Non-Windows
|
45 |
+
|
46 |
+
#if defined(_MSC_VER)
|
47 |
+
#pragma warning(pop)
|
48 |
+
#endif
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/ProducerConsumerQueue.h
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Vendored from git tag v2021.02.15.00
|
2 |
+
|
3 |
+
/*
|
4 |
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
5 |
+
*
|
6 |
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
+
* you may not use this file except in compliance with the License.
|
8 |
+
* You may obtain a copy of the License at
|
9 |
+
*
|
10 |
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11 |
+
*
|
12 |
+
* Unless required by applicable law or agreed to in writing, software
|
13 |
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
+
* See the License for the specific language governing permissions and
|
16 |
+
* limitations under the License.
|
17 |
+
*/
|
18 |
+
|
19 |
+
// @author Bo Hu ([email protected])
|
20 |
+
// @author Jordan DeLong ([email protected])
|
21 |
+
|
22 |
+
// This file has been modified as part of Apache Arrow to conform to
|
23 |
+
// Apache Arrow's coding conventions
|
24 |
+
|
25 |
+
#pragma once
|
26 |
+
|
27 |
+
#include <atomic>
|
28 |
+
#include <cassert>
|
29 |
+
#include <cstdlib>
|
30 |
+
#include <memory>
|
31 |
+
#include <stdexcept>
|
32 |
+
#include <type_traits>
|
33 |
+
#include <utility>
|
34 |
+
|
35 |
+
namespace arrow_vendored {
|
36 |
+
namespace folly {
|
37 |
+
|
38 |
+
// Vendored from folly/Portability.h
|
39 |
+
namespace {
|
40 |
+
#if defined(__arm__)
|
41 |
+
#define FOLLY_ARM 1
|
42 |
+
#else
|
43 |
+
#define FOLLY_ARM 0
|
44 |
+
#endif
|
45 |
+
|
46 |
+
#if defined(__s390x__)
|
47 |
+
#define FOLLY_S390X 1
|
48 |
+
#else
|
49 |
+
#define FOLLY_S390X 0
|
50 |
+
#endif
|
51 |
+
|
52 |
+
constexpr bool kIsArchArm = FOLLY_ARM == 1;
|
53 |
+
constexpr bool kIsArchS390X = FOLLY_S390X == 1;
|
54 |
+
} // namespace
|
55 |
+
|
56 |
+
// Vendored from folly/lang/Align.h
|
57 |
+
namespace {
|
58 |
+
|
59 |
+
constexpr std::size_t hardware_destructive_interference_size =
|
60 |
+
(kIsArchArm || kIsArchS390X) ? 64 : 128;
|
61 |
+
|
62 |
+
} // namespace
|
63 |
+
|
64 |
+
/*
|
65 |
+
* ProducerConsumerQueue is a one producer and one consumer queue
|
66 |
+
* without locks.
|
67 |
+
*/
|
68 |
+
template <class T>
|
69 |
+
struct ProducerConsumerQueue {
|
70 |
+
typedef T value_type;
|
71 |
+
|
72 |
+
ProducerConsumerQueue(const ProducerConsumerQueue&) = delete;
|
73 |
+
ProducerConsumerQueue& operator=(const ProducerConsumerQueue&) = delete;
|
74 |
+
|
75 |
+
// size must be >= 2.
|
76 |
+
//
|
77 |
+
// Also, note that the number of usable slots in the queue at any
|
78 |
+
// given time is actually (size-1), so if you start with an empty queue,
|
79 |
+
// IsFull() will return true after size-1 insertions.
|
80 |
+
explicit ProducerConsumerQueue(uint32_t size)
|
81 |
+
: size_(size),
|
82 |
+
records_(static_cast<T*>(std::malloc(sizeof(T) * size))),
|
83 |
+
readIndex_(0),
|
84 |
+
writeIndex_(0) {
|
85 |
+
assert(size >= 2);
|
86 |
+
if (!records_) {
|
87 |
+
throw std::bad_alloc();
|
88 |
+
}
|
89 |
+
}
|
90 |
+
|
91 |
+
~ProducerConsumerQueue() {
|
92 |
+
// We need to destruct anything that may still exist in our queue.
|
93 |
+
// (No real synchronization needed at destructor time: only one
|
94 |
+
// thread can be doing this.)
|
95 |
+
if (!std::is_trivially_destructible<T>::value) {
|
96 |
+
size_t readIndex = readIndex_;
|
97 |
+
size_t endIndex = writeIndex_;
|
98 |
+
while (readIndex != endIndex) {
|
99 |
+
records_[readIndex].~T();
|
100 |
+
if (++readIndex == size_) {
|
101 |
+
readIndex = 0;
|
102 |
+
}
|
103 |
+
}
|
104 |
+
}
|
105 |
+
|
106 |
+
std::free(records_);
|
107 |
+
}
|
108 |
+
|
109 |
+
template <class... Args>
|
110 |
+
bool Write(Args&&... recordArgs) {
|
111 |
+
auto const currentWrite = writeIndex_.load(std::memory_order_relaxed);
|
112 |
+
auto nextRecord = currentWrite + 1;
|
113 |
+
if (nextRecord == size_) {
|
114 |
+
nextRecord = 0;
|
115 |
+
}
|
116 |
+
if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
|
117 |
+
new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
|
118 |
+
writeIndex_.store(nextRecord, std::memory_order_release);
|
119 |
+
return true;
|
120 |
+
}
|
121 |
+
|
122 |
+
// queue is full
|
123 |
+
return false;
|
124 |
+
}
|
125 |
+
|
126 |
+
// move the value at the front of the queue to given variable
|
127 |
+
bool Read(T& record) {
|
128 |
+
auto const currentRead = readIndex_.load(std::memory_order_relaxed);
|
129 |
+
if (currentRead == writeIndex_.load(std::memory_order_acquire)) {
|
130 |
+
// queue is empty
|
131 |
+
return false;
|
132 |
+
}
|
133 |
+
|
134 |
+
auto nextRecord = currentRead + 1;
|
135 |
+
if (nextRecord == size_) {
|
136 |
+
nextRecord = 0;
|
137 |
+
}
|
138 |
+
record = std::move(records_[currentRead]);
|
139 |
+
records_[currentRead].~T();
|
140 |
+
readIndex_.store(nextRecord, std::memory_order_release);
|
141 |
+
return true;
|
142 |
+
}
|
143 |
+
|
144 |
+
// pointer to the value at the front of the queue (for use in-place) or
|
145 |
+
// nullptr if empty.
|
146 |
+
T* FrontPtr() {
|
147 |
+
auto const currentRead = readIndex_.load(std::memory_order_relaxed);
|
148 |
+
if (currentRead == writeIndex_.load(std::memory_order_acquire)) {
|
149 |
+
// queue is empty
|
150 |
+
return nullptr;
|
151 |
+
}
|
152 |
+
return &records_[currentRead];
|
153 |
+
}
|
154 |
+
|
155 |
+
// queue must not be empty
|
156 |
+
void PopFront() {
|
157 |
+
auto const currentRead = readIndex_.load(std::memory_order_relaxed);
|
158 |
+
assert(currentRead != writeIndex_.load(std::memory_order_acquire));
|
159 |
+
|
160 |
+
auto nextRecord = currentRead + 1;
|
161 |
+
if (nextRecord == size_) {
|
162 |
+
nextRecord = 0;
|
163 |
+
}
|
164 |
+
records_[currentRead].~T();
|
165 |
+
readIndex_.store(nextRecord, std::memory_order_release);
|
166 |
+
}
|
167 |
+
|
168 |
+
bool IsEmpty() const {
|
169 |
+
return readIndex_.load(std::memory_order_acquire) ==
|
170 |
+
writeIndex_.load(std::memory_order_acquire);
|
171 |
+
}
|
172 |
+
|
173 |
+
bool IsFull() const {
|
174 |
+
auto nextRecord = writeIndex_.load(std::memory_order_acquire) + 1;
|
175 |
+
if (nextRecord == size_) {
|
176 |
+
nextRecord = 0;
|
177 |
+
}
|
178 |
+
if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
|
179 |
+
return false;
|
180 |
+
}
|
181 |
+
// queue is full
|
182 |
+
return true;
|
183 |
+
}
|
184 |
+
|
185 |
+
// * If called by consumer, then true size may be more (because producer may
|
186 |
+
// be adding items concurrently).
|
187 |
+
// * If called by producer, then true size may be less (because consumer may
|
188 |
+
// be removing items concurrently).
|
189 |
+
// * It is undefined to call this from any other thread.
|
190 |
+
size_t SizeGuess() const {
|
191 |
+
int ret = writeIndex_.load(std::memory_order_acquire) -
|
192 |
+
readIndex_.load(std::memory_order_acquire);
|
193 |
+
if (ret < 0) {
|
194 |
+
ret += size_;
|
195 |
+
}
|
196 |
+
return ret;
|
197 |
+
}
|
198 |
+
|
199 |
+
// maximum number of items in the queue.
|
200 |
+
size_t capacity() const { return size_ - 1; }
|
201 |
+
|
202 |
+
private:
|
203 |
+
using AtomicIndex = std::atomic<unsigned int>;
|
204 |
+
|
205 |
+
char pad0_[hardware_destructive_interference_size];
|
206 |
+
const uint32_t size_;
|
207 |
+
T* const records_;
|
208 |
+
|
209 |
+
AtomicIndex readIndex_;
|
210 |
+
char pad1_[hardware_destructive_interference_size - sizeof(AtomicIndex)];
|
211 |
+
AtomicIndex writeIndex_;
|
212 |
+
|
213 |
+
char pad2_[hardware_destructive_interference_size - sizeof(AtomicIndex)];
|
214 |
+
};
|
215 |
+
|
216 |
+
} // namespace folly
|
217 |
+
} // namespace arrow_vendored
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime.h
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include "arrow/vendored/datetime/date.h" // IWYU pragma: export
|
21 |
+
#include "arrow/vendored/datetime/tz.h" // IWYU pragma: export
|
22 |
+
|
23 |
+
// Can be defined by date.h.
|
24 |
+
#ifdef NOEXCEPT
|
25 |
+
#undef NOEXCEPT
|
26 |
+
#endif
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/date.h
ADDED
The diff for this file is too large to render.
See raw diff
|
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/ios.h
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
//
|
2 |
+
// ios.h
|
3 |
+
// DateTimeLib
|
4 |
+
//
|
5 |
+
// The MIT License (MIT)
|
6 |
+
//
|
7 |
+
// Copyright (c) 2016 Alexander Kormanovsky
|
8 |
+
//
|
9 |
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
10 |
+
// of this software and associated documentation files (the "Software"), to deal
|
11 |
+
// in the Software without restriction, including without limitation the rights
|
12 |
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13 |
+
// copies of the Software, and to permit persons to whom the Software is
|
14 |
+
// furnished to do so, subject to the following conditions:
|
15 |
+
//
|
16 |
+
// The above copyright notice and this permission notice shall be included in all
|
17 |
+
// copies or substantial portions of the Software.
|
18 |
+
//
|
19 |
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20 |
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21 |
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22 |
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23 |
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24 |
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
25 |
+
// SOFTWARE.
|
26 |
+
|
27 |
+
#ifndef ios_hpp
|
28 |
+
#define ios_hpp
|
29 |
+
|
30 |
+
#if __APPLE__
|
31 |
+
# include <TargetConditionals.h>
|
32 |
+
# if TARGET_OS_IPHONE
|
33 |
+
# include <string>
|
34 |
+
|
35 |
+
namespace arrow_vendored
|
36 |
+
{
|
37 |
+
namespace date
|
38 |
+
{
|
39 |
+
namespace iOSUtils
|
40 |
+
{
|
41 |
+
|
42 |
+
std::string get_tzdata_path();
|
43 |
+
std::string get_current_timezone();
|
44 |
+
|
45 |
+
} // namespace iOSUtils
|
46 |
+
} // namespace date
|
47 |
+
} // namespace arrow_vendored
|
48 |
+
|
49 |
+
# endif // TARGET_OS_IPHONE
|
50 |
+
#else // !__APPLE__
|
51 |
+
# define TARGET_OS_IPHONE 0
|
52 |
+
#endif // !__APPLE__
|
53 |
+
#endif // ios_hpp
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/tz.h
ADDED
@@ -0,0 +1,2801 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef TZ_H
|
2 |
+
#define TZ_H
|
3 |
+
|
4 |
+
// The MIT License (MIT)
|
5 |
+
//
|
6 |
+
// Copyright (c) 2015, 2016, 2017 Howard Hinnant
|
7 |
+
// Copyright (c) 2017 Jiangang Zhuang
|
8 |
+
// Copyright (c) 2017 Aaron Bishop
|
9 |
+
// Copyright (c) 2017 Tomasz Kamiński
|
10 |
+
//
|
11 |
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
12 |
+
// of this software and associated documentation files (the "Software"), to deal
|
13 |
+
// in the Software without restriction, including without limitation the rights
|
14 |
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
15 |
+
// copies of the Software, and to permit persons to whom the Software is
|
16 |
+
// furnished to do so, subject to the following conditions:
|
17 |
+
//
|
18 |
+
// The above copyright notice and this permission notice shall be included in all
|
19 |
+
// copies or substantial portions of the Software.
|
20 |
+
//
|
21 |
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22 |
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23 |
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24 |
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25 |
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
26 |
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
27 |
+
// SOFTWARE.
|
28 |
+
//
|
29 |
+
// Our apologies. When the previous paragraph was written, lowercase had not yet
|
30 |
+
// been invented (that would involve another several millennia of evolution).
|
31 |
+
// We did not mean to shout.
|
32 |
+
|
33 |
+
// Get more recent database at http://www.iana.org/time-zones
|
34 |
+
|
35 |
+
// The notion of "current timezone" is something the operating system is expected to "just
|
36 |
+
// know". How it knows this is system specific. It's often a value set by the user at OS
|
37 |
+
// installation time and recorded by the OS somewhere. On Linux and Mac systems the current
|
38 |
+
// timezone name is obtained by looking at the name or contents of a particular file on
|
39 |
+
// disk. On Windows the current timezone name comes from the registry. In either method,
|
40 |
+
// there is no guarantee that the "native" current timezone name obtained will match any
|
41 |
+
// of the "Standard" names in this library's "database". On Linux, the names usually do
|
42 |
+
// seem to match so mapping functions to map from native to "Standard" are typically not
|
43 |
+
// required. On Windows, the names are never "Standard" so mapping is always required.
|
44 |
+
// Technically any OS may use the mapping process but currently only Windows does use it.
|
45 |
+
|
46 |
+
// NOTE(ARROW): If this is not set, then the library will attempt to
|
47 |
+
// use libcurl to obtain a timezone database, and we probably do not want this.
|
48 |
+
#ifndef _WIN32
|
49 |
+
#define USE_OS_TZDB 1
|
50 |
+
#endif
|
51 |
+
|
52 |
+
#ifndef USE_OS_TZDB
|
53 |
+
# define USE_OS_TZDB 0
|
54 |
+
#endif
|
55 |
+
|
56 |
+
#ifndef HAS_REMOTE_API
|
57 |
+
# if USE_OS_TZDB == 0
|
58 |
+
# ifdef _WIN32
|
59 |
+
# define HAS_REMOTE_API 0
|
60 |
+
# else
|
61 |
+
# define HAS_REMOTE_API 1
|
62 |
+
# endif
|
63 |
+
# else // HAS_REMOTE_API makes no sense when using the OS timezone database
|
64 |
+
# define HAS_REMOTE_API 0
|
65 |
+
# endif
|
66 |
+
#endif
|
67 |
+
|
68 |
+
#ifdef __clang__
|
69 |
+
# pragma clang diagnostic push
|
70 |
+
# pragma clang diagnostic ignored "-Wconstant-logical-operand"
|
71 |
+
#endif
|
72 |
+
|
73 |
+
static_assert(!(USE_OS_TZDB && HAS_REMOTE_API),
|
74 |
+
"USE_OS_TZDB and HAS_REMOTE_API can not be used together");
|
75 |
+
|
76 |
+
#ifdef __clang__
|
77 |
+
# pragma clang diagnostic pop
|
78 |
+
#endif
|
79 |
+
|
80 |
+
#ifndef AUTO_DOWNLOAD
|
81 |
+
# define AUTO_DOWNLOAD HAS_REMOTE_API
|
82 |
+
#endif
|
83 |
+
|
84 |
+
static_assert(HAS_REMOTE_API == 0 ? AUTO_DOWNLOAD == 0 : true,
|
85 |
+
"AUTO_DOWNLOAD can not be turned on without HAS_REMOTE_API");
|
86 |
+
|
87 |
+
#ifndef USE_SHELL_API
|
88 |
+
# define USE_SHELL_API 1
|
89 |
+
#endif
|
90 |
+
|
91 |
+
#if USE_OS_TZDB
|
92 |
+
# ifdef _WIN32
|
93 |
+
# error "USE_OS_TZDB can not be used on Windows"
|
94 |
+
# endif
|
95 |
+
#endif
|
96 |
+
|
97 |
+
#ifndef HAS_DEDUCTION_GUIDES
|
98 |
+
# if __cplusplus >= 201703
|
99 |
+
# define HAS_DEDUCTION_GUIDES 1
|
100 |
+
# else
|
101 |
+
# define HAS_DEDUCTION_GUIDES 0
|
102 |
+
# endif
|
103 |
+
#endif // HAS_DEDUCTION_GUIDES
|
104 |
+
|
105 |
+
#include "date.h"
|
106 |
+
|
107 |
+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
108 |
+
#include "tz_private.h"
|
109 |
+
#endif
|
110 |
+
|
111 |
+
#include <algorithm>
|
112 |
+
#include <atomic>
|
113 |
+
#include <cassert>
|
114 |
+
#include <chrono>
|
115 |
+
#include <istream>
|
116 |
+
#include <locale>
|
117 |
+
#include <memory>
|
118 |
+
#include <mutex>
|
119 |
+
#include <ostream>
|
120 |
+
#include <sstream>
|
121 |
+
#include <stdexcept>
|
122 |
+
#include <string>
|
123 |
+
#include <type_traits>
|
124 |
+
#include <utility>
|
125 |
+
#include <vector>
|
126 |
+
|
127 |
+
#ifdef _WIN32
|
128 |
+
# ifdef DATE_BUILD_DLL
|
129 |
+
# define DATE_API __declspec(dllexport)
|
130 |
+
# elif defined(DATE_USE_DLL)
|
131 |
+
# define DATE_API __declspec(dllimport)
|
132 |
+
# else
|
133 |
+
# define DATE_API
|
134 |
+
# endif
|
135 |
+
#else
|
136 |
+
# ifdef DATE_BUILD_DLL
|
137 |
+
# define DATE_API __attribute__ ((visibility ("default")))
|
138 |
+
# else
|
139 |
+
# define DATE_API
|
140 |
+
# endif
|
141 |
+
#endif
|
142 |
+
|
143 |
+
namespace arrow_vendored
|
144 |
+
{
|
145 |
+
namespace date
|
146 |
+
{
|
147 |
+
|
148 |
+
enum class choose {earliest, latest};
|
149 |
+
|
150 |
+
namespace detail
|
151 |
+
{
|
152 |
+
struct undocumented;
|
153 |
+
|
154 |
+
template<typename T>
|
155 |
+
struct nodeduct
|
156 |
+
{
|
157 |
+
using type = T;
|
158 |
+
};
|
159 |
+
|
160 |
+
template<typename T>
|
161 |
+
using nodeduct_t = typename nodeduct<T>::type;
|
162 |
+
}
|
163 |
+
|
164 |
+
struct sys_info
|
165 |
+
{
|
166 |
+
sys_seconds begin;
|
167 |
+
sys_seconds end;
|
168 |
+
std::chrono::seconds offset;
|
169 |
+
std::chrono::minutes save;
|
170 |
+
std::string abbrev;
|
171 |
+
};
|
172 |
+
|
173 |
+
template<class CharT, class Traits>
|
174 |
+
std::basic_ostream<CharT, Traits>&
|
175 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const sys_info& r)
|
176 |
+
{
|
177 |
+
os << r.begin << '\n';
|
178 |
+
os << r.end << '\n';
|
179 |
+
os << make_time(r.offset) << "\n";
|
180 |
+
os << make_time(r.save) << "\n";
|
181 |
+
os << r.abbrev << '\n';
|
182 |
+
return os;
|
183 |
+
}
|
184 |
+
|
185 |
+
struct local_info
|
186 |
+
{
|
187 |
+
enum {unique, nonexistent, ambiguous} result;
|
188 |
+
sys_info first;
|
189 |
+
sys_info second;
|
190 |
+
};
|
191 |
+
|
192 |
+
template<class CharT, class Traits>
|
193 |
+
std::basic_ostream<CharT, Traits>&
|
194 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const local_info& r)
|
195 |
+
{
|
196 |
+
if (r.result == local_info::nonexistent)
|
197 |
+
os << "nonexistent between\n";
|
198 |
+
else if (r.result == local_info::ambiguous)
|
199 |
+
os << "ambiguous between\n";
|
200 |
+
os << r.first;
|
201 |
+
if (r.result != local_info::unique)
|
202 |
+
{
|
203 |
+
os << "and\n";
|
204 |
+
os << r.second;
|
205 |
+
}
|
206 |
+
return os;
|
207 |
+
}
|
208 |
+
|
209 |
+
class nonexistent_local_time
|
210 |
+
: public std::runtime_error
|
211 |
+
{
|
212 |
+
public:
|
213 |
+
template <class Duration>
|
214 |
+
nonexistent_local_time(local_time<Duration> tp, const local_info& i);
|
215 |
+
|
216 |
+
private:
|
217 |
+
template <class Duration>
|
218 |
+
static
|
219 |
+
std::string
|
220 |
+
make_msg(local_time<Duration> tp, const local_info& i);
|
221 |
+
};
|
222 |
+
|
223 |
+
template <class Duration>
|
224 |
+
inline
|
225 |
+
nonexistent_local_time::nonexistent_local_time(local_time<Duration> tp,
|
226 |
+
const local_info& i)
|
227 |
+
: std::runtime_error(make_msg(tp, i))
|
228 |
+
{
|
229 |
+
}
|
230 |
+
|
231 |
+
template <class Duration>
|
232 |
+
std::string
|
233 |
+
nonexistent_local_time::make_msg(local_time<Duration> tp, const local_info& i)
|
234 |
+
{
|
235 |
+
assert(i.result == local_info::nonexistent);
|
236 |
+
std::ostringstream os;
|
237 |
+
os << tp << " is in a gap between\n"
|
238 |
+
<< local_seconds{i.first.end.time_since_epoch()} + i.first.offset << ' '
|
239 |
+
<< i.first.abbrev << " and\n"
|
240 |
+
<< local_seconds{i.second.begin.time_since_epoch()} + i.second.offset << ' '
|
241 |
+
<< i.second.abbrev
|
242 |
+
<< " which are both equivalent to\n"
|
243 |
+
<< i.first.end << " UTC";
|
244 |
+
return os.str();
|
245 |
+
}
|
246 |
+
|
247 |
+
class ambiguous_local_time
|
248 |
+
: public std::runtime_error
|
249 |
+
{
|
250 |
+
public:
|
251 |
+
template <class Duration>
|
252 |
+
ambiguous_local_time(local_time<Duration> tp, const local_info& i);
|
253 |
+
|
254 |
+
private:
|
255 |
+
template <class Duration>
|
256 |
+
static
|
257 |
+
std::string
|
258 |
+
make_msg(local_time<Duration> tp, const local_info& i);
|
259 |
+
};
|
260 |
+
|
261 |
+
template <class Duration>
|
262 |
+
inline
|
263 |
+
ambiguous_local_time::ambiguous_local_time(local_time<Duration> tp, const local_info& i)
|
264 |
+
: std::runtime_error(make_msg(tp, i))
|
265 |
+
{
|
266 |
+
}
|
267 |
+
|
268 |
+
template <class Duration>
|
269 |
+
std::string
|
270 |
+
ambiguous_local_time::make_msg(local_time<Duration> tp, const local_info& i)
|
271 |
+
{
|
272 |
+
assert(i.result == local_info::ambiguous);
|
273 |
+
std::ostringstream os;
|
274 |
+
os << tp << " is ambiguous. It could be\n"
|
275 |
+
<< tp << ' ' << i.first.abbrev << " == "
|
276 |
+
<< tp - i.first.offset << " UTC or\n"
|
277 |
+
<< tp << ' ' << i.second.abbrev << " == "
|
278 |
+
<< tp - i.second.offset << " UTC";
|
279 |
+
return os.str();
|
280 |
+
}
|
281 |
+
|
282 |
+
class time_zone;
|
283 |
+
|
284 |
+
#if HAS_STRING_VIEW
|
285 |
+
DATE_API const time_zone* locate_zone(std::string_view tz_name);
|
286 |
+
#else
|
287 |
+
DATE_API const time_zone* locate_zone(const std::string& tz_name);
|
288 |
+
#endif
|
289 |
+
|
290 |
+
DATE_API const time_zone* current_zone();
|
291 |
+
|
292 |
+
template <class T>
|
293 |
+
struct zoned_traits
|
294 |
+
{
|
295 |
+
};
|
296 |
+
|
297 |
+
template <>
|
298 |
+
struct zoned_traits<const time_zone*>
|
299 |
+
{
|
300 |
+
static
|
301 |
+
const time_zone*
|
302 |
+
default_zone()
|
303 |
+
{
|
304 |
+
return date::locate_zone("Etc/UTC");
|
305 |
+
}
|
306 |
+
|
307 |
+
#if HAS_STRING_VIEW
|
308 |
+
|
309 |
+
static
|
310 |
+
const time_zone*
|
311 |
+
locate_zone(std::string_view name)
|
312 |
+
{
|
313 |
+
return date::locate_zone(name);
|
314 |
+
}
|
315 |
+
|
316 |
+
#else // !HAS_STRING_VIEW
|
317 |
+
|
318 |
+
static
|
319 |
+
const time_zone*
|
320 |
+
locate_zone(const std::string& name)
|
321 |
+
{
|
322 |
+
return date::locate_zone(name);
|
323 |
+
}
|
324 |
+
|
325 |
+
static
|
326 |
+
const time_zone*
|
327 |
+
locate_zone(const char* name)
|
328 |
+
{
|
329 |
+
return date::locate_zone(name);
|
330 |
+
}
|
331 |
+
|
332 |
+
#endif // !HAS_STRING_VIEW
|
333 |
+
};
|
334 |
+
|
335 |
+
template <class Duration, class TimeZonePtr>
|
336 |
+
class zoned_time;
|
337 |
+
|
338 |
+
template <class Duration1, class Duration2, class TimeZonePtr>
|
339 |
+
bool
|
340 |
+
operator==(const zoned_time<Duration1, TimeZonePtr>& x,
|
341 |
+
const zoned_time<Duration2, TimeZonePtr>& y);
|
342 |
+
|
343 |
+
template <class Duration, class TimeZonePtr = const time_zone*>
|
344 |
+
class zoned_time
|
345 |
+
{
|
346 |
+
public:
|
347 |
+
using duration = typename std::common_type<Duration, std::chrono::seconds>::type;
|
348 |
+
|
349 |
+
private:
|
350 |
+
TimeZonePtr zone_;
|
351 |
+
sys_time<duration> tp_;
|
352 |
+
|
353 |
+
public:
|
354 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
355 |
+
template <class T = TimeZonePtr,
|
356 |
+
class = decltype(zoned_traits<T>::default_zone())>
|
357 |
+
#endif
|
358 |
+
zoned_time();
|
359 |
+
|
360 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
361 |
+
template <class T = TimeZonePtr,
|
362 |
+
class = decltype(zoned_traits<T>::default_zone())>
|
363 |
+
#endif
|
364 |
+
zoned_time(const sys_time<Duration>& st);
|
365 |
+
explicit zoned_time(TimeZonePtr z);
|
366 |
+
|
367 |
+
#if HAS_STRING_VIEW
|
368 |
+
template <class T = TimeZonePtr,
|
369 |
+
class = typename std::enable_if
|
370 |
+
<
|
371 |
+
std::is_constructible
|
372 |
+
<
|
373 |
+
zoned_time,
|
374 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view()))
|
375 |
+
>::value
|
376 |
+
>::type>
|
377 |
+
explicit zoned_time(std::string_view name);
|
378 |
+
#else
|
379 |
+
# if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
380 |
+
template <class T = TimeZonePtr,
|
381 |
+
class = typename std::enable_if
|
382 |
+
<
|
383 |
+
std::is_constructible
|
384 |
+
<
|
385 |
+
zoned_time,
|
386 |
+
decltype(zoned_traits<T>::locate_zone(std::string()))
|
387 |
+
>::value
|
388 |
+
>::type>
|
389 |
+
# endif
|
390 |
+
explicit zoned_time(const std::string& name);
|
391 |
+
#endif
|
392 |
+
|
393 |
+
template <class Duration2,
|
394 |
+
class = typename std::enable_if
|
395 |
+
<
|
396 |
+
std::is_convertible<sys_time<Duration2>,
|
397 |
+
sys_time<Duration>>::value
|
398 |
+
>::type>
|
399 |
+
zoned_time(const zoned_time<Duration2, TimeZonePtr>& zt) NOEXCEPT;
|
400 |
+
|
401 |
+
zoned_time(TimeZonePtr z, const sys_time<Duration>& st);
|
402 |
+
|
403 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
404 |
+
template <class T = TimeZonePtr,
|
405 |
+
class = typename std::enable_if
|
406 |
+
<
|
407 |
+
std::is_convertible
|
408 |
+
<
|
409 |
+
decltype(std::declval<T&>()->to_sys(local_time<Duration>{})),
|
410 |
+
sys_time<duration>
|
411 |
+
>::value
|
412 |
+
>::type>
|
413 |
+
#endif
|
414 |
+
zoned_time(TimeZonePtr z, const local_time<Duration>& tp);
|
415 |
+
|
416 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
417 |
+
template <class T = TimeZonePtr,
|
418 |
+
class = typename std::enable_if
|
419 |
+
<
|
420 |
+
std::is_convertible
|
421 |
+
<
|
422 |
+
decltype(std::declval<T&>()->to_sys(local_time<Duration>{},
|
423 |
+
choose::earliest)),
|
424 |
+
sys_time<duration>
|
425 |
+
>::value
|
426 |
+
>::type>
|
427 |
+
#endif
|
428 |
+
zoned_time(TimeZonePtr z, const local_time<Duration>& tp, choose c);
|
429 |
+
|
430 |
+
template <class Duration2, class TimeZonePtr2,
|
431 |
+
class = typename std::enable_if
|
432 |
+
<
|
433 |
+
std::is_convertible<sys_time<Duration2>,
|
434 |
+
sys_time<Duration>>::value
|
435 |
+
>::type>
|
436 |
+
zoned_time(TimeZonePtr z, const zoned_time<Duration2, TimeZonePtr2>& zt);
|
437 |
+
|
438 |
+
template <class Duration2, class TimeZonePtr2,
|
439 |
+
class = typename std::enable_if
|
440 |
+
<
|
441 |
+
std::is_convertible<sys_time<Duration2>,
|
442 |
+
sys_time<Duration>>::value
|
443 |
+
>::type>
|
444 |
+
zoned_time(TimeZonePtr z, const zoned_time<Duration2, TimeZonePtr2>& zt, choose);
|
445 |
+
|
446 |
+
#if HAS_STRING_VIEW
|
447 |
+
|
448 |
+
template <class T = TimeZonePtr,
|
449 |
+
class = typename std::enable_if
|
450 |
+
<
|
451 |
+
std::is_constructible
|
452 |
+
<
|
453 |
+
zoned_time,
|
454 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view())),
|
455 |
+
sys_time<Duration>
|
456 |
+
>::value
|
457 |
+
>::type>
|
458 |
+
zoned_time(std::string_view name, detail::nodeduct_t<const sys_time<Duration>&> st);
|
459 |
+
|
460 |
+
template <class T = TimeZonePtr,
|
461 |
+
class = typename std::enable_if
|
462 |
+
<
|
463 |
+
std::is_constructible
|
464 |
+
<
|
465 |
+
zoned_time,
|
466 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view())),
|
467 |
+
local_time<Duration>
|
468 |
+
>::value
|
469 |
+
>::type>
|
470 |
+
zoned_time(std::string_view name, detail::nodeduct_t<const local_time<Duration>&> tp);
|
471 |
+
|
472 |
+
template <class T = TimeZonePtr,
|
473 |
+
class = typename std::enable_if
|
474 |
+
<
|
475 |
+
std::is_constructible
|
476 |
+
<
|
477 |
+
zoned_time,
|
478 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view())),
|
479 |
+
local_time<Duration>,
|
480 |
+
choose
|
481 |
+
>::value
|
482 |
+
>::type>
|
483 |
+
zoned_time(std::string_view name, detail::nodeduct_t<const local_time<Duration>&> tp, choose c);
|
484 |
+
|
485 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
486 |
+
class = typename std::enable_if
|
487 |
+
<
|
488 |
+
std::is_convertible<sys_time<Duration2>,
|
489 |
+
sys_time<Duration>>::value &&
|
490 |
+
std::is_constructible
|
491 |
+
<
|
492 |
+
zoned_time,
|
493 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view())),
|
494 |
+
zoned_time
|
495 |
+
>::value
|
496 |
+
>::type>
|
497 |
+
zoned_time(std::string_view name, const zoned_time<Duration2, TimeZonePtr2>& zt);
|
498 |
+
|
499 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
500 |
+
class = typename std::enable_if
|
501 |
+
<
|
502 |
+
std::is_convertible<sys_time<Duration2>,
|
503 |
+
sys_time<Duration>>::value &&
|
504 |
+
std::is_constructible
|
505 |
+
<
|
506 |
+
zoned_time,
|
507 |
+
decltype(zoned_traits<T>::locate_zone(std::string_view())),
|
508 |
+
zoned_time,
|
509 |
+
choose
|
510 |
+
>::value
|
511 |
+
>::type>
|
512 |
+
zoned_time(std::string_view name, const zoned_time<Duration2, TimeZonePtr2>& zt, choose);
|
513 |
+
|
514 |
+
#else // !HAS_STRING_VIEW
|
515 |
+
|
516 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
517 |
+
template <class T = TimeZonePtr,
|
518 |
+
class = typename std::enable_if
|
519 |
+
<
|
520 |
+
std::is_constructible
|
521 |
+
<
|
522 |
+
zoned_time,
|
523 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
524 |
+
sys_time<Duration>
|
525 |
+
>::value
|
526 |
+
>::type>
|
527 |
+
#endif
|
528 |
+
zoned_time(const std::string& name, const sys_time<Duration>& st);
|
529 |
+
|
530 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
531 |
+
template <class T = TimeZonePtr,
|
532 |
+
class = typename std::enable_if
|
533 |
+
<
|
534 |
+
std::is_constructible
|
535 |
+
<
|
536 |
+
zoned_time,
|
537 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
538 |
+
sys_time<Duration>
|
539 |
+
>::value
|
540 |
+
>::type>
|
541 |
+
#endif
|
542 |
+
zoned_time(const char* name, const sys_time<Duration>& st);
|
543 |
+
|
544 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
545 |
+
template <class T = TimeZonePtr,
|
546 |
+
class = typename std::enable_if
|
547 |
+
<
|
548 |
+
std::is_constructible
|
549 |
+
<
|
550 |
+
zoned_time,
|
551 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
552 |
+
local_time<Duration>
|
553 |
+
>::value
|
554 |
+
>::type>
|
555 |
+
#endif
|
556 |
+
zoned_time(const std::string& name, const local_time<Duration>& tp);
|
557 |
+
|
558 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
559 |
+
template <class T = TimeZonePtr,
|
560 |
+
class = typename std::enable_if
|
561 |
+
<
|
562 |
+
std::is_constructible
|
563 |
+
<
|
564 |
+
zoned_time,
|
565 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
566 |
+
local_time<Duration>
|
567 |
+
>::value
|
568 |
+
>::type>
|
569 |
+
#endif
|
570 |
+
zoned_time(const char* name, const local_time<Duration>& tp);
|
571 |
+
|
572 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
573 |
+
template <class T = TimeZonePtr,
|
574 |
+
class = typename std::enable_if
|
575 |
+
<
|
576 |
+
std::is_constructible
|
577 |
+
<
|
578 |
+
zoned_time,
|
579 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
580 |
+
local_time<Duration>,
|
581 |
+
choose
|
582 |
+
>::value
|
583 |
+
>::type>
|
584 |
+
#endif
|
585 |
+
zoned_time(const std::string& name, const local_time<Duration>& tp, choose c);
|
586 |
+
|
587 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
588 |
+
template <class T = TimeZonePtr,
|
589 |
+
class = typename std::enable_if
|
590 |
+
<
|
591 |
+
std::is_constructible
|
592 |
+
<
|
593 |
+
zoned_time,
|
594 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
595 |
+
local_time<Duration>,
|
596 |
+
choose
|
597 |
+
>::value
|
598 |
+
>::type>
|
599 |
+
#endif
|
600 |
+
zoned_time(const char* name, const local_time<Duration>& tp, choose c);
|
601 |
+
|
602 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
603 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
604 |
+
class = typename std::enable_if
|
605 |
+
<
|
606 |
+
std::is_convertible<sys_time<Duration2>,
|
607 |
+
sys_time<Duration>>::value &&
|
608 |
+
std::is_constructible
|
609 |
+
<
|
610 |
+
zoned_time,
|
611 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
612 |
+
zoned_time
|
613 |
+
>::value
|
614 |
+
>::type>
|
615 |
+
#else
|
616 |
+
template <class Duration2, class TimeZonePtr2>
|
617 |
+
#endif
|
618 |
+
zoned_time(const std::string& name, const zoned_time<Duration2, TimeZonePtr2>& zt);
|
619 |
+
|
620 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
621 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
622 |
+
class = typename std::enable_if
|
623 |
+
<
|
624 |
+
std::is_convertible<sys_time<Duration2>,
|
625 |
+
sys_time<Duration>>::value &&
|
626 |
+
std::is_constructible
|
627 |
+
<
|
628 |
+
zoned_time,
|
629 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
630 |
+
zoned_time
|
631 |
+
>::value
|
632 |
+
>::type>
|
633 |
+
#else
|
634 |
+
template <class Duration2, class TimeZonePtr2>
|
635 |
+
#endif
|
636 |
+
zoned_time(const char* name, const zoned_time<Duration2, TimeZonePtr2>& zt);
|
637 |
+
|
638 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
639 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
640 |
+
class = typename std::enable_if
|
641 |
+
<
|
642 |
+
std::is_convertible<sys_time<Duration2>,
|
643 |
+
sys_time<Duration>>::value &&
|
644 |
+
std::is_constructible
|
645 |
+
<
|
646 |
+
zoned_time,
|
647 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
648 |
+
zoned_time,
|
649 |
+
choose
|
650 |
+
>::value
|
651 |
+
>::type>
|
652 |
+
#else
|
653 |
+
template <class Duration2, class TimeZonePtr2>
|
654 |
+
#endif
|
655 |
+
zoned_time(const std::string& name, const zoned_time<Duration2, TimeZonePtr2>& zt,
|
656 |
+
choose);
|
657 |
+
|
658 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
659 |
+
template <class Duration2, class TimeZonePtr2, class T = TimeZonePtr,
|
660 |
+
class = typename std::enable_if
|
661 |
+
<
|
662 |
+
std::is_convertible<sys_time<Duration2>,
|
663 |
+
sys_time<Duration>>::value &&
|
664 |
+
std::is_constructible
|
665 |
+
<
|
666 |
+
zoned_time,
|
667 |
+
decltype(zoned_traits<T>::locate_zone(std::string())),
|
668 |
+
zoned_time,
|
669 |
+
choose
|
670 |
+
>::value
|
671 |
+
>::type>
|
672 |
+
#else
|
673 |
+
template <class Duration2, class TimeZonePtr2>
|
674 |
+
#endif
|
675 |
+
zoned_time(const char* name, const zoned_time<Duration2, TimeZonePtr2>& zt,
|
676 |
+
choose);
|
677 |
+
|
678 |
+
#endif // !HAS_STRING_VIEW
|
679 |
+
|
680 |
+
zoned_time& operator=(const sys_time<Duration>& st);
|
681 |
+
zoned_time& operator=(const local_time<Duration>& ut);
|
682 |
+
|
683 |
+
explicit operator sys_time<duration>() const;
|
684 |
+
explicit operator local_time<duration>() const;
|
685 |
+
|
686 |
+
TimeZonePtr get_time_zone() const;
|
687 |
+
local_time<duration> get_local_time() const;
|
688 |
+
sys_time<duration> get_sys_time() const;
|
689 |
+
sys_info get_info() const;
|
690 |
+
|
691 |
+
template <class Duration1, class Duration2, class TimeZonePtr1>
|
692 |
+
friend
|
693 |
+
bool
|
694 |
+
operator==(const zoned_time<Duration1, TimeZonePtr1>& x,
|
695 |
+
const zoned_time<Duration2, TimeZonePtr1>& y);
|
696 |
+
|
697 |
+
template <class CharT, class Traits, class Duration1, class TimeZonePtr1>
|
698 |
+
friend
|
699 |
+
std::basic_ostream<CharT, Traits>&
|
700 |
+
operator<<(std::basic_ostream<CharT, Traits>& os,
|
701 |
+
const zoned_time<Duration1, TimeZonePtr1>& t);
|
702 |
+
|
703 |
+
private:
|
704 |
+
template <class D, class T> friend class zoned_time;
|
705 |
+
|
706 |
+
template <class TimeZonePtr2>
|
707 |
+
static
|
708 |
+
TimeZonePtr2&&
|
709 |
+
check(TimeZonePtr2&& p);
|
710 |
+
};
|
711 |
+
|
712 |
+
using zoned_seconds = zoned_time<std::chrono::seconds>;
|
713 |
+
|
714 |
+
#if HAS_DEDUCTION_GUIDES
|
715 |
+
|
716 |
+
namespace detail
|
717 |
+
{
|
718 |
+
template<typename TimeZonePtrOrName>
|
719 |
+
using time_zone_representation =
|
720 |
+
std::conditional_t
|
721 |
+
<
|
722 |
+
std::is_convertible<TimeZonePtrOrName, std::string_view>::value,
|
723 |
+
time_zone const*,
|
724 |
+
std::remove_cv_t<std::remove_reference_t<TimeZonePtrOrName>>
|
725 |
+
>;
|
726 |
+
}
|
727 |
+
|
728 |
+
zoned_time()
|
729 |
+
-> zoned_time<std::chrono::seconds>;
|
730 |
+
|
731 |
+
template <class Duration>
|
732 |
+
zoned_time(sys_time<Duration>)
|
733 |
+
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
|
734 |
+
|
735 |
+
template <class TimeZonePtrOrName>
|
736 |
+
zoned_time(TimeZonePtrOrName&&)
|
737 |
+
-> zoned_time<std::chrono::seconds, detail::time_zone_representation<TimeZonePtrOrName>>;
|
738 |
+
|
739 |
+
template <class TimeZonePtrOrName, class Duration>
|
740 |
+
zoned_time(TimeZonePtrOrName&&, sys_time<Duration>)
|
741 |
+
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>, detail::time_zone_representation<TimeZonePtrOrName>>;
|
742 |
+
|
743 |
+
template <class TimeZonePtrOrName, class Duration>
|
744 |
+
zoned_time(TimeZonePtrOrName&&, local_time<Duration>, choose = choose::earliest)
|
745 |
+
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>, detail::time_zone_representation<TimeZonePtrOrName>>;
|
746 |
+
|
747 |
+
template <class Duration, class TimeZonePtrOrName, class TimeZonePtr2>
|
748 |
+
zoned_time(TimeZonePtrOrName&&, zoned_time<Duration, TimeZonePtr2>, choose = choose::earliest)
|
749 |
+
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>, detail::time_zone_representation<TimeZonePtrOrName>>;
|
750 |
+
|
751 |
+
#endif // HAS_DEDUCTION_GUIDES
|
752 |
+
|
753 |
+
template <class Duration1, class Duration2, class TimeZonePtr>
|
754 |
+
inline
|
755 |
+
bool
|
756 |
+
operator==(const zoned_time<Duration1, TimeZonePtr>& x,
|
757 |
+
const zoned_time<Duration2, TimeZonePtr>& y)
|
758 |
+
{
|
759 |
+
return x.zone_ == y.zone_ && x.tp_ == y.tp_;
|
760 |
+
}
|
761 |
+
|
762 |
+
template <class Duration1, class Duration2, class TimeZonePtr>
|
763 |
+
inline
|
764 |
+
bool
|
765 |
+
operator!=(const zoned_time<Duration1, TimeZonePtr>& x,
|
766 |
+
const zoned_time<Duration2, TimeZonePtr>& y)
|
767 |
+
{
|
768 |
+
return !(x == y);
|
769 |
+
}
|
770 |
+
|
771 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
772 |
+
|
773 |
+
namespace detail
|
774 |
+
{
|
775 |
+
# if USE_OS_TZDB
|
776 |
+
struct transition;
|
777 |
+
struct expanded_ttinfo;
|
778 |
+
# else // !USE_OS_TZDB
|
779 |
+
struct zonelet;
|
780 |
+
class Rule;
|
781 |
+
# endif // !USE_OS_TZDB
|
782 |
+
}
|
783 |
+
|
784 |
+
#endif // !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
785 |
+
|
786 |
+
class time_zone
|
787 |
+
{
|
788 |
+
private:
|
789 |
+
std::string name_;
|
790 |
+
#if USE_OS_TZDB
|
791 |
+
std::vector<detail::transition> transitions_;
|
792 |
+
std::vector<detail::expanded_ttinfo> ttinfos_;
|
793 |
+
#else // !USE_OS_TZDB
|
794 |
+
std::vector<detail::zonelet> zonelets_;
|
795 |
+
#endif // !USE_OS_TZDB
|
796 |
+
std::unique_ptr<std::once_flag> adjusted_;
|
797 |
+
|
798 |
+
public:
|
799 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
800 |
+
time_zone(time_zone&&) = default;
|
801 |
+
time_zone& operator=(time_zone&&) = default;
|
802 |
+
#else // defined(_MSC_VER) && (_MSC_VER < 1900)
|
803 |
+
time_zone(time_zone&& src);
|
804 |
+
time_zone& operator=(time_zone&& src);
|
805 |
+
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
|
806 |
+
|
807 |
+
DATE_API explicit time_zone(const std::string& s, detail::undocumented);
|
808 |
+
|
809 |
+
const std::string& name() const NOEXCEPT;
|
810 |
+
|
811 |
+
template <class Duration> sys_info get_info(sys_time<Duration> st) const;
|
812 |
+
template <class Duration> local_info get_info(local_time<Duration> tp) const;
|
813 |
+
|
814 |
+
template <class Duration>
|
815 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
816 |
+
to_sys(local_time<Duration> tp) const;
|
817 |
+
|
818 |
+
template <class Duration>
|
819 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
820 |
+
to_sys(local_time<Duration> tp, choose z) const;
|
821 |
+
|
822 |
+
template <class Duration>
|
823 |
+
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
824 |
+
to_local(sys_time<Duration> tp) const;
|
825 |
+
|
826 |
+
friend bool operator==(const time_zone& x, const time_zone& y) NOEXCEPT;
|
827 |
+
friend bool operator< (const time_zone& x, const time_zone& y) NOEXCEPT;
|
828 |
+
friend DATE_API std::ostream& operator<<(std::ostream& os, const time_zone& z);
|
829 |
+
|
830 |
+
#if !USE_OS_TZDB
|
831 |
+
DATE_API void add(const std::string& s);
|
832 |
+
#endif // !USE_OS_TZDB
|
833 |
+
|
834 |
+
private:
|
835 |
+
DATE_API sys_info get_info_impl(sys_seconds tp) const;
|
836 |
+
DATE_API local_info get_info_impl(local_seconds tp) const;
|
837 |
+
|
838 |
+
template <class Duration>
|
839 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
840 |
+
to_sys_impl(local_time<Duration> tp, choose z, std::false_type) const;
|
841 |
+
template <class Duration>
|
842 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
843 |
+
to_sys_impl(local_time<Duration> tp, choose, std::true_type) const;
|
844 |
+
|
845 |
+
#if USE_OS_TZDB
|
846 |
+
DATE_API void init() const;
|
847 |
+
DATE_API void init_impl();
|
848 |
+
DATE_API sys_info
|
849 |
+
load_sys_info(std::vector<detail::transition>::const_iterator i) const;
|
850 |
+
|
851 |
+
template <class TimeType>
|
852 |
+
DATE_API void
|
853 |
+
load_data(std::istream& inf, std::int32_t tzh_leapcnt, std::int32_t tzh_timecnt,
|
854 |
+
std::int32_t tzh_typecnt, std::int32_t tzh_charcnt);
|
855 |
+
#else // !USE_OS_TZDB
|
856 |
+
DATE_API sys_info get_info_impl(sys_seconds tp, int tz_int) const;
|
857 |
+
DATE_API void adjust_infos(const std::vector<detail::Rule>& rules);
|
858 |
+
DATE_API void parse_info(std::istream& in);
|
859 |
+
#endif // !USE_OS_TZDB
|
860 |
+
};
|
861 |
+
|
862 |
+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
863 |
+
|
864 |
+
inline
|
865 |
+
time_zone::time_zone(time_zone&& src)
|
866 |
+
: name_(std::move(src.name_))
|
867 |
+
, zonelets_(std::move(src.zonelets_))
|
868 |
+
, adjusted_(std::move(src.adjusted_))
|
869 |
+
{}
|
870 |
+
|
871 |
+
inline
|
872 |
+
time_zone&
|
873 |
+
time_zone::operator=(time_zone&& src)
|
874 |
+
{
|
875 |
+
name_ = std::move(src.name_);
|
876 |
+
zonelets_ = std::move(src.zonelets_);
|
877 |
+
adjusted_ = std::move(src.adjusted_);
|
878 |
+
return *this;
|
879 |
+
}
|
880 |
+
|
881 |
+
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
|
882 |
+
|
883 |
+
inline
|
884 |
+
const std::string&
|
885 |
+
time_zone::name() const NOEXCEPT
|
886 |
+
{
|
887 |
+
return name_;
|
888 |
+
}
|
889 |
+
|
890 |
+
template <class Duration>
|
891 |
+
inline
|
892 |
+
sys_info
|
893 |
+
time_zone::get_info(sys_time<Duration> st) const
|
894 |
+
{
|
895 |
+
return get_info_impl(date::floor<std::chrono::seconds>(st));
|
896 |
+
}
|
897 |
+
|
898 |
+
template <class Duration>
|
899 |
+
inline
|
900 |
+
local_info
|
901 |
+
time_zone::get_info(local_time<Duration> tp) const
|
902 |
+
{
|
903 |
+
return get_info_impl(date::floor<std::chrono::seconds>(tp));
|
904 |
+
}
|
905 |
+
|
906 |
+
template <class Duration>
|
907 |
+
inline
|
908 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
909 |
+
time_zone::to_sys(local_time<Duration> tp) const
|
910 |
+
{
|
911 |
+
return to_sys_impl(tp, choose{}, std::true_type{});
|
912 |
+
}
|
913 |
+
|
914 |
+
template <class Duration>
|
915 |
+
inline
|
916 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
917 |
+
time_zone::to_sys(local_time<Duration> tp, choose z) const
|
918 |
+
{
|
919 |
+
return to_sys_impl(tp, z, std::false_type{});
|
920 |
+
}
|
921 |
+
|
922 |
+
template <class Duration>
|
923 |
+
inline
|
924 |
+
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
925 |
+
time_zone::to_local(sys_time<Duration> tp) const
|
926 |
+
{
|
927 |
+
using LT = local_time<typename std::common_type<Duration, std::chrono::seconds>::type>;
|
928 |
+
auto i = get_info(tp);
|
929 |
+
return LT{(tp + i.offset).time_since_epoch()};
|
930 |
+
}
|
931 |
+
|
932 |
+
inline bool operator==(const time_zone& x, const time_zone& y) NOEXCEPT {return x.name_ == y.name_;}
|
933 |
+
inline bool operator< (const time_zone& x, const time_zone& y) NOEXCEPT {return x.name_ < y.name_;}
|
934 |
+
|
935 |
+
inline bool operator!=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(x == y);}
|
936 |
+
inline bool operator> (const time_zone& x, const time_zone& y) NOEXCEPT {return y < x;}
|
937 |
+
inline bool operator<=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(y < x);}
|
938 |
+
inline bool operator>=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(x < y);}
|
939 |
+
|
940 |
+
template <class Duration>
|
941 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
942 |
+
time_zone::to_sys_impl(local_time<Duration> tp, choose z, std::false_type) const
|
943 |
+
{
|
944 |
+
auto i = get_info(tp);
|
945 |
+
if (i.result == local_info::nonexistent)
|
946 |
+
{
|
947 |
+
return i.first.end;
|
948 |
+
}
|
949 |
+
else if (i.result == local_info::ambiguous)
|
950 |
+
{
|
951 |
+
if (z == choose::latest)
|
952 |
+
return sys_time<Duration>{tp.time_since_epoch()} - i.second.offset;
|
953 |
+
}
|
954 |
+
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
|
955 |
+
}
|
956 |
+
|
957 |
+
template <class Duration>
|
958 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
959 |
+
time_zone::to_sys_impl(local_time<Duration> tp, choose, std::true_type) const
|
960 |
+
{
|
961 |
+
auto i = get_info(tp);
|
962 |
+
if (i.result == local_info::nonexistent)
|
963 |
+
throw nonexistent_local_time(tp, i);
|
964 |
+
else if (i.result == local_info::ambiguous)
|
965 |
+
throw ambiguous_local_time(tp, i);
|
966 |
+
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
|
967 |
+
}
|
968 |
+
|
969 |
+
#if !USE_OS_TZDB
|
970 |
+
|
971 |
+
class time_zone_link
|
972 |
+
{
|
973 |
+
private:
|
974 |
+
std::string name_;
|
975 |
+
std::string target_;
|
976 |
+
public:
|
977 |
+
DATE_API explicit time_zone_link(const std::string& s);
|
978 |
+
|
979 |
+
const std::string& name() const {return name_;}
|
980 |
+
const std::string& target() const {return target_;}
|
981 |
+
|
982 |
+
friend bool operator==(const time_zone_link& x, const time_zone_link& y) {return x.name_ == y.name_;}
|
983 |
+
friend bool operator< (const time_zone_link& x, const time_zone_link& y) {return x.name_ < y.name_;}
|
984 |
+
|
985 |
+
friend DATE_API std::ostream& operator<<(std::ostream& os, const time_zone_link& x);
|
986 |
+
};
|
987 |
+
|
988 |
+
using link = time_zone_link;
|
989 |
+
|
990 |
+
inline bool operator!=(const time_zone_link& x, const time_zone_link& y) {return !(x == y);}
|
991 |
+
inline bool operator> (const time_zone_link& x, const time_zone_link& y) {return y < x;}
|
992 |
+
inline bool operator<=(const time_zone_link& x, const time_zone_link& y) {return !(y < x);}
|
993 |
+
inline bool operator>=(const time_zone_link& x, const time_zone_link& y) {return !(x < y);}
|
994 |
+
|
995 |
+
#endif // !USE_OS_TZDB
|
996 |
+
|
997 |
+
class leap_second
|
998 |
+
{
|
999 |
+
private:
|
1000 |
+
sys_seconds date_;
|
1001 |
+
|
1002 |
+
public:
|
1003 |
+
#if USE_OS_TZDB
|
1004 |
+
DATE_API explicit leap_second(const sys_seconds& s, detail::undocumented);
|
1005 |
+
#else
|
1006 |
+
DATE_API explicit leap_second(const std::string& s, detail::undocumented);
|
1007 |
+
#endif
|
1008 |
+
|
1009 |
+
sys_seconds date() const {return date_;}
|
1010 |
+
|
1011 |
+
friend bool operator==(const leap_second& x, const leap_second& y) {return x.date_ == y.date_;}
|
1012 |
+
friend bool operator< (const leap_second& x, const leap_second& y) {return x.date_ < y.date_;}
|
1013 |
+
|
1014 |
+
template <class Duration>
|
1015 |
+
friend
|
1016 |
+
bool
|
1017 |
+
operator==(const leap_second& x, const sys_time<Duration>& y)
|
1018 |
+
{
|
1019 |
+
return x.date_ == y;
|
1020 |
+
}
|
1021 |
+
|
1022 |
+
template <class Duration>
|
1023 |
+
friend
|
1024 |
+
bool
|
1025 |
+
operator< (const leap_second& x, const sys_time<Duration>& y)
|
1026 |
+
{
|
1027 |
+
return x.date_ < y;
|
1028 |
+
}
|
1029 |
+
|
1030 |
+
template <class Duration>
|
1031 |
+
friend
|
1032 |
+
bool
|
1033 |
+
operator< (const sys_time<Duration>& x, const leap_second& y)
|
1034 |
+
{
|
1035 |
+
return x < y.date_;
|
1036 |
+
}
|
1037 |
+
|
1038 |
+
friend DATE_API std::ostream& operator<<(std::ostream& os, const leap_second& x);
|
1039 |
+
};
|
1040 |
+
|
1041 |
+
inline bool operator!=(const leap_second& x, const leap_second& y) {return !(x == y);}
|
1042 |
+
inline bool operator> (const leap_second& x, const leap_second& y) {return y < x;}
|
1043 |
+
inline bool operator<=(const leap_second& x, const leap_second& y) {return !(y < x);}
|
1044 |
+
inline bool operator>=(const leap_second& x, const leap_second& y) {return !(x < y);}
|
1045 |
+
|
1046 |
+
template <class Duration>
|
1047 |
+
inline
|
1048 |
+
bool
|
1049 |
+
operator==(const sys_time<Duration>& x, const leap_second& y)
|
1050 |
+
{
|
1051 |
+
return y == x;
|
1052 |
+
}
|
1053 |
+
|
1054 |
+
template <class Duration>
|
1055 |
+
inline
|
1056 |
+
bool
|
1057 |
+
operator!=(const leap_second& x, const sys_time<Duration>& y)
|
1058 |
+
{
|
1059 |
+
return !(x == y);
|
1060 |
+
}
|
1061 |
+
|
1062 |
+
template <class Duration>
|
1063 |
+
inline
|
1064 |
+
bool
|
1065 |
+
operator!=(const sys_time<Duration>& x, const leap_second& y)
|
1066 |
+
{
|
1067 |
+
return !(x == y);
|
1068 |
+
}
|
1069 |
+
|
1070 |
+
template <class Duration>
|
1071 |
+
inline
|
1072 |
+
bool
|
1073 |
+
operator> (const leap_second& x, const sys_time<Duration>& y)
|
1074 |
+
{
|
1075 |
+
return y < x;
|
1076 |
+
}
|
1077 |
+
|
1078 |
+
template <class Duration>
|
1079 |
+
inline
|
1080 |
+
bool
|
1081 |
+
operator> (const sys_time<Duration>& x, const leap_second& y)
|
1082 |
+
{
|
1083 |
+
return y < x;
|
1084 |
+
}
|
1085 |
+
|
1086 |
+
template <class Duration>
|
1087 |
+
inline
|
1088 |
+
bool
|
1089 |
+
operator<=(const leap_second& x, const sys_time<Duration>& y)
|
1090 |
+
{
|
1091 |
+
return !(y < x);
|
1092 |
+
}
|
1093 |
+
|
1094 |
+
template <class Duration>
|
1095 |
+
inline
|
1096 |
+
bool
|
1097 |
+
operator<=(const sys_time<Duration>& x, const leap_second& y)
|
1098 |
+
{
|
1099 |
+
return !(y < x);
|
1100 |
+
}
|
1101 |
+
|
1102 |
+
template <class Duration>
|
1103 |
+
inline
|
1104 |
+
bool
|
1105 |
+
operator>=(const leap_second& x, const sys_time<Duration>& y)
|
1106 |
+
{
|
1107 |
+
return !(x < y);
|
1108 |
+
}
|
1109 |
+
|
1110 |
+
template <class Duration>
|
1111 |
+
inline
|
1112 |
+
bool
|
1113 |
+
operator>=(const sys_time<Duration>& x, const leap_second& y)
|
1114 |
+
{
|
1115 |
+
return !(x < y);
|
1116 |
+
}
|
1117 |
+
|
1118 |
+
using leap = leap_second;
|
1119 |
+
|
1120 |
+
#ifdef _WIN32
|
1121 |
+
|
1122 |
+
namespace detail
|
1123 |
+
{
|
1124 |
+
|
1125 |
+
// The time zone mapping is modelled after this data file:
|
1126 |
+
// http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml
|
1127 |
+
// and the field names match the element names from the mapZone element
|
1128 |
+
// of windowsZones.xml.
|
1129 |
+
// The website displays this file here:
|
1130 |
+
// http://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html
|
1131 |
+
// The html view is sorted before being displayed but is otherwise the same
|
1132 |
+
// There is a mapping between the os centric view (in this case windows)
|
1133 |
+
// the html displays uses and the generic view the xml file.
|
1134 |
+
// That mapping is this:
|
1135 |
+
// display column "windows" -> xml field "other".
|
1136 |
+
// display column "region" -> xml field "territory".
|
1137 |
+
// display column "tzid" -> xml field "type".
|
1138 |
+
// This structure uses the generic terminology because it could be
|
1139 |
+
// used to to support other os/native name conversions, not just windows,
|
1140 |
+
// and using the same generic names helps retain the connection to the
|
1141 |
+
// origin of the data that we are using.
|
1142 |
+
struct timezone_mapping
|
1143 |
+
{
|
1144 |
+
timezone_mapping(const char* other, const char* territory, const char* type)
|
1145 |
+
: other(other), territory(territory), type(type)
|
1146 |
+
{
|
1147 |
+
}
|
1148 |
+
timezone_mapping() = default;
|
1149 |
+
std::string other;
|
1150 |
+
std::string territory;
|
1151 |
+
std::string type;
|
1152 |
+
};
|
1153 |
+
|
1154 |
+
} // detail
|
1155 |
+
|
1156 |
+
#endif // _WIN32
|
1157 |
+
|
1158 |
+
struct tzdb
|
1159 |
+
{
|
1160 |
+
std::string version = "unknown";
|
1161 |
+
std::vector<time_zone> zones;
|
1162 |
+
#if !USE_OS_TZDB
|
1163 |
+
std::vector<time_zone_link> links;
|
1164 |
+
#endif
|
1165 |
+
std::vector<leap_second> leap_seconds;
|
1166 |
+
#if !USE_OS_TZDB
|
1167 |
+
std::vector<detail::Rule> rules;
|
1168 |
+
#endif
|
1169 |
+
#ifdef _WIN32
|
1170 |
+
std::vector<detail::timezone_mapping> mappings;
|
1171 |
+
#endif
|
1172 |
+
tzdb* next = nullptr;
|
1173 |
+
|
1174 |
+
tzdb() = default;
|
1175 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
1176 |
+
tzdb(tzdb&&) = default;
|
1177 |
+
tzdb& operator=(tzdb&&) = default;
|
1178 |
+
#else // defined(_MSC_VER) && (_MSC_VER < 1900)
|
1179 |
+
tzdb(tzdb&& src)
|
1180 |
+
: version(std::move(src.version))
|
1181 |
+
, zones(std::move(src.zones))
|
1182 |
+
, links(std::move(src.links))
|
1183 |
+
, leap_seconds(std::move(src.leap_seconds))
|
1184 |
+
, rules(std::move(src.rules))
|
1185 |
+
, mappings(std::move(src.mappings))
|
1186 |
+
{}
|
1187 |
+
|
1188 |
+
tzdb& operator=(tzdb&& src)
|
1189 |
+
{
|
1190 |
+
version = std::move(src.version);
|
1191 |
+
zones = std::move(src.zones);
|
1192 |
+
links = std::move(src.links);
|
1193 |
+
leap_seconds = std::move(src.leap_seconds);
|
1194 |
+
rules = std::move(src.rules);
|
1195 |
+
mappings = std::move(src.mappings);
|
1196 |
+
return *this;
|
1197 |
+
}
|
1198 |
+
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
|
1199 |
+
|
1200 |
+
#if HAS_STRING_VIEW
|
1201 |
+
const time_zone* locate_zone(std::string_view tz_name) const;
|
1202 |
+
#else
|
1203 |
+
const time_zone* locate_zone(const std::string& tz_name) const;
|
1204 |
+
#endif
|
1205 |
+
const time_zone* current_zone() const;
|
1206 |
+
};
|
1207 |
+
|
1208 |
+
using TZ_DB = tzdb;
|
1209 |
+
|
1210 |
+
DATE_API std::ostream&
|
1211 |
+
operator<<(std::ostream& os, const tzdb& db);
|
1212 |
+
|
1213 |
+
DATE_API const tzdb& get_tzdb();
|
1214 |
+
|
1215 |
+
class tzdb_list
|
1216 |
+
{
|
1217 |
+
std::atomic<tzdb*> head_{nullptr};
|
1218 |
+
|
1219 |
+
public:
|
1220 |
+
~tzdb_list();
|
1221 |
+
tzdb_list() = default;
|
1222 |
+
tzdb_list(tzdb_list&& x) NOEXCEPT;
|
1223 |
+
|
1224 |
+
const tzdb& front() const NOEXCEPT {return *head_;}
|
1225 |
+
tzdb& front() NOEXCEPT {return *head_;}
|
1226 |
+
|
1227 |
+
class const_iterator;
|
1228 |
+
|
1229 |
+
const_iterator begin() const NOEXCEPT;
|
1230 |
+
const_iterator end() const NOEXCEPT;
|
1231 |
+
|
1232 |
+
const_iterator cbegin() const NOEXCEPT;
|
1233 |
+
const_iterator cend() const NOEXCEPT;
|
1234 |
+
|
1235 |
+
const_iterator erase_after(const_iterator p) NOEXCEPT;
|
1236 |
+
|
1237 |
+
struct undocumented_helper;
|
1238 |
+
private:
|
1239 |
+
void push_front(tzdb* tzdb) NOEXCEPT;
|
1240 |
+
};
|
1241 |
+
|
1242 |
+
class tzdb_list::const_iterator
|
1243 |
+
{
|
1244 |
+
tzdb* p_ = nullptr;
|
1245 |
+
|
1246 |
+
explicit const_iterator(tzdb* p) NOEXCEPT : p_{p} {}
|
1247 |
+
public:
|
1248 |
+
const_iterator() = default;
|
1249 |
+
|
1250 |
+
using iterator_category = std::forward_iterator_tag;
|
1251 |
+
using value_type = tzdb;
|
1252 |
+
using reference = const value_type&;
|
1253 |
+
using pointer = const value_type*;
|
1254 |
+
using difference_type = std::ptrdiff_t;
|
1255 |
+
|
1256 |
+
reference operator*() const NOEXCEPT {return *p_;}
|
1257 |
+
pointer operator->() const NOEXCEPT {return p_;}
|
1258 |
+
|
1259 |
+
const_iterator& operator++() NOEXCEPT {p_ = p_->next; return *this;}
|
1260 |
+
const_iterator operator++(int) NOEXCEPT {auto t = *this; ++(*this); return t;}
|
1261 |
+
|
1262 |
+
friend
|
1263 |
+
bool
|
1264 |
+
operator==(const const_iterator& x, const const_iterator& y) NOEXCEPT
|
1265 |
+
{return x.p_ == y.p_;}
|
1266 |
+
|
1267 |
+
friend
|
1268 |
+
bool
|
1269 |
+
operator!=(const const_iterator& x, const const_iterator& y) NOEXCEPT
|
1270 |
+
{return !(x == y);}
|
1271 |
+
|
1272 |
+
friend class tzdb_list;
|
1273 |
+
};
|
1274 |
+
|
1275 |
+
inline
|
1276 |
+
tzdb_list::const_iterator
|
1277 |
+
tzdb_list::begin() const NOEXCEPT
|
1278 |
+
{
|
1279 |
+
return const_iterator{head_};
|
1280 |
+
}
|
1281 |
+
|
1282 |
+
inline
|
1283 |
+
tzdb_list::const_iterator
|
1284 |
+
tzdb_list::end() const NOEXCEPT
|
1285 |
+
{
|
1286 |
+
return const_iterator{nullptr};
|
1287 |
+
}
|
1288 |
+
|
1289 |
+
inline
|
1290 |
+
tzdb_list::const_iterator
|
1291 |
+
tzdb_list::cbegin() const NOEXCEPT
|
1292 |
+
{
|
1293 |
+
return begin();
|
1294 |
+
}
|
1295 |
+
|
1296 |
+
inline
|
1297 |
+
tzdb_list::const_iterator
|
1298 |
+
tzdb_list::cend() const NOEXCEPT
|
1299 |
+
{
|
1300 |
+
return end();
|
1301 |
+
}
|
1302 |
+
|
1303 |
+
DATE_API tzdb_list& get_tzdb_list();
|
1304 |
+
|
1305 |
+
#if !USE_OS_TZDB
|
1306 |
+
|
1307 |
+
DATE_API const tzdb& reload_tzdb();
|
1308 |
+
DATE_API void set_install(const std::string& install);
|
1309 |
+
|
1310 |
+
#endif // !USE_OS_TZDB
|
1311 |
+
|
1312 |
+
#if HAS_REMOTE_API
|
1313 |
+
|
1314 |
+
DATE_API std::string remote_version();
|
1315 |
+
// if provided error_buffer size should be at least CURL_ERROR_SIZE
|
1316 |
+
DATE_API bool remote_download(const std::string& version, char* error_buffer = nullptr);
|
1317 |
+
DATE_API bool remote_install(const std::string& version);
|
1318 |
+
|
1319 |
+
#endif
|
1320 |
+
|
1321 |
+
// zoned_time
|
1322 |
+
|
1323 |
+
namespace detail
|
1324 |
+
{
|
1325 |
+
|
1326 |
+
template <class T>
|
1327 |
+
inline
|
1328 |
+
T*
|
1329 |
+
to_raw_pointer(T* p) NOEXCEPT
|
1330 |
+
{
|
1331 |
+
return p;
|
1332 |
+
}
|
1333 |
+
|
1334 |
+
template <class Pointer>
|
1335 |
+
inline
|
1336 |
+
auto
|
1337 |
+
to_raw_pointer(Pointer p) NOEXCEPT
|
1338 |
+
-> decltype(detail::to_raw_pointer(p.operator->()))
|
1339 |
+
{
|
1340 |
+
return detail::to_raw_pointer(p.operator->());
|
1341 |
+
}
|
1342 |
+
|
1343 |
+
} // namespace detail
|
1344 |
+
|
1345 |
+
template <class Duration, class TimeZonePtr>
|
1346 |
+
template <class TimeZonePtr2>
|
1347 |
+
inline
|
1348 |
+
TimeZonePtr2&&
|
1349 |
+
zoned_time<Duration, TimeZonePtr>::check(TimeZonePtr2&& p)
|
1350 |
+
{
|
1351 |
+
if (detail::to_raw_pointer(p) == nullptr)
|
1352 |
+
throw std::runtime_error(
|
1353 |
+
"zoned_time constructed with a time zone pointer == nullptr");
|
1354 |
+
return std::forward<TimeZonePtr2>(p);
|
1355 |
+
}
|
1356 |
+
|
1357 |
+
template <class Duration, class TimeZonePtr>
|
1358 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1359 |
+
template <class T, class>
|
1360 |
+
#endif
|
1361 |
+
inline
|
1362 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time()
|
1363 |
+
: zone_(check(zoned_traits<TimeZonePtr>::default_zone()))
|
1364 |
+
{}
|
1365 |
+
|
1366 |
+
template <class Duration, class TimeZonePtr>
|
1367 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1368 |
+
template <class T, class>
|
1369 |
+
#endif
|
1370 |
+
inline
|
1371 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const sys_time<Duration>& st)
|
1372 |
+
: zone_(check(zoned_traits<TimeZonePtr>::default_zone()))
|
1373 |
+
, tp_(st)
|
1374 |
+
{}
|
1375 |
+
|
1376 |
+
template <class Duration, class TimeZonePtr>
|
1377 |
+
inline
|
1378 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z)
|
1379 |
+
: zone_(check(std::move(z)))
|
1380 |
+
{}
|
1381 |
+
|
1382 |
+
#if HAS_STRING_VIEW
|
1383 |
+
|
1384 |
+
template <class Duration, class TimeZonePtr>
|
1385 |
+
template <class T, class>
|
1386 |
+
inline
|
1387 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name)
|
1388 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name))
|
1389 |
+
{}
|
1390 |
+
|
1391 |
+
#else // !HAS_STRING_VIEW
|
1392 |
+
|
1393 |
+
template <class Duration, class TimeZonePtr>
|
1394 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1395 |
+
template <class T, class>
|
1396 |
+
#endif
|
1397 |
+
inline
|
1398 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name)
|
1399 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name))
|
1400 |
+
{}
|
1401 |
+
|
1402 |
+
#endif // !HAS_STRING_VIEW
|
1403 |
+
|
1404 |
+
template <class Duration, class TimeZonePtr>
|
1405 |
+
template <class Duration2, class>
|
1406 |
+
inline
|
1407 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const zoned_time<Duration2, TimeZonePtr>& zt) NOEXCEPT
|
1408 |
+
: zone_(zt.zone_)
|
1409 |
+
, tp_(zt.tp_)
|
1410 |
+
{}
|
1411 |
+
|
1412 |
+
template <class Duration, class TimeZonePtr>
|
1413 |
+
inline
|
1414 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z, const sys_time<Duration>& st)
|
1415 |
+
: zone_(check(std::move(z)))
|
1416 |
+
, tp_(st)
|
1417 |
+
{}
|
1418 |
+
|
1419 |
+
template <class Duration, class TimeZonePtr>
|
1420 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1421 |
+
template <class T, class>
|
1422 |
+
#endif
|
1423 |
+
inline
|
1424 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z, const local_time<Duration>& t)
|
1425 |
+
: zone_(check(std::move(z)))
|
1426 |
+
, tp_(zone_->to_sys(t))
|
1427 |
+
{}
|
1428 |
+
|
1429 |
+
template <class Duration, class TimeZonePtr>
|
1430 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1431 |
+
template <class T, class>
|
1432 |
+
#endif
|
1433 |
+
inline
|
1434 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z, const local_time<Duration>& t,
|
1435 |
+
choose c)
|
1436 |
+
: zone_(check(std::move(z)))
|
1437 |
+
, tp_(zone_->to_sys(t, c))
|
1438 |
+
{}
|
1439 |
+
|
1440 |
+
template <class Duration, class TimeZonePtr>
|
1441 |
+
template <class Duration2, class TimeZonePtr2, class>
|
1442 |
+
inline
|
1443 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z,
|
1444 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt)
|
1445 |
+
: zone_(check(std::move(z)))
|
1446 |
+
, tp_(zt.tp_)
|
1447 |
+
{}
|
1448 |
+
|
1449 |
+
template <class Duration, class TimeZonePtr>
|
1450 |
+
template <class Duration2, class TimeZonePtr2, class>
|
1451 |
+
inline
|
1452 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(TimeZonePtr z,
|
1453 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt, choose)
|
1454 |
+
: zoned_time(std::move(z), zt)
|
1455 |
+
{}
|
1456 |
+
|
1457 |
+
#if HAS_STRING_VIEW
|
1458 |
+
|
1459 |
+
template <class Duration, class TimeZonePtr>
|
1460 |
+
template <class T, class>
|
1461 |
+
inline
|
1462 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name,
|
1463 |
+
detail::nodeduct_t<const sys_time<Duration>&> st)
|
1464 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), st)
|
1465 |
+
{}
|
1466 |
+
|
1467 |
+
template <class Duration, class TimeZonePtr>
|
1468 |
+
template <class T, class>
|
1469 |
+
inline
|
1470 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name,
|
1471 |
+
detail::nodeduct_t<const local_time<Duration>&> t)
|
1472 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t)
|
1473 |
+
{}
|
1474 |
+
|
1475 |
+
template <class Duration, class TimeZonePtr>
|
1476 |
+
template <class T, class>
|
1477 |
+
inline
|
1478 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name,
|
1479 |
+
detail::nodeduct_t<const local_time<Duration>&> t, choose c)
|
1480 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t, c)
|
1481 |
+
{}
|
1482 |
+
|
1483 |
+
template <class Duration, class TimeZonePtr>
|
1484 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1485 |
+
inline
|
1486 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name,
|
1487 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt)
|
1488 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt)
|
1489 |
+
{}
|
1490 |
+
|
1491 |
+
template <class Duration, class TimeZonePtr>
|
1492 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1493 |
+
inline
|
1494 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(std::string_view name,
|
1495 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt,
|
1496 |
+
choose c)
|
1497 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt, c)
|
1498 |
+
{}
|
1499 |
+
|
1500 |
+
#else // !HAS_STRING_VIEW
|
1501 |
+
|
1502 |
+
template <class Duration, class TimeZonePtr>
|
1503 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1504 |
+
template <class T, class>
|
1505 |
+
#endif
|
1506 |
+
inline
|
1507 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name,
|
1508 |
+
const sys_time<Duration>& st)
|
1509 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), st)
|
1510 |
+
{}
|
1511 |
+
|
1512 |
+
template <class Duration, class TimeZonePtr>
|
1513 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1514 |
+
template <class T, class>
|
1515 |
+
#endif
|
1516 |
+
inline
|
1517 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const char* name,
|
1518 |
+
const sys_time<Duration>& st)
|
1519 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), st)
|
1520 |
+
{}
|
1521 |
+
|
1522 |
+
template <class Duration, class TimeZonePtr>
|
1523 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1524 |
+
template <class T, class>
|
1525 |
+
#endif
|
1526 |
+
inline
|
1527 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name,
|
1528 |
+
const local_time<Duration>& t)
|
1529 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t)
|
1530 |
+
{}
|
1531 |
+
|
1532 |
+
template <class Duration, class TimeZonePtr>
|
1533 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1534 |
+
template <class T, class>
|
1535 |
+
#endif
|
1536 |
+
inline
|
1537 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const char* name,
|
1538 |
+
const local_time<Duration>& t)
|
1539 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t)
|
1540 |
+
{}
|
1541 |
+
|
1542 |
+
template <class Duration, class TimeZonePtr>
|
1543 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1544 |
+
template <class T, class>
|
1545 |
+
#endif
|
1546 |
+
inline
|
1547 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name,
|
1548 |
+
const local_time<Duration>& t, choose c)
|
1549 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t, c)
|
1550 |
+
{}
|
1551 |
+
|
1552 |
+
template <class Duration, class TimeZonePtr>
|
1553 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1554 |
+
template <class T, class>
|
1555 |
+
#endif
|
1556 |
+
inline
|
1557 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const char* name,
|
1558 |
+
const local_time<Duration>& t, choose c)
|
1559 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), t, c)
|
1560 |
+
{}
|
1561 |
+
|
1562 |
+
template <class Duration, class TimeZonePtr>
|
1563 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1564 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1565 |
+
#else
|
1566 |
+
template <class Duration2, class TimeZonePtr2>
|
1567 |
+
#endif
|
1568 |
+
inline
|
1569 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name,
|
1570 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt)
|
1571 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt)
|
1572 |
+
{}
|
1573 |
+
|
1574 |
+
template <class Duration, class TimeZonePtr>
|
1575 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1576 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1577 |
+
#else
|
1578 |
+
template <class Duration2, class TimeZonePtr2>
|
1579 |
+
#endif
|
1580 |
+
inline
|
1581 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const char* name,
|
1582 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt)
|
1583 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt)
|
1584 |
+
{}
|
1585 |
+
|
1586 |
+
template <class Duration, class TimeZonePtr>
|
1587 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1588 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1589 |
+
#else
|
1590 |
+
template <class Duration2, class TimeZonePtr2>
|
1591 |
+
#endif
|
1592 |
+
inline
|
1593 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const std::string& name,
|
1594 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt,
|
1595 |
+
choose c)
|
1596 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt, c)
|
1597 |
+
{}
|
1598 |
+
|
1599 |
+
template <class Duration, class TimeZonePtr>
|
1600 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1601 |
+
template <class Duration2, class TimeZonePtr2, class, class>
|
1602 |
+
#else
|
1603 |
+
template <class Duration2, class TimeZonePtr2>
|
1604 |
+
#endif
|
1605 |
+
inline
|
1606 |
+
zoned_time<Duration, TimeZonePtr>::zoned_time(const char* name,
|
1607 |
+
const zoned_time<Duration2, TimeZonePtr2>& zt,
|
1608 |
+
choose c)
|
1609 |
+
: zoned_time(zoned_traits<TimeZonePtr>::locate_zone(name), zt, c)
|
1610 |
+
{}
|
1611 |
+
|
1612 |
+
#endif // HAS_STRING_VIEW
|
1613 |
+
|
1614 |
+
template <class Duration, class TimeZonePtr>
|
1615 |
+
inline
|
1616 |
+
zoned_time<Duration, TimeZonePtr>&
|
1617 |
+
zoned_time<Duration, TimeZonePtr>::operator=(const sys_time<Duration>& st)
|
1618 |
+
{
|
1619 |
+
tp_ = st;
|
1620 |
+
return *this;
|
1621 |
+
}
|
1622 |
+
|
1623 |
+
template <class Duration, class TimeZonePtr>
|
1624 |
+
inline
|
1625 |
+
zoned_time<Duration, TimeZonePtr>&
|
1626 |
+
zoned_time<Duration, TimeZonePtr>::operator=(const local_time<Duration>& ut)
|
1627 |
+
{
|
1628 |
+
tp_ = zone_->to_sys(ut);
|
1629 |
+
return *this;
|
1630 |
+
}
|
1631 |
+
|
1632 |
+
template <class Duration, class TimeZonePtr>
|
1633 |
+
inline
|
1634 |
+
zoned_time<Duration, TimeZonePtr>::operator local_time<typename zoned_time<Duration, TimeZonePtr>::duration>() const
|
1635 |
+
{
|
1636 |
+
return get_local_time();
|
1637 |
+
}
|
1638 |
+
|
1639 |
+
template <class Duration, class TimeZonePtr>
|
1640 |
+
inline
|
1641 |
+
zoned_time<Duration, TimeZonePtr>::operator sys_time<typename zoned_time<Duration, TimeZonePtr>::duration>() const
|
1642 |
+
{
|
1643 |
+
return get_sys_time();
|
1644 |
+
}
|
1645 |
+
|
1646 |
+
template <class Duration, class TimeZonePtr>
|
1647 |
+
inline
|
1648 |
+
TimeZonePtr
|
1649 |
+
zoned_time<Duration, TimeZonePtr>::get_time_zone() const
|
1650 |
+
{
|
1651 |
+
return zone_;
|
1652 |
+
}
|
1653 |
+
|
1654 |
+
template <class Duration, class TimeZonePtr>
|
1655 |
+
inline
|
1656 |
+
local_time<typename zoned_time<Duration, TimeZonePtr>::duration>
|
1657 |
+
zoned_time<Duration, TimeZonePtr>::get_local_time() const
|
1658 |
+
{
|
1659 |
+
return zone_->to_local(tp_);
|
1660 |
+
}
|
1661 |
+
|
1662 |
+
template <class Duration, class TimeZonePtr>
|
1663 |
+
inline
|
1664 |
+
sys_time<typename zoned_time<Duration, TimeZonePtr>::duration>
|
1665 |
+
zoned_time<Duration, TimeZonePtr>::get_sys_time() const
|
1666 |
+
{
|
1667 |
+
return tp_;
|
1668 |
+
}
|
1669 |
+
|
1670 |
+
template <class Duration, class TimeZonePtr>
|
1671 |
+
inline
|
1672 |
+
sys_info
|
1673 |
+
zoned_time<Duration, TimeZonePtr>::get_info() const
|
1674 |
+
{
|
1675 |
+
return zone_->get_info(tp_);
|
1676 |
+
}
|
1677 |
+
|
1678 |
+
// make_zoned_time
|
1679 |
+
|
1680 |
+
inline
|
1681 |
+
zoned_time<std::chrono::seconds>
|
1682 |
+
make_zoned()
|
1683 |
+
{
|
1684 |
+
return zoned_time<std::chrono::seconds>();
|
1685 |
+
}
|
1686 |
+
|
1687 |
+
template <class Duration>
|
1688 |
+
inline
|
1689 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1690 |
+
make_zoned(const sys_time<Duration>& tp)
|
1691 |
+
{
|
1692 |
+
return zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type>(tp);
|
1693 |
+
}
|
1694 |
+
|
1695 |
+
template <class TimeZonePtr
|
1696 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1697 |
+
#if !defined(__INTEL_COMPILER) || (__INTEL_COMPILER > 1600)
|
1698 |
+
, class = typename std::enable_if
|
1699 |
+
<
|
1700 |
+
std::is_class
|
1701 |
+
<
|
1702 |
+
typename std::decay
|
1703 |
+
<
|
1704 |
+
decltype(*detail::to_raw_pointer(std::declval<TimeZonePtr&>()))
|
1705 |
+
>::type
|
1706 |
+
>{}
|
1707 |
+
>::type
|
1708 |
+
#endif
|
1709 |
+
#endif
|
1710 |
+
>
|
1711 |
+
inline
|
1712 |
+
zoned_time<std::chrono::seconds, TimeZonePtr>
|
1713 |
+
make_zoned(TimeZonePtr z)
|
1714 |
+
{
|
1715 |
+
return zoned_time<std::chrono::seconds, TimeZonePtr>(std::move(z));
|
1716 |
+
}
|
1717 |
+
|
1718 |
+
inline
|
1719 |
+
zoned_seconds
|
1720 |
+
make_zoned(const std::string& name)
|
1721 |
+
{
|
1722 |
+
return zoned_seconds(name);
|
1723 |
+
}
|
1724 |
+
|
1725 |
+
template <class Duration, class TimeZonePtr
|
1726 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1727 |
+
#if !defined(__INTEL_COMPILER) || (__INTEL_COMPILER > 1600)
|
1728 |
+
, class = typename std::enable_if
|
1729 |
+
<
|
1730 |
+
std::is_class<typename std::decay<decltype(*std::declval<TimeZonePtr&>())>::type>{}
|
1731 |
+
>::type
|
1732 |
+
#endif
|
1733 |
+
#endif
|
1734 |
+
>
|
1735 |
+
inline
|
1736 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type, TimeZonePtr>
|
1737 |
+
make_zoned(TimeZonePtr zone, const local_time<Duration>& tp)
|
1738 |
+
{
|
1739 |
+
return zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type,
|
1740 |
+
TimeZonePtr>(std::move(zone), tp);
|
1741 |
+
}
|
1742 |
+
|
1743 |
+
template <class Duration, class TimeZonePtr
|
1744 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1745 |
+
#if !defined(__INTEL_COMPILER) || (__INTEL_COMPILER > 1600)
|
1746 |
+
, class = typename std::enable_if
|
1747 |
+
<
|
1748 |
+
std::is_class<typename std::decay<decltype(*std::declval<TimeZonePtr&>())>::type>{}
|
1749 |
+
>::type
|
1750 |
+
#endif
|
1751 |
+
#endif
|
1752 |
+
>
|
1753 |
+
inline
|
1754 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type, TimeZonePtr>
|
1755 |
+
make_zoned(TimeZonePtr zone, const local_time<Duration>& tp, choose c)
|
1756 |
+
{
|
1757 |
+
return zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type,
|
1758 |
+
TimeZonePtr>(std::move(zone), tp, c);
|
1759 |
+
}
|
1760 |
+
|
1761 |
+
template <class Duration>
|
1762 |
+
inline
|
1763 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1764 |
+
make_zoned(const std::string& name, const local_time<Duration>& tp)
|
1765 |
+
{
|
1766 |
+
return zoned_time<typename std::common_type<Duration,
|
1767 |
+
std::chrono::seconds>::type>(name, tp);
|
1768 |
+
}
|
1769 |
+
|
1770 |
+
template <class Duration>
|
1771 |
+
inline
|
1772 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1773 |
+
make_zoned(const std::string& name, const local_time<Duration>& tp, choose c)
|
1774 |
+
{
|
1775 |
+
return zoned_time<typename std::common_type<Duration,
|
1776 |
+
std::chrono::seconds>::type>(name, tp, c);
|
1777 |
+
}
|
1778 |
+
|
1779 |
+
template <class Duration, class TimeZonePtr>
|
1780 |
+
inline
|
1781 |
+
zoned_time<Duration, TimeZonePtr>
|
1782 |
+
make_zoned(TimeZonePtr zone, const zoned_time<Duration, TimeZonePtr>& zt)
|
1783 |
+
{
|
1784 |
+
return zoned_time<Duration, TimeZonePtr>(std::move(zone), zt);
|
1785 |
+
}
|
1786 |
+
|
1787 |
+
template <class Duration, class TimeZonePtr>
|
1788 |
+
inline
|
1789 |
+
zoned_time<Duration, TimeZonePtr>
|
1790 |
+
make_zoned(const std::string& name, const zoned_time<Duration, TimeZonePtr>& zt)
|
1791 |
+
{
|
1792 |
+
return zoned_time<Duration, TimeZonePtr>(name, zt);
|
1793 |
+
}
|
1794 |
+
|
1795 |
+
template <class Duration, class TimeZonePtr>
|
1796 |
+
inline
|
1797 |
+
zoned_time<Duration, TimeZonePtr>
|
1798 |
+
make_zoned(TimeZonePtr zone, const zoned_time<Duration, TimeZonePtr>& zt, choose c)
|
1799 |
+
{
|
1800 |
+
return zoned_time<Duration, TimeZonePtr>(std::move(zone), zt, c);
|
1801 |
+
}
|
1802 |
+
|
1803 |
+
template <class Duration, class TimeZonePtr>
|
1804 |
+
inline
|
1805 |
+
zoned_time<Duration, TimeZonePtr>
|
1806 |
+
make_zoned(const std::string& name, const zoned_time<Duration, TimeZonePtr>& zt, choose c)
|
1807 |
+
{
|
1808 |
+
return zoned_time<Duration, TimeZonePtr>(name, zt, c);
|
1809 |
+
}
|
1810 |
+
|
1811 |
+
template <class Duration, class TimeZonePtr
|
1812 |
+
#if !defined(_MSC_VER) || (_MSC_VER > 1916)
|
1813 |
+
#if !defined(__INTEL_COMPILER) || (__INTEL_COMPILER > 1600)
|
1814 |
+
, class = typename std::enable_if
|
1815 |
+
<
|
1816 |
+
std::is_class<typename std::decay<decltype(*std::declval<TimeZonePtr&>())>::type>{}
|
1817 |
+
>::type
|
1818 |
+
#endif
|
1819 |
+
#endif
|
1820 |
+
>
|
1821 |
+
inline
|
1822 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type, TimeZonePtr>
|
1823 |
+
make_zoned(TimeZonePtr zone, const sys_time<Duration>& st)
|
1824 |
+
{
|
1825 |
+
return zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type,
|
1826 |
+
TimeZonePtr>(std::move(zone), st);
|
1827 |
+
}
|
1828 |
+
|
1829 |
+
template <class Duration>
|
1830 |
+
inline
|
1831 |
+
zoned_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1832 |
+
make_zoned(const std::string& name, const sys_time<Duration>& st)
|
1833 |
+
{
|
1834 |
+
return zoned_time<typename std::common_type<Duration,
|
1835 |
+
std::chrono::seconds>::type>(name, st);
|
1836 |
+
}
|
1837 |
+
|
1838 |
+
template <class CharT, class Traits, class Duration, class TimeZonePtr>
|
1839 |
+
std::basic_ostream<CharT, Traits>&
|
1840 |
+
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
|
1841 |
+
const zoned_time<Duration, TimeZonePtr>& tp)
|
1842 |
+
{
|
1843 |
+
using duration = typename zoned_time<Duration, TimeZonePtr>::duration;
|
1844 |
+
using LT = local_time<duration>;
|
1845 |
+
auto const st = tp.get_sys_time();
|
1846 |
+
auto const info = tp.get_time_zone()->get_info(st);
|
1847 |
+
return to_stream(os, fmt, LT{(st+info.offset).time_since_epoch()},
|
1848 |
+
&info.abbrev, &info.offset);
|
1849 |
+
}
|
1850 |
+
|
1851 |
+
template <class CharT, class Traits, class Duration, class TimeZonePtr>
|
1852 |
+
inline
|
1853 |
+
std::basic_ostream<CharT, Traits>&
|
1854 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration, TimeZonePtr>& t)
|
1855 |
+
{
|
1856 |
+
const CharT fmt[] = {'%', 'F', ' ', '%', 'T', ' ', '%', 'Z', CharT{}};
|
1857 |
+
return to_stream(os, fmt, t);
|
1858 |
+
}
|
1859 |
+
|
1860 |
+
class utc_clock
|
1861 |
+
{
|
1862 |
+
public:
|
1863 |
+
using duration = std::chrono::system_clock::duration;
|
1864 |
+
using rep = duration::rep;
|
1865 |
+
using period = duration::period;
|
1866 |
+
using time_point = std::chrono::time_point<utc_clock>;
|
1867 |
+
static CONSTDATA bool is_steady = false;
|
1868 |
+
|
1869 |
+
static time_point now();
|
1870 |
+
|
1871 |
+
template<typename Duration>
|
1872 |
+
static
|
1873 |
+
std::chrono::time_point<std::chrono::system_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
1874 |
+
to_sys(const std::chrono::time_point<utc_clock, Duration>&);
|
1875 |
+
|
1876 |
+
template<typename Duration>
|
1877 |
+
static
|
1878 |
+
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
1879 |
+
from_sys(const std::chrono::time_point<std::chrono::system_clock, Duration>&);
|
1880 |
+
|
1881 |
+
template<typename Duration>
|
1882 |
+
static
|
1883 |
+
std::chrono::time_point<local_t, typename std::common_type<Duration, std::chrono::seconds>::type>
|
1884 |
+
to_local(const std::chrono::time_point<utc_clock, Duration>&);
|
1885 |
+
|
1886 |
+
template<typename Duration>
|
1887 |
+
static
|
1888 |
+
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
1889 |
+
from_local(const std::chrono::time_point<local_t, Duration>&);
|
1890 |
+
};
|
1891 |
+
|
1892 |
+
template <class Duration>
|
1893 |
+
using utc_time = std::chrono::time_point<utc_clock, Duration>;
|
1894 |
+
|
1895 |
+
using utc_seconds = utc_time<std::chrono::seconds>;
|
1896 |
+
|
1897 |
+
template <class Duration>
|
1898 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1899 |
+
utc_clock::from_sys(const sys_time<Duration>& st)
|
1900 |
+
{
|
1901 |
+
using std::chrono::seconds;
|
1902 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
1903 |
+
auto const& leaps = get_tzdb().leap_seconds;
|
1904 |
+
auto const lt = std::upper_bound(leaps.begin(), leaps.end(), st);
|
1905 |
+
return utc_time<CD>{st.time_since_epoch() + seconds{lt-leaps.begin()}};
|
1906 |
+
}
|
1907 |
+
|
1908 |
+
// Return pair<is_leap_second, seconds{number_of_leap_seconds_since_1970}>
|
1909 |
+
// first is true if ut is during a leap second insertion, otherwise false.
|
1910 |
+
// If ut is during a leap second insertion, that leap second is included in the count
|
1911 |
+
template <class Duration>
|
1912 |
+
std::pair<bool, std::chrono::seconds>
|
1913 |
+
is_leap_second(date::utc_time<Duration> const& ut)
|
1914 |
+
{
|
1915 |
+
using std::chrono::seconds;
|
1916 |
+
using duration = typename std::common_type<Duration, seconds>::type;
|
1917 |
+
auto const& leaps = get_tzdb().leap_seconds;
|
1918 |
+
auto tp = sys_time<duration>{ut.time_since_epoch()};
|
1919 |
+
auto const lt = std::upper_bound(leaps.begin(), leaps.end(), tp);
|
1920 |
+
auto ds = seconds{lt-leaps.begin()};
|
1921 |
+
tp -= ds;
|
1922 |
+
auto ls = false;
|
1923 |
+
if (lt > leaps.begin())
|
1924 |
+
{
|
1925 |
+
if (tp < lt[-1])
|
1926 |
+
{
|
1927 |
+
if (tp >= lt[-1].date() - seconds{1})
|
1928 |
+
ls = true;
|
1929 |
+
else
|
1930 |
+
--ds;
|
1931 |
+
}
|
1932 |
+
}
|
1933 |
+
return {ls, ds};
|
1934 |
+
}
|
1935 |
+
|
1936 |
+
struct leap_second_info
|
1937 |
+
{
|
1938 |
+
bool is_leap_second;
|
1939 |
+
std::chrono::seconds elapsed;
|
1940 |
+
};
|
1941 |
+
|
1942 |
+
template <class Duration>
|
1943 |
+
leap_second_info
|
1944 |
+
get_leap_second_info(date::utc_time<Duration> const& ut)
|
1945 |
+
{
|
1946 |
+
auto p = is_leap_second(ut);
|
1947 |
+
return {p.first, p.second};
|
1948 |
+
}
|
1949 |
+
|
1950 |
+
template <class Duration>
|
1951 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1952 |
+
utc_clock::to_sys(const utc_time<Duration>& ut)
|
1953 |
+
{
|
1954 |
+
using std::chrono::seconds;
|
1955 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
1956 |
+
auto ls = is_leap_second(ut);
|
1957 |
+
auto tp = sys_time<CD>{ut.time_since_epoch() - ls.second};
|
1958 |
+
if (ls.first)
|
1959 |
+
tp = floor<seconds>(tp) + seconds{1} - CD{1};
|
1960 |
+
return tp;
|
1961 |
+
}
|
1962 |
+
|
1963 |
+
inline
|
1964 |
+
utc_clock::time_point
|
1965 |
+
utc_clock::now()
|
1966 |
+
{
|
1967 |
+
return from_sys(std::chrono::system_clock::now());
|
1968 |
+
}
|
1969 |
+
|
1970 |
+
template <class Duration>
|
1971 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1972 |
+
utc_clock::from_local(const local_time<Duration>& st)
|
1973 |
+
{
|
1974 |
+
return from_sys(sys_time<Duration>{st.time_since_epoch()});
|
1975 |
+
}
|
1976 |
+
|
1977 |
+
template <class Duration>
|
1978 |
+
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
1979 |
+
utc_clock::to_local(const utc_time<Duration>& ut)
|
1980 |
+
{
|
1981 |
+
using CD = typename std::common_type<Duration, std::chrono::seconds>::type;
|
1982 |
+
return local_time<CD>{to_sys(ut).time_since_epoch()};
|
1983 |
+
}
|
1984 |
+
|
1985 |
+
template <class CharT, class Traits, class Duration>
|
1986 |
+
std::basic_ostream<CharT, Traits>&
|
1987 |
+
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
|
1988 |
+
const utc_time<Duration>& t)
|
1989 |
+
{
|
1990 |
+
using std::chrono::seconds;
|
1991 |
+
using CT = typename std::common_type<Duration, seconds>::type;
|
1992 |
+
const std::string abbrev("UTC");
|
1993 |
+
CONSTDATA seconds offset{0};
|
1994 |
+
auto ls = is_leap_second(t);
|
1995 |
+
auto tp = sys_time<CT>{t.time_since_epoch() - ls.second};
|
1996 |
+
auto const sd = floor<days>(tp);
|
1997 |
+
year_month_day ymd = sd;
|
1998 |
+
auto time = make_time(tp - sys_seconds{sd});
|
1999 |
+
time.seconds(detail::undocumented{}) += seconds{ls.first};
|
2000 |
+
fields<CT> fds{ymd, time};
|
2001 |
+
return to_stream(os, fmt, fds, &abbrev, &offset);
|
2002 |
+
}
|
2003 |
+
|
2004 |
+
template <class CharT, class Traits, class Duration>
|
2005 |
+
std::basic_ostream<CharT, Traits>&
|
2006 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const utc_time<Duration>& t)
|
2007 |
+
{
|
2008 |
+
const CharT fmt[] = {'%', 'F', ' ', '%', 'T', CharT{}};
|
2009 |
+
return to_stream(os, fmt, t);
|
2010 |
+
}
|
2011 |
+
|
2012 |
+
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
|
2013 |
+
std::basic_istream<CharT, Traits>&
|
2014 |
+
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
|
2015 |
+
utc_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
|
2016 |
+
std::chrono::minutes* offset = nullptr)
|
2017 |
+
{
|
2018 |
+
using std::chrono::seconds;
|
2019 |
+
using std::chrono::minutes;
|
2020 |
+
using CT = typename std::common_type<Duration, seconds>::type;
|
2021 |
+
minutes offset_local{};
|
2022 |
+
auto offptr = offset ? offset : &offset_local;
|
2023 |
+
fields<CT> fds{};
|
2024 |
+
fds.has_tod = true;
|
2025 |
+
from_stream(is, fmt, fds, abbrev, offptr);
|
2026 |
+
if (!fds.ymd.ok())
|
2027 |
+
is.setstate(std::ios::failbit);
|
2028 |
+
if (!is.fail())
|
2029 |
+
{
|
2030 |
+
bool is_60_sec = fds.tod.seconds() == seconds{60};
|
2031 |
+
if (is_60_sec)
|
2032 |
+
fds.tod.seconds(detail::undocumented{}) -= seconds{1};
|
2033 |
+
auto tmp = utc_clock::from_sys(sys_days(fds.ymd) - *offptr + fds.tod.to_duration());
|
2034 |
+
if (is_60_sec)
|
2035 |
+
tmp += seconds{1};
|
2036 |
+
if (is_60_sec != is_leap_second(tmp).first || !fds.tod.in_conventional_range())
|
2037 |
+
{
|
2038 |
+
is.setstate(std::ios::failbit);
|
2039 |
+
return is;
|
2040 |
+
}
|
2041 |
+
tp = std::chrono::time_point_cast<Duration>(tmp);
|
2042 |
+
}
|
2043 |
+
return is;
|
2044 |
+
}
|
2045 |
+
|
2046 |
+
// tai_clock
|
2047 |
+
|
2048 |
+
class tai_clock
|
2049 |
+
{
|
2050 |
+
public:
|
2051 |
+
using duration = std::chrono::system_clock::duration;
|
2052 |
+
using rep = duration::rep;
|
2053 |
+
using period = duration::period;
|
2054 |
+
using time_point = std::chrono::time_point<tai_clock>;
|
2055 |
+
static const bool is_steady = false;
|
2056 |
+
|
2057 |
+
static time_point now();
|
2058 |
+
|
2059 |
+
template<typename Duration>
|
2060 |
+
static
|
2061 |
+
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
2062 |
+
to_utc(const std::chrono::time_point<tai_clock, Duration>&) NOEXCEPT;
|
2063 |
+
|
2064 |
+
template<typename Duration>
|
2065 |
+
static
|
2066 |
+
std::chrono::time_point<tai_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
2067 |
+
from_utc(const std::chrono::time_point<utc_clock, Duration>&) NOEXCEPT;
|
2068 |
+
|
2069 |
+
template<typename Duration>
|
2070 |
+
static
|
2071 |
+
std::chrono::time_point<local_t, typename std::common_type<Duration, date::days>::type>
|
2072 |
+
to_local(const std::chrono::time_point<tai_clock, Duration>&) NOEXCEPT;
|
2073 |
+
|
2074 |
+
template<typename Duration>
|
2075 |
+
static
|
2076 |
+
std::chrono::time_point<tai_clock, typename std::common_type<Duration, date::days>::type>
|
2077 |
+
from_local(const std::chrono::time_point<local_t, Duration>&) NOEXCEPT;
|
2078 |
+
};
|
2079 |
+
|
2080 |
+
template <class Duration>
|
2081 |
+
using tai_time = std::chrono::time_point<tai_clock, Duration>;
|
2082 |
+
|
2083 |
+
using tai_seconds = tai_time<std::chrono::seconds>;
|
2084 |
+
|
2085 |
+
template <class Duration>
|
2086 |
+
inline
|
2087 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2088 |
+
tai_clock::to_utc(const tai_time<Duration>& t) NOEXCEPT
|
2089 |
+
{
|
2090 |
+
using std::chrono::seconds;
|
2091 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
2092 |
+
return utc_time<CD>{t.time_since_epoch()} -
|
2093 |
+
(sys_days(year{1970}/January/1) - sys_days(year{1958}/January/1) + seconds{10});
|
2094 |
+
}
|
2095 |
+
|
2096 |
+
template <class Duration>
|
2097 |
+
inline
|
2098 |
+
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2099 |
+
tai_clock::from_utc(const utc_time<Duration>& t) NOEXCEPT
|
2100 |
+
{
|
2101 |
+
using std::chrono::seconds;
|
2102 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
2103 |
+
return tai_time<CD>{t.time_since_epoch()} +
|
2104 |
+
(sys_days(year{1970}/January/1) - sys_days(year{1958}/January/1) + seconds{10});
|
2105 |
+
}
|
2106 |
+
|
2107 |
+
inline
|
2108 |
+
tai_clock::time_point
|
2109 |
+
tai_clock::now()
|
2110 |
+
{
|
2111 |
+
return from_utc(utc_clock::now());
|
2112 |
+
}
|
2113 |
+
|
2114 |
+
template <class Duration>
|
2115 |
+
inline
|
2116 |
+
local_time<typename std::common_type<Duration, date::days>::type>
|
2117 |
+
tai_clock::to_local(const tai_time<Duration>& t) NOEXCEPT
|
2118 |
+
{
|
2119 |
+
using CD = typename std::common_type<Duration, date::days>::type;
|
2120 |
+
return local_time<CD>{t.time_since_epoch()} -
|
2121 |
+
(local_days(year{1970}/January/1) - local_days(year{1958}/January/1));
|
2122 |
+
}
|
2123 |
+
|
2124 |
+
template <class Duration>
|
2125 |
+
inline
|
2126 |
+
tai_time<typename std::common_type<Duration, date::days>::type>
|
2127 |
+
tai_clock::from_local(const local_time<Duration>& t) NOEXCEPT
|
2128 |
+
{
|
2129 |
+
using CD = typename std::common_type<Duration, date::days>::type;
|
2130 |
+
return tai_time<CD>{t.time_since_epoch()} +
|
2131 |
+
(local_days(year{1970}/January/1) - local_days(year{1958}/January/1));
|
2132 |
+
}
|
2133 |
+
|
2134 |
+
template <class CharT, class Traits, class Duration>
|
2135 |
+
std::basic_ostream<CharT, Traits>&
|
2136 |
+
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
|
2137 |
+
const tai_time<Duration>& t)
|
2138 |
+
{
|
2139 |
+
const std::string abbrev("TAI");
|
2140 |
+
CONSTDATA std::chrono::seconds offset{0};
|
2141 |
+
return to_stream(os, fmt, tai_clock::to_local(t), &abbrev, &offset);
|
2142 |
+
}
|
2143 |
+
|
2144 |
+
template <class CharT, class Traits, class Duration>
|
2145 |
+
std::basic_ostream<CharT, Traits>&
|
2146 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const tai_time<Duration>& t)
|
2147 |
+
{
|
2148 |
+
const CharT fmt[] = {'%', 'F', ' ', '%', 'T', CharT{}};
|
2149 |
+
return to_stream(os, fmt, t);
|
2150 |
+
}
|
2151 |
+
|
2152 |
+
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
|
2153 |
+
std::basic_istream<CharT, Traits>&
|
2154 |
+
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
|
2155 |
+
tai_time<Duration>& tp,
|
2156 |
+
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
|
2157 |
+
std::chrono::minutes* offset = nullptr)
|
2158 |
+
{
|
2159 |
+
local_time<Duration> lp;
|
2160 |
+
from_stream(is, fmt, lp, abbrev, offset);
|
2161 |
+
if (!is.fail())
|
2162 |
+
tp = tai_clock::from_local(lp);
|
2163 |
+
return is;
|
2164 |
+
}
|
2165 |
+
|
2166 |
+
// gps_clock
|
2167 |
+
|
2168 |
+
class gps_clock
|
2169 |
+
{
|
2170 |
+
public:
|
2171 |
+
using duration = std::chrono::system_clock::duration;
|
2172 |
+
using rep = duration::rep;
|
2173 |
+
using period = duration::period;
|
2174 |
+
using time_point = std::chrono::time_point<gps_clock>;
|
2175 |
+
static const bool is_steady = false;
|
2176 |
+
|
2177 |
+
static time_point now();
|
2178 |
+
|
2179 |
+
template<typename Duration>
|
2180 |
+
static
|
2181 |
+
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
2182 |
+
to_utc(const std::chrono::time_point<gps_clock, Duration>&) NOEXCEPT;
|
2183 |
+
|
2184 |
+
template<typename Duration>
|
2185 |
+
static
|
2186 |
+
std::chrono::time_point<gps_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
|
2187 |
+
from_utc(const std::chrono::time_point<utc_clock, Duration>&) NOEXCEPT;
|
2188 |
+
|
2189 |
+
template<typename Duration>
|
2190 |
+
static
|
2191 |
+
std::chrono::time_point<local_t, typename std::common_type<Duration, date::days>::type>
|
2192 |
+
to_local(const std::chrono::time_point<gps_clock, Duration>&) NOEXCEPT;
|
2193 |
+
|
2194 |
+
template<typename Duration>
|
2195 |
+
static
|
2196 |
+
std::chrono::time_point<gps_clock, typename std::common_type<Duration, date::days>::type>
|
2197 |
+
from_local(const std::chrono::time_point<local_t, Duration>&) NOEXCEPT;
|
2198 |
+
};
|
2199 |
+
|
2200 |
+
template <class Duration>
|
2201 |
+
using gps_time = std::chrono::time_point<gps_clock, Duration>;
|
2202 |
+
|
2203 |
+
using gps_seconds = gps_time<std::chrono::seconds>;
|
2204 |
+
|
2205 |
+
template <class Duration>
|
2206 |
+
inline
|
2207 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2208 |
+
gps_clock::to_utc(const gps_time<Duration>& t) NOEXCEPT
|
2209 |
+
{
|
2210 |
+
using std::chrono::seconds;
|
2211 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
2212 |
+
return utc_time<CD>{t.time_since_epoch()} +
|
2213 |
+
(sys_days(year{1980}/January/Sunday[1]) - sys_days(year{1970}/January/1) +
|
2214 |
+
seconds{9});
|
2215 |
+
}
|
2216 |
+
|
2217 |
+
template <class Duration>
|
2218 |
+
inline
|
2219 |
+
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2220 |
+
gps_clock::from_utc(const utc_time<Duration>& t) NOEXCEPT
|
2221 |
+
{
|
2222 |
+
using std::chrono::seconds;
|
2223 |
+
using CD = typename std::common_type<Duration, seconds>::type;
|
2224 |
+
return gps_time<CD>{t.time_since_epoch()} -
|
2225 |
+
(sys_days(year{1980}/January/Sunday[1]) - sys_days(year{1970}/January/1) +
|
2226 |
+
seconds{9});
|
2227 |
+
}
|
2228 |
+
|
2229 |
+
inline
|
2230 |
+
gps_clock::time_point
|
2231 |
+
gps_clock::now()
|
2232 |
+
{
|
2233 |
+
return from_utc(utc_clock::now());
|
2234 |
+
}
|
2235 |
+
|
2236 |
+
template <class Duration>
|
2237 |
+
inline
|
2238 |
+
local_time<typename std::common_type<Duration, date::days>::type>
|
2239 |
+
gps_clock::to_local(const gps_time<Duration>& t) NOEXCEPT
|
2240 |
+
{
|
2241 |
+
using CD = typename std::common_type<Duration, date::days>::type;
|
2242 |
+
return local_time<CD>{t.time_since_epoch()} +
|
2243 |
+
(local_days(year{1980}/January/Sunday[1]) - local_days(year{1970}/January/1));
|
2244 |
+
}
|
2245 |
+
|
2246 |
+
template <class Duration>
|
2247 |
+
inline
|
2248 |
+
gps_time<typename std::common_type<Duration, date::days>::type>
|
2249 |
+
gps_clock::from_local(const local_time<Duration>& t) NOEXCEPT
|
2250 |
+
{
|
2251 |
+
using CD = typename std::common_type<Duration, date::days>::type;
|
2252 |
+
return gps_time<CD>{t.time_since_epoch()} -
|
2253 |
+
(local_days(year{1980}/January/Sunday[1]) - local_days(year{1970}/January/1));
|
2254 |
+
}
|
2255 |
+
|
2256 |
+
|
2257 |
+
template <class CharT, class Traits, class Duration>
|
2258 |
+
std::basic_ostream<CharT, Traits>&
|
2259 |
+
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
|
2260 |
+
const gps_time<Duration>& t)
|
2261 |
+
{
|
2262 |
+
const std::string abbrev("GPS");
|
2263 |
+
CONSTDATA std::chrono::seconds offset{0};
|
2264 |
+
return to_stream(os, fmt, gps_clock::to_local(t), &abbrev, &offset);
|
2265 |
+
}
|
2266 |
+
|
2267 |
+
template <class CharT, class Traits, class Duration>
|
2268 |
+
std::basic_ostream<CharT, Traits>&
|
2269 |
+
operator<<(std::basic_ostream<CharT, Traits>& os, const gps_time<Duration>& t)
|
2270 |
+
{
|
2271 |
+
const CharT fmt[] = {'%', 'F', ' ', '%', 'T', CharT{}};
|
2272 |
+
return to_stream(os, fmt, t);
|
2273 |
+
}
|
2274 |
+
|
2275 |
+
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
|
2276 |
+
std::basic_istream<CharT, Traits>&
|
2277 |
+
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
|
2278 |
+
gps_time<Duration>& tp,
|
2279 |
+
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
|
2280 |
+
std::chrono::minutes* offset = nullptr)
|
2281 |
+
{
|
2282 |
+
local_time<Duration> lp;
|
2283 |
+
from_stream(is, fmt, lp, abbrev, offset);
|
2284 |
+
if (!is.fail())
|
2285 |
+
tp = gps_clock::from_local(lp);
|
2286 |
+
return is;
|
2287 |
+
}
|
2288 |
+
|
2289 |
+
// clock_time_conversion
|
2290 |
+
|
2291 |
+
template <class DstClock, class SrcClock>
|
2292 |
+
struct clock_time_conversion
|
2293 |
+
{};
|
2294 |
+
|
2295 |
+
template <>
|
2296 |
+
struct clock_time_conversion<std::chrono::system_clock, std::chrono::system_clock>
|
2297 |
+
{
|
2298 |
+
template <class Duration>
|
2299 |
+
CONSTCD14
|
2300 |
+
sys_time<Duration>
|
2301 |
+
operator()(const sys_time<Duration>& st) const
|
2302 |
+
{
|
2303 |
+
return st;
|
2304 |
+
}
|
2305 |
+
};
|
2306 |
+
|
2307 |
+
template <>
|
2308 |
+
struct clock_time_conversion<utc_clock, utc_clock>
|
2309 |
+
{
|
2310 |
+
template <class Duration>
|
2311 |
+
CONSTCD14
|
2312 |
+
utc_time<Duration>
|
2313 |
+
operator()(const utc_time<Duration>& ut) const
|
2314 |
+
{
|
2315 |
+
return ut;
|
2316 |
+
}
|
2317 |
+
};
|
2318 |
+
|
2319 |
+
template<>
|
2320 |
+
struct clock_time_conversion<local_t, local_t>
|
2321 |
+
{
|
2322 |
+
template <class Duration>
|
2323 |
+
CONSTCD14
|
2324 |
+
local_time<Duration>
|
2325 |
+
operator()(const local_time<Duration>& lt) const
|
2326 |
+
{
|
2327 |
+
return lt;
|
2328 |
+
}
|
2329 |
+
};
|
2330 |
+
|
2331 |
+
template <>
|
2332 |
+
struct clock_time_conversion<utc_clock, std::chrono::system_clock>
|
2333 |
+
{
|
2334 |
+
template <class Duration>
|
2335 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2336 |
+
operator()(const sys_time<Duration>& st) const
|
2337 |
+
{
|
2338 |
+
return utc_clock::from_sys(st);
|
2339 |
+
}
|
2340 |
+
};
|
2341 |
+
|
2342 |
+
template <>
|
2343 |
+
struct clock_time_conversion<std::chrono::system_clock, utc_clock>
|
2344 |
+
{
|
2345 |
+
template <class Duration>
|
2346 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2347 |
+
operator()(const utc_time<Duration>& ut) const
|
2348 |
+
{
|
2349 |
+
return utc_clock::to_sys(ut);
|
2350 |
+
}
|
2351 |
+
};
|
2352 |
+
|
2353 |
+
template<>
|
2354 |
+
struct clock_time_conversion<local_t, std::chrono::system_clock>
|
2355 |
+
{
|
2356 |
+
template <class Duration>
|
2357 |
+
CONSTCD14
|
2358 |
+
local_time<Duration>
|
2359 |
+
operator()(const sys_time<Duration>& st) const
|
2360 |
+
{
|
2361 |
+
return local_time<Duration>{st.time_since_epoch()};
|
2362 |
+
}
|
2363 |
+
};
|
2364 |
+
|
2365 |
+
template<>
|
2366 |
+
struct clock_time_conversion<std::chrono::system_clock, local_t>
|
2367 |
+
{
|
2368 |
+
template <class Duration>
|
2369 |
+
CONSTCD14
|
2370 |
+
sys_time<Duration>
|
2371 |
+
operator()(const local_time<Duration>& lt) const
|
2372 |
+
{
|
2373 |
+
return sys_time<Duration>{lt.time_since_epoch()};
|
2374 |
+
}
|
2375 |
+
};
|
2376 |
+
|
2377 |
+
template<>
|
2378 |
+
struct clock_time_conversion<utc_clock, local_t>
|
2379 |
+
{
|
2380 |
+
template <class Duration>
|
2381 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2382 |
+
operator()(const local_time<Duration>& lt) const
|
2383 |
+
{
|
2384 |
+
return utc_clock::from_local(lt);
|
2385 |
+
}
|
2386 |
+
};
|
2387 |
+
|
2388 |
+
template<>
|
2389 |
+
struct clock_time_conversion<local_t, utc_clock>
|
2390 |
+
{
|
2391 |
+
template <class Duration>
|
2392 |
+
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2393 |
+
operator()(const utc_time<Duration>& ut) const
|
2394 |
+
{
|
2395 |
+
return utc_clock::to_local(ut);
|
2396 |
+
}
|
2397 |
+
};
|
2398 |
+
|
2399 |
+
template<typename Clock>
|
2400 |
+
struct clock_time_conversion<Clock, Clock>
|
2401 |
+
{
|
2402 |
+
template <class Duration>
|
2403 |
+
CONSTCD14
|
2404 |
+
std::chrono::time_point<Clock, Duration>
|
2405 |
+
operator()(const std::chrono::time_point<Clock, Duration>& tp) const
|
2406 |
+
{
|
2407 |
+
return tp;
|
2408 |
+
}
|
2409 |
+
};
|
2410 |
+
|
2411 |
+
namespace ctc_detail
|
2412 |
+
{
|
2413 |
+
|
2414 |
+
template <class Clock, class Duration>
|
2415 |
+
using time_point = std::chrono::time_point<Clock, Duration>;
|
2416 |
+
|
2417 |
+
using std::declval;
|
2418 |
+
using std::chrono::system_clock;
|
2419 |
+
|
2420 |
+
//Check if TimePoint is time for given clock,
|
2421 |
+
//if not emits hard error
|
2422 |
+
template <class Clock, class TimePoint>
|
2423 |
+
struct return_clock_time
|
2424 |
+
{
|
2425 |
+
using clock_time_point = time_point<Clock, typename TimePoint::duration>;
|
2426 |
+
using type = TimePoint;
|
2427 |
+
|
2428 |
+
static_assert(std::is_same<TimePoint, clock_time_point>::value,
|
2429 |
+
"time point with appropariate clock shall be returned");
|
2430 |
+
};
|
2431 |
+
|
2432 |
+
// Check if Clock has to_sys method accepting TimePoint with given duration const& and
|
2433 |
+
// returning sys_time. If so has nested type member equal to return type to_sys.
|
2434 |
+
template <class Clock, class Duration, class = void>
|
2435 |
+
struct return_to_sys
|
2436 |
+
{};
|
2437 |
+
|
2438 |
+
template <class Clock, class Duration>
|
2439 |
+
struct return_to_sys
|
2440 |
+
<
|
2441 |
+
Clock, Duration,
|
2442 |
+
decltype(Clock::to_sys(declval<time_point<Clock, Duration> const&>()), void())
|
2443 |
+
>
|
2444 |
+
: return_clock_time
|
2445 |
+
<
|
2446 |
+
system_clock,
|
2447 |
+
decltype(Clock::to_sys(declval<time_point<Clock, Duration> const&>()))
|
2448 |
+
>
|
2449 |
+
{};
|
2450 |
+
|
2451 |
+
// Similiar to above
|
2452 |
+
template <class Clock, class Duration, class = void>
|
2453 |
+
struct return_from_sys
|
2454 |
+
{};
|
2455 |
+
|
2456 |
+
template <class Clock, class Duration>
|
2457 |
+
struct return_from_sys
|
2458 |
+
<
|
2459 |
+
Clock, Duration,
|
2460 |
+
decltype(Clock::from_sys(declval<time_point<system_clock, Duration> const&>()),
|
2461 |
+
void())
|
2462 |
+
>
|
2463 |
+
: return_clock_time
|
2464 |
+
<
|
2465 |
+
Clock,
|
2466 |
+
decltype(Clock::from_sys(declval<time_point<system_clock, Duration> const&>()))
|
2467 |
+
>
|
2468 |
+
{};
|
2469 |
+
|
2470 |
+
// Similiar to above
|
2471 |
+
template <class Clock, class Duration, class = void>
|
2472 |
+
struct return_to_utc
|
2473 |
+
{};
|
2474 |
+
|
2475 |
+
template <class Clock, class Duration>
|
2476 |
+
struct return_to_utc
|
2477 |
+
<
|
2478 |
+
Clock, Duration,
|
2479 |
+
decltype(Clock::to_utc(declval<time_point<Clock, Duration> const&>()), void())
|
2480 |
+
>
|
2481 |
+
: return_clock_time
|
2482 |
+
<
|
2483 |
+
utc_clock,
|
2484 |
+
decltype(Clock::to_utc(declval<time_point<Clock, Duration> const&>()))>
|
2485 |
+
{};
|
2486 |
+
|
2487 |
+
// Similiar to above
|
2488 |
+
template <class Clock, class Duration, class = void>
|
2489 |
+
struct return_from_utc
|
2490 |
+
{};
|
2491 |
+
|
2492 |
+
template <class Clock, class Duration>
|
2493 |
+
struct return_from_utc
|
2494 |
+
<
|
2495 |
+
Clock, Duration,
|
2496 |
+
decltype(Clock::from_utc(declval<time_point<utc_clock, Duration> const&>()),
|
2497 |
+
void())
|
2498 |
+
>
|
2499 |
+
: return_clock_time
|
2500 |
+
<
|
2501 |
+
Clock,
|
2502 |
+
decltype(Clock::from_utc(declval<time_point<utc_clock, Duration> const&>()))
|
2503 |
+
>
|
2504 |
+
{};
|
2505 |
+
|
2506 |
+
// Similiar to above
|
2507 |
+
template<typename Clock, typename Duration, typename = void>
|
2508 |
+
struct return_to_local
|
2509 |
+
{};
|
2510 |
+
|
2511 |
+
template<typename Clock, typename Duration>
|
2512 |
+
struct return_to_local
|
2513 |
+
<
|
2514 |
+
Clock, Duration,
|
2515 |
+
decltype(Clock::to_local(declval<time_point<Clock, Duration> const&>()),
|
2516 |
+
void())
|
2517 |
+
>
|
2518 |
+
: return_clock_time
|
2519 |
+
<
|
2520 |
+
local_t,
|
2521 |
+
decltype(Clock::to_local(declval<time_point<Clock, Duration> const&>()))
|
2522 |
+
>
|
2523 |
+
{};
|
2524 |
+
|
2525 |
+
// Similiar to above
|
2526 |
+
template<typename Clock, typename Duration, typename = void>
|
2527 |
+
struct return_from_local
|
2528 |
+
{};
|
2529 |
+
|
2530 |
+
template<typename Clock, typename Duration>
|
2531 |
+
struct return_from_local
|
2532 |
+
<
|
2533 |
+
Clock, Duration,
|
2534 |
+
decltype(Clock::from_local(declval<time_point<local_t, Duration> const&>()),
|
2535 |
+
void())
|
2536 |
+
>
|
2537 |
+
: return_clock_time
|
2538 |
+
<
|
2539 |
+
Clock,
|
2540 |
+
decltype(Clock::from_local(declval<time_point<local_t, Duration> const&>()))
|
2541 |
+
>
|
2542 |
+
{};
|
2543 |
+
|
2544 |
+
} // namespace ctc_detail
|
2545 |
+
|
2546 |
+
template <class SrcClock>
|
2547 |
+
struct clock_time_conversion<std::chrono::system_clock, SrcClock>
|
2548 |
+
{
|
2549 |
+
template <class Duration>
|
2550 |
+
CONSTCD14
|
2551 |
+
typename ctc_detail::return_to_sys<SrcClock, Duration>::type
|
2552 |
+
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
2553 |
+
{
|
2554 |
+
return SrcClock::to_sys(tp);
|
2555 |
+
}
|
2556 |
+
};
|
2557 |
+
|
2558 |
+
template <class DstClock>
|
2559 |
+
struct clock_time_conversion<DstClock, std::chrono::system_clock>
|
2560 |
+
{
|
2561 |
+
template <class Duration>
|
2562 |
+
CONSTCD14
|
2563 |
+
typename ctc_detail::return_from_sys<DstClock, Duration>::type
|
2564 |
+
operator()(const sys_time<Duration>& st) const
|
2565 |
+
{
|
2566 |
+
return DstClock::from_sys(st);
|
2567 |
+
}
|
2568 |
+
};
|
2569 |
+
|
2570 |
+
template <class SrcClock>
|
2571 |
+
struct clock_time_conversion<utc_clock, SrcClock>
|
2572 |
+
{
|
2573 |
+
template <class Duration>
|
2574 |
+
CONSTCD14
|
2575 |
+
typename ctc_detail::return_to_utc<SrcClock, Duration>::type
|
2576 |
+
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
2577 |
+
{
|
2578 |
+
return SrcClock::to_utc(tp);
|
2579 |
+
}
|
2580 |
+
};
|
2581 |
+
|
2582 |
+
template <class DstClock>
|
2583 |
+
struct clock_time_conversion<DstClock, utc_clock>
|
2584 |
+
{
|
2585 |
+
template <class Duration>
|
2586 |
+
CONSTCD14
|
2587 |
+
typename ctc_detail::return_from_utc<DstClock, Duration>::type
|
2588 |
+
operator()(const utc_time<Duration>& ut) const
|
2589 |
+
{
|
2590 |
+
return DstClock::from_utc(ut);
|
2591 |
+
}
|
2592 |
+
};
|
2593 |
+
|
2594 |
+
template<typename SrcClock>
|
2595 |
+
struct clock_time_conversion<local_t, SrcClock>
|
2596 |
+
{
|
2597 |
+
template <class Duration>
|
2598 |
+
CONSTCD14
|
2599 |
+
typename ctc_detail::return_to_local<SrcClock, Duration>::type
|
2600 |
+
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
2601 |
+
{
|
2602 |
+
return SrcClock::to_local(tp);
|
2603 |
+
}
|
2604 |
+
};
|
2605 |
+
|
2606 |
+
template<typename DstClock>
|
2607 |
+
struct clock_time_conversion<DstClock, local_t>
|
2608 |
+
{
|
2609 |
+
template <class Duration>
|
2610 |
+
CONSTCD14
|
2611 |
+
typename ctc_detail::return_from_local<DstClock, Duration>::type
|
2612 |
+
operator()(const local_time<Duration>& lt) const
|
2613 |
+
{
|
2614 |
+
return DstClock::from_local(lt);
|
2615 |
+
}
|
2616 |
+
};
|
2617 |
+
|
2618 |
+
namespace clock_cast_detail
|
2619 |
+
{
|
2620 |
+
|
2621 |
+
template <class Clock, class Duration>
|
2622 |
+
using time_point = std::chrono::time_point<Clock, Duration>;
|
2623 |
+
using std::chrono::system_clock;
|
2624 |
+
|
2625 |
+
template <class DstClock, class SrcClock, class Duration>
|
2626 |
+
CONSTCD14
|
2627 |
+
auto
|
2628 |
+
conv_clock(const time_point<SrcClock, Duration>& t)
|
2629 |
+
-> decltype(std::declval<clock_time_conversion<DstClock, SrcClock>>()(t))
|
2630 |
+
{
|
2631 |
+
return clock_time_conversion<DstClock, SrcClock>{}(t);
|
2632 |
+
}
|
2633 |
+
|
2634 |
+
//direct trait conversion, 1st candidate
|
2635 |
+
template <class DstClock, class SrcClock, class Duration>
|
2636 |
+
CONSTCD14
|
2637 |
+
auto
|
2638 |
+
cc_impl(const time_point<SrcClock, Duration>& t, const time_point<SrcClock, Duration>*)
|
2639 |
+
-> decltype(conv_clock<DstClock>(t))
|
2640 |
+
{
|
2641 |
+
return conv_clock<DstClock>(t);
|
2642 |
+
}
|
2643 |
+
|
2644 |
+
//conversion through sys, 2nd candidate
|
2645 |
+
template <class DstClock, class SrcClock, class Duration>
|
2646 |
+
CONSTCD14
|
2647 |
+
auto
|
2648 |
+
cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
2649 |
+
-> decltype(conv_clock<DstClock>(conv_clock<system_clock>(t)))
|
2650 |
+
{
|
2651 |
+
return conv_clock<DstClock>(conv_clock<system_clock>(t));
|
2652 |
+
}
|
2653 |
+
|
2654 |
+
//conversion through utc, 2nd candidate
|
2655 |
+
template <class DstClock, class SrcClock, class Duration>
|
2656 |
+
CONSTCD14
|
2657 |
+
auto
|
2658 |
+
cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
2659 |
+
-> decltype(0, // MSVC_WORKAROUND
|
2660 |
+
conv_clock<DstClock>(conv_clock<utc_clock>(t)))
|
2661 |
+
{
|
2662 |
+
return conv_clock<DstClock>(conv_clock<utc_clock>(t));
|
2663 |
+
}
|
2664 |
+
|
2665 |
+
//conversion through sys and utc, 3rd candidate
|
2666 |
+
template <class DstClock, class SrcClock, class Duration>
|
2667 |
+
CONSTCD14
|
2668 |
+
auto
|
2669 |
+
cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
2670 |
+
-> decltype(conv_clock<DstClock>(conv_clock<utc_clock>(conv_clock<system_clock>(t))))
|
2671 |
+
{
|
2672 |
+
return conv_clock<DstClock>(conv_clock<utc_clock>(conv_clock<system_clock>(t)));
|
2673 |
+
}
|
2674 |
+
|
2675 |
+
//conversion through utc and sys, 3rd candidate
|
2676 |
+
template <class DstClock, class SrcClock, class Duration>
|
2677 |
+
CONSTCD14
|
2678 |
+
auto
|
2679 |
+
cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
2680 |
+
-> decltype(0, // MSVC_WORKAROUND
|
2681 |
+
conv_clock<DstClock>(conv_clock<system_clock>(conv_clock<utc_clock>(t))))
|
2682 |
+
{
|
2683 |
+
return conv_clock<DstClock>(conv_clock<system_clock>(conv_clock<utc_clock>(t)));
|
2684 |
+
}
|
2685 |
+
|
2686 |
+
} // namespace clock_cast_detail
|
2687 |
+
|
2688 |
+
template <class DstClock, class SrcClock, class Duration>
|
2689 |
+
CONSTCD14
|
2690 |
+
auto
|
2691 |
+
clock_cast(const std::chrono::time_point<SrcClock, Duration>& tp)
|
2692 |
+
-> decltype(clock_cast_detail::cc_impl<DstClock>(tp, &tp))
|
2693 |
+
{
|
2694 |
+
return clock_cast_detail::cc_impl<DstClock>(tp, &tp);
|
2695 |
+
}
|
2696 |
+
|
2697 |
+
// Deprecated API
|
2698 |
+
|
2699 |
+
template <class Duration>
|
2700 |
+
inline
|
2701 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2702 |
+
to_sys_time(const utc_time<Duration>& t)
|
2703 |
+
{
|
2704 |
+
return utc_clock::to_sys(t);
|
2705 |
+
}
|
2706 |
+
|
2707 |
+
template <class Duration>
|
2708 |
+
inline
|
2709 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2710 |
+
to_sys_time(const tai_time<Duration>& t)
|
2711 |
+
{
|
2712 |
+
return utc_clock::to_sys(tai_clock::to_utc(t));
|
2713 |
+
}
|
2714 |
+
|
2715 |
+
template <class Duration>
|
2716 |
+
inline
|
2717 |
+
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2718 |
+
to_sys_time(const gps_time<Duration>& t)
|
2719 |
+
{
|
2720 |
+
return utc_clock::to_sys(gps_clock::to_utc(t));
|
2721 |
+
}
|
2722 |
+
|
2723 |
+
|
2724 |
+
template <class Duration>
|
2725 |
+
inline
|
2726 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2727 |
+
to_utc_time(const sys_time<Duration>& t)
|
2728 |
+
{
|
2729 |
+
return utc_clock::from_sys(t);
|
2730 |
+
}
|
2731 |
+
|
2732 |
+
template <class Duration>
|
2733 |
+
inline
|
2734 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2735 |
+
to_utc_time(const tai_time<Duration>& t)
|
2736 |
+
{
|
2737 |
+
return tai_clock::to_utc(t);
|
2738 |
+
}
|
2739 |
+
|
2740 |
+
template <class Duration>
|
2741 |
+
inline
|
2742 |
+
utc_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2743 |
+
to_utc_time(const gps_time<Duration>& t)
|
2744 |
+
{
|
2745 |
+
return gps_clock::to_utc(t);
|
2746 |
+
}
|
2747 |
+
|
2748 |
+
|
2749 |
+
template <class Duration>
|
2750 |
+
inline
|
2751 |
+
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2752 |
+
to_tai_time(const sys_time<Duration>& t)
|
2753 |
+
{
|
2754 |
+
return tai_clock::from_utc(utc_clock::from_sys(t));
|
2755 |
+
}
|
2756 |
+
|
2757 |
+
template <class Duration>
|
2758 |
+
inline
|
2759 |
+
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2760 |
+
to_tai_time(const utc_time<Duration>& t)
|
2761 |
+
{
|
2762 |
+
return tai_clock::from_utc(t);
|
2763 |
+
}
|
2764 |
+
|
2765 |
+
template <class Duration>
|
2766 |
+
inline
|
2767 |
+
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2768 |
+
to_tai_time(const gps_time<Duration>& t)
|
2769 |
+
{
|
2770 |
+
return tai_clock::from_utc(gps_clock::to_utc(t));
|
2771 |
+
}
|
2772 |
+
|
2773 |
+
|
2774 |
+
template <class Duration>
|
2775 |
+
inline
|
2776 |
+
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2777 |
+
to_gps_time(const sys_time<Duration>& t)
|
2778 |
+
{
|
2779 |
+
return gps_clock::from_utc(utc_clock::from_sys(t));
|
2780 |
+
}
|
2781 |
+
|
2782 |
+
template <class Duration>
|
2783 |
+
inline
|
2784 |
+
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2785 |
+
to_gps_time(const utc_time<Duration>& t)
|
2786 |
+
{
|
2787 |
+
return gps_clock::from_utc(t);
|
2788 |
+
}
|
2789 |
+
|
2790 |
+
template <class Duration>
|
2791 |
+
inline
|
2792 |
+
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
2793 |
+
to_gps_time(const tai_time<Duration>& t)
|
2794 |
+
{
|
2795 |
+
return gps_clock::from_utc(tai_clock::to_utc(t));
|
2796 |
+
}
|
2797 |
+
|
2798 |
+
} // namespace date
|
2799 |
+
} // namespace arrow_vendored
|
2800 |
+
|
2801 |
+
#endif // TZ_H
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/tz_private.h
ADDED
@@ -0,0 +1,319 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef TZ_PRIVATE_H
|
2 |
+
#define TZ_PRIVATE_H
|
3 |
+
|
4 |
+
// The MIT License (MIT)
|
5 |
+
//
|
6 |
+
// Copyright (c) 2015, 2016 Howard Hinnant
|
7 |
+
//
|
8 |
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 |
+
// of this software and associated documentation files (the "Software"), to deal
|
10 |
+
// in the Software without restriction, including without limitation the rights
|
11 |
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
+
// copies of the Software, and to permit persons to whom the Software is
|
13 |
+
// furnished to do so, subject to the following conditions:
|
14 |
+
//
|
15 |
+
// The above copyright notice and this permission notice shall be included in all
|
16 |
+
// copies or substantial portions of the Software.
|
17 |
+
//
|
18 |
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 |
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 |
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21 |
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 |
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 |
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24 |
+
// SOFTWARE.
|
25 |
+
//
|
26 |
+
// Our apologies. When the previous paragraph was written, lowercase had not yet
|
27 |
+
// been invented (that would involve another several millennia of evolution).
|
28 |
+
// We did not mean to shout.
|
29 |
+
|
30 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
31 |
+
#include "tz.h"
|
32 |
+
#else
|
33 |
+
#include "date.h"
|
34 |
+
#include <vector>
|
35 |
+
#endif
|
36 |
+
|
37 |
+
namespace arrow_vendored
|
38 |
+
{
|
39 |
+
namespace date
|
40 |
+
{
|
41 |
+
|
42 |
+
namespace detail
|
43 |
+
{
|
44 |
+
|
45 |
+
#if !USE_OS_TZDB
|
46 |
+
|
47 |
+
enum class tz {utc, local, standard};
|
48 |
+
|
49 |
+
//forward declare to avoid warnings in gcc 6.2
|
50 |
+
class MonthDayTime;
|
51 |
+
std::istream& operator>>(std::istream& is, MonthDayTime& x);
|
52 |
+
std::ostream& operator<<(std::ostream& os, const MonthDayTime& x);
|
53 |
+
|
54 |
+
|
55 |
+
class MonthDayTime
|
56 |
+
{
|
57 |
+
private:
|
58 |
+
struct pair
|
59 |
+
{
|
60 |
+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
61 |
+
pair() : month_day_(date::jan / 1), weekday_(0U) {}
|
62 |
+
|
63 |
+
pair(const date::month_day& month_day, const date::weekday& weekday)
|
64 |
+
: month_day_(month_day), weekday_(weekday) {}
|
65 |
+
#endif
|
66 |
+
|
67 |
+
date::month_day month_day_;
|
68 |
+
date::weekday weekday_;
|
69 |
+
};
|
70 |
+
|
71 |
+
enum Type {month_day, month_last_dow, lteq, gteq};
|
72 |
+
|
73 |
+
Type type_{month_day};
|
74 |
+
|
75 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
76 |
+
union U
|
77 |
+
#else
|
78 |
+
struct U
|
79 |
+
#endif
|
80 |
+
{
|
81 |
+
date::month_day month_day_;
|
82 |
+
date::month_weekday_last month_weekday_last_;
|
83 |
+
pair month_day_weekday_;
|
84 |
+
|
85 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
86 |
+
U() : month_day_{date::jan/1} {}
|
87 |
+
#else
|
88 |
+
U() :
|
89 |
+
month_day_(date::jan/1),
|
90 |
+
month_weekday_last_(date::month(0U), date::weekday_last(date::weekday(0U)))
|
91 |
+
{}
|
92 |
+
|
93 |
+
#endif // !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
94 |
+
|
95 |
+
U& operator=(const date::month_day& x);
|
96 |
+
U& operator=(const date::month_weekday_last& x);
|
97 |
+
U& operator=(const pair& x);
|
98 |
+
} u;
|
99 |
+
|
100 |
+
std::chrono::hours h_{0};
|
101 |
+
std::chrono::minutes m_{0};
|
102 |
+
std::chrono::seconds s_{0};
|
103 |
+
tz zone_{tz::local};
|
104 |
+
|
105 |
+
public:
|
106 |
+
MonthDayTime() = default;
|
107 |
+
MonthDayTime(local_seconds tp, tz timezone);
|
108 |
+
MonthDayTime(const date::month_day& md, tz timezone);
|
109 |
+
|
110 |
+
date::day day() const;
|
111 |
+
date::month month() const;
|
112 |
+
tz zone() const {return zone_;}
|
113 |
+
|
114 |
+
void canonicalize(date::year y);
|
115 |
+
|
116 |
+
sys_seconds
|
117 |
+
to_sys(date::year y, std::chrono::seconds offset, std::chrono::seconds save) const;
|
118 |
+
sys_days to_sys_days(date::year y) const;
|
119 |
+
|
120 |
+
sys_seconds to_time_point(date::year y) const;
|
121 |
+
int compare(date::year y, const MonthDayTime& x, date::year yx,
|
122 |
+
std::chrono::seconds offset, std::chrono::minutes prev_save) const;
|
123 |
+
|
124 |
+
friend std::istream& operator>>(std::istream& is, MonthDayTime& x);
|
125 |
+
friend std::ostream& operator<<(std::ostream& os, const MonthDayTime& x);
|
126 |
+
};
|
127 |
+
|
128 |
+
// A Rule specifies one or more set of datetimes without using an offset.
|
129 |
+
// Multiple dates are specified with multiple years. The years in effect
|
130 |
+
// go from starting_year_ to ending_year_, inclusive. starting_year_ <=
|
131 |
+
// ending_year_. save_ is in effect for times from the specified time
|
132 |
+
// onward, including the specified time. When the specified time is
|
133 |
+
// local, it uses the save_ from the chronologically previous Rule, or if
|
134 |
+
// there is none, 0.
|
135 |
+
|
136 |
+
//forward declare to avoid warnings in gcc 6.2
|
137 |
+
class Rule;
|
138 |
+
bool operator==(const Rule& x, const Rule& y);
|
139 |
+
bool operator<(const Rule& x, const Rule& y);
|
140 |
+
bool operator==(const Rule& x, const date::year& y);
|
141 |
+
bool operator<(const Rule& x, const date::year& y);
|
142 |
+
bool operator==(const date::year& x, const Rule& y);
|
143 |
+
bool operator<(const date::year& x, const Rule& y);
|
144 |
+
bool operator==(const Rule& x, const std::string& y);
|
145 |
+
bool operator<(const Rule& x, const std::string& y);
|
146 |
+
bool operator==(const std::string& x, const Rule& y);
|
147 |
+
bool operator<(const std::string& x, const Rule& y);
|
148 |
+
std::ostream& operator<<(std::ostream& os, const Rule& r);
|
149 |
+
|
150 |
+
class Rule
|
151 |
+
{
|
152 |
+
private:
|
153 |
+
std::string name_;
|
154 |
+
date::year starting_year_{0};
|
155 |
+
date::year ending_year_{0};
|
156 |
+
MonthDayTime starting_at_;
|
157 |
+
std::chrono::minutes save_{0};
|
158 |
+
std::string abbrev_;
|
159 |
+
|
160 |
+
public:
|
161 |
+
Rule() = default;
|
162 |
+
explicit Rule(const std::string& s);
|
163 |
+
Rule(const Rule& r, date::year starting_year, date::year ending_year);
|
164 |
+
|
165 |
+
const std::string& name() const {return name_;}
|
166 |
+
const std::string& abbrev() const {return abbrev_;}
|
167 |
+
|
168 |
+
const MonthDayTime& mdt() const {return starting_at_;}
|
169 |
+
const date::year& starting_year() const {return starting_year_;}
|
170 |
+
const date::year& ending_year() const {return ending_year_;}
|
171 |
+
const std::chrono::minutes& save() const {return save_;}
|
172 |
+
|
173 |
+
static void split_overlaps(std::vector<Rule>& rules);
|
174 |
+
|
175 |
+
friend bool operator==(const Rule& x, const Rule& y);
|
176 |
+
friend bool operator<(const Rule& x, const Rule& y);
|
177 |
+
friend bool operator==(const Rule& x, const date::year& y);
|
178 |
+
friend bool operator<(const Rule& x, const date::year& y);
|
179 |
+
friend bool operator==(const date::year& x, const Rule& y);
|
180 |
+
friend bool operator<(const date::year& x, const Rule& y);
|
181 |
+
friend bool operator==(const Rule& x, const std::string& y);
|
182 |
+
friend bool operator<(const Rule& x, const std::string& y);
|
183 |
+
friend bool operator==(const std::string& x, const Rule& y);
|
184 |
+
friend bool operator<(const std::string& x, const Rule& y);
|
185 |
+
|
186 |
+
friend std::ostream& operator<<(std::ostream& os, const Rule& r);
|
187 |
+
|
188 |
+
private:
|
189 |
+
date::day day() const;
|
190 |
+
date::month month() const;
|
191 |
+
static void split_overlaps(std::vector<Rule>& rules, std::size_t i, std::size_t& e);
|
192 |
+
static bool overlaps(const Rule& x, const Rule& y);
|
193 |
+
static void split(std::vector<Rule>& rules, std::size_t i, std::size_t k,
|
194 |
+
std::size_t& e);
|
195 |
+
};
|
196 |
+
|
197 |
+
inline bool operator!=(const Rule& x, const Rule& y) {return !(x == y);}
|
198 |
+
inline bool operator> (const Rule& x, const Rule& y) {return y < x;}
|
199 |
+
inline bool operator<=(const Rule& x, const Rule& y) {return !(y < x);}
|
200 |
+
inline bool operator>=(const Rule& x, const Rule& y) {return !(x < y);}
|
201 |
+
|
202 |
+
inline bool operator!=(const Rule& x, const date::year& y) {return !(x == y);}
|
203 |
+
inline bool operator> (const Rule& x, const date::year& y) {return y < x;}
|
204 |
+
inline bool operator<=(const Rule& x, const date::year& y) {return !(y < x);}
|
205 |
+
inline bool operator>=(const Rule& x, const date::year& y) {return !(x < y);}
|
206 |
+
|
207 |
+
inline bool operator!=(const date::year& x, const Rule& y) {return !(x == y);}
|
208 |
+
inline bool operator> (const date::year& x, const Rule& y) {return y < x;}
|
209 |
+
inline bool operator<=(const date::year& x, const Rule& y) {return !(y < x);}
|
210 |
+
inline bool operator>=(const date::year& x, const Rule& y) {return !(x < y);}
|
211 |
+
|
212 |
+
inline bool operator!=(const Rule& x, const std::string& y) {return !(x == y);}
|
213 |
+
inline bool operator> (const Rule& x, const std::string& y) {return y < x;}
|
214 |
+
inline bool operator<=(const Rule& x, const std::string& y) {return !(y < x);}
|
215 |
+
inline bool operator>=(const Rule& x, const std::string& y) {return !(x < y);}
|
216 |
+
|
217 |
+
inline bool operator!=(const std::string& x, const Rule& y) {return !(x == y);}
|
218 |
+
inline bool operator> (const std::string& x, const Rule& y) {return y < x;}
|
219 |
+
inline bool operator<=(const std::string& x, const Rule& y) {return !(y < x);}
|
220 |
+
inline bool operator>=(const std::string& x, const Rule& y) {return !(x < y);}
|
221 |
+
|
222 |
+
struct zonelet
|
223 |
+
{
|
224 |
+
enum tag {has_rule, has_save, is_empty};
|
225 |
+
|
226 |
+
std::chrono::seconds gmtoff_;
|
227 |
+
tag tag_ = has_rule;
|
228 |
+
|
229 |
+
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
|
230 |
+
union U
|
231 |
+
#else
|
232 |
+
struct U
|
233 |
+
#endif
|
234 |
+
{
|
235 |
+
std::string rule_;
|
236 |
+
std::chrono::minutes save_;
|
237 |
+
|
238 |
+
~U() {}
|
239 |
+
U() {}
|
240 |
+
U(const U&) {}
|
241 |
+
U& operator=(const U&) = delete;
|
242 |
+
} u;
|
243 |
+
|
244 |
+
std::string format_;
|
245 |
+
date::year until_year_{0};
|
246 |
+
MonthDayTime until_date_;
|
247 |
+
sys_seconds until_utc_;
|
248 |
+
local_seconds until_std_;
|
249 |
+
local_seconds until_loc_;
|
250 |
+
std::chrono::minutes initial_save_{0};
|
251 |
+
std::string initial_abbrev_;
|
252 |
+
std::pair<const Rule*, date::year> first_rule_{nullptr, date::year::min()};
|
253 |
+
std::pair<const Rule*, date::year> last_rule_{nullptr, date::year::max()};
|
254 |
+
|
255 |
+
~zonelet();
|
256 |
+
zonelet();
|
257 |
+
zonelet(const zonelet& i);
|
258 |
+
zonelet& operator=(const zonelet&) = delete;
|
259 |
+
};
|
260 |
+
|
261 |
+
#else // USE_OS_TZDB
|
262 |
+
|
263 |
+
struct ttinfo
|
264 |
+
{
|
265 |
+
std::int32_t tt_gmtoff;
|
266 |
+
unsigned char tt_isdst;
|
267 |
+
unsigned char tt_abbrind;
|
268 |
+
unsigned char pad[2];
|
269 |
+
};
|
270 |
+
|
271 |
+
static_assert(sizeof(ttinfo) == 8, "");
|
272 |
+
|
273 |
+
struct expanded_ttinfo
|
274 |
+
{
|
275 |
+
std::chrono::seconds offset;
|
276 |
+
std::string abbrev;
|
277 |
+
bool is_dst;
|
278 |
+
};
|
279 |
+
|
280 |
+
struct transition
|
281 |
+
{
|
282 |
+
sys_seconds timepoint;
|
283 |
+
const expanded_ttinfo* info;
|
284 |
+
|
285 |
+
transition(sys_seconds tp, const expanded_ttinfo* i = nullptr)
|
286 |
+
: timepoint(tp)
|
287 |
+
, info(i)
|
288 |
+
{}
|
289 |
+
|
290 |
+
friend
|
291 |
+
std::ostream&
|
292 |
+
operator<<(std::ostream& os, const transition& t)
|
293 |
+
{
|
294 |
+
using date::operator<<;
|
295 |
+
os << t.timepoint << "Z ";
|
296 |
+
if (t.info->offset >= std::chrono::seconds{0})
|
297 |
+
os << '+';
|
298 |
+
os << make_time(t.info->offset);
|
299 |
+
if (t.info->is_dst > 0)
|
300 |
+
os << " daylight ";
|
301 |
+
else
|
302 |
+
os << " standard ";
|
303 |
+
os << t.info->abbrev;
|
304 |
+
return os;
|
305 |
+
}
|
306 |
+
};
|
307 |
+
|
308 |
+
#endif // USE_OS_TZDB
|
309 |
+
|
310 |
+
} // namespace detail
|
311 |
+
|
312 |
+
} // namespace date
|
313 |
+
} // namespace arrow_vendored
|
314 |
+
|
315 |
+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
316 |
+
#include "tz.h"
|
317 |
+
#endif
|
318 |
+
|
319 |
+
#endif // TZ_PRIVATE_H
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/datetime/visibility.h
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#if defined(ARROW_STATIC)
|
21 |
+
// intentially empty
|
22 |
+
#elif defined(ARROW_EXPORTING)
|
23 |
+
#define DATE_BUILD_DLL
|
24 |
+
#else
|
25 |
+
#define DATE_USE_DLL
|
26 |
+
#endif
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/double-conversion/fast-dtoa.h
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Copyright 2010 the V8 project authors. All rights reserved.
|
2 |
+
// Redistribution and use in source and binary forms, with or without
|
3 |
+
// modification, are permitted provided that the following conditions are
|
4 |
+
// met:
|
5 |
+
//
|
6 |
+
// * Redistributions of source code must retain the above copyright
|
7 |
+
// notice, this list of conditions and the following disclaimer.
|
8 |
+
// * Redistributions in binary form must reproduce the above
|
9 |
+
// copyright notice, this list of conditions and the following
|
10 |
+
// disclaimer in the documentation and/or other materials provided
|
11 |
+
// with the distribution.
|
12 |
+
// * Neither the name of Google Inc. nor the names of its
|
13 |
+
// contributors may be used to endorse or promote products derived
|
14 |
+
// from this software without specific prior written permission.
|
15 |
+
//
|
16 |
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17 |
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18 |
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19 |
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
20 |
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23 |
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24 |
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25 |
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26 |
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
+
|
28 |
+
#ifndef DOUBLE_CONVERSION_FAST_DTOA_H_
|
29 |
+
#define DOUBLE_CONVERSION_FAST_DTOA_H_
|
30 |
+
|
31 |
+
#include "utils.h"
|
32 |
+
|
33 |
+
namespace arrow_vendored {
|
34 |
+
namespace double_conversion {
|
35 |
+
|
36 |
+
enum FastDtoaMode {
|
37 |
+
// Computes the shortest representation of the given input. The returned
|
38 |
+
// result will be the most accurate number of this length. Longer
|
39 |
+
// representations might be more accurate.
|
40 |
+
FAST_DTOA_SHORTEST,
|
41 |
+
// Same as FAST_DTOA_SHORTEST but for single-precision floats.
|
42 |
+
FAST_DTOA_SHORTEST_SINGLE,
|
43 |
+
// Computes a representation where the precision (number of digits) is
|
44 |
+
// given as input. The precision is independent of the decimal point.
|
45 |
+
FAST_DTOA_PRECISION
|
46 |
+
};
|
47 |
+
|
48 |
+
// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
|
49 |
+
// include the terminating '\0' character.
|
50 |
+
static const int kFastDtoaMaximalLength = 17;
|
51 |
+
// Same for single-precision numbers.
|
52 |
+
static const int kFastDtoaMaximalSingleLength = 9;
|
53 |
+
|
54 |
+
// Provides a decimal representation of v.
|
55 |
+
// The result should be interpreted as buffer * 10^(point - length).
|
56 |
+
//
|
57 |
+
// Precondition:
|
58 |
+
// * v must be a strictly positive finite double.
|
59 |
+
//
|
60 |
+
// Returns true if it succeeds, otherwise the result can not be trusted.
|
61 |
+
// There will be *length digits inside the buffer followed by a null terminator.
|
62 |
+
// If the function returns true and mode equals
|
63 |
+
// - FAST_DTOA_SHORTEST, then
|
64 |
+
// the parameter requested_digits is ignored.
|
65 |
+
// The result satisfies
|
66 |
+
// v == (double) (buffer * 10^(point - length)).
|
67 |
+
// The digits in the buffer are the shortest representation possible. E.g.
|
68 |
+
// if 0.099999999999 and 0.1 represent the same double then "1" is returned
|
69 |
+
// with point = 0.
|
70 |
+
// The last digit will be closest to the actual v. That is, even if several
|
71 |
+
// digits might correctly yield 'v' when read again, the buffer will contain
|
72 |
+
// the one closest to v.
|
73 |
+
// - FAST_DTOA_PRECISION, then
|
74 |
+
// the buffer contains requested_digits digits.
|
75 |
+
// the difference v - (buffer * 10^(point-length)) is closest to zero for
|
76 |
+
// all possible representations of requested_digits digits.
|
77 |
+
// If there are two values that are equally close, then FastDtoa returns
|
78 |
+
// false.
|
79 |
+
// For both modes the buffer must be large enough to hold the result.
|
80 |
+
bool FastDtoa(double d,
|
81 |
+
FastDtoaMode mode,
|
82 |
+
int requested_digits,
|
83 |
+
Vector<char> buffer,
|
84 |
+
int* length,
|
85 |
+
int* decimal_point);
|
86 |
+
|
87 |
+
} // namespace double_conversion
|
88 |
+
} // namespace arrow_vendored
|
89 |
+
|
90 |
+
#endif // DOUBLE_CONVERSION_FAST_DTOA_H_
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/double-conversion/fixed-dtoa.h
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Copyright 2010 the V8 project authors. All rights reserved.
|
2 |
+
// Redistribution and use in source and binary forms, with or without
|
3 |
+
// modification, are permitted provided that the following conditions are
|
4 |
+
// met:
|
5 |
+
//
|
6 |
+
// * Redistributions of source code must retain the above copyright
|
7 |
+
// notice, this list of conditions and the following disclaimer.
|
8 |
+
// * Redistributions in binary form must reproduce the above
|
9 |
+
// copyright notice, this list of conditions and the following
|
10 |
+
// disclaimer in the documentation and/or other materials provided
|
11 |
+
// with the distribution.
|
12 |
+
// * Neither the name of Google Inc. nor the names of its
|
13 |
+
// contributors may be used to endorse or promote products derived
|
14 |
+
// from this software without specific prior written permission.
|
15 |
+
//
|
16 |
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17 |
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18 |
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19 |
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
20 |
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23 |
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24 |
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25 |
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26 |
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
+
|
28 |
+
#ifndef DOUBLE_CONVERSION_FIXED_DTOA_H_
|
29 |
+
#define DOUBLE_CONVERSION_FIXED_DTOA_H_
|
30 |
+
|
31 |
+
#include "utils.h"
|
32 |
+
|
33 |
+
namespace arrow_vendored {
|
34 |
+
namespace double_conversion {
|
35 |
+
|
36 |
+
// Produces digits necessary to print a given number with
|
37 |
+
// 'fractional_count' digits after the decimal point.
|
38 |
+
// The buffer must be big enough to hold the result plus one terminating null
|
39 |
+
// character.
|
40 |
+
//
|
41 |
+
// The produced digits might be too short in which case the caller has to fill
|
42 |
+
// the gaps with '0's.
|
43 |
+
// Example: FastFixedDtoa(0.001, 5, ...) is allowed to return buffer = "1", and
|
44 |
+
// decimal_point = -2.
|
45 |
+
// Halfway cases are rounded towards +/-Infinity (away from 0). The call
|
46 |
+
// FastFixedDtoa(0.15, 2, ...) thus returns buffer = "2", decimal_point = 0.
|
47 |
+
// The returned buffer may contain digits that would be truncated from the
|
48 |
+
// shortest representation of the input.
|
49 |
+
//
|
50 |
+
// This method only works for some parameters. If it can't handle the input it
|
51 |
+
// returns false. The output is null-terminated when the function succeeds.
|
52 |
+
bool FastFixedDtoa(double v, int fractional_count,
|
53 |
+
Vector<char> buffer, int* length, int* decimal_point);
|
54 |
+
|
55 |
+
} // namespace double_conversion
|
56 |
+
} // namespace arrow_vendored
|
57 |
+
|
58 |
+
#endif // DOUBLE_CONVERSION_FIXED_DTOA_H_
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_extras.hpp
ADDED
@@ -0,0 +1,649 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* PCG Random Number Generation for C++
|
3 |
+
*
|
4 |
+
* Copyright 2014-2017 Melissa O'Neill <[email protected]>,
|
5 |
+
* and the PCG Project contributors.
|
6 |
+
*
|
7 |
+
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
8 |
+
*
|
9 |
+
* Licensed under the Apache License, Version 2.0 (provided in
|
10 |
+
* LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
|
11 |
+
* or under the MIT license (provided in LICENSE-MIT.txt and at
|
12 |
+
* http://opensource.org/licenses/MIT), at your option. This file may not
|
13 |
+
* be copied, modified, or distributed except according to those terms.
|
14 |
+
*
|
15 |
+
* Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
|
16 |
+
* express or implied. See your chosen license for details.
|
17 |
+
*
|
18 |
+
* For additional information about the PCG random number generation scheme,
|
19 |
+
* visit http://www.pcg-random.org/.
|
20 |
+
*/
|
21 |
+
|
22 |
+
/*
|
23 |
+
* This file provides support code that is useful for random-number generation
|
24 |
+
* but not specific to the PCG generation scheme, including:
|
25 |
+
* - 128-bit int support for platforms where it isn't available natively
|
26 |
+
* - bit twiddling operations
|
27 |
+
* - I/O of 128-bit and 8-bit integers
|
28 |
+
* - Handling the evilness of SeedSeq
|
29 |
+
* - Support for efficiently producing random numbers less than a given
|
30 |
+
* bound
|
31 |
+
*/
|
32 |
+
|
33 |
+
#ifndef PCG_EXTRAS_HPP_INCLUDED
|
34 |
+
#define PCG_EXTRAS_HPP_INCLUDED 1
|
35 |
+
|
36 |
+
#include <cinttypes>
|
37 |
+
#include <cstddef>
|
38 |
+
#include <cstdlib>
|
39 |
+
#include <cstring>
|
40 |
+
#include <cassert>
|
41 |
+
#include <limits>
|
42 |
+
#include <iostream>
|
43 |
+
#include <type_traits>
|
44 |
+
#include <utility>
|
45 |
+
#include <locale>
|
46 |
+
#include <iterator>
|
47 |
+
|
48 |
+
#ifdef __GNUC__
|
49 |
+
#include <cxxabi.h>
|
50 |
+
#endif
|
51 |
+
|
52 |
+
/*
|
53 |
+
* Abstractions for compiler-specific directives
|
54 |
+
*/
|
55 |
+
|
56 |
+
#ifdef __GNUC__
|
57 |
+
#define PCG_NOINLINE __attribute__((noinline))
|
58 |
+
#else
|
59 |
+
#define PCG_NOINLINE
|
60 |
+
#endif
|
61 |
+
|
62 |
+
/*
|
63 |
+
* Some members of the PCG library use 128-bit math. When compiling on 64-bit
|
64 |
+
* platforms, both GCC and Clang provide 128-bit integer types that are ideal
|
65 |
+
* for the job.
|
66 |
+
*
|
67 |
+
* On 32-bit platforms (or with other compilers), we fall back to a C++
|
68 |
+
* class that provides 128-bit unsigned integers instead. It may seem
|
69 |
+
* like we're reinventing the wheel here, because libraries already exist
|
70 |
+
* that support large integers, but most existing libraries provide a very
|
71 |
+
* generic multiprecision code, but here we're operating at a fixed size.
|
72 |
+
* Also, most other libraries are fairly heavyweight. So we use a direct
|
73 |
+
* implementation. Sadly, it's much slower than hand-coded assembly or
|
74 |
+
* direct CPU support.
|
75 |
+
*
|
76 |
+
*/
|
77 |
+
#if __SIZEOF_INT128__ && !PCG_FORCE_EMULATED_128BIT_MATH
|
78 |
+
namespace arrow_vendored {
|
79 |
+
namespace pcg_extras {
|
80 |
+
typedef __uint128_t pcg128_t;
|
81 |
+
}
|
82 |
+
}
|
83 |
+
#define PCG_128BIT_CONSTANT(high,low) \
|
84 |
+
((pcg_extras::pcg128_t(high) << 64) + low)
|
85 |
+
#else
|
86 |
+
#include "pcg_uint128.hpp"
|
87 |
+
namespace arrow_vendored {
|
88 |
+
namespace pcg_extras {
|
89 |
+
typedef pcg_extras::uint_x4<uint32_t,uint64_t> pcg128_t;
|
90 |
+
}
|
91 |
+
}
|
92 |
+
#define PCG_128BIT_CONSTANT(high,low) \
|
93 |
+
pcg_extras::pcg128_t(high,low)
|
94 |
+
#define PCG_EMULATED_128BIT_MATH 1
|
95 |
+
#endif
|
96 |
+
|
97 |
+
|
98 |
+
namespace arrow_vendored {
|
99 |
+
namespace pcg_extras {
|
100 |
+
|
101 |
+
/*
|
102 |
+
* We often need to represent a "number of bits". When used normally, these
|
103 |
+
* numbers are never greater than 128, so an unsigned char is plenty.
|
104 |
+
* If you're using a nonstandard generator of a larger size, you can set
|
105 |
+
* PCG_BITCOUNT_T to have it define it as a larger size. (Some compilers
|
106 |
+
* might produce faster code if you set it to an unsigned int.)
|
107 |
+
*/
|
108 |
+
|
109 |
+
#ifndef PCG_BITCOUNT_T
|
110 |
+
typedef uint8_t bitcount_t;
|
111 |
+
#else
|
112 |
+
typedef PCG_BITCOUNT_T bitcount_t;
|
113 |
+
#endif
|
114 |
+
|
115 |
+
/*
|
116 |
+
* C++ requires us to be able to serialize RNG state by printing or reading
|
117 |
+
* it from a stream. Because we use 128-bit ints, we also need to be able
|
118 |
+
* ot print them, so here is code to do so.
|
119 |
+
*
|
120 |
+
* This code provides enough functionality to print 128-bit ints in decimal
|
121 |
+
* and zero-padded in hex. It's not a full-featured implementation.
|
122 |
+
*/
|
123 |
+
|
124 |
+
template <typename CharT, typename Traits>
|
125 |
+
std::basic_ostream<CharT,Traits>&
|
126 |
+
operator<<(std::basic_ostream<CharT,Traits>& out, pcg128_t value)
|
127 |
+
{
|
128 |
+
auto desired_base = out.flags() & out.basefield;
|
129 |
+
bool want_hex = desired_base == out.hex;
|
130 |
+
|
131 |
+
if (want_hex) {
|
132 |
+
uint64_t highpart = uint64_t(value >> 64);
|
133 |
+
uint64_t lowpart = uint64_t(value);
|
134 |
+
auto desired_width = out.width();
|
135 |
+
if (desired_width > 16) {
|
136 |
+
out.width(desired_width - 16);
|
137 |
+
}
|
138 |
+
if (highpart != 0 || desired_width > 16)
|
139 |
+
out << highpart;
|
140 |
+
CharT oldfill = '\0';
|
141 |
+
if (highpart != 0) {
|
142 |
+
out.width(16);
|
143 |
+
oldfill = out.fill('0');
|
144 |
+
}
|
145 |
+
auto oldflags = out.setf(decltype(desired_base){}, out.showbase);
|
146 |
+
out << lowpart;
|
147 |
+
out.setf(oldflags);
|
148 |
+
if (highpart != 0) {
|
149 |
+
out.fill(oldfill);
|
150 |
+
}
|
151 |
+
return out;
|
152 |
+
}
|
153 |
+
constexpr size_t MAX_CHARS_128BIT = 40;
|
154 |
+
|
155 |
+
char buffer[MAX_CHARS_128BIT];
|
156 |
+
char* pos = buffer+sizeof(buffer);
|
157 |
+
*(--pos) = '\0';
|
158 |
+
constexpr auto BASE = pcg128_t(10ULL);
|
159 |
+
do {
|
160 |
+
auto div = value / BASE;
|
161 |
+
auto mod = uint32_t(value - (div * BASE));
|
162 |
+
*(--pos) = '0' + char(mod);
|
163 |
+
value = div;
|
164 |
+
} while(value != pcg128_t(0ULL));
|
165 |
+
return out << pos;
|
166 |
+
}
|
167 |
+
|
168 |
+
template <typename CharT, typename Traits>
|
169 |
+
std::basic_istream<CharT,Traits>&
|
170 |
+
operator>>(std::basic_istream<CharT,Traits>& in, pcg128_t& value)
|
171 |
+
{
|
172 |
+
typename std::basic_istream<CharT,Traits>::sentry s(in);
|
173 |
+
|
174 |
+
if (!s)
|
175 |
+
return in;
|
176 |
+
|
177 |
+
constexpr auto BASE = pcg128_t(10ULL);
|
178 |
+
pcg128_t current(0ULL);
|
179 |
+
bool did_nothing = true;
|
180 |
+
bool overflow = false;
|
181 |
+
for(;;) {
|
182 |
+
CharT wide_ch = in.get();
|
183 |
+
if (!in.good())
|
184 |
+
break;
|
185 |
+
auto ch = in.narrow(wide_ch, '\0');
|
186 |
+
if (ch < '0' || ch > '9') {
|
187 |
+
in.unget();
|
188 |
+
break;
|
189 |
+
}
|
190 |
+
did_nothing = false;
|
191 |
+
pcg128_t digit(uint32_t(ch - '0'));
|
192 |
+
pcg128_t timesbase = current*BASE;
|
193 |
+
overflow = overflow || timesbase < current;
|
194 |
+
current = timesbase + digit;
|
195 |
+
overflow = overflow || current < digit;
|
196 |
+
}
|
197 |
+
|
198 |
+
if (did_nothing || overflow) {
|
199 |
+
in.setstate(std::ios::failbit);
|
200 |
+
if (overflow)
|
201 |
+
current = ~pcg128_t(0ULL);
|
202 |
+
}
|
203 |
+
|
204 |
+
value = current;
|
205 |
+
|
206 |
+
return in;
|
207 |
+
}
|
208 |
+
|
209 |
+
/*
|
210 |
+
* Likewise, if people use tiny rngs, we'll be serializing uint8_t.
|
211 |
+
* If we just used the provided IO operators, they'd read/write chars,
|
212 |
+
* not ints, so we need to define our own. We *can* redefine this operator
|
213 |
+
* here because we're in our own namespace.
|
214 |
+
*/
|
215 |
+
|
216 |
+
template <typename CharT, typename Traits>
|
217 |
+
std::basic_ostream<CharT,Traits>&
|
218 |
+
operator<<(std::basic_ostream<CharT,Traits>&out, uint8_t value)
|
219 |
+
{
|
220 |
+
return out << uint32_t(value);
|
221 |
+
}
|
222 |
+
|
223 |
+
template <typename CharT, typename Traits>
|
224 |
+
std::basic_istream<CharT,Traits>&
|
225 |
+
operator>>(std::basic_istream<CharT,Traits>& in, uint8_t& target)
|
226 |
+
{
|
227 |
+
uint32_t value = 0xdecea5edU;
|
228 |
+
in >> value;
|
229 |
+
if (!in && value == 0xdecea5edU)
|
230 |
+
return in;
|
231 |
+
if (value > uint8_t(~0)) {
|
232 |
+
in.setstate(std::ios::failbit);
|
233 |
+
value = ~0U;
|
234 |
+
}
|
235 |
+
target = uint8_t(value);
|
236 |
+
return in;
|
237 |
+
}
|
238 |
+
|
239 |
+
/* Unfortunately, the above functions don't get found in preference to the
|
240 |
+
* built in ones, so we create some more specific overloads that will.
|
241 |
+
* Ugh.
|
242 |
+
*/
|
243 |
+
|
244 |
+
inline std::ostream& operator<<(std::ostream& out, uint8_t value)
|
245 |
+
{
|
246 |
+
return pcg_extras::operator<< <char>(out, value);
|
247 |
+
}
|
248 |
+
|
249 |
+
inline std::istream& operator>>(std::istream& in, uint8_t& value)
|
250 |
+
{
|
251 |
+
return pcg_extras::operator>> <char>(in, value);
|
252 |
+
}
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
+
/*
|
257 |
+
* Useful bitwise operations.
|
258 |
+
*/
|
259 |
+
|
260 |
+
/*
|
261 |
+
* XorShifts are invertable, but they are someting of a pain to invert.
|
262 |
+
* This function backs them out. It's used by the whacky "inside out"
|
263 |
+
* generator defined later.
|
264 |
+
*/
|
265 |
+
|
266 |
+
template <typename itype>
|
267 |
+
inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift)
|
268 |
+
{
|
269 |
+
if (2*shift >= bits) {
|
270 |
+
return x ^ (x >> shift);
|
271 |
+
}
|
272 |
+
itype lowmask1 = (itype(1U) << (bits - shift*2)) - 1;
|
273 |
+
itype highmask1 = ~lowmask1;
|
274 |
+
itype top1 = x;
|
275 |
+
itype bottom1 = x & lowmask1;
|
276 |
+
top1 ^= top1 >> shift;
|
277 |
+
top1 &= highmask1;
|
278 |
+
x = top1 | bottom1;
|
279 |
+
itype lowmask2 = (itype(1U) << (bits - shift)) - 1;
|
280 |
+
itype bottom2 = x & lowmask2;
|
281 |
+
bottom2 = unxorshift(bottom2, bits - shift, shift);
|
282 |
+
bottom2 &= lowmask1;
|
283 |
+
return top1 | bottom2;
|
284 |
+
}
|
285 |
+
|
286 |
+
/*
|
287 |
+
* Rotate left and right.
|
288 |
+
*
|
289 |
+
* In ideal world, compilers would spot idiomatic rotate code and convert it
|
290 |
+
* to a rotate instruction. Of course, opinions vary on what the correct
|
291 |
+
* idiom is and how to spot it. For clang, sometimes it generates better
|
292 |
+
* (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
|
293 |
+
*/
|
294 |
+
|
295 |
+
template <typename itype>
|
296 |
+
inline itype rotl(itype value, bitcount_t rot)
|
297 |
+
{
|
298 |
+
constexpr bitcount_t bits = sizeof(itype) * 8;
|
299 |
+
constexpr bitcount_t mask = bits - 1;
|
300 |
+
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
301 |
+
return rot ? (value << rot) | (value >> (bits - rot)) : value;
|
302 |
+
#else
|
303 |
+
return (value << rot) | (value >> ((- rot) & mask));
|
304 |
+
#endif
|
305 |
+
}
|
306 |
+
|
307 |
+
template <typename itype>
|
308 |
+
inline itype rotr(itype value, bitcount_t rot)
|
309 |
+
{
|
310 |
+
constexpr bitcount_t bits = sizeof(itype) * 8;
|
311 |
+
constexpr bitcount_t mask = bits - 1;
|
312 |
+
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
313 |
+
return rot ? (value >> rot) | (value << (bits - rot)) : value;
|
314 |
+
#else
|
315 |
+
return (value >> rot) | (value << ((- rot) & mask));
|
316 |
+
#endif
|
317 |
+
}
|
318 |
+
|
319 |
+
/* Unfortunately, both Clang and GCC sometimes perform poorly when it comes
|
320 |
+
* to properly recognizing idiomatic rotate code, so for we also provide
|
321 |
+
* assembler directives (enabled with PCG_USE_INLINE_ASM). Boo, hiss.
|
322 |
+
* (I hope that these compilers get better so that this code can die.)
|
323 |
+
*
|
324 |
+
* These overloads will be preferred over the general template code above.
|
325 |
+
*/
|
326 |
+
#if PCG_USE_INLINE_ASM && __GNUC__ && (__x86_64__ || __i386__)
|
327 |
+
|
328 |
+
inline uint8_t rotr(uint8_t value, bitcount_t rot)
|
329 |
+
{
|
330 |
+
asm ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
331 |
+
return value;
|
332 |
+
}
|
333 |
+
|
334 |
+
inline uint16_t rotr(uint16_t value, bitcount_t rot)
|
335 |
+
{
|
336 |
+
asm ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
337 |
+
return value;
|
338 |
+
}
|
339 |
+
|
340 |
+
inline uint32_t rotr(uint32_t value, bitcount_t rot)
|
341 |
+
{
|
342 |
+
asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
343 |
+
return value;
|
344 |
+
}
|
345 |
+
|
346 |
+
#if __x86_64__
|
347 |
+
inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
348 |
+
{
|
349 |
+
asm ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
350 |
+
return value;
|
351 |
+
}
|
352 |
+
#endif // __x86_64__
|
353 |
+
|
354 |
+
#elif defined(_MSC_VER)
|
355 |
+
// Use MSVC++ bit rotation intrinsics
|
356 |
+
|
357 |
+
#pragma intrinsic(_rotr, _rotr64, _rotr8, _rotr16)
|
358 |
+
|
359 |
+
inline uint8_t rotr(uint8_t value, bitcount_t rot)
|
360 |
+
{
|
361 |
+
return _rotr8(value, rot);
|
362 |
+
}
|
363 |
+
|
364 |
+
inline uint16_t rotr(uint16_t value, bitcount_t rot)
|
365 |
+
{
|
366 |
+
return _rotr16(value, rot);
|
367 |
+
}
|
368 |
+
|
369 |
+
inline uint32_t rotr(uint32_t value, bitcount_t rot)
|
370 |
+
{
|
371 |
+
return _rotr(value, rot);
|
372 |
+
}
|
373 |
+
|
374 |
+
inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
375 |
+
{
|
376 |
+
return _rotr64(value, rot);
|
377 |
+
}
|
378 |
+
|
379 |
+
#endif // PCG_USE_INLINE_ASM
|
380 |
+
|
381 |
+
|
382 |
+
/*
|
383 |
+
* The C++ SeedSeq concept (modelled by seed_seq) can fill an array of
|
384 |
+
* 32-bit integers with seed data, but sometimes we want to produce
|
385 |
+
* larger or smaller integers.
|
386 |
+
*
|
387 |
+
* The following code handles this annoyance.
|
388 |
+
*
|
389 |
+
* uneven_copy will copy an array of 32-bit ints to an array of larger or
|
390 |
+
* smaller ints (actually, the code is general it only needing forward
|
391 |
+
* iterators). The copy is identical to the one that would be performed if
|
392 |
+
* we just did memcpy on a standard little-endian machine, but works
|
393 |
+
* regardless of the endian of the machine (or the weirdness of the ints
|
394 |
+
* involved).
|
395 |
+
*
|
396 |
+
* generate_to initializes an array of integers using a SeedSeq
|
397 |
+
* object. It is given the size as a static constant at compile time and
|
398 |
+
* tries to avoid memory allocation. If we're filling in 32-bit constants
|
399 |
+
* we just do it directly. If we need a separate buffer and it's small,
|
400 |
+
* we allocate it on the stack. Otherwise, we fall back to heap allocation.
|
401 |
+
* Ugh.
|
402 |
+
*
|
403 |
+
* generate_one produces a single value of some integral type using a
|
404 |
+
* SeedSeq object.
|
405 |
+
*/
|
406 |
+
|
407 |
+
/* uneven_copy helper, case where destination ints are less than 32 bit. */
|
408 |
+
|
409 |
+
template<class SrcIter, class DestIter>
|
410 |
+
SrcIter uneven_copy_impl(
|
411 |
+
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
412 |
+
std::true_type)
|
413 |
+
{
|
414 |
+
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
415 |
+
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
416 |
+
|
417 |
+
constexpr bitcount_t SRC_SIZE = sizeof(src_t);
|
418 |
+
constexpr bitcount_t DEST_SIZE = sizeof(dest_t);
|
419 |
+
constexpr bitcount_t DEST_BITS = DEST_SIZE * 8;
|
420 |
+
constexpr bitcount_t SCALE = SRC_SIZE / DEST_SIZE;
|
421 |
+
|
422 |
+
size_t count = 0;
|
423 |
+
src_t value = 0;
|
424 |
+
|
425 |
+
while (dest_first != dest_last) {
|
426 |
+
if ((count++ % SCALE) == 0)
|
427 |
+
value = *src_first++; // Get more bits
|
428 |
+
else
|
429 |
+
value >>= DEST_BITS; // Move down bits
|
430 |
+
|
431 |
+
*dest_first++ = dest_t(value); // Truncates, ignores high bits.
|
432 |
+
}
|
433 |
+
return src_first;
|
434 |
+
}
|
435 |
+
|
436 |
+
/* uneven_copy helper, case where destination ints are more than 32 bit. */
|
437 |
+
|
438 |
+
template<class SrcIter, class DestIter>
|
439 |
+
SrcIter uneven_copy_impl(
|
440 |
+
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
441 |
+
std::false_type)
|
442 |
+
{
|
443 |
+
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
444 |
+
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
445 |
+
|
446 |
+
constexpr auto SRC_SIZE = sizeof(src_t);
|
447 |
+
constexpr auto SRC_BITS = SRC_SIZE * 8;
|
448 |
+
constexpr auto DEST_SIZE = sizeof(dest_t);
|
449 |
+
constexpr auto SCALE = (DEST_SIZE+SRC_SIZE-1) / SRC_SIZE;
|
450 |
+
|
451 |
+
while (dest_first != dest_last) {
|
452 |
+
dest_t value(0UL);
|
453 |
+
unsigned int shift = 0;
|
454 |
+
|
455 |
+
for (size_t i = 0; i < SCALE; ++i) {
|
456 |
+
value |= dest_t(*src_first++) << shift;
|
457 |
+
shift += SRC_BITS;
|
458 |
+
}
|
459 |
+
|
460 |
+
*dest_first++ = value;
|
461 |
+
}
|
462 |
+
return src_first;
|
463 |
+
}
|
464 |
+
|
465 |
+
/* uneven_copy, call the right code for larger vs. smaller */
|
466 |
+
|
467 |
+
template<class SrcIter, class DestIter>
|
468 |
+
inline SrcIter uneven_copy(SrcIter src_first,
|
469 |
+
DestIter dest_first, DestIter dest_last)
|
470 |
+
{
|
471 |
+
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
472 |
+
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
473 |
+
|
474 |
+
constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
|
475 |
+
|
476 |
+
return uneven_copy_impl(src_first, dest_first, dest_last,
|
477 |
+
std::integral_constant<bool, DEST_IS_SMALLER>{});
|
478 |
+
}
|
479 |
+
|
480 |
+
/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
|
481 |
+
* (actually works for any random-access iterator)
|
482 |
+
*/
|
483 |
+
|
484 |
+
template <size_t size, typename SeedSeq, typename DestIter>
|
485 |
+
inline void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
486 |
+
std::true_type)
|
487 |
+
{
|
488 |
+
generator.generate(dest, dest+size);
|
489 |
+
}
|
490 |
+
|
491 |
+
template <size_t size, typename SeedSeq, typename DestIter>
|
492 |
+
void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
493 |
+
std::false_type)
|
494 |
+
{
|
495 |
+
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
496 |
+
constexpr auto DEST_SIZE = sizeof(dest_t);
|
497 |
+
constexpr auto GEN_SIZE = sizeof(uint32_t);
|
498 |
+
|
499 |
+
constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
|
500 |
+
constexpr size_t FROM_ELEMS =
|
501 |
+
GEN_IS_SMALLER
|
502 |
+
? size * ((DEST_SIZE+GEN_SIZE-1) / GEN_SIZE)
|
503 |
+
: (size + (GEN_SIZE / DEST_SIZE) - 1)
|
504 |
+
/ ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
|
505 |
+
// this odd code ^^^^^^^^^^^^^^^^^ is work-around for
|
506 |
+
// a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
|
507 |
+
|
508 |
+
if (FROM_ELEMS <= 1024) {
|
509 |
+
uint32_t buffer[FROM_ELEMS];
|
510 |
+
generator.generate(buffer, buffer+FROM_ELEMS);
|
511 |
+
uneven_copy(buffer, dest, dest+size);
|
512 |
+
} else {
|
513 |
+
uint32_t* buffer = static_cast<uint32_t*>(malloc(GEN_SIZE * FROM_ELEMS));
|
514 |
+
generator.generate(buffer, buffer+FROM_ELEMS);
|
515 |
+
uneven_copy(buffer, dest, dest+size);
|
516 |
+
free(static_cast<void*>(buffer));
|
517 |
+
}
|
518 |
+
}
|
519 |
+
|
520 |
+
template <size_t size, typename SeedSeq, typename DestIter>
|
521 |
+
inline void generate_to(SeedSeq&& generator, DestIter dest)
|
522 |
+
{
|
523 |
+
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
524 |
+
constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
|
525 |
+
|
526 |
+
generate_to_impl<size>(std::forward<SeedSeq>(generator), dest,
|
527 |
+
std::integral_constant<bool, IS_32BIT>{});
|
528 |
+
}
|
529 |
+
|
530 |
+
/* generate_one, produce a value of integral type using a SeedSeq
|
531 |
+
* (optionally, we can have it produce more than one and pick which one
|
532 |
+
* we want)
|
533 |
+
*/
|
534 |
+
|
535 |
+
template <typename UInt, size_t i = 0UL, size_t N = i+1UL, typename SeedSeq>
|
536 |
+
inline UInt generate_one(SeedSeq&& generator)
|
537 |
+
{
|
538 |
+
UInt result[N];
|
539 |
+
generate_to<N>(std::forward<SeedSeq>(generator), result);
|
540 |
+
return result[i];
|
541 |
+
}
|
542 |
+
|
543 |
+
template <typename RngType>
|
544 |
+
auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
|
545 |
+
-> typename RngType::result_type
|
546 |
+
{
|
547 |
+
typedef typename RngType::result_type rtype;
|
548 |
+
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
|
549 |
+
% upper_bound;
|
550 |
+
for (;;) {
|
551 |
+
rtype r = rng() - RngType::min();
|
552 |
+
if (r >= threshold)
|
553 |
+
return r % upper_bound;
|
554 |
+
}
|
555 |
+
}
|
556 |
+
|
557 |
+
template <typename Iter, typename RandType>
|
558 |
+
void shuffle(Iter from, Iter to, RandType&& rng)
|
559 |
+
{
|
560 |
+
typedef typename std::iterator_traits<Iter>::difference_type delta_t;
|
561 |
+
typedef typename std::remove_reference<RandType>::type::result_type result_t;
|
562 |
+
auto count = to - from;
|
563 |
+
while (count > 1) {
|
564 |
+
delta_t chosen = delta_t(bounded_rand(rng, result_t(count)));
|
565 |
+
--count;
|
566 |
+
--to;
|
567 |
+
using std::swap;
|
568 |
+
swap(*(from + chosen), *to);
|
569 |
+
}
|
570 |
+
}
|
571 |
+
|
572 |
+
/*
|
573 |
+
* Although std::seed_seq is useful, it isn't everything. Often we want to
|
574 |
+
* initialize a random-number generator some other way, such as from a random
|
575 |
+
* device.
|
576 |
+
*
|
577 |
+
* Technically, it does not meet the requirements of a SeedSequence because
|
578 |
+
* it lacks some of the rarely-used member functions (some of which would
|
579 |
+
* be impossible to provide). However the C++ standard is quite specific
|
580 |
+
* that actual engines only called the generate method, so it ought not to be
|
581 |
+
* a problem in practice.
|
582 |
+
*/
|
583 |
+
|
584 |
+
template <typename RngType>
|
585 |
+
class seed_seq_from {
|
586 |
+
private:
|
587 |
+
RngType rng_;
|
588 |
+
|
589 |
+
typedef uint_least32_t result_type;
|
590 |
+
|
591 |
+
public:
|
592 |
+
template<typename... Args>
|
593 |
+
seed_seq_from(Args&&... args) :
|
594 |
+
rng_(std::forward<Args>(args)...)
|
595 |
+
{
|
596 |
+
// Nothing (else) to do...
|
597 |
+
}
|
598 |
+
|
599 |
+
template<typename Iter>
|
600 |
+
void generate(Iter start, Iter finish)
|
601 |
+
{
|
602 |
+
for (auto i = start; i != finish; ++i)
|
603 |
+
*i = result_type(rng_());
|
604 |
+
}
|
605 |
+
|
606 |
+
constexpr size_t size() const
|
607 |
+
{
|
608 |
+
return (sizeof(typename RngType::result_type) > sizeof(result_type)
|
609 |
+
&& RngType::max() > ~size_t(0UL))
|
610 |
+
? ~size_t(0UL)
|
611 |
+
: size_t(RngType::max());
|
612 |
+
}
|
613 |
+
};
|
614 |
+
|
615 |
+
// Sometimes, when debugging or testing, it's handy to be able print the name
|
616 |
+
// of a (in human-readable form). This code allows the idiom:
|
617 |
+
//
|
618 |
+
// cout << printable_typename<my_foo_type_t>()
|
619 |
+
//
|
620 |
+
// to print out my_foo_type_t (or its concrete type if it is a synonym)
|
621 |
+
|
622 |
+
#if __cpp_rtti || __GXX_RTTI
|
623 |
+
|
624 |
+
template <typename T>
|
625 |
+
struct printable_typename {};
|
626 |
+
|
627 |
+
template <typename T>
|
628 |
+
std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
|
629 |
+
const char *implementation_typename = typeid(T).name();
|
630 |
+
#ifdef __GNUC__
|
631 |
+
int status;
|
632 |
+
char* pretty_name =
|
633 |
+
abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
|
634 |
+
if (status == 0)
|
635 |
+
out << pretty_name;
|
636 |
+
free(static_cast<void*>(pretty_name));
|
637 |
+
if (status == 0)
|
638 |
+
return out;
|
639 |
+
#endif
|
640 |
+
out << implementation_typename;
|
641 |
+
return out;
|
642 |
+
}
|
643 |
+
|
644 |
+
#endif // __cpp_rtti || __GXX_RTTI
|
645 |
+
|
646 |
+
} // namespace pcg_extras
|
647 |
+
} // namespace arrow_vendored
|
648 |
+
|
649 |
+
#endif // PCG_EXTRAS_HPP_INCLUDED
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_random.hpp
ADDED
@@ -0,0 +1,1954 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* PCG Random Number Generation for C++
|
3 |
+
*
|
4 |
+
* Copyright 2014-2019 Melissa O'Neill <[email protected]>,
|
5 |
+
* and the PCG Project contributors.
|
6 |
+
*
|
7 |
+
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
8 |
+
*
|
9 |
+
* Licensed under the Apache License, Version 2.0 (provided in
|
10 |
+
* LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
|
11 |
+
* or under the MIT license (provided in LICENSE-MIT.txt and at
|
12 |
+
* http://opensource.org/licenses/MIT), at your option. This file may not
|
13 |
+
* be copied, modified, or distributed except according to those terms.
|
14 |
+
*
|
15 |
+
* Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
|
16 |
+
* express or implied. See your chosen license for details.
|
17 |
+
*
|
18 |
+
* For additional information about the PCG random number generation scheme,
|
19 |
+
* visit http://www.pcg-random.org/.
|
20 |
+
*/
|
21 |
+
|
22 |
+
/*
|
23 |
+
* This code provides the reference implementation of the PCG family of
|
24 |
+
* random number generators. The code is complex because it implements
|
25 |
+
*
|
26 |
+
* - several members of the PCG family, specifically members corresponding
|
27 |
+
* to the output functions:
|
28 |
+
* - XSH RR (good for 64-bit state, 32-bit output)
|
29 |
+
* - XSH RS (good for 64-bit state, 32-bit output)
|
30 |
+
* - XSL RR (good for 128-bit state, 64-bit output)
|
31 |
+
* - RXS M XS (statistically most powerful generator)
|
32 |
+
* - XSL RR RR (good for 128-bit state, 128-bit output)
|
33 |
+
* - and RXS, RXS M, XSH, XSL (mostly for testing)
|
34 |
+
* - at potentially *arbitrary* bit sizes
|
35 |
+
* - with four different techniques for random streams (MCG, one-stream
|
36 |
+
* LCG, settable-stream LCG, unique-stream LCG)
|
37 |
+
* - and the extended generation schemes allowing arbitrary periods
|
38 |
+
* - with all features of C++11 random number generation (and more),
|
39 |
+
* some of which are somewhat painful, including
|
40 |
+
* - initializing with a SeedSequence which writes 32-bit values
|
41 |
+
* to memory, even though the state of the generator may not
|
42 |
+
* use 32-bit values (it might use smaller or larger integers)
|
43 |
+
* - I/O for RNGs and a prescribed format, which needs to handle
|
44 |
+
* the issue that 8-bit and 128-bit integers don't have working
|
45 |
+
* I/O routines (e.g., normally 8-bit = char, not integer)
|
46 |
+
* - equality and inequality for RNGs
|
47 |
+
* - and a number of convenience typedefs to mask all the complexity
|
48 |
+
*
|
49 |
+
* The code employes a fairly heavy level of abstraction, and has to deal
|
50 |
+
* with various C++ minutia. If you're looking to learn about how the PCG
|
51 |
+
* scheme works, you're probably best of starting with one of the other
|
52 |
+
* codebases (see www.pcg-random.org). But if you're curious about the
|
53 |
+
* constants for the various output functions used in those other, simpler,
|
54 |
+
* codebases, this code shows how they are calculated.
|
55 |
+
*
|
56 |
+
* On the positive side, at least there are convenience typedefs so that you
|
57 |
+
* can say
|
58 |
+
*
|
59 |
+
* pcg32 myRNG;
|
60 |
+
*
|
61 |
+
* rather than:
|
62 |
+
*
|
63 |
+
* pcg_detail::engine<
|
64 |
+
* uint32_t, // Output Type
|
65 |
+
* uint64_t, // State Type
|
66 |
+
* pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func
|
67 |
+
* pcg_detail::specific_stream<uint64_t>, // Stream Kind
|
68 |
+
* pcg_detail::default_multiplier<uint64_t> // LCG Mult
|
69 |
+
* > myRNG;
|
70 |
+
*
|
71 |
+
*/
|
72 |
+
|
73 |
+
#ifndef PCG_RAND_HPP_INCLUDED
|
74 |
+
#define PCG_RAND_HPP_INCLUDED 1
|
75 |
+
|
76 |
+
#include <algorithm>
|
77 |
+
#include <cinttypes>
|
78 |
+
#include <cstddef>
|
79 |
+
#include <cstdlib>
|
80 |
+
#include <cstring>
|
81 |
+
#include <cassert>
|
82 |
+
#include <limits>
|
83 |
+
#include <iostream>
|
84 |
+
#include <iterator>
|
85 |
+
#include <type_traits>
|
86 |
+
#include <utility>
|
87 |
+
#include <locale>
|
88 |
+
#include <new>
|
89 |
+
#include <stdexcept>
|
90 |
+
|
91 |
+
#ifdef _MSC_VER
|
92 |
+
#pragma warning(disable:4146)
|
93 |
+
#endif
|
94 |
+
|
95 |
+
#ifdef _MSC_VER
|
96 |
+
#define PCG_ALWAYS_INLINE __forceinline
|
97 |
+
#elif __GNUC__
|
98 |
+
#define PCG_ALWAYS_INLINE __attribute__((always_inline))
|
99 |
+
#else
|
100 |
+
#define PCG_ALWAYS_INLINE inline
|
101 |
+
#endif
|
102 |
+
|
103 |
+
/*
|
104 |
+
* The pcg_extras namespace contains some support code that is likley to
|
105 |
+
* be useful for a variety of RNGs, including:
|
106 |
+
* - 128-bit int support for platforms where it isn't available natively
|
107 |
+
* - bit twiddling operations
|
108 |
+
* - I/O of 128-bit and 8-bit integers
|
109 |
+
* - Handling the evilness of SeedSeq
|
110 |
+
* - Support for efficiently producing random numbers less than a given
|
111 |
+
* bound
|
112 |
+
*/
|
113 |
+
|
114 |
+
#include "pcg_extras.hpp"
|
115 |
+
|
116 |
+
namespace arrow_vendored {
|
117 |
+
namespace pcg_detail {
|
118 |
+
|
119 |
+
using namespace pcg_extras;
|
120 |
+
|
121 |
+
/*
|
122 |
+
* The LCG generators need some constants to function. This code lets you
|
123 |
+
* look up the constant by *type*. For example
|
124 |
+
*
|
125 |
+
* default_multiplier<uint32_t>::multiplier()
|
126 |
+
*
|
127 |
+
* gives you the default multipler for 32-bit integers. We use the name
|
128 |
+
* of the constant and not a generic word like value to allow these classes
|
129 |
+
* to be used as mixins.
|
130 |
+
*/
|
131 |
+
|
132 |
+
template <typename T>
|
133 |
+
struct default_multiplier {
|
134 |
+
// Not defined for an arbitrary type
|
135 |
+
};
|
136 |
+
|
137 |
+
template <typename T>
|
138 |
+
struct default_increment {
|
139 |
+
// Not defined for an arbitrary type
|
140 |
+
};
|
141 |
+
|
142 |
+
#define PCG_DEFINE_CONSTANT(type, what, kind, constant) \
|
143 |
+
template <> \
|
144 |
+
struct what ## _ ## kind<type> { \
|
145 |
+
static constexpr type kind() { \
|
146 |
+
return constant; \
|
147 |
+
} \
|
148 |
+
};
|
149 |
+
|
150 |
+
PCG_DEFINE_CONSTANT(uint8_t, default, multiplier, 141U)
|
151 |
+
PCG_DEFINE_CONSTANT(uint8_t, default, increment, 77U)
|
152 |
+
|
153 |
+
PCG_DEFINE_CONSTANT(uint16_t, default, multiplier, 12829U)
|
154 |
+
PCG_DEFINE_CONSTANT(uint16_t, default, increment, 47989U)
|
155 |
+
|
156 |
+
PCG_DEFINE_CONSTANT(uint32_t, default, multiplier, 747796405U)
|
157 |
+
PCG_DEFINE_CONSTANT(uint32_t, default, increment, 2891336453U)
|
158 |
+
|
159 |
+
PCG_DEFINE_CONSTANT(uint64_t, default, multiplier, 6364136223846793005ULL)
|
160 |
+
PCG_DEFINE_CONSTANT(uint64_t, default, increment, 1442695040888963407ULL)
|
161 |
+
|
162 |
+
PCG_DEFINE_CONSTANT(pcg128_t, default, multiplier,
|
163 |
+
PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL))
|
164 |
+
PCG_DEFINE_CONSTANT(pcg128_t, default, increment,
|
165 |
+
PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL))
|
166 |
+
|
167 |
+
/* Alternative (cheaper) multipliers for 128-bit */
|
168 |
+
|
169 |
+
template <typename T>
|
170 |
+
struct cheap_multiplier : public default_multiplier<T> {
|
171 |
+
// For most types just use the default.
|
172 |
+
};
|
173 |
+
|
174 |
+
template <>
|
175 |
+
struct cheap_multiplier<pcg128_t> {
|
176 |
+
static constexpr uint64_t multiplier() {
|
177 |
+
return 0xda942042e4dd58b5ULL;
|
178 |
+
}
|
179 |
+
};
|
180 |
+
|
181 |
+
|
182 |
+
/*
|
183 |
+
* Each PCG generator is available in four variants, based on how it applies
|
184 |
+
* the additive constant for its underlying LCG; the variations are:
|
185 |
+
*
|
186 |
+
* single stream - all instances use the same fixed constant, thus
|
187 |
+
* the RNG always somewhere in same sequence
|
188 |
+
* mcg - adds zero, resulting in a single stream and reduced
|
189 |
+
* period
|
190 |
+
* specific stream - the constant can be changed at any time, selecting
|
191 |
+
* a different random sequence
|
192 |
+
* unique stream - the constant is based on the memory address of the
|
193 |
+
* object, thus every RNG has its own unique sequence
|
194 |
+
*
|
195 |
+
* This variation is provided though mixin classes which define a function
|
196 |
+
* value called increment() that returns the nesessary additive constant.
|
197 |
+
*/
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
/*
|
202 |
+
* unique stream
|
203 |
+
*/
|
204 |
+
|
205 |
+
|
206 |
+
template <typename itype>
|
207 |
+
class unique_stream {
|
208 |
+
protected:
|
209 |
+
static constexpr bool is_mcg = false;
|
210 |
+
|
211 |
+
// Is never called, but is provided for symmetry with specific_stream
|
212 |
+
void set_stream(...)
|
213 |
+
{
|
214 |
+
abort();
|
215 |
+
}
|
216 |
+
|
217 |
+
public:
|
218 |
+
typedef itype state_type;
|
219 |
+
|
220 |
+
constexpr itype increment() const {
|
221 |
+
return itype(reinterpret_cast<uintptr_t>(this) | 1);
|
222 |
+
}
|
223 |
+
|
224 |
+
constexpr itype stream() const
|
225 |
+
{
|
226 |
+
return increment() >> 1;
|
227 |
+
}
|
228 |
+
|
229 |
+
static constexpr bool can_specify_stream = false;
|
230 |
+
|
231 |
+
static constexpr size_t streams_pow2()
|
232 |
+
{
|
233 |
+
return (sizeof(itype) < sizeof(size_t) ? sizeof(itype)
|
234 |
+
: sizeof(size_t))*8 - 1u;
|
235 |
+
}
|
236 |
+
|
237 |
+
protected:
|
238 |
+
constexpr unique_stream() = default;
|
239 |
+
};
|
240 |
+
|
241 |
+
|
242 |
+
/*
|
243 |
+
* no stream (mcg)
|
244 |
+
*/
|
245 |
+
|
246 |
+
template <typename itype>
|
247 |
+
class no_stream {
|
248 |
+
protected:
|
249 |
+
static constexpr bool is_mcg = true;
|
250 |
+
|
251 |
+
// Is never called, but is provided for symmetry with specific_stream
|
252 |
+
void set_stream(...)
|
253 |
+
{
|
254 |
+
abort();
|
255 |
+
}
|
256 |
+
|
257 |
+
public:
|
258 |
+
typedef itype state_type;
|
259 |
+
|
260 |
+
static constexpr itype increment() {
|
261 |
+
return 0;
|
262 |
+
}
|
263 |
+
|
264 |
+
static constexpr bool can_specify_stream = false;
|
265 |
+
|
266 |
+
static constexpr size_t streams_pow2()
|
267 |
+
{
|
268 |
+
return 0u;
|
269 |
+
}
|
270 |
+
|
271 |
+
protected:
|
272 |
+
constexpr no_stream() = default;
|
273 |
+
};
|
274 |
+
|
275 |
+
|
276 |
+
/*
|
277 |
+
* single stream/sequence (oneseq)
|
278 |
+
*/
|
279 |
+
|
280 |
+
template <typename itype>
|
281 |
+
class oneseq_stream : public default_increment<itype> {
|
282 |
+
protected:
|
283 |
+
static constexpr bool is_mcg = false;
|
284 |
+
|
285 |
+
// Is never called, but is provided for symmetry with specific_stream
|
286 |
+
void set_stream(...)
|
287 |
+
{
|
288 |
+
abort();
|
289 |
+
}
|
290 |
+
|
291 |
+
public:
|
292 |
+
typedef itype state_type;
|
293 |
+
|
294 |
+
static constexpr itype stream()
|
295 |
+
{
|
296 |
+
return default_increment<itype>::increment() >> 1;
|
297 |
+
}
|
298 |
+
|
299 |
+
static constexpr bool can_specify_stream = false;
|
300 |
+
|
301 |
+
static constexpr size_t streams_pow2()
|
302 |
+
{
|
303 |
+
return 0u;
|
304 |
+
}
|
305 |
+
|
306 |
+
protected:
|
307 |
+
constexpr oneseq_stream() = default;
|
308 |
+
};
|
309 |
+
|
310 |
+
|
311 |
+
/*
|
312 |
+
* specific stream
|
313 |
+
*/
|
314 |
+
|
315 |
+
template <typename itype>
|
316 |
+
class specific_stream {
|
317 |
+
protected:
|
318 |
+
static constexpr bool is_mcg = false;
|
319 |
+
|
320 |
+
itype inc_ = default_increment<itype>::increment();
|
321 |
+
|
322 |
+
public:
|
323 |
+
typedef itype state_type;
|
324 |
+
typedef itype stream_state;
|
325 |
+
|
326 |
+
constexpr itype increment() const {
|
327 |
+
return inc_;
|
328 |
+
}
|
329 |
+
|
330 |
+
itype stream()
|
331 |
+
{
|
332 |
+
return inc_ >> 1;
|
333 |
+
}
|
334 |
+
|
335 |
+
void set_stream(itype specific_seq)
|
336 |
+
{
|
337 |
+
inc_ = (specific_seq << 1) | 1;
|
338 |
+
}
|
339 |
+
|
340 |
+
static constexpr bool can_specify_stream = true;
|
341 |
+
|
342 |
+
static constexpr size_t streams_pow2()
|
343 |
+
{
|
344 |
+
return (sizeof(itype)*8) - 1u;
|
345 |
+
}
|
346 |
+
|
347 |
+
protected:
|
348 |
+
specific_stream() = default;
|
349 |
+
|
350 |
+
specific_stream(itype specific_seq)
|
351 |
+
: inc_(itype(specific_seq << 1) | itype(1U))
|
352 |
+
{
|
353 |
+
// Nothing (else) to do.
|
354 |
+
}
|
355 |
+
};
|
356 |
+
|
357 |
+
|
358 |
+
/*
|
359 |
+
* This is where it all comes together. This function joins together three
|
360 |
+
* mixin classes which define
|
361 |
+
* - the LCG additive constant (the stream)
|
362 |
+
* - the LCG multiplier
|
363 |
+
* - the output function
|
364 |
+
* in addition, we specify the type of the LCG state, and the result type,
|
365 |
+
* and whether to use the pre-advance version of the state for the output
|
366 |
+
* (increasing instruction-level parallelism) or the post-advance version
|
367 |
+
* (reducing register pressure).
|
368 |
+
*
|
369 |
+
* Given the high level of parameterization, the code has to use some
|
370 |
+
* template-metaprogramming tricks to handle some of the suble variations
|
371 |
+
* involved.
|
372 |
+
*/
|
373 |
+
|
374 |
+
template <typename xtype, typename itype,
|
375 |
+
typename output_mixin,
|
376 |
+
bool output_previous = true,
|
377 |
+
typename stream_mixin = oneseq_stream<itype>,
|
378 |
+
typename multiplier_mixin = default_multiplier<itype> >
|
379 |
+
class engine : protected output_mixin,
|
380 |
+
public stream_mixin,
|
381 |
+
protected multiplier_mixin {
|
382 |
+
protected:
|
383 |
+
itype state_;
|
384 |
+
|
385 |
+
struct can_specify_stream_tag {};
|
386 |
+
struct no_specifiable_stream_tag {};
|
387 |
+
|
388 |
+
using stream_mixin::increment;
|
389 |
+
using multiplier_mixin::multiplier;
|
390 |
+
|
391 |
+
public:
|
392 |
+
typedef xtype result_type;
|
393 |
+
typedef itype state_type;
|
394 |
+
|
395 |
+
static constexpr size_t period_pow2()
|
396 |
+
{
|
397 |
+
return sizeof(state_type)*8 - 2*stream_mixin::is_mcg;
|
398 |
+
}
|
399 |
+
|
400 |
+
// It would be nice to use std::numeric_limits for these, but
|
401 |
+
// we can't be sure that it'd be defined for the 128-bit types.
|
402 |
+
|
403 |
+
static constexpr result_type min()
|
404 |
+
{
|
405 |
+
return result_type(0UL);
|
406 |
+
}
|
407 |
+
|
408 |
+
static constexpr result_type max()
|
409 |
+
{
|
410 |
+
return result_type(~result_type(0UL));
|
411 |
+
}
|
412 |
+
|
413 |
+
protected:
|
414 |
+
itype bump(itype state)
|
415 |
+
{
|
416 |
+
return state * multiplier() + increment();
|
417 |
+
}
|
418 |
+
|
419 |
+
itype base_generate()
|
420 |
+
{
|
421 |
+
return state_ = bump(state_);
|
422 |
+
}
|
423 |
+
|
424 |
+
itype base_generate0()
|
425 |
+
{
|
426 |
+
itype old_state = state_;
|
427 |
+
state_ = bump(state_);
|
428 |
+
return old_state;
|
429 |
+
}
|
430 |
+
|
431 |
+
public:
|
432 |
+
result_type operator()()
|
433 |
+
{
|
434 |
+
if (output_previous)
|
435 |
+
return this->output(base_generate0());
|
436 |
+
else
|
437 |
+
return this->output(base_generate());
|
438 |
+
}
|
439 |
+
|
440 |
+
result_type operator()(result_type upper_bound)
|
441 |
+
{
|
442 |
+
return bounded_rand(*this, upper_bound);
|
443 |
+
}
|
444 |
+
|
445 |
+
protected:
|
446 |
+
static itype advance(itype state, itype delta,
|
447 |
+
itype cur_mult, itype cur_plus);
|
448 |
+
|
449 |
+
static itype distance(itype cur_state, itype newstate, itype cur_mult,
|
450 |
+
itype cur_plus, itype mask = ~itype(0U));
|
451 |
+
|
452 |
+
itype distance(itype newstate, itype mask = itype(~itype(0U))) const
|
453 |
+
{
|
454 |
+
return distance(state_, newstate, multiplier(), increment(), mask);
|
455 |
+
}
|
456 |
+
|
457 |
+
public:
|
458 |
+
void advance(itype delta)
|
459 |
+
{
|
460 |
+
state_ = advance(state_, delta, this->multiplier(), this->increment());
|
461 |
+
}
|
462 |
+
|
463 |
+
void backstep(itype delta)
|
464 |
+
{
|
465 |
+
advance(-delta);
|
466 |
+
}
|
467 |
+
|
468 |
+
void discard(itype delta)
|
469 |
+
{
|
470 |
+
advance(delta);
|
471 |
+
}
|
472 |
+
|
473 |
+
bool wrapped()
|
474 |
+
{
|
475 |
+
if (stream_mixin::is_mcg) {
|
476 |
+
// For MCGs, the low order two bits never change. In this
|
477 |
+
// implementation, we keep them fixed at 3 to make this test
|
478 |
+
// easier.
|
479 |
+
return state_ == 3;
|
480 |
+
} else {
|
481 |
+
return state_ == 0;
|
482 |
+
}
|
483 |
+
}
|
484 |
+
|
485 |
+
engine(itype state = itype(0xcafef00dd15ea5e5ULL))
|
486 |
+
: state_(this->is_mcg ? state|state_type(3U)
|
487 |
+
: bump(state + this->increment()))
|
488 |
+
{
|
489 |
+
// Nothing else to do.
|
490 |
+
}
|
491 |
+
|
492 |
+
// This function may or may not exist. It thus has to be a template
|
493 |
+
// to use SFINAE; users don't have to worry about its template-ness.
|
494 |
+
|
495 |
+
template <typename sm = stream_mixin>
|
496 |
+
engine(itype state, typename sm::stream_state stream_seed)
|
497 |
+
: stream_mixin(stream_seed),
|
498 |
+
state_(this->is_mcg ? state|state_type(3U)
|
499 |
+
: bump(state + this->increment()))
|
500 |
+
{
|
501 |
+
// Nothing else to do.
|
502 |
+
}
|
503 |
+
|
504 |
+
template<typename SeedSeq>
|
505 |
+
engine(SeedSeq&& seedSeq, typename std::enable_if<
|
506 |
+
!stream_mixin::can_specify_stream
|
507 |
+
&& !std::is_convertible<SeedSeq, itype>::value
|
508 |
+
&& !std::is_convertible<SeedSeq, engine>::value,
|
509 |
+
no_specifiable_stream_tag>::type = {})
|
510 |
+
: engine(generate_one<itype>(std::forward<SeedSeq>(seedSeq)))
|
511 |
+
{
|
512 |
+
// Nothing else to do.
|
513 |
+
}
|
514 |
+
|
515 |
+
template<typename SeedSeq>
|
516 |
+
engine(SeedSeq&& seedSeq, typename std::enable_if<
|
517 |
+
stream_mixin::can_specify_stream
|
518 |
+
&& !std::is_convertible<SeedSeq, itype>::value
|
519 |
+
&& !std::is_convertible<SeedSeq, engine>::value,
|
520 |
+
can_specify_stream_tag>::type = {})
|
521 |
+
{
|
522 |
+
itype seeddata[2];
|
523 |
+
generate_to<2>(std::forward<SeedSeq>(seedSeq), seeddata);
|
524 |
+
seed(seeddata[1], seeddata[0]);
|
525 |
+
}
|
526 |
+
|
527 |
+
|
528 |
+
template<typename... Args>
|
529 |
+
void seed(Args&&... args)
|
530 |
+
{
|
531 |
+
new (this) engine(std::forward<Args>(args)...);
|
532 |
+
}
|
533 |
+
|
534 |
+
template <typename xtype1, typename itype1,
|
535 |
+
typename output_mixin1, bool output_previous1,
|
536 |
+
typename stream_mixin_lhs, typename multiplier_mixin_lhs,
|
537 |
+
typename stream_mixin_rhs, typename multiplier_mixin_rhs>
|
538 |
+
friend bool operator==(const engine<xtype1,itype1,
|
539 |
+
output_mixin1,output_previous1,
|
540 |
+
stream_mixin_lhs, multiplier_mixin_lhs>&,
|
541 |
+
const engine<xtype1,itype1,
|
542 |
+
output_mixin1,output_previous1,
|
543 |
+
stream_mixin_rhs, multiplier_mixin_rhs>&);
|
544 |
+
|
545 |
+
template <typename xtype1, typename itype1,
|
546 |
+
typename output_mixin1, bool output_previous1,
|
547 |
+
typename stream_mixin_lhs, typename multiplier_mixin_lhs,
|
548 |
+
typename stream_mixin_rhs, typename multiplier_mixin_rhs>
|
549 |
+
friend itype1 operator-(const engine<xtype1,itype1,
|
550 |
+
output_mixin1,output_previous1,
|
551 |
+
stream_mixin_lhs, multiplier_mixin_lhs>&,
|
552 |
+
const engine<xtype1,itype1,
|
553 |
+
output_mixin1,output_previous1,
|
554 |
+
stream_mixin_rhs, multiplier_mixin_rhs>&);
|
555 |
+
|
556 |
+
template <typename CharT, typename Traits,
|
557 |
+
typename xtype1, typename itype1,
|
558 |
+
typename output_mixin1, bool output_previous1,
|
559 |
+
typename stream_mixin1, typename multiplier_mixin1>
|
560 |
+
friend std::basic_ostream<CharT,Traits>&
|
561 |
+
operator<<(std::basic_ostream<CharT,Traits>& out,
|
562 |
+
const engine<xtype1,itype1,
|
563 |
+
output_mixin1,output_previous1,
|
564 |
+
stream_mixin1, multiplier_mixin1>&);
|
565 |
+
|
566 |
+
template <typename CharT, typename Traits,
|
567 |
+
typename xtype1, typename itype1,
|
568 |
+
typename output_mixin1, bool output_previous1,
|
569 |
+
typename stream_mixin1, typename multiplier_mixin1>
|
570 |
+
friend std::basic_istream<CharT,Traits>&
|
571 |
+
operator>>(std::basic_istream<CharT,Traits>& in,
|
572 |
+
engine<xtype1, itype1,
|
573 |
+
output_mixin1, output_previous1,
|
574 |
+
stream_mixin1, multiplier_mixin1>& rng);
|
575 |
+
};
|
576 |
+
|
577 |
+
template <typename CharT, typename Traits,
|
578 |
+
typename xtype, typename itype,
|
579 |
+
typename output_mixin, bool output_previous,
|
580 |
+
typename stream_mixin, typename multiplier_mixin>
|
581 |
+
std::basic_ostream<CharT,Traits>&
|
582 |
+
operator<<(std::basic_ostream<CharT,Traits>& out,
|
583 |
+
const engine<xtype,itype,
|
584 |
+
output_mixin,output_previous,
|
585 |
+
stream_mixin, multiplier_mixin>& rng)
|
586 |
+
{
|
587 |
+
using pcg_extras::operator<<;
|
588 |
+
|
589 |
+
auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
|
590 |
+
auto space = out.widen(' ');
|
591 |
+
auto orig_fill = out.fill();
|
592 |
+
|
593 |
+
out << rng.multiplier() << space
|
594 |
+
<< rng.increment() << space
|
595 |
+
<< rng.state_;
|
596 |
+
|
597 |
+
out.flags(orig_flags);
|
598 |
+
out.fill(orig_fill);
|
599 |
+
return out;
|
600 |
+
}
|
601 |
+
|
602 |
+
|
603 |
+
template <typename CharT, typename Traits,
|
604 |
+
typename xtype, typename itype,
|
605 |
+
typename output_mixin, bool output_previous,
|
606 |
+
typename stream_mixin, typename multiplier_mixin>
|
607 |
+
std::basic_istream<CharT,Traits>&
|
608 |
+
operator>>(std::basic_istream<CharT,Traits>& in,
|
609 |
+
engine<xtype,itype,
|
610 |
+
output_mixin,output_previous,
|
611 |
+
stream_mixin, multiplier_mixin>& rng)
|
612 |
+
{
|
613 |
+
using pcg_extras::operator>>;
|
614 |
+
|
615 |
+
auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
|
616 |
+
|
617 |
+
itype multiplier, increment, state;
|
618 |
+
in >> multiplier >> increment >> state;
|
619 |
+
|
620 |
+
if (!in.fail()) {
|
621 |
+
bool good = true;
|
622 |
+
if (multiplier != rng.multiplier()) {
|
623 |
+
good = false;
|
624 |
+
} else if (rng.can_specify_stream) {
|
625 |
+
rng.set_stream(increment >> 1);
|
626 |
+
} else if (increment != rng.increment()) {
|
627 |
+
good = false;
|
628 |
+
}
|
629 |
+
if (good) {
|
630 |
+
rng.state_ = state;
|
631 |
+
} else {
|
632 |
+
in.clear(std::ios::failbit);
|
633 |
+
}
|
634 |
+
}
|
635 |
+
|
636 |
+
in.flags(orig_flags);
|
637 |
+
return in;
|
638 |
+
}
|
639 |
+
|
640 |
+
|
641 |
+
template <typename xtype, typename itype,
|
642 |
+
typename output_mixin, bool output_previous,
|
643 |
+
typename stream_mixin, typename multiplier_mixin>
|
644 |
+
itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
|
645 |
+
multiplier_mixin>::advance(
|
646 |
+
itype state, itype delta, itype cur_mult, itype cur_plus)
|
647 |
+
{
|
648 |
+
// The method used here is based on Brown, "Random Number Generation
|
649 |
+
// with Arbitrary Stride,", Transactions of the American Nuclear
|
650 |
+
// Society (Nov. 1994). The algorithm is very similar to fast
|
651 |
+
// exponentiation.
|
652 |
+
//
|
653 |
+
// Even though delta is an unsigned integer, we can pass a
|
654 |
+
// signed integer to go backwards, it just goes "the long way round".
|
655 |
+
|
656 |
+
constexpr itype ZERO = 0u; // itype may be a non-trivial types, so
|
657 |
+
constexpr itype ONE = 1u; // we define some ugly constants.
|
658 |
+
itype acc_mult = 1;
|
659 |
+
itype acc_plus = 0;
|
660 |
+
while (delta > ZERO) {
|
661 |
+
if (delta & ONE) {
|
662 |
+
acc_mult *= cur_mult;
|
663 |
+
acc_plus = acc_plus*cur_mult + cur_plus;
|
664 |
+
}
|
665 |
+
cur_plus = (cur_mult+ONE)*cur_plus;
|
666 |
+
cur_mult *= cur_mult;
|
667 |
+
delta >>= 1;
|
668 |
+
}
|
669 |
+
return acc_mult * state + acc_plus;
|
670 |
+
}
|
671 |
+
|
672 |
+
template <typename xtype, typename itype,
|
673 |
+
typename output_mixin, bool output_previous,
|
674 |
+
typename stream_mixin, typename multiplier_mixin>
|
675 |
+
itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
|
676 |
+
multiplier_mixin>::distance(
|
677 |
+
itype cur_state, itype newstate, itype cur_mult, itype cur_plus, itype mask)
|
678 |
+
{
|
679 |
+
constexpr itype ONE = 1u; // itype could be weird, so use constant
|
680 |
+
bool is_mcg = cur_plus == itype(0);
|
681 |
+
itype the_bit = is_mcg ? itype(4u) : itype(1u);
|
682 |
+
itype distance = 0u;
|
683 |
+
while ((cur_state & mask) != (newstate & mask)) {
|
684 |
+
if ((cur_state & the_bit) != (newstate & the_bit)) {
|
685 |
+
cur_state = cur_state * cur_mult + cur_plus;
|
686 |
+
distance |= the_bit;
|
687 |
+
}
|
688 |
+
assert((cur_state & the_bit) == (newstate & the_bit));
|
689 |
+
the_bit <<= 1;
|
690 |
+
cur_plus = (cur_mult+ONE)*cur_plus;
|
691 |
+
cur_mult *= cur_mult;
|
692 |
+
}
|
693 |
+
return is_mcg ? distance >> 2 : distance;
|
694 |
+
}
|
695 |
+
|
696 |
+
template <typename xtype, typename itype,
|
697 |
+
typename output_mixin, bool output_previous,
|
698 |
+
typename stream_mixin_lhs, typename multiplier_mixin_lhs,
|
699 |
+
typename stream_mixin_rhs, typename multiplier_mixin_rhs>
|
700 |
+
itype operator-(const engine<xtype,itype,
|
701 |
+
output_mixin,output_previous,
|
702 |
+
stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
|
703 |
+
const engine<xtype,itype,
|
704 |
+
output_mixin,output_previous,
|
705 |
+
stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
|
706 |
+
{
|
707 |
+
static_assert(
|
708 |
+
std::is_same<stream_mixin_lhs, stream_mixin_rhs>::value &&
|
709 |
+
std::is_same<multiplier_mixin_lhs, multiplier_mixin_rhs>::value,
|
710 |
+
"Incomparable generators");
|
711 |
+
if (lhs.increment() == rhs.increment()) {
|
712 |
+
return rhs.distance(lhs.state_);
|
713 |
+
} else {
|
714 |
+
constexpr itype ONE = 1u;
|
715 |
+
itype lhs_diff = lhs.increment() + (lhs.multiplier()-ONE) * lhs.state_;
|
716 |
+
itype rhs_diff = rhs.increment() + (rhs.multiplier()-ONE) * rhs.state_;
|
717 |
+
if ((lhs_diff & itype(3u)) != (rhs_diff & itype(3u))) {
|
718 |
+
rhs_diff = -rhs_diff;
|
719 |
+
}
|
720 |
+
return rhs.distance(rhs_diff, lhs_diff, rhs.multiplier(), itype(0u));
|
721 |
+
}
|
722 |
+
}
|
723 |
+
|
724 |
+
|
725 |
+
template <typename xtype, typename itype,
|
726 |
+
typename output_mixin, bool output_previous,
|
727 |
+
typename stream_mixin_lhs, typename multiplier_mixin_lhs,
|
728 |
+
typename stream_mixin_rhs, typename multiplier_mixin_rhs>
|
729 |
+
bool operator==(const engine<xtype,itype,
|
730 |
+
output_mixin,output_previous,
|
731 |
+
stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
|
732 |
+
const engine<xtype,itype,
|
733 |
+
output_mixin,output_previous,
|
734 |
+
stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
|
735 |
+
{
|
736 |
+
return (lhs.multiplier() == rhs.multiplier())
|
737 |
+
&& (lhs.increment() == rhs.increment())
|
738 |
+
&& (lhs.state_ == rhs.state_);
|
739 |
+
}
|
740 |
+
|
741 |
+
template <typename xtype, typename itype,
|
742 |
+
typename output_mixin, bool output_previous,
|
743 |
+
typename stream_mixin_lhs, typename multiplier_mixin_lhs,
|
744 |
+
typename stream_mixin_rhs, typename multiplier_mixin_rhs>
|
745 |
+
inline bool operator!=(const engine<xtype,itype,
|
746 |
+
output_mixin,output_previous,
|
747 |
+
stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
|
748 |
+
const engine<xtype,itype,
|
749 |
+
output_mixin,output_previous,
|
750 |
+
stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
|
751 |
+
{
|
752 |
+
return !operator==(lhs,rhs);
|
753 |
+
}
|
754 |
+
|
755 |
+
|
756 |
+
template <typename xtype, typename itype,
|
757 |
+
template<typename XT,typename IT> class output_mixin,
|
758 |
+
bool output_previous = (sizeof(itype) <= 8),
|
759 |
+
template<typename IT> class multiplier_mixin = default_multiplier>
|
760 |
+
using oneseq_base = engine<xtype, itype,
|
761 |
+
output_mixin<xtype, itype>, output_previous,
|
762 |
+
oneseq_stream<itype>,
|
763 |
+
multiplier_mixin<itype> >;
|
764 |
+
|
765 |
+
template <typename xtype, typename itype,
|
766 |
+
template<typename XT,typename IT> class output_mixin,
|
767 |
+
bool output_previous = (sizeof(itype) <= 8),
|
768 |
+
template<typename IT> class multiplier_mixin = default_multiplier>
|
769 |
+
using unique_base = engine<xtype, itype,
|
770 |
+
output_mixin<xtype, itype>, output_previous,
|
771 |
+
unique_stream<itype>,
|
772 |
+
multiplier_mixin<itype> >;
|
773 |
+
|
774 |
+
template <typename xtype, typename itype,
|
775 |
+
template<typename XT,typename IT> class output_mixin,
|
776 |
+
bool output_previous = (sizeof(itype) <= 8),
|
777 |
+
template<typename IT> class multiplier_mixin = default_multiplier>
|
778 |
+
using setseq_base = engine<xtype, itype,
|
779 |
+
output_mixin<xtype, itype>, output_previous,
|
780 |
+
specific_stream<itype>,
|
781 |
+
multiplier_mixin<itype> >;
|
782 |
+
|
783 |
+
template <typename xtype, typename itype,
|
784 |
+
template<typename XT,typename IT> class output_mixin,
|
785 |
+
bool output_previous = (sizeof(itype) <= 8),
|
786 |
+
template<typename IT> class multiplier_mixin = default_multiplier>
|
787 |
+
using mcg_base = engine<xtype, itype,
|
788 |
+
output_mixin<xtype, itype>, output_previous,
|
789 |
+
no_stream<itype>,
|
790 |
+
multiplier_mixin<itype> >;
|
791 |
+
|
792 |
+
/*
|
793 |
+
* OUTPUT FUNCTIONS.
|
794 |
+
*
|
795 |
+
* These are the core of the PCG generation scheme. They specify how to
|
796 |
+
* turn the base LCG's internal state into the output value of the final
|
797 |
+
* generator.
|
798 |
+
*
|
799 |
+
* They're implemented as mixin classes.
|
800 |
+
*
|
801 |
+
* All of the classes have code that is written to allow it to be applied
|
802 |
+
* at *arbitrary* bit sizes, although in practice they'll only be used at
|
803 |
+
* standard sizes supported by C++.
|
804 |
+
*/
|
805 |
+
|
806 |
+
/*
|
807 |
+
* XSH RS -- high xorshift, followed by a random shift
|
808 |
+
*
|
809 |
+
* Fast. A good performer.
|
810 |
+
*/
|
811 |
+
|
812 |
+
template <typename xtype, typename itype>
|
813 |
+
struct xsh_rs_mixin {
|
814 |
+
static xtype output(itype internal)
|
815 |
+
{
|
816 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
817 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
818 |
+
constexpr bitcount_t sparebits = bits - xtypebits;
|
819 |
+
constexpr bitcount_t opbits =
|
820 |
+
sparebits-5 >= 64 ? 5
|
821 |
+
: sparebits-4 >= 32 ? 4
|
822 |
+
: sparebits-3 >= 16 ? 3
|
823 |
+
: sparebits-2 >= 4 ? 2
|
824 |
+
: sparebits-1 >= 1 ? 1
|
825 |
+
: 0;
|
826 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
827 |
+
constexpr bitcount_t maxrandshift = mask;
|
828 |
+
constexpr bitcount_t topspare = opbits;
|
829 |
+
constexpr bitcount_t bottomspare = sparebits - topspare;
|
830 |
+
constexpr bitcount_t xshift = topspare + (xtypebits+maxrandshift)/2;
|
831 |
+
bitcount_t rshift =
|
832 |
+
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
|
833 |
+
internal ^= internal >> xshift;
|
834 |
+
xtype result = xtype(internal >> (bottomspare - maxrandshift + rshift));
|
835 |
+
return result;
|
836 |
+
}
|
837 |
+
};
|
838 |
+
|
839 |
+
/*
|
840 |
+
* XSH RR -- high xorshift, followed by a random rotate
|
841 |
+
*
|
842 |
+
* Fast. A good performer. Slightly better statistically than XSH RS.
|
843 |
+
*/
|
844 |
+
|
845 |
+
template <typename xtype, typename itype>
|
846 |
+
struct xsh_rr_mixin {
|
847 |
+
static xtype output(itype internal)
|
848 |
+
{
|
849 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
850 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
|
851 |
+
constexpr bitcount_t sparebits = bits - xtypebits;
|
852 |
+
constexpr bitcount_t wantedopbits =
|
853 |
+
xtypebits >= 128 ? 7
|
854 |
+
: xtypebits >= 64 ? 6
|
855 |
+
: xtypebits >= 32 ? 5
|
856 |
+
: xtypebits >= 16 ? 4
|
857 |
+
: 3;
|
858 |
+
constexpr bitcount_t opbits =
|
859 |
+
sparebits >= wantedopbits ? wantedopbits
|
860 |
+
: sparebits;
|
861 |
+
constexpr bitcount_t amplifier = wantedopbits - opbits;
|
862 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
863 |
+
constexpr bitcount_t topspare = opbits;
|
864 |
+
constexpr bitcount_t bottomspare = sparebits - topspare;
|
865 |
+
constexpr bitcount_t xshift = (topspare + xtypebits)/2;
|
866 |
+
bitcount_t rot = opbits ? bitcount_t(internal >> (bits - opbits)) & mask
|
867 |
+
: 0;
|
868 |
+
bitcount_t amprot = (rot << amplifier) & mask;
|
869 |
+
internal ^= internal >> xshift;
|
870 |
+
xtype result = xtype(internal >> bottomspare);
|
871 |
+
result = rotr(result, amprot);
|
872 |
+
return result;
|
873 |
+
}
|
874 |
+
};
|
875 |
+
|
876 |
+
/*
|
877 |
+
* RXS -- random xorshift
|
878 |
+
*/
|
879 |
+
|
880 |
+
template <typename xtype, typename itype>
|
881 |
+
struct rxs_mixin {
|
882 |
+
static xtype output_rxs(itype internal)
|
883 |
+
{
|
884 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
885 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
|
886 |
+
constexpr bitcount_t shift = bits - xtypebits;
|
887 |
+
constexpr bitcount_t extrashift = (xtypebits - shift)/2;
|
888 |
+
bitcount_t rshift = shift > 64+8 ? (internal >> (bits - 6)) & 63
|
889 |
+
: shift > 32+4 ? (internal >> (bits - 5)) & 31
|
890 |
+
: shift > 16+2 ? (internal >> (bits - 4)) & 15
|
891 |
+
: shift > 8+1 ? (internal >> (bits - 3)) & 7
|
892 |
+
: shift > 4+1 ? (internal >> (bits - 2)) & 3
|
893 |
+
: shift > 2+1 ? (internal >> (bits - 1)) & 1
|
894 |
+
: 0;
|
895 |
+
internal ^= internal >> (shift + extrashift - rshift);
|
896 |
+
xtype result = internal >> rshift;
|
897 |
+
return result;
|
898 |
+
}
|
899 |
+
};
|
900 |
+
|
901 |
+
/*
|
902 |
+
* RXS M XS -- random xorshift, mcg multiply, fixed xorshift
|
903 |
+
*
|
904 |
+
* The most statistically powerful generator, but all those steps
|
905 |
+
* make it slower than some of the others. We give it the rottenest jobs.
|
906 |
+
*
|
907 |
+
* Because it's usually used in contexts where the state type and the
|
908 |
+
* result type are the same, it is a permutation and is thus invertable.
|
909 |
+
* We thus provide a function to invert it. This function is used to
|
910 |
+
* for the "inside out" generator used by the extended generator.
|
911 |
+
*/
|
912 |
+
|
913 |
+
/* Defined type-based concepts for the multiplication step. They're actually
|
914 |
+
* all derived by truncating the 128-bit, which was computed to be a good
|
915 |
+
* "universal" constant.
|
916 |
+
*/
|
917 |
+
|
918 |
+
template <typename T>
|
919 |
+
struct mcg_multiplier {
|
920 |
+
// Not defined for an arbitrary type
|
921 |
+
};
|
922 |
+
|
923 |
+
template <typename T>
|
924 |
+
struct mcg_unmultiplier {
|
925 |
+
// Not defined for an arbitrary type
|
926 |
+
};
|
927 |
+
|
928 |
+
PCG_DEFINE_CONSTANT(uint8_t, mcg, multiplier, 217U)
|
929 |
+
PCG_DEFINE_CONSTANT(uint8_t, mcg, unmultiplier, 105U)
|
930 |
+
|
931 |
+
PCG_DEFINE_CONSTANT(uint16_t, mcg, multiplier, 62169U)
|
932 |
+
PCG_DEFINE_CONSTANT(uint16_t, mcg, unmultiplier, 28009U)
|
933 |
+
|
934 |
+
PCG_DEFINE_CONSTANT(uint32_t, mcg, multiplier, 277803737U)
|
935 |
+
PCG_DEFINE_CONSTANT(uint32_t, mcg, unmultiplier, 2897767785U)
|
936 |
+
|
937 |
+
PCG_DEFINE_CONSTANT(uint64_t, mcg, multiplier, 12605985483714917081ULL)
|
938 |
+
PCG_DEFINE_CONSTANT(uint64_t, mcg, unmultiplier, 15009553638781119849ULL)
|
939 |
+
|
940 |
+
PCG_DEFINE_CONSTANT(pcg128_t, mcg, multiplier,
|
941 |
+
PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL))
|
942 |
+
PCG_DEFINE_CONSTANT(pcg128_t, mcg, unmultiplier,
|
943 |
+
PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL))
|
944 |
+
|
945 |
+
|
946 |
+
template <typename xtype, typename itype>
|
947 |
+
struct rxs_m_xs_mixin {
|
948 |
+
static xtype output(itype internal)
|
949 |
+
{
|
950 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
951 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
952 |
+
constexpr bitcount_t opbits = xtypebits >= 128 ? 6
|
953 |
+
: xtypebits >= 64 ? 5
|
954 |
+
: xtypebits >= 32 ? 4
|
955 |
+
: xtypebits >= 16 ? 3
|
956 |
+
: 2;
|
957 |
+
constexpr bitcount_t shift = bits - xtypebits;
|
958 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
959 |
+
bitcount_t rshift =
|
960 |
+
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
|
961 |
+
internal ^= internal >> (opbits + rshift);
|
962 |
+
internal *= mcg_multiplier<itype>::multiplier();
|
963 |
+
xtype result = internal >> shift;
|
964 |
+
result ^= result >> ((2U*xtypebits+2U)/3U);
|
965 |
+
return result;
|
966 |
+
}
|
967 |
+
|
968 |
+
static itype unoutput(itype internal)
|
969 |
+
{
|
970 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
971 |
+
constexpr bitcount_t opbits = bits >= 128 ? 6
|
972 |
+
: bits >= 64 ? 5
|
973 |
+
: bits >= 32 ? 4
|
974 |
+
: bits >= 16 ? 3
|
975 |
+
: 2;
|
976 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
977 |
+
|
978 |
+
internal = unxorshift(internal, bits, (2U*bits+2U)/3U);
|
979 |
+
|
980 |
+
internal *= mcg_unmultiplier<itype>::unmultiplier();
|
981 |
+
|
982 |
+
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
|
983 |
+
internal = unxorshift(internal, bits, opbits + rshift);
|
984 |
+
|
985 |
+
return internal;
|
986 |
+
}
|
987 |
+
};
|
988 |
+
|
989 |
+
|
990 |
+
/*
|
991 |
+
* RXS M -- random xorshift, mcg multiply
|
992 |
+
*/
|
993 |
+
|
994 |
+
template <typename xtype, typename itype>
|
995 |
+
struct rxs_m_mixin {
|
996 |
+
static xtype output(itype internal)
|
997 |
+
{
|
998 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
999 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
1000 |
+
constexpr bitcount_t opbits = xtypebits >= 128 ? 6
|
1001 |
+
: xtypebits >= 64 ? 5
|
1002 |
+
: xtypebits >= 32 ? 4
|
1003 |
+
: xtypebits >= 16 ? 3
|
1004 |
+
: 2;
|
1005 |
+
constexpr bitcount_t shift = bits - xtypebits;
|
1006 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
1007 |
+
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
|
1008 |
+
internal ^= internal >> (opbits + rshift);
|
1009 |
+
internal *= mcg_multiplier<itype>::multiplier();
|
1010 |
+
xtype result = internal >> shift;
|
1011 |
+
return result;
|
1012 |
+
}
|
1013 |
+
};
|
1014 |
+
|
1015 |
+
|
1016 |
+
/*
|
1017 |
+
* DXSM -- double xorshift multiply
|
1018 |
+
*
|
1019 |
+
* This is a new, more powerful output permutation (added in 2019). It's
|
1020 |
+
* a more comprehensive scrambling than RXS M, but runs faster on 128-bit
|
1021 |
+
* types. Although primarily intended for use at large sizes, also works
|
1022 |
+
* at smaller sizes as well.
|
1023 |
+
*
|
1024 |
+
* This permutation is similar to xorshift multiply hash functions, except
|
1025 |
+
* that one of the multipliers is the LCG multiplier (to avoid needing to
|
1026 |
+
* have a second constant) and the other is based on the low-order bits.
|
1027 |
+
* This latter aspect means that the scrambling applied to the high bits
|
1028 |
+
* depends on the low bits, and makes it (to my eye) impractical to back
|
1029 |
+
* out the permutation without having the low-order bits.
|
1030 |
+
*/
|
1031 |
+
|
1032 |
+
template <typename xtype, typename itype>
|
1033 |
+
struct dxsm_mixin {
|
1034 |
+
inline xtype output(itype internal)
|
1035 |
+
{
|
1036 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
1037 |
+
constexpr bitcount_t itypebits = bitcount_t(sizeof(itype) * 8);
|
1038 |
+
static_assert(xtypebits <= itypebits/2,
|
1039 |
+
"Output type must be half the size of the state type.");
|
1040 |
+
|
1041 |
+
xtype hi = xtype(internal >> (itypebits - xtypebits));
|
1042 |
+
xtype lo = xtype(internal);
|
1043 |
+
|
1044 |
+
lo |= 1;
|
1045 |
+
hi ^= hi >> (xtypebits/2);
|
1046 |
+
hi *= xtype(cheap_multiplier<itype>::multiplier());
|
1047 |
+
hi ^= hi >> (3*(xtypebits/4));
|
1048 |
+
hi *= lo;
|
1049 |
+
return hi;
|
1050 |
+
}
|
1051 |
+
};
|
1052 |
+
|
1053 |
+
|
1054 |
+
/*
|
1055 |
+
* XSL RR -- fixed xorshift (to low bits), random rotate
|
1056 |
+
*
|
1057 |
+
* Useful for 128-bit types that are split across two CPU registers.
|
1058 |
+
*/
|
1059 |
+
|
1060 |
+
template <typename xtype, typename itype>
|
1061 |
+
struct xsl_rr_mixin {
|
1062 |
+
static xtype output(itype internal)
|
1063 |
+
{
|
1064 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
1065 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
1066 |
+
constexpr bitcount_t sparebits = bits - xtypebits;
|
1067 |
+
constexpr bitcount_t wantedopbits = xtypebits >= 128 ? 7
|
1068 |
+
: xtypebits >= 64 ? 6
|
1069 |
+
: xtypebits >= 32 ? 5
|
1070 |
+
: xtypebits >= 16 ? 4
|
1071 |
+
: 3;
|
1072 |
+
constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
|
1073 |
+
: sparebits;
|
1074 |
+
constexpr bitcount_t amplifier = wantedopbits - opbits;
|
1075 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
1076 |
+
constexpr bitcount_t topspare = sparebits;
|
1077 |
+
constexpr bitcount_t bottomspare = sparebits - topspare;
|
1078 |
+
constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
|
1079 |
+
|
1080 |
+
bitcount_t rot =
|
1081 |
+
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
|
1082 |
+
bitcount_t amprot = (rot << amplifier) & mask;
|
1083 |
+
internal ^= internal >> xshift;
|
1084 |
+
xtype result = xtype(internal >> bottomspare);
|
1085 |
+
result = rotr(result, amprot);
|
1086 |
+
return result;
|
1087 |
+
}
|
1088 |
+
};
|
1089 |
+
|
1090 |
+
|
1091 |
+
/*
|
1092 |
+
* XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts)
|
1093 |
+
*
|
1094 |
+
* Useful for 128-bit types that are split across two CPU registers.
|
1095 |
+
* If you really want an invertable 128-bit RNG, I guess this is the one.
|
1096 |
+
*/
|
1097 |
+
|
1098 |
+
template <typename T> struct halfsize_trait {};
|
1099 |
+
template <> struct halfsize_trait<pcg128_t> { typedef uint64_t type; };
|
1100 |
+
template <> struct halfsize_trait<uint64_t> { typedef uint32_t type; };
|
1101 |
+
template <> struct halfsize_trait<uint32_t> { typedef uint16_t type; };
|
1102 |
+
template <> struct halfsize_trait<uint16_t> { typedef uint8_t type; };
|
1103 |
+
|
1104 |
+
template <typename xtype, typename itype>
|
1105 |
+
struct xsl_rr_rr_mixin {
|
1106 |
+
typedef typename halfsize_trait<itype>::type htype;
|
1107 |
+
|
1108 |
+
static itype output(itype internal)
|
1109 |
+
{
|
1110 |
+
constexpr bitcount_t htypebits = bitcount_t(sizeof(htype) * 8);
|
1111 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
1112 |
+
constexpr bitcount_t sparebits = bits - htypebits;
|
1113 |
+
constexpr bitcount_t wantedopbits = htypebits >= 128 ? 7
|
1114 |
+
: htypebits >= 64 ? 6
|
1115 |
+
: htypebits >= 32 ? 5
|
1116 |
+
: htypebits >= 16 ? 4
|
1117 |
+
: 3;
|
1118 |
+
constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
|
1119 |
+
: sparebits;
|
1120 |
+
constexpr bitcount_t amplifier = wantedopbits - opbits;
|
1121 |
+
constexpr bitcount_t mask = (1 << opbits) - 1;
|
1122 |
+
constexpr bitcount_t topspare = sparebits;
|
1123 |
+
constexpr bitcount_t xshift = (topspare + htypebits) / 2;
|
1124 |
+
|
1125 |
+
bitcount_t rot =
|
1126 |
+
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
|
1127 |
+
bitcount_t amprot = (rot << amplifier) & mask;
|
1128 |
+
internal ^= internal >> xshift;
|
1129 |
+
htype lowbits = htype(internal);
|
1130 |
+
lowbits = rotr(lowbits, amprot);
|
1131 |
+
htype highbits = htype(internal >> topspare);
|
1132 |
+
bitcount_t rot2 = lowbits & mask;
|
1133 |
+
bitcount_t amprot2 = (rot2 << amplifier) & mask;
|
1134 |
+
highbits = rotr(highbits, amprot2);
|
1135 |
+
return (itype(highbits) << topspare) ^ itype(lowbits);
|
1136 |
+
}
|
1137 |
+
};
|
1138 |
+
|
1139 |
+
|
1140 |
+
/*
|
1141 |
+
* XSH -- fixed xorshift (to high bits)
|
1142 |
+
*
|
1143 |
+
* You shouldn't use this at 64-bits or less.
|
1144 |
+
*/
|
1145 |
+
|
1146 |
+
template <typename xtype, typename itype>
|
1147 |
+
struct xsh_mixin {
|
1148 |
+
static xtype output(itype internal)
|
1149 |
+
{
|
1150 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
1151 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
1152 |
+
constexpr bitcount_t sparebits = bits - xtypebits;
|
1153 |
+
constexpr bitcount_t topspare = 0;
|
1154 |
+
constexpr bitcount_t bottomspare = sparebits - topspare;
|
1155 |
+
constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
|
1156 |
+
|
1157 |
+
internal ^= internal >> xshift;
|
1158 |
+
xtype result = internal >> bottomspare;
|
1159 |
+
return result;
|
1160 |
+
}
|
1161 |
+
};
|
1162 |
+
|
1163 |
+
/*
|
1164 |
+
* XSL -- fixed xorshift (to low bits)
|
1165 |
+
*
|
1166 |
+
* You shouldn't use this at 64-bits or less.
|
1167 |
+
*/
|
1168 |
+
|
1169 |
+
template <typename xtype, typename itype>
|
1170 |
+
struct xsl_mixin {
|
1171 |
+
inline xtype output(itype internal)
|
1172 |
+
{
|
1173 |
+
constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
|
1174 |
+
constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
|
1175 |
+
constexpr bitcount_t sparebits = bits - xtypebits;
|
1176 |
+
constexpr bitcount_t topspare = sparebits;
|
1177 |
+
constexpr bitcount_t bottomspare = sparebits - topspare;
|
1178 |
+
constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
|
1179 |
+
|
1180 |
+
internal ^= internal >> xshift;
|
1181 |
+
xtype result = internal >> bottomspare;
|
1182 |
+
return result;
|
1183 |
+
}
|
1184 |
+
};
|
1185 |
+
|
1186 |
+
|
1187 |
+
/* ---- End of Output Functions ---- */
|
1188 |
+
|
1189 |
+
|
1190 |
+
template <typename baseclass>
|
1191 |
+
struct inside_out : private baseclass {
|
1192 |
+
inside_out() = delete;
|
1193 |
+
|
1194 |
+
typedef typename baseclass::result_type result_type;
|
1195 |
+
typedef typename baseclass::state_type state_type;
|
1196 |
+
static_assert(sizeof(result_type) == sizeof(state_type),
|
1197 |
+
"Require a RNG whose output function is a permutation");
|
1198 |
+
|
1199 |
+
static bool external_step(result_type& randval, size_t i)
|
1200 |
+
{
|
1201 |
+
state_type state = baseclass::unoutput(randval);
|
1202 |
+
state = state * baseclass::multiplier() + baseclass::increment()
|
1203 |
+
+ state_type(i*2);
|
1204 |
+
result_type result = baseclass::output(state);
|
1205 |
+
randval = result;
|
1206 |
+
state_type zero =
|
1207 |
+
baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
|
1208 |
+
return result == zero;
|
1209 |
+
}
|
1210 |
+
|
1211 |
+
static bool external_advance(result_type& randval, size_t i,
|
1212 |
+
result_type delta, bool forwards = true)
|
1213 |
+
{
|
1214 |
+
state_type state = baseclass::unoutput(randval);
|
1215 |
+
state_type mult = baseclass::multiplier();
|
1216 |
+
state_type inc = baseclass::increment() + state_type(i*2);
|
1217 |
+
state_type zero =
|
1218 |
+
baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
|
1219 |
+
state_type dist_to_zero = baseclass::distance(state, zero, mult, inc);
|
1220 |
+
bool crosses_zero =
|
1221 |
+
forwards ? dist_to_zero <= delta
|
1222 |
+
: (-dist_to_zero) <= delta;
|
1223 |
+
if (!forwards)
|
1224 |
+
delta = -delta;
|
1225 |
+
state = baseclass::advance(state, delta, mult, inc);
|
1226 |
+
randval = baseclass::output(state);
|
1227 |
+
return crosses_zero;
|
1228 |
+
}
|
1229 |
+
};
|
1230 |
+
|
1231 |
+
|
1232 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, typename baseclass, typename extvalclass, bool kdd = true>
|
1233 |
+
class extended : public baseclass {
|
1234 |
+
public:
|
1235 |
+
typedef typename baseclass::state_type state_type;
|
1236 |
+
typedef typename baseclass::result_type result_type;
|
1237 |
+
typedef inside_out<extvalclass> insideout;
|
1238 |
+
|
1239 |
+
private:
|
1240 |
+
static constexpr bitcount_t rtypebits = sizeof(result_type)*8;
|
1241 |
+
static constexpr bitcount_t stypebits = sizeof(state_type)*8;
|
1242 |
+
|
1243 |
+
static constexpr bitcount_t tick_limit_pow2 = 64U;
|
1244 |
+
|
1245 |
+
static constexpr size_t table_size = 1UL << table_pow2;
|
1246 |
+
static constexpr size_t table_shift = stypebits - table_pow2;
|
1247 |
+
static constexpr state_type table_mask =
|
1248 |
+
(state_type(1U) << table_pow2) - state_type(1U);
|
1249 |
+
|
1250 |
+
static constexpr bool may_tick =
|
1251 |
+
(advance_pow2 < stypebits) && (advance_pow2 < tick_limit_pow2);
|
1252 |
+
static constexpr size_t tick_shift = stypebits - advance_pow2;
|
1253 |
+
static constexpr state_type tick_mask =
|
1254 |
+
may_tick ? state_type(
|
1255 |
+
(uint64_t(1) << (advance_pow2*may_tick)) - 1)
|
1256 |
+
// ^-- stupidity to appease GCC warnings
|
1257 |
+
: ~state_type(0U);
|
1258 |
+
|
1259 |
+
static constexpr bool may_tock = stypebits < tick_limit_pow2;
|
1260 |
+
|
1261 |
+
result_type data_[table_size];
|
1262 |
+
|
1263 |
+
PCG_NOINLINE void advance_table();
|
1264 |
+
|
1265 |
+
PCG_NOINLINE void advance_table(state_type delta, bool isForwards = true);
|
1266 |
+
|
1267 |
+
result_type& get_extended_value()
|
1268 |
+
{
|
1269 |
+
state_type state = this->state_;
|
1270 |
+
if (kdd && baseclass::is_mcg) {
|
1271 |
+
// The low order bits of an MCG are constant, so drop them.
|
1272 |
+
state >>= 2;
|
1273 |
+
}
|
1274 |
+
size_t index = kdd ? state & table_mask
|
1275 |
+
: state >> table_shift;
|
1276 |
+
|
1277 |
+
if (may_tick) {
|
1278 |
+
bool tick = kdd ? (state & tick_mask) == state_type(0u)
|
1279 |
+
: (state >> tick_shift) == state_type(0u);
|
1280 |
+
if (tick)
|
1281 |
+
advance_table();
|
1282 |
+
}
|
1283 |
+
if (may_tock) {
|
1284 |
+
bool tock = state == state_type(0u);
|
1285 |
+
if (tock)
|
1286 |
+
advance_table();
|
1287 |
+
}
|
1288 |
+
return data_[index];
|
1289 |
+
}
|
1290 |
+
|
1291 |
+
public:
|
1292 |
+
static constexpr size_t period_pow2()
|
1293 |
+
{
|
1294 |
+
return baseclass::period_pow2() + table_size*extvalclass::period_pow2();
|
1295 |
+
}
|
1296 |
+
|
1297 |
+
PCG_ALWAYS_INLINE result_type operator()()
|
1298 |
+
{
|
1299 |
+
result_type rhs = get_extended_value();
|
1300 |
+
result_type lhs = this->baseclass::operator()();
|
1301 |
+
return lhs ^ rhs;
|
1302 |
+
}
|
1303 |
+
|
1304 |
+
result_type operator()(result_type upper_bound)
|
1305 |
+
{
|
1306 |
+
return bounded_rand(*this, upper_bound);
|
1307 |
+
}
|
1308 |
+
|
1309 |
+
void set(result_type wanted)
|
1310 |
+
{
|
1311 |
+
result_type& rhs = get_extended_value();
|
1312 |
+
result_type lhs = this->baseclass::operator()();
|
1313 |
+
rhs = lhs ^ wanted;
|
1314 |
+
}
|
1315 |
+
|
1316 |
+
void advance(state_type distance, bool forwards = true);
|
1317 |
+
|
1318 |
+
void backstep(state_type distance)
|
1319 |
+
{
|
1320 |
+
advance(distance, false);
|
1321 |
+
}
|
1322 |
+
|
1323 |
+
extended(const result_type* data)
|
1324 |
+
: baseclass()
|
1325 |
+
{
|
1326 |
+
datainit(data);
|
1327 |
+
}
|
1328 |
+
|
1329 |
+
extended(const result_type* data, state_type seed)
|
1330 |
+
: baseclass(seed)
|
1331 |
+
{
|
1332 |
+
datainit(data);
|
1333 |
+
}
|
1334 |
+
|
1335 |
+
// This function may or may not exist. It thus has to be a template
|
1336 |
+
// to use SFINAE; users don't have to worry about its template-ness.
|
1337 |
+
|
1338 |
+
template <typename bc = baseclass>
|
1339 |
+
extended(const result_type* data, state_type seed,
|
1340 |
+
typename bc::stream_state stream_seed)
|
1341 |
+
: baseclass(seed, stream_seed)
|
1342 |
+
{
|
1343 |
+
datainit(data);
|
1344 |
+
}
|
1345 |
+
|
1346 |
+
extended()
|
1347 |
+
: baseclass()
|
1348 |
+
{
|
1349 |
+
selfinit();
|
1350 |
+
}
|
1351 |
+
|
1352 |
+
extended(state_type seed)
|
1353 |
+
: baseclass(seed)
|
1354 |
+
{
|
1355 |
+
selfinit();
|
1356 |
+
}
|
1357 |
+
|
1358 |
+
// This function may or may not exist. It thus has to be a template
|
1359 |
+
// to use SFINAE; users don't have to worry about its template-ness.
|
1360 |
+
|
1361 |
+
template <typename bc = baseclass>
|
1362 |
+
extended(state_type seed, typename bc::stream_state stream_seed)
|
1363 |
+
: baseclass(seed, stream_seed)
|
1364 |
+
{
|
1365 |
+
selfinit();
|
1366 |
+
}
|
1367 |
+
|
1368 |
+
private:
|
1369 |
+
void selfinit();
|
1370 |
+
void datainit(const result_type* data);
|
1371 |
+
|
1372 |
+
public:
|
1373 |
+
|
1374 |
+
template<typename SeedSeq, typename = typename std::enable_if<
|
1375 |
+
!std::is_convertible<SeedSeq, result_type>::value
|
1376 |
+
&& !std::is_convertible<SeedSeq, extended>::value>::type>
|
1377 |
+
extended(SeedSeq&& seedSeq)
|
1378 |
+
: baseclass(seedSeq)
|
1379 |
+
{
|
1380 |
+
generate_to<table_size>(seedSeq, data_);
|
1381 |
+
}
|
1382 |
+
|
1383 |
+
template<typename... Args>
|
1384 |
+
void seed(Args&&... args)
|
1385 |
+
{
|
1386 |
+
new (this) extended(std::forward<Args>(args)...);
|
1387 |
+
}
|
1388 |
+
|
1389 |
+
template <bitcount_t table_pow2_, bitcount_t advance_pow2_,
|
1390 |
+
typename baseclass_, typename extvalclass_, bool kdd_>
|
1391 |
+
friend bool operator==(const extended<table_pow2_, advance_pow2_,
|
1392 |
+
baseclass_, extvalclass_, kdd_>&,
|
1393 |
+
const extended<table_pow2_, advance_pow2_,
|
1394 |
+
baseclass_, extvalclass_, kdd_>&);
|
1395 |
+
|
1396 |
+
template <typename CharT, typename Traits,
|
1397 |
+
bitcount_t table_pow2_, bitcount_t advance_pow2_,
|
1398 |
+
typename baseclass_, typename extvalclass_, bool kdd_>
|
1399 |
+
friend std::basic_ostream<CharT,Traits>&
|
1400 |
+
operator<<(std::basic_ostream<CharT,Traits>& out,
|
1401 |
+
const extended<table_pow2_, advance_pow2_,
|
1402 |
+
baseclass_, extvalclass_, kdd_>&);
|
1403 |
+
|
1404 |
+
template <typename CharT, typename Traits,
|
1405 |
+
bitcount_t table_pow2_, bitcount_t advance_pow2_,
|
1406 |
+
typename baseclass_, typename extvalclass_, bool kdd_>
|
1407 |
+
friend std::basic_istream<CharT,Traits>&
|
1408 |
+
operator>>(std::basic_istream<CharT,Traits>& in,
|
1409 |
+
extended<table_pow2_, advance_pow2_,
|
1410 |
+
baseclass_, extvalclass_, kdd_>&);
|
1411 |
+
|
1412 |
+
};
|
1413 |
+
|
1414 |
+
|
1415 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1416 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1417 |
+
void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::datainit(
|
1418 |
+
const result_type* data)
|
1419 |
+
{
|
1420 |
+
for (size_t i = 0; i < table_size; ++i)
|
1421 |
+
data_[i] = data[i];
|
1422 |
+
}
|
1423 |
+
|
1424 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1425 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1426 |
+
void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit()
|
1427 |
+
{
|
1428 |
+
// We need to fill the extended table with something, and we have
|
1429 |
+
// very little provided data, so we use the base generator to
|
1430 |
+
// produce values. Although not ideal (use a seed sequence, folks!),
|
1431 |
+
// unexpected correlations are mitigated by
|
1432 |
+
// - using XOR differences rather than the number directly
|
1433 |
+
// - the way the table is accessed, its values *won't* be accessed
|
1434 |
+
// in the same order the were written.
|
1435 |
+
// - any strange correlations would only be apparent if we
|
1436 |
+
// were to backstep the generator so that the base generator
|
1437 |
+
// was generating the same values again
|
1438 |
+
result_type lhs = baseclass::operator()();
|
1439 |
+
result_type rhs = baseclass::operator()();
|
1440 |
+
result_type xdiff = lhs - rhs;
|
1441 |
+
for (size_t i = 0; i < table_size; ++i) {
|
1442 |
+
data_[i] = baseclass::operator()() ^ xdiff;
|
1443 |
+
}
|
1444 |
+
}
|
1445 |
+
|
1446 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1447 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1448 |
+
bool operator==(const extended<table_pow2, advance_pow2,
|
1449 |
+
baseclass, extvalclass, kdd>& lhs,
|
1450 |
+
const extended<table_pow2, advance_pow2,
|
1451 |
+
baseclass, extvalclass, kdd>& rhs)
|
1452 |
+
{
|
1453 |
+
auto& base_lhs = static_cast<const baseclass&>(lhs);
|
1454 |
+
auto& base_rhs = static_cast<const baseclass&>(rhs);
|
1455 |
+
return base_lhs == base_rhs
|
1456 |
+
&& std::equal(
|
1457 |
+
std::begin(lhs.data_), std::end(lhs.data_),
|
1458 |
+
std::begin(rhs.data_)
|
1459 |
+
);
|
1460 |
+
}
|
1461 |
+
|
1462 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1463 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1464 |
+
inline bool operator!=(const extended<table_pow2, advance_pow2,
|
1465 |
+
baseclass, extvalclass, kdd>& lhs,
|
1466 |
+
const extended<table_pow2, advance_pow2,
|
1467 |
+
baseclass, extvalclass, kdd>& rhs)
|
1468 |
+
{
|
1469 |
+
return !operator==(lhs, rhs);
|
1470 |
+
}
|
1471 |
+
|
1472 |
+
template <typename CharT, typename Traits,
|
1473 |
+
bitcount_t table_pow2, bitcount_t advance_pow2,
|
1474 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1475 |
+
std::basic_ostream<CharT,Traits>&
|
1476 |
+
operator<<(std::basic_ostream<CharT,Traits>& out,
|
1477 |
+
const extended<table_pow2, advance_pow2,
|
1478 |
+
baseclass, extvalclass, kdd>& rng)
|
1479 |
+
{
|
1480 |
+
using pcg_extras::operator<<;
|
1481 |
+
|
1482 |
+
auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
|
1483 |
+
auto space = out.widen(' ');
|
1484 |
+
auto orig_fill = out.fill();
|
1485 |
+
|
1486 |
+
out << rng.multiplier() << space
|
1487 |
+
<< rng.increment() << space
|
1488 |
+
<< rng.state_;
|
1489 |
+
|
1490 |
+
for (const auto& datum : rng.data_)
|
1491 |
+
out << space << datum;
|
1492 |
+
|
1493 |
+
out.flags(orig_flags);
|
1494 |
+
out.fill(orig_fill);
|
1495 |
+
return out;
|
1496 |
+
}
|
1497 |
+
|
1498 |
+
template <typename CharT, typename Traits,
|
1499 |
+
bitcount_t table_pow2, bitcount_t advance_pow2,
|
1500 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1501 |
+
std::basic_istream<CharT,Traits>&
|
1502 |
+
operator>>(std::basic_istream<CharT,Traits>& in,
|
1503 |
+
extended<table_pow2, advance_pow2,
|
1504 |
+
baseclass, extvalclass, kdd>& rng)
|
1505 |
+
{
|
1506 |
+
extended<table_pow2, advance_pow2, baseclass, extvalclass> new_rng;
|
1507 |
+
auto& base_rng = static_cast<baseclass&>(new_rng);
|
1508 |
+
in >> base_rng;
|
1509 |
+
|
1510 |
+
if (in.fail())
|
1511 |
+
return in;
|
1512 |
+
|
1513 |
+
using pcg_extras::operator>>;
|
1514 |
+
|
1515 |
+
auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
|
1516 |
+
|
1517 |
+
for (auto& datum : new_rng.data_) {
|
1518 |
+
in >> datum;
|
1519 |
+
if (in.fail())
|
1520 |
+
goto bail;
|
1521 |
+
}
|
1522 |
+
|
1523 |
+
rng = new_rng;
|
1524 |
+
|
1525 |
+
bail:
|
1526 |
+
in.flags(orig_flags);
|
1527 |
+
return in;
|
1528 |
+
}
|
1529 |
+
|
1530 |
+
|
1531 |
+
|
1532 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1533 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1534 |
+
void
|
1535 |
+
extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table()
|
1536 |
+
{
|
1537 |
+
bool carry = false;
|
1538 |
+
for (size_t i = 0; i < table_size; ++i) {
|
1539 |
+
if (carry) {
|
1540 |
+
carry = insideout::external_step(data_[i],i+1);
|
1541 |
+
}
|
1542 |
+
bool carry2 = insideout::external_step(data_[i],i+1);
|
1543 |
+
carry = carry || carry2;
|
1544 |
+
}
|
1545 |
+
}
|
1546 |
+
|
1547 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1548 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1549 |
+
void
|
1550 |
+
extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table(
|
1551 |
+
state_type delta, bool isForwards)
|
1552 |
+
{
|
1553 |
+
typedef typename baseclass::state_type base_state_t;
|
1554 |
+
typedef typename extvalclass::state_type ext_state_t;
|
1555 |
+
constexpr bitcount_t basebits = sizeof(base_state_t)*8;
|
1556 |
+
constexpr bitcount_t extbits = sizeof(ext_state_t)*8;
|
1557 |
+
static_assert(basebits <= extbits || advance_pow2 > 0,
|
1558 |
+
"Current implementation might overflow its carry");
|
1559 |
+
|
1560 |
+
base_state_t carry = 0;
|
1561 |
+
for (size_t i = 0; i < table_size; ++i) {
|
1562 |
+
base_state_t total_delta = carry + delta;
|
1563 |
+
ext_state_t trunc_delta = ext_state_t(total_delta);
|
1564 |
+
if (basebits > extbits) {
|
1565 |
+
carry = total_delta >> extbits;
|
1566 |
+
} else {
|
1567 |
+
carry = 0;
|
1568 |
+
}
|
1569 |
+
carry +=
|
1570 |
+
insideout::external_advance(data_[i],i+1, trunc_delta, isForwards);
|
1571 |
+
}
|
1572 |
+
}
|
1573 |
+
|
1574 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1575 |
+
typename baseclass, typename extvalclass, bool kdd>
|
1576 |
+
void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance(
|
1577 |
+
state_type distance, bool forwards)
|
1578 |
+
{
|
1579 |
+
static_assert(kdd,
|
1580 |
+
"Efficient advance is too hard for non-kdd extension. "
|
1581 |
+
"For a weak advance, cast to base class");
|
1582 |
+
state_type zero =
|
1583 |
+
baseclass::is_mcg ? this->state_ & state_type(3U) : state_type(0U);
|
1584 |
+
if (may_tick) {
|
1585 |
+
state_type ticks = distance >> (advance_pow2*may_tick);
|
1586 |
+
// ^-- stupidity to appease GCC
|
1587 |
+
// warnings
|
1588 |
+
state_type adv_mask =
|
1589 |
+
baseclass::is_mcg ? tick_mask << 2 : tick_mask;
|
1590 |
+
state_type next_advance_distance = this->distance(zero, adv_mask);
|
1591 |
+
if (!forwards)
|
1592 |
+
next_advance_distance = (-next_advance_distance) & tick_mask;
|
1593 |
+
if (next_advance_distance < (distance & tick_mask)) {
|
1594 |
+
++ticks;
|
1595 |
+
}
|
1596 |
+
if (ticks)
|
1597 |
+
advance_table(ticks, forwards);
|
1598 |
+
}
|
1599 |
+
if (forwards) {
|
1600 |
+
if (may_tock && this->distance(zero) <= distance)
|
1601 |
+
advance_table();
|
1602 |
+
baseclass::advance(distance);
|
1603 |
+
} else {
|
1604 |
+
if (may_tock && -(this->distance(zero)) <= distance)
|
1605 |
+
advance_table(state_type(1U), false);
|
1606 |
+
baseclass::advance(-distance);
|
1607 |
+
}
|
1608 |
+
}
|
1609 |
+
|
1610 |
+
} // namespace pcg_detail
|
1611 |
+
|
1612 |
+
namespace pcg_engines {
|
1613 |
+
|
1614 |
+
using namespace pcg_detail;
|
1615 |
+
|
1616 |
+
/* Predefined types for XSH RS */
|
1617 |
+
|
1618 |
+
typedef oneseq_base<uint8_t, uint16_t, xsh_rs_mixin> oneseq_xsh_rs_16_8;
|
1619 |
+
typedef oneseq_base<uint16_t, uint32_t, xsh_rs_mixin> oneseq_xsh_rs_32_16;
|
1620 |
+
typedef oneseq_base<uint32_t, uint64_t, xsh_rs_mixin> oneseq_xsh_rs_64_32;
|
1621 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin> oneseq_xsh_rs_128_64;
|
1622 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
|
1623 |
+
cm_oneseq_xsh_rs_128_64;
|
1624 |
+
|
1625 |
+
typedef unique_base<uint8_t, uint16_t, xsh_rs_mixin> unique_xsh_rs_16_8;
|
1626 |
+
typedef unique_base<uint16_t, uint32_t, xsh_rs_mixin> unique_xsh_rs_32_16;
|
1627 |
+
typedef unique_base<uint32_t, uint64_t, xsh_rs_mixin> unique_xsh_rs_64_32;
|
1628 |
+
typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin> unique_xsh_rs_128_64;
|
1629 |
+
typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
|
1630 |
+
cm_unique_xsh_rs_128_64;
|
1631 |
+
|
1632 |
+
typedef setseq_base<uint8_t, uint16_t, xsh_rs_mixin> setseq_xsh_rs_16_8;
|
1633 |
+
typedef setseq_base<uint16_t, uint32_t, xsh_rs_mixin> setseq_xsh_rs_32_16;
|
1634 |
+
typedef setseq_base<uint32_t, uint64_t, xsh_rs_mixin> setseq_xsh_rs_64_32;
|
1635 |
+
typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin> setseq_xsh_rs_128_64;
|
1636 |
+
typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
|
1637 |
+
cm_setseq_xsh_rs_128_64;
|
1638 |
+
|
1639 |
+
typedef mcg_base<uint8_t, uint16_t, xsh_rs_mixin> mcg_xsh_rs_16_8;
|
1640 |
+
typedef mcg_base<uint16_t, uint32_t, xsh_rs_mixin> mcg_xsh_rs_32_16;
|
1641 |
+
typedef mcg_base<uint32_t, uint64_t, xsh_rs_mixin> mcg_xsh_rs_64_32;
|
1642 |
+
typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin> mcg_xsh_rs_128_64;
|
1643 |
+
typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
|
1644 |
+
cm_mcg_xsh_rs_128_64;
|
1645 |
+
|
1646 |
+
/* Predefined types for XSH RR */
|
1647 |
+
|
1648 |
+
typedef oneseq_base<uint8_t, uint16_t, xsh_rr_mixin> oneseq_xsh_rr_16_8;
|
1649 |
+
typedef oneseq_base<uint16_t, uint32_t, xsh_rr_mixin> oneseq_xsh_rr_32_16;
|
1650 |
+
typedef oneseq_base<uint32_t, uint64_t, xsh_rr_mixin> oneseq_xsh_rr_64_32;
|
1651 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin> oneseq_xsh_rr_128_64;
|
1652 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
|
1653 |
+
cm_oneseq_xsh_rr_128_64;
|
1654 |
+
|
1655 |
+
typedef unique_base<uint8_t, uint16_t, xsh_rr_mixin> unique_xsh_rr_16_8;
|
1656 |
+
typedef unique_base<uint16_t, uint32_t, xsh_rr_mixin> unique_xsh_rr_32_16;
|
1657 |
+
typedef unique_base<uint32_t, uint64_t, xsh_rr_mixin> unique_xsh_rr_64_32;
|
1658 |
+
typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin> unique_xsh_rr_128_64;
|
1659 |
+
typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
|
1660 |
+
cm_unique_xsh_rr_128_64;
|
1661 |
+
|
1662 |
+
typedef setseq_base<uint8_t, uint16_t, xsh_rr_mixin> setseq_xsh_rr_16_8;
|
1663 |
+
typedef setseq_base<uint16_t, uint32_t, xsh_rr_mixin> setseq_xsh_rr_32_16;
|
1664 |
+
typedef setseq_base<uint32_t, uint64_t, xsh_rr_mixin> setseq_xsh_rr_64_32;
|
1665 |
+
typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin> setseq_xsh_rr_128_64;
|
1666 |
+
typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
|
1667 |
+
cm_setseq_xsh_rr_128_64;
|
1668 |
+
|
1669 |
+
typedef mcg_base<uint8_t, uint16_t, xsh_rr_mixin> mcg_xsh_rr_16_8;
|
1670 |
+
typedef mcg_base<uint16_t, uint32_t, xsh_rr_mixin> mcg_xsh_rr_32_16;
|
1671 |
+
typedef mcg_base<uint32_t, uint64_t, xsh_rr_mixin> mcg_xsh_rr_64_32;
|
1672 |
+
typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin> mcg_xsh_rr_128_64;
|
1673 |
+
typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
|
1674 |
+
cm_mcg_xsh_rr_128_64;
|
1675 |
+
|
1676 |
+
|
1677 |
+
/* Predefined types for RXS M XS */
|
1678 |
+
|
1679 |
+
typedef oneseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_8_8;
|
1680 |
+
typedef oneseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_16_16;
|
1681 |
+
typedef oneseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_32_32;
|
1682 |
+
typedef oneseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_64_64;
|
1683 |
+
typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin>
|
1684 |
+
oneseq_rxs_m_xs_128_128;
|
1685 |
+
typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
|
1686 |
+
cm_oneseq_rxs_m_xs_128_128;
|
1687 |
+
|
1688 |
+
typedef unique_base<uint8_t, uint8_t, rxs_m_xs_mixin> unique_rxs_m_xs_8_8;
|
1689 |
+
typedef unique_base<uint16_t, uint16_t, rxs_m_xs_mixin> unique_rxs_m_xs_16_16;
|
1690 |
+
typedef unique_base<uint32_t, uint32_t, rxs_m_xs_mixin> unique_rxs_m_xs_32_32;
|
1691 |
+
typedef unique_base<uint64_t, uint64_t, rxs_m_xs_mixin> unique_rxs_m_xs_64_64;
|
1692 |
+
typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> unique_rxs_m_xs_128_128;
|
1693 |
+
typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
|
1694 |
+
cm_unique_rxs_m_xs_128_128;
|
1695 |
+
|
1696 |
+
typedef setseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> setseq_rxs_m_xs_8_8;
|
1697 |
+
typedef setseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> setseq_rxs_m_xs_16_16;
|
1698 |
+
typedef setseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> setseq_rxs_m_xs_32_32;
|
1699 |
+
typedef setseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> setseq_rxs_m_xs_64_64;
|
1700 |
+
typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> setseq_rxs_m_xs_128_128;
|
1701 |
+
typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
|
1702 |
+
cm_setseq_rxs_m_xs_128_128;
|
1703 |
+
|
1704 |
+
// MCG versions don't make sense here, so aren't defined.
|
1705 |
+
|
1706 |
+
/* Predefined types for RXS M */
|
1707 |
+
|
1708 |
+
typedef oneseq_base<uint8_t, uint16_t, rxs_m_mixin> oneseq_rxs_m_16_8;
|
1709 |
+
typedef oneseq_base<uint16_t, uint32_t, rxs_m_mixin> oneseq_rxs_m_32_16;
|
1710 |
+
typedef oneseq_base<uint32_t, uint64_t, rxs_m_mixin> oneseq_rxs_m_64_32;
|
1711 |
+
typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin> oneseq_rxs_m_128_64;
|
1712 |
+
typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
|
1713 |
+
cm_oneseq_rxs_m_128_64;
|
1714 |
+
|
1715 |
+
typedef unique_base<uint8_t, uint16_t, rxs_m_mixin> unique_rxs_m_16_8;
|
1716 |
+
typedef unique_base<uint16_t, uint32_t, rxs_m_mixin> unique_rxs_m_32_16;
|
1717 |
+
typedef unique_base<uint32_t, uint64_t, rxs_m_mixin> unique_rxs_m_64_32;
|
1718 |
+
typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin> unique_rxs_m_128_64;
|
1719 |
+
typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
|
1720 |
+
cm_unique_rxs_m_128_64;
|
1721 |
+
|
1722 |
+
typedef setseq_base<uint8_t, uint16_t, rxs_m_mixin> setseq_rxs_m_16_8;
|
1723 |
+
typedef setseq_base<uint16_t, uint32_t, rxs_m_mixin> setseq_rxs_m_32_16;
|
1724 |
+
typedef setseq_base<uint32_t, uint64_t, rxs_m_mixin> setseq_rxs_m_64_32;
|
1725 |
+
typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin> setseq_rxs_m_128_64;
|
1726 |
+
typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
|
1727 |
+
cm_setseq_rxs_m_128_64;
|
1728 |
+
|
1729 |
+
typedef mcg_base<uint8_t, uint16_t, rxs_m_mixin> mcg_rxs_m_16_8;
|
1730 |
+
typedef mcg_base<uint16_t, uint32_t, rxs_m_mixin> mcg_rxs_m_32_16;
|
1731 |
+
typedef mcg_base<uint32_t, uint64_t, rxs_m_mixin> mcg_rxs_m_64_32;
|
1732 |
+
typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin> mcg_rxs_m_128_64;
|
1733 |
+
typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
|
1734 |
+
cm_mcg_rxs_m_128_64;
|
1735 |
+
|
1736 |
+
/* Predefined types for DXSM */
|
1737 |
+
|
1738 |
+
typedef oneseq_base<uint8_t, uint16_t, dxsm_mixin> oneseq_dxsm_16_8;
|
1739 |
+
typedef oneseq_base<uint16_t, uint32_t, dxsm_mixin> oneseq_dxsm_32_16;
|
1740 |
+
typedef oneseq_base<uint32_t, uint64_t, dxsm_mixin> oneseq_dxsm_64_32;
|
1741 |
+
typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin> oneseq_dxsm_128_64;
|
1742 |
+
typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
|
1743 |
+
cm_oneseq_dxsm_128_64;
|
1744 |
+
|
1745 |
+
typedef unique_base<uint8_t, uint16_t, dxsm_mixin> unique_dxsm_16_8;
|
1746 |
+
typedef unique_base<uint16_t, uint32_t, dxsm_mixin> unique_dxsm_32_16;
|
1747 |
+
typedef unique_base<uint32_t, uint64_t, dxsm_mixin> unique_dxsm_64_32;
|
1748 |
+
typedef unique_base<uint64_t, pcg128_t, dxsm_mixin> unique_dxsm_128_64;
|
1749 |
+
typedef unique_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
|
1750 |
+
cm_unique_dxsm_128_64;
|
1751 |
+
|
1752 |
+
typedef setseq_base<uint8_t, uint16_t, dxsm_mixin> setseq_dxsm_16_8;
|
1753 |
+
typedef setseq_base<uint16_t, uint32_t, dxsm_mixin> setseq_dxsm_32_16;
|
1754 |
+
typedef setseq_base<uint32_t, uint64_t, dxsm_mixin> setseq_dxsm_64_32;
|
1755 |
+
typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin> setseq_dxsm_128_64;
|
1756 |
+
typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
|
1757 |
+
cm_setseq_dxsm_128_64;
|
1758 |
+
|
1759 |
+
typedef mcg_base<uint8_t, uint16_t, dxsm_mixin> mcg_dxsm_16_8;
|
1760 |
+
typedef mcg_base<uint16_t, uint32_t, dxsm_mixin> mcg_dxsm_32_16;
|
1761 |
+
typedef mcg_base<uint32_t, uint64_t, dxsm_mixin> mcg_dxsm_64_32;
|
1762 |
+
typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin> mcg_dxsm_128_64;
|
1763 |
+
typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
|
1764 |
+
cm_mcg_dxsm_128_64;
|
1765 |
+
|
1766 |
+
/* Predefined types for XSL RR (only defined for "large" types) */
|
1767 |
+
|
1768 |
+
typedef oneseq_base<uint32_t, uint64_t, xsl_rr_mixin> oneseq_xsl_rr_64_32;
|
1769 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin> oneseq_xsl_rr_128_64;
|
1770 |
+
typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
|
1771 |
+
cm_oneseq_xsl_rr_128_64;
|
1772 |
+
|
1773 |
+
typedef unique_base<uint32_t, uint64_t, xsl_rr_mixin> unique_xsl_rr_64_32;
|
1774 |
+
typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin> unique_xsl_rr_128_64;
|
1775 |
+
typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
|
1776 |
+
cm_unique_xsl_rr_128_64;
|
1777 |
+
|
1778 |
+
typedef setseq_base<uint32_t, uint64_t, xsl_rr_mixin> setseq_xsl_rr_64_32;
|
1779 |
+
typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin> setseq_xsl_rr_128_64;
|
1780 |
+
typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
|
1781 |
+
cm_setseq_xsl_rr_128_64;
|
1782 |
+
|
1783 |
+
typedef mcg_base<uint32_t, uint64_t, xsl_rr_mixin> mcg_xsl_rr_64_32;
|
1784 |
+
typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin> mcg_xsl_rr_128_64;
|
1785 |
+
typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
|
1786 |
+
cm_mcg_xsl_rr_128_64;
|
1787 |
+
|
1788 |
+
|
1789 |
+
/* Predefined types for XSL RR RR (only defined for "large" types) */
|
1790 |
+
|
1791 |
+
typedef oneseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
|
1792 |
+
oneseq_xsl_rr_rr_64_64;
|
1793 |
+
typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
|
1794 |
+
oneseq_xsl_rr_rr_128_128;
|
1795 |
+
typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
|
1796 |
+
cm_oneseq_xsl_rr_rr_128_128;
|
1797 |
+
|
1798 |
+
typedef unique_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
|
1799 |
+
unique_xsl_rr_rr_64_64;
|
1800 |
+
typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
|
1801 |
+
unique_xsl_rr_rr_128_128;
|
1802 |
+
typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
|
1803 |
+
cm_unique_xsl_rr_rr_128_128;
|
1804 |
+
|
1805 |
+
typedef setseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
|
1806 |
+
setseq_xsl_rr_rr_64_64;
|
1807 |
+
typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
|
1808 |
+
setseq_xsl_rr_rr_128_128;
|
1809 |
+
typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
|
1810 |
+
cm_setseq_xsl_rr_rr_128_128;
|
1811 |
+
|
1812 |
+
// MCG versions don't make sense here, so aren't defined.
|
1813 |
+
|
1814 |
+
/* Extended generators */
|
1815 |
+
|
1816 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1817 |
+
typename BaseRNG, bool kdd = true>
|
1818 |
+
using ext_std8 = extended<table_pow2, advance_pow2, BaseRNG,
|
1819 |
+
oneseq_rxs_m_xs_8_8, kdd>;
|
1820 |
+
|
1821 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1822 |
+
typename BaseRNG, bool kdd = true>
|
1823 |
+
using ext_std16 = extended<table_pow2, advance_pow2, BaseRNG,
|
1824 |
+
oneseq_rxs_m_xs_16_16, kdd>;
|
1825 |
+
|
1826 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1827 |
+
typename BaseRNG, bool kdd = true>
|
1828 |
+
using ext_std32 = extended<table_pow2, advance_pow2, BaseRNG,
|
1829 |
+
oneseq_rxs_m_xs_32_32, kdd>;
|
1830 |
+
|
1831 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2,
|
1832 |
+
typename BaseRNG, bool kdd = true>
|
1833 |
+
using ext_std64 = extended<table_pow2, advance_pow2, BaseRNG,
|
1834 |
+
oneseq_rxs_m_xs_64_64, kdd>;
|
1835 |
+
|
1836 |
+
|
1837 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1838 |
+
using ext_oneseq_rxs_m_xs_32_32 =
|
1839 |
+
ext_std32<table_pow2, advance_pow2, oneseq_rxs_m_xs_32_32, kdd>;
|
1840 |
+
|
1841 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1842 |
+
using ext_mcg_xsh_rs_64_32 =
|
1843 |
+
ext_std32<table_pow2, advance_pow2, mcg_xsh_rs_64_32, kdd>;
|
1844 |
+
|
1845 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1846 |
+
using ext_oneseq_xsh_rs_64_32 =
|
1847 |
+
ext_std32<table_pow2, advance_pow2, oneseq_xsh_rs_64_32, kdd>;
|
1848 |
+
|
1849 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1850 |
+
using ext_setseq_xsh_rr_64_32 =
|
1851 |
+
ext_std32<table_pow2, advance_pow2, setseq_xsh_rr_64_32, kdd>;
|
1852 |
+
|
1853 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1854 |
+
using ext_mcg_xsl_rr_128_64 =
|
1855 |
+
ext_std64<table_pow2, advance_pow2, mcg_xsl_rr_128_64, kdd>;
|
1856 |
+
|
1857 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1858 |
+
using ext_oneseq_xsl_rr_128_64 =
|
1859 |
+
ext_std64<table_pow2, advance_pow2, oneseq_xsl_rr_128_64, kdd>;
|
1860 |
+
|
1861 |
+
template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
|
1862 |
+
using ext_setseq_xsl_rr_128_64 =
|
1863 |
+
ext_std64<table_pow2, advance_pow2, setseq_xsl_rr_128_64, kdd>;
|
1864 |
+
|
1865 |
+
} // namespace pcg_engines
|
1866 |
+
|
1867 |
+
typedef pcg_engines::setseq_xsh_rr_64_32 pcg32;
|
1868 |
+
typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq;
|
1869 |
+
typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique;
|
1870 |
+
typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast;
|
1871 |
+
|
1872 |
+
typedef pcg_engines::setseq_xsl_rr_128_64 pcg64;
|
1873 |
+
typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq;
|
1874 |
+
typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique;
|
1875 |
+
typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast;
|
1876 |
+
|
1877 |
+
typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure;
|
1878 |
+
typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure;
|
1879 |
+
typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure;
|
1880 |
+
typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure;
|
1881 |
+
typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure;
|
1882 |
+
|
1883 |
+
typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure;
|
1884 |
+
typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure;
|
1885 |
+
typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure;
|
1886 |
+
typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure;
|
1887 |
+
typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure;
|
1888 |
+
|
1889 |
+
|
1890 |
+
// These two extended RNGs provide two-dimensionally equidistributed
|
1891 |
+
// 32-bit generators. pcg32_k2_fast occupies the same space as pcg64,
|
1892 |
+
// and can be called twice to generate 64 bits, but does not required
|
1893 |
+
// 128-bit math; on 32-bit systems, it's faster than pcg64 as well.
|
1894 |
+
|
1895 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<1,16,true> pcg32_k2;
|
1896 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<1,32,true> pcg32_k2_fast;
|
1897 |
+
|
1898 |
+
// These eight extended RNGs have about as much state as arc4random
|
1899 |
+
//
|
1900 |
+
// - the k variants are k-dimensionally equidistributed
|
1901 |
+
// - the c variants offer better crypographic security
|
1902 |
+
//
|
1903 |
+
// (just how good the cryptographic security is an open question)
|
1904 |
+
|
1905 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k64;
|
1906 |
+
typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,true> pcg32_k64_oneseq;
|
1907 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k64_fast;
|
1908 |
+
|
1909 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,false> pcg32_c64;
|
1910 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,false> pcg32_c64_oneseq;
|
1911 |
+
typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,false> pcg32_c64_fast;
|
1912 |
+
|
1913 |
+
typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,true> pcg64_k32;
|
1914 |
+
typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,true> pcg64_k32_oneseq;
|
1915 |
+
typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,true> pcg64_k32_fast;
|
1916 |
+
|
1917 |
+
typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,false> pcg64_c32;
|
1918 |
+
typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,false> pcg64_c32_oneseq;
|
1919 |
+
typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,false> pcg64_c32_fast;
|
1920 |
+
|
1921 |
+
// These eight extended RNGs have more state than the Mersenne twister
|
1922 |
+
//
|
1923 |
+
// - the k variants are k-dimensionally equidistributed
|
1924 |
+
// - the c variants offer better crypographic security
|
1925 |
+
//
|
1926 |
+
// (just how good the cryptographic security is an open question)
|
1927 |
+
|
1928 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,true> pcg32_k1024;
|
1929 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,true> pcg32_k1024_fast;
|
1930 |
+
|
1931 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,false> pcg32_c1024;
|
1932 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,false> pcg32_c1024_fast;
|
1933 |
+
|
1934 |
+
typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,true> pcg64_k1024;
|
1935 |
+
typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,true> pcg64_k1024_fast;
|
1936 |
+
|
1937 |
+
typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,false> pcg64_c1024;
|
1938 |
+
typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,false> pcg64_c1024_fast;
|
1939 |
+
|
1940 |
+
// These generators have an insanely huge period (2^524352), and is suitable
|
1941 |
+
// for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary
|
1942 |
+
// point in the future. [Actually, over the full period of the generator, it
|
1943 |
+
// will produce every 64 KB ZIP file 2^64 times!]
|
1944 |
+
|
1945 |
+
typedef pcg_engines::ext_setseq_xsh_rr_64_32<14,16,true> pcg32_k16384;
|
1946 |
+
typedef pcg_engines::ext_oneseq_xsh_rs_64_32<14,32,true> pcg32_k16384_fast;
|
1947 |
+
|
1948 |
+
} // namespace arrow_vendored
|
1949 |
+
|
1950 |
+
#ifdef _MSC_VER
|
1951 |
+
#pragma warning(default:4146)
|
1952 |
+
#endif
|
1953 |
+
|
1954 |
+
#endif // PCG_RAND_HPP_INCLUDED
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/pcg/pcg_uint128.hpp
ADDED
@@ -0,0 +1,1008 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* PCG Random Number Generation for C++
|
3 |
+
*
|
4 |
+
* Copyright 2014-2021 Melissa O'Neill <[email protected]>,
|
5 |
+
* and the PCG Project contributors.
|
6 |
+
*
|
7 |
+
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
8 |
+
*
|
9 |
+
* Licensed under the Apache License, Version 2.0 (provided in
|
10 |
+
* LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
|
11 |
+
* or under the MIT license (provided in LICENSE-MIT.txt and at
|
12 |
+
* http://opensource.org/licenses/MIT), at your option. This file may not
|
13 |
+
* be copied, modified, or distributed except according to those terms.
|
14 |
+
*
|
15 |
+
* Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
|
16 |
+
* express or implied. See your chosen license for details.
|
17 |
+
*
|
18 |
+
* For additional information about the PCG random number generation scheme,
|
19 |
+
* visit http://www.pcg-random.org/.
|
20 |
+
*/
|
21 |
+
|
22 |
+
/*
|
23 |
+
* This code provides a a C++ class that can provide 128-bit (or higher)
|
24 |
+
* integers. To produce 2K-bit integers, it uses two K-bit integers,
|
25 |
+
* placed in a union that allowes the code to also see them as four K/2 bit
|
26 |
+
* integers (and access them either directly name, or by index).
|
27 |
+
*
|
28 |
+
* It may seem like we're reinventing the wheel here, because several
|
29 |
+
* libraries already exist that support large integers, but most existing
|
30 |
+
* libraries provide a very generic multiprecision code, but here we're
|
31 |
+
* operating at a fixed size. Also, most other libraries are fairly
|
32 |
+
* heavyweight. So we use a direct implementation. Sadly, it's much slower
|
33 |
+
* than hand-coded assembly or direct CPU support.
|
34 |
+
*/
|
35 |
+
|
36 |
+
#ifndef PCG_UINT128_HPP_INCLUDED
|
37 |
+
#define PCG_UINT128_HPP_INCLUDED 1
|
38 |
+
|
39 |
+
#include <cstdint>
|
40 |
+
#include <cstdio>
|
41 |
+
#include <cassert>
|
42 |
+
#include <climits>
|
43 |
+
#include <utility>
|
44 |
+
#include <initializer_list>
|
45 |
+
#include <type_traits>
|
46 |
+
|
47 |
+
#if defined(_MSC_VER) // Use MSVC++ intrinsics
|
48 |
+
#include <intrin.h>
|
49 |
+
#endif
|
50 |
+
|
51 |
+
/*
|
52 |
+
* We want to lay the type out the same way that a native type would be laid
|
53 |
+
* out, which means we must know the machine's endian, at compile time.
|
54 |
+
* This ugliness attempts to do so.
|
55 |
+
*/
|
56 |
+
|
57 |
+
#ifndef PCG_LITTLE_ENDIAN
|
58 |
+
#if defined(__BYTE_ORDER__)
|
59 |
+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
60 |
+
#define PCG_LITTLE_ENDIAN 1
|
61 |
+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
62 |
+
#define PCG_LITTLE_ENDIAN 0
|
63 |
+
#else
|
64 |
+
#error __BYTE_ORDER__ does not match a standard endian, pick a side
|
65 |
+
#endif
|
66 |
+
#elif __LITTLE_ENDIAN__ || _LITTLE_ENDIAN
|
67 |
+
#define PCG_LITTLE_ENDIAN 1
|
68 |
+
#elif __BIG_ENDIAN__ || _BIG_ENDIAN
|
69 |
+
#define PCG_LITTLE_ENDIAN 0
|
70 |
+
#elif __x86_64 || __x86_64__ || _M_X64 || __i386 || __i386__ || _M_IX86
|
71 |
+
#define PCG_LITTLE_ENDIAN 1
|
72 |
+
#elif __powerpc__ || __POWERPC__ || __ppc__ || __PPC__ \
|
73 |
+
|| __m68k__ || __mc68000__
|
74 |
+
#define PCG_LITTLE_ENDIAN 0
|
75 |
+
#else
|
76 |
+
#error Unable to determine target endianness
|
77 |
+
#endif
|
78 |
+
#endif
|
79 |
+
|
80 |
+
#if INTPTR_MAX == INT64_MAX && !defined(PCG_64BIT_SPECIALIZATIONS)
|
81 |
+
#define PCG_64BIT_SPECIALIZATIONS 1
|
82 |
+
#endif
|
83 |
+
|
84 |
+
namespace arrow_vendored {
|
85 |
+
namespace pcg_extras {
|
86 |
+
|
87 |
+
// Recent versions of GCC have intrinsics we can use to quickly calculate
|
88 |
+
// the number of leading and trailing zeros in a number. If possible, we
|
89 |
+
// use them, otherwise we fall back to old-fashioned bit twiddling to figure
|
90 |
+
// them out.
|
91 |
+
|
92 |
+
#ifndef PCG_BITCOUNT_T
|
93 |
+
typedef uint8_t bitcount_t;
|
94 |
+
#else
|
95 |
+
typedef PCG_BITCOUNT_T bitcount_t;
|
96 |
+
#endif
|
97 |
+
|
98 |
+
/*
|
99 |
+
* Provide some useful helper functions
|
100 |
+
* * flog2 floor(log2(x))
|
101 |
+
* * trailingzeros number of trailing zero bits
|
102 |
+
*/
|
103 |
+
|
104 |
+
#if defined(__GNUC__) // Any GNU-compatible compiler supporting C++11 has
|
105 |
+
// some useful intrinsics we can use.
|
106 |
+
|
107 |
+
inline bitcount_t flog2(uint32_t v)
|
108 |
+
{
|
109 |
+
return 31 - __builtin_clz(v);
|
110 |
+
}
|
111 |
+
|
112 |
+
inline bitcount_t trailingzeros(uint32_t v)
|
113 |
+
{
|
114 |
+
return __builtin_ctz(v);
|
115 |
+
}
|
116 |
+
|
117 |
+
inline bitcount_t flog2(uint64_t v)
|
118 |
+
{
|
119 |
+
#if UINT64_MAX == ULONG_MAX
|
120 |
+
return 63 - __builtin_clzl(v);
|
121 |
+
#elif UINT64_MAX == ULLONG_MAX
|
122 |
+
return 63 - __builtin_clzll(v);
|
123 |
+
#else
|
124 |
+
#error Cannot find a function for uint64_t
|
125 |
+
#endif
|
126 |
+
}
|
127 |
+
|
128 |
+
inline bitcount_t trailingzeros(uint64_t v)
|
129 |
+
{
|
130 |
+
#if UINT64_MAX == ULONG_MAX
|
131 |
+
return __builtin_ctzl(v);
|
132 |
+
#elif UINT64_MAX == ULLONG_MAX
|
133 |
+
return __builtin_ctzll(v);
|
134 |
+
#else
|
135 |
+
#error Cannot find a function for uint64_t
|
136 |
+
#endif
|
137 |
+
}
|
138 |
+
|
139 |
+
#elif defined(_MSC_VER) // Use MSVC++ intrinsics
|
140 |
+
|
141 |
+
#pragma intrinsic(_BitScanReverse, _BitScanForward)
|
142 |
+
#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
|
143 |
+
#pragma intrinsic(_BitScanReverse64, _BitScanForward64)
|
144 |
+
#endif
|
145 |
+
|
146 |
+
inline bitcount_t flog2(uint32_t v)
|
147 |
+
{
|
148 |
+
unsigned long i;
|
149 |
+
_BitScanReverse(&i, v);
|
150 |
+
return bitcount_t(i);
|
151 |
+
}
|
152 |
+
|
153 |
+
inline bitcount_t trailingzeros(uint32_t v)
|
154 |
+
{
|
155 |
+
unsigned long i;
|
156 |
+
_BitScanForward(&i, v);
|
157 |
+
return bitcount_t(i);
|
158 |
+
}
|
159 |
+
|
160 |
+
inline bitcount_t flog2(uint64_t v)
|
161 |
+
{
|
162 |
+
#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
|
163 |
+
unsigned long i;
|
164 |
+
_BitScanReverse64(&i, v);
|
165 |
+
return bitcount_t(i);
|
166 |
+
#else
|
167 |
+
// 32-bit x86
|
168 |
+
uint32_t high = v >> 32;
|
169 |
+
uint32_t low = uint32_t(v);
|
170 |
+
return high ? 32+flog2(high) : flog2(low);
|
171 |
+
#endif
|
172 |
+
}
|
173 |
+
|
174 |
+
inline bitcount_t trailingzeros(uint64_t v)
|
175 |
+
{
|
176 |
+
#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
|
177 |
+
unsigned long i;
|
178 |
+
_BitScanForward64(&i, v);
|
179 |
+
return bitcount_t(i);
|
180 |
+
#else
|
181 |
+
// 32-bit x86
|
182 |
+
uint32_t high = v >> 32;
|
183 |
+
uint32_t low = uint32_t(v);
|
184 |
+
return low ? trailingzeros(low) : trailingzeros(high)+32;
|
185 |
+
#endif
|
186 |
+
}
|
187 |
+
|
188 |
+
#else // Otherwise, we fall back to bit twiddling
|
189 |
+
// implementations
|
190 |
+
|
191 |
+
inline bitcount_t flog2(uint32_t v)
|
192 |
+
{
|
193 |
+
// Based on code by Eric Cole and Mark Dickinson, which appears at
|
194 |
+
// https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
|
195 |
+
|
196 |
+
static const uint8_t multiplyDeBruijnBitPos[32] = {
|
197 |
+
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
198 |
+
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
199 |
+
};
|
200 |
+
|
201 |
+
v |= v >> 1; // first round down to one less than a power of 2
|
202 |
+
v |= v >> 2;
|
203 |
+
v |= v >> 4;
|
204 |
+
v |= v >> 8;
|
205 |
+
v |= v >> 16;
|
206 |
+
|
207 |
+
return multiplyDeBruijnBitPos[(uint32_t)(v * 0x07C4ACDDU) >> 27];
|
208 |
+
}
|
209 |
+
|
210 |
+
inline bitcount_t trailingzeros(uint32_t v)
|
211 |
+
{
|
212 |
+
static const uint8_t multiplyDeBruijnBitPos[32] = {
|
213 |
+
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
214 |
+
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
215 |
+
};
|
216 |
+
|
217 |
+
return multiplyDeBruijnBitPos[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
|
218 |
+
}
|
219 |
+
|
220 |
+
inline bitcount_t flog2(uint64_t v)
|
221 |
+
{
|
222 |
+
uint32_t high = v >> 32;
|
223 |
+
uint32_t low = uint32_t(v);
|
224 |
+
|
225 |
+
return high ? 32+flog2(high) : flog2(low);
|
226 |
+
}
|
227 |
+
|
228 |
+
inline bitcount_t trailingzeros(uint64_t v)
|
229 |
+
{
|
230 |
+
uint32_t high = v >> 32;
|
231 |
+
uint32_t low = uint32_t(v);
|
232 |
+
|
233 |
+
return low ? trailingzeros(low) : trailingzeros(high)+32;
|
234 |
+
}
|
235 |
+
|
236 |
+
#endif
|
237 |
+
|
238 |
+
inline bitcount_t flog2(uint8_t v)
|
239 |
+
{
|
240 |
+
return flog2(uint32_t(v));
|
241 |
+
}
|
242 |
+
|
243 |
+
inline bitcount_t flog2(uint16_t v)
|
244 |
+
{
|
245 |
+
return flog2(uint32_t(v));
|
246 |
+
}
|
247 |
+
|
248 |
+
#if __SIZEOF_INT128__
|
249 |
+
inline bitcount_t flog2(__uint128_t v)
|
250 |
+
{
|
251 |
+
uint64_t high = uint64_t(v >> 64);
|
252 |
+
uint64_t low = uint64_t(v);
|
253 |
+
|
254 |
+
return high ? 64+flog2(high) : flog2(low);
|
255 |
+
}
|
256 |
+
#endif
|
257 |
+
|
258 |
+
inline bitcount_t trailingzeros(uint8_t v)
|
259 |
+
{
|
260 |
+
return trailingzeros(uint32_t(v));
|
261 |
+
}
|
262 |
+
|
263 |
+
inline bitcount_t trailingzeros(uint16_t v)
|
264 |
+
{
|
265 |
+
return trailingzeros(uint32_t(v));
|
266 |
+
}
|
267 |
+
|
268 |
+
#if __SIZEOF_INT128__
|
269 |
+
inline bitcount_t trailingzeros(__uint128_t v)
|
270 |
+
{
|
271 |
+
uint64_t high = uint64_t(v >> 64);
|
272 |
+
uint64_t low = uint64_t(v);
|
273 |
+
return low ? trailingzeros(low) : trailingzeros(high)+64;
|
274 |
+
}
|
275 |
+
#endif
|
276 |
+
|
277 |
+
template <typename UInt>
|
278 |
+
inline bitcount_t clog2(UInt v)
|
279 |
+
{
|
280 |
+
return flog2(v) + ((v & (-v)) != v);
|
281 |
+
}
|
282 |
+
|
283 |
+
template <typename UInt>
|
284 |
+
inline UInt addwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
|
285 |
+
{
|
286 |
+
UInt half_result = y + carryin;
|
287 |
+
UInt result = x + half_result;
|
288 |
+
*carryout = (half_result < y) || (result < x);
|
289 |
+
return result;
|
290 |
+
}
|
291 |
+
|
292 |
+
template <typename UInt>
|
293 |
+
inline UInt subwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
|
294 |
+
{
|
295 |
+
UInt half_result = y + carryin;
|
296 |
+
UInt result = x - half_result;
|
297 |
+
*carryout = (half_result < y) || (result > x);
|
298 |
+
return result;
|
299 |
+
}
|
300 |
+
|
301 |
+
|
302 |
+
template <typename UInt, typename UIntX2>
|
303 |
+
class uint_x4 {
|
304 |
+
// private:
|
305 |
+
static constexpr unsigned int UINT_BITS = sizeof(UInt) * CHAR_BIT;
|
306 |
+
public:
|
307 |
+
union {
|
308 |
+
#if PCG_LITTLE_ENDIAN
|
309 |
+
struct {
|
310 |
+
UInt v0, v1, v2, v3;
|
311 |
+
} w;
|
312 |
+
struct {
|
313 |
+
UIntX2 v01, v23;
|
314 |
+
} d;
|
315 |
+
#else
|
316 |
+
struct {
|
317 |
+
UInt v3, v2, v1, v0;
|
318 |
+
} w;
|
319 |
+
struct {
|
320 |
+
UIntX2 v23, v01;
|
321 |
+
} d;
|
322 |
+
#endif
|
323 |
+
// For the array access versions, the code that uses the array
|
324 |
+
// must handle endian itself. Yuck.
|
325 |
+
UInt wa[4];
|
326 |
+
};
|
327 |
+
|
328 |
+
public:
|
329 |
+
uint_x4() = default;
|
330 |
+
|
331 |
+
constexpr uint_x4(UInt v3, UInt v2, UInt v1, UInt v0)
|
332 |
+
#if PCG_LITTLE_ENDIAN
|
333 |
+
: w{v0, v1, v2, v3}
|
334 |
+
#else
|
335 |
+
: w{v3, v2, v1, v0}
|
336 |
+
#endif
|
337 |
+
{
|
338 |
+
// Nothing (else) to do
|
339 |
+
}
|
340 |
+
|
341 |
+
constexpr uint_x4(UIntX2 v23, UIntX2 v01)
|
342 |
+
#if PCG_LITTLE_ENDIAN
|
343 |
+
: d{v01,v23}
|
344 |
+
#else
|
345 |
+
: d{v23,v01}
|
346 |
+
#endif
|
347 |
+
{
|
348 |
+
// Nothing (else) to do
|
349 |
+
}
|
350 |
+
|
351 |
+
constexpr uint_x4(UIntX2 v01)
|
352 |
+
#if PCG_LITTLE_ENDIAN
|
353 |
+
: d{v01, UIntX2(0)}
|
354 |
+
#else
|
355 |
+
: d{UIntX2(0),v01}
|
356 |
+
#endif
|
357 |
+
{
|
358 |
+
// Nothing (else) to do
|
359 |
+
}
|
360 |
+
|
361 |
+
template<class Integral,
|
362 |
+
typename std::enable_if<(std::is_integral<Integral>::value
|
363 |
+
&& sizeof(Integral) <= sizeof(UIntX2))
|
364 |
+
>::type* = nullptr>
|
365 |
+
constexpr uint_x4(Integral v01)
|
366 |
+
#if PCG_LITTLE_ENDIAN
|
367 |
+
: d{UIntX2(v01), UIntX2(0)}
|
368 |
+
#else
|
369 |
+
: d{UIntX2(0), UIntX2(v01)}
|
370 |
+
#endif
|
371 |
+
{
|
372 |
+
// Nothing (else) to do
|
373 |
+
}
|
374 |
+
|
375 |
+
explicit constexpr operator UIntX2() const
|
376 |
+
{
|
377 |
+
return d.v01;
|
378 |
+
}
|
379 |
+
|
380 |
+
template<class Integral,
|
381 |
+
typename std::enable_if<(std::is_integral<Integral>::value
|
382 |
+
&& sizeof(Integral) <= sizeof(UIntX2))
|
383 |
+
>::type* = nullptr>
|
384 |
+
explicit constexpr operator Integral() const
|
385 |
+
{
|
386 |
+
return Integral(d.v01);
|
387 |
+
}
|
388 |
+
|
389 |
+
explicit constexpr operator bool() const
|
390 |
+
{
|
391 |
+
return d.v01 || d.v23;
|
392 |
+
}
|
393 |
+
|
394 |
+
template<typename U, typename V>
|
395 |
+
friend uint_x4<U,V> operator*(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
396 |
+
|
397 |
+
template<typename U, typename V>
|
398 |
+
friend uint_x4<U,V> operator*(const uint_x4<U,V>&, V);
|
399 |
+
|
400 |
+
template<typename U, typename V>
|
401 |
+
friend std::pair< uint_x4<U,V>,uint_x4<U,V> >
|
402 |
+
divmod(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
403 |
+
|
404 |
+
template<typename U, typename V>
|
405 |
+
friend uint_x4<U,V> operator+(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
406 |
+
|
407 |
+
template<typename U, typename V>
|
408 |
+
friend uint_x4<U,V> operator-(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
409 |
+
|
410 |
+
template<typename U, typename V>
|
411 |
+
friend uint_x4<U,V> operator<<(const uint_x4<U,V>&, const bitcount_t shift);
|
412 |
+
|
413 |
+
template<typename U, typename V>
|
414 |
+
friend uint_x4<U,V> operator>>(const uint_x4<U,V>&, const bitcount_t shift);
|
415 |
+
|
416 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
417 |
+
template<typename U>
|
418 |
+
friend uint_x4<U,uint64_t> operator<<(const uint_x4<U,uint64_t>&, const bitcount_t shift);
|
419 |
+
|
420 |
+
template<typename U>
|
421 |
+
friend uint_x4<U,uint64_t> operator>>(const uint_x4<U,uint64_t>&, const bitcount_t shift);
|
422 |
+
#endif
|
423 |
+
|
424 |
+
template<typename U, typename V>
|
425 |
+
friend uint_x4<U,V> operator&(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
426 |
+
|
427 |
+
template<typename U, typename V>
|
428 |
+
friend uint_x4<U,V> operator|(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
429 |
+
|
430 |
+
template<typename U, typename V>
|
431 |
+
friend uint_x4<U,V> operator^(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
432 |
+
|
433 |
+
template<typename U, typename V>
|
434 |
+
friend bool operator==(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
435 |
+
|
436 |
+
template<typename U, typename V>
|
437 |
+
friend bool operator!=(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
438 |
+
|
439 |
+
template<typename U, typename V>
|
440 |
+
friend bool operator<(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
441 |
+
|
442 |
+
template<typename U, typename V>
|
443 |
+
friend bool operator<=(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
444 |
+
|
445 |
+
template<typename U, typename V>
|
446 |
+
friend bool operator>(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
447 |
+
|
448 |
+
template<typename U, typename V>
|
449 |
+
friend bool operator>=(const uint_x4<U,V>&, const uint_x4<U,V>&);
|
450 |
+
|
451 |
+
template<typename U, typename V>
|
452 |
+
friend uint_x4<U,V> operator~(const uint_x4<U,V>&);
|
453 |
+
|
454 |
+
template<typename U, typename V>
|
455 |
+
friend uint_x4<U,V> operator-(const uint_x4<U,V>&);
|
456 |
+
|
457 |
+
template<typename U, typename V>
|
458 |
+
friend bitcount_t flog2(const uint_x4<U,V>&);
|
459 |
+
|
460 |
+
template<typename U, typename V>
|
461 |
+
friend bitcount_t trailingzeros(const uint_x4<U,V>&);
|
462 |
+
|
463 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
464 |
+
template<typename U>
|
465 |
+
friend bitcount_t flog2(const uint_x4<U,uint64_t>&);
|
466 |
+
|
467 |
+
template<typename U>
|
468 |
+
friend bitcount_t trailingzeros(const uint_x4<U,uint64_t>&);
|
469 |
+
#endif
|
470 |
+
|
471 |
+
uint_x4& operator*=(const uint_x4& rhs)
|
472 |
+
{
|
473 |
+
uint_x4 result = *this * rhs;
|
474 |
+
return *this = result;
|
475 |
+
}
|
476 |
+
|
477 |
+
uint_x4& operator*=(UIntX2 rhs)
|
478 |
+
{
|
479 |
+
uint_x4 result = *this * rhs;
|
480 |
+
return *this = result;
|
481 |
+
}
|
482 |
+
|
483 |
+
uint_x4& operator/=(const uint_x4& rhs)
|
484 |
+
{
|
485 |
+
uint_x4 result = *this / rhs;
|
486 |
+
return *this = result;
|
487 |
+
}
|
488 |
+
|
489 |
+
uint_x4& operator%=(const uint_x4& rhs)
|
490 |
+
{
|
491 |
+
uint_x4 result = *this % rhs;
|
492 |
+
return *this = result;
|
493 |
+
}
|
494 |
+
|
495 |
+
uint_x4& operator+=(const uint_x4& rhs)
|
496 |
+
{
|
497 |
+
uint_x4 result = *this + rhs;
|
498 |
+
return *this = result;
|
499 |
+
}
|
500 |
+
|
501 |
+
uint_x4& operator-=(const uint_x4& rhs)
|
502 |
+
{
|
503 |
+
uint_x4 result = *this - rhs;
|
504 |
+
return *this = result;
|
505 |
+
}
|
506 |
+
|
507 |
+
uint_x4& operator&=(const uint_x4& rhs)
|
508 |
+
{
|
509 |
+
uint_x4 result = *this & rhs;
|
510 |
+
return *this = result;
|
511 |
+
}
|
512 |
+
|
513 |
+
uint_x4& operator|=(const uint_x4& rhs)
|
514 |
+
{
|
515 |
+
uint_x4 result = *this | rhs;
|
516 |
+
return *this = result;
|
517 |
+
}
|
518 |
+
|
519 |
+
uint_x4& operator^=(const uint_x4& rhs)
|
520 |
+
{
|
521 |
+
uint_x4 result = *this ^ rhs;
|
522 |
+
return *this = result;
|
523 |
+
}
|
524 |
+
|
525 |
+
uint_x4& operator>>=(bitcount_t shift)
|
526 |
+
{
|
527 |
+
uint_x4 result = *this >> shift;
|
528 |
+
return *this = result;
|
529 |
+
}
|
530 |
+
|
531 |
+
uint_x4& operator<<=(bitcount_t shift)
|
532 |
+
{
|
533 |
+
uint_x4 result = *this << shift;
|
534 |
+
return *this = result;
|
535 |
+
}
|
536 |
+
|
537 |
+
};
|
538 |
+
|
539 |
+
template<typename U, typename V>
|
540 |
+
bitcount_t flog2(const uint_x4<U,V>& v)
|
541 |
+
{
|
542 |
+
#if PCG_LITTLE_ENDIAN
|
543 |
+
for (uint8_t i = 4; i !=0; /* dec in loop */) {
|
544 |
+
--i;
|
545 |
+
#else
|
546 |
+
for (uint8_t i = 0; i < 4; ++i) {
|
547 |
+
#endif
|
548 |
+
if (v.wa[i] == 0)
|
549 |
+
continue;
|
550 |
+
return flog2(v.wa[i]) + uint_x4<U,V>::UINT_BITS*i;
|
551 |
+
}
|
552 |
+
abort();
|
553 |
+
}
|
554 |
+
|
555 |
+
template<typename U, typename V>
|
556 |
+
bitcount_t trailingzeros(const uint_x4<U,V>& v)
|
557 |
+
{
|
558 |
+
#if PCG_LITTLE_ENDIAN
|
559 |
+
for (uint8_t i = 0; i < 4; ++i) {
|
560 |
+
#else
|
561 |
+
for (uint8_t i = 4; i !=0; /* dec in loop */) {
|
562 |
+
--i;
|
563 |
+
#endif
|
564 |
+
if (v.wa[i] != 0)
|
565 |
+
return trailingzeros(v.wa[i]) + uint_x4<U,V>::UINT_BITS*i;
|
566 |
+
}
|
567 |
+
return uint_x4<U,V>::UINT_BITS*4;
|
568 |
+
}
|
569 |
+
|
570 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
571 |
+
template<typename UInt32>
|
572 |
+
bitcount_t flog2(const uint_x4<UInt32,uint64_t>& v)
|
573 |
+
{
|
574 |
+
return v.d.v23 > 0 ? flog2(v.d.v23) + uint_x4<UInt32,uint64_t>::UINT_BITS*2
|
575 |
+
: flog2(v.d.v01);
|
576 |
+
}
|
577 |
+
|
578 |
+
template<typename UInt32>
|
579 |
+
bitcount_t trailingzeros(const uint_x4<UInt32,uint64_t>& v)
|
580 |
+
{
|
581 |
+
return v.d.v01 == 0 ? trailingzeros(v.d.v23) + uint_x4<UInt32,uint64_t>::UINT_BITS*2
|
582 |
+
: trailingzeros(v.d.v01);
|
583 |
+
}
|
584 |
+
#endif
|
585 |
+
|
586 |
+
template <typename UInt, typename UIntX2>
|
587 |
+
std::pair< uint_x4<UInt,UIntX2>, uint_x4<UInt,UIntX2> >
|
588 |
+
divmod(const uint_x4<UInt,UIntX2>& orig_dividend,
|
589 |
+
const uint_x4<UInt,UIntX2>& divisor)
|
590 |
+
{
|
591 |
+
// If the dividend is less than the divisor, the answer is always zero.
|
592 |
+
// This takes care of boundary cases like 0/x (which would otherwise be
|
593 |
+
// problematic because we can't take the log of zero. (The boundary case
|
594 |
+
// of division by zero is undefined.)
|
595 |
+
if (orig_dividend < divisor)
|
596 |
+
return { uint_x4<UInt,UIntX2>(UIntX2(0)), orig_dividend };
|
597 |
+
|
598 |
+
auto dividend = orig_dividend;
|
599 |
+
|
600 |
+
auto log2_divisor = flog2(divisor);
|
601 |
+
auto log2_dividend = flog2(dividend);
|
602 |
+
// assert(log2_dividend >= log2_divisor);
|
603 |
+
bitcount_t logdiff = log2_dividend - log2_divisor;
|
604 |
+
|
605 |
+
constexpr uint_x4<UInt,UIntX2> ONE(UIntX2(1));
|
606 |
+
if (logdiff == 0)
|
607 |
+
return { ONE, dividend - divisor };
|
608 |
+
|
609 |
+
// Now we change the log difference to
|
610 |
+
// floor(log2(divisor)) - ceil(log2(dividend))
|
611 |
+
// to ensure that we *underestimate* the result.
|
612 |
+
logdiff -= 1;
|
613 |
+
|
614 |
+
uint_x4<UInt,UIntX2> quotient(UIntX2(0));
|
615 |
+
|
616 |
+
auto qfactor = ONE << logdiff;
|
617 |
+
auto factor = divisor << logdiff;
|
618 |
+
|
619 |
+
do {
|
620 |
+
dividend -= factor;
|
621 |
+
quotient += qfactor;
|
622 |
+
while (dividend < factor) {
|
623 |
+
factor >>= 1;
|
624 |
+
qfactor >>= 1;
|
625 |
+
}
|
626 |
+
} while (dividend >= divisor);
|
627 |
+
|
628 |
+
return { quotient, dividend };
|
629 |
+
}
|
630 |
+
|
631 |
+
template <typename UInt, typename UIntX2>
|
632 |
+
uint_x4<UInt,UIntX2> operator/(const uint_x4<UInt,UIntX2>& dividend,
|
633 |
+
const uint_x4<UInt,UIntX2>& divisor)
|
634 |
+
{
|
635 |
+
return divmod(dividend, divisor).first;
|
636 |
+
}
|
637 |
+
|
638 |
+
template <typename UInt, typename UIntX2>
|
639 |
+
uint_x4<UInt,UIntX2> operator%(const uint_x4<UInt,UIntX2>& dividend,
|
640 |
+
const uint_x4<UInt,UIntX2>& divisor)
|
641 |
+
{
|
642 |
+
return divmod(dividend, divisor).second;
|
643 |
+
}
|
644 |
+
|
645 |
+
|
646 |
+
template <typename UInt, typename UIntX2>
|
647 |
+
uint_x4<UInt,UIntX2> operator*(const uint_x4<UInt,UIntX2>& a,
|
648 |
+
const uint_x4<UInt,UIntX2>& b)
|
649 |
+
{
|
650 |
+
constexpr auto UINT_BITS = uint_x4<UInt,UIntX2>::UINT_BITS;
|
651 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
652 |
+
bool carryin = false;
|
653 |
+
bool carryout;
|
654 |
+
UIntX2 a0b0 = UIntX2(a.w.v0) * UIntX2(b.w.v0);
|
655 |
+
r.w.v0 = UInt(a0b0);
|
656 |
+
r.w.v1 = UInt(a0b0 >> UINT_BITS);
|
657 |
+
|
658 |
+
UIntX2 a1b0 = UIntX2(a.w.v1) * UIntX2(b.w.v0);
|
659 |
+
r.w.v2 = UInt(a1b0 >> UINT_BITS);
|
660 |
+
r.w.v1 = addwithcarry(r.w.v1, UInt(a1b0), carryin, &carryout);
|
661 |
+
carryin = carryout;
|
662 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
|
663 |
+
carryin = carryout;
|
664 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
665 |
+
|
666 |
+
UIntX2 a0b1 = UIntX2(a.w.v0) * UIntX2(b.w.v1);
|
667 |
+
carryin = false;
|
668 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(a0b1 >> UINT_BITS), carryin, &carryout);
|
669 |
+
carryin = carryout;
|
670 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
671 |
+
|
672 |
+
carryin = false;
|
673 |
+
r.w.v1 = addwithcarry(r.w.v1, UInt(a0b1), carryin, &carryout);
|
674 |
+
carryin = carryout;
|
675 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
|
676 |
+
carryin = carryout;
|
677 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
678 |
+
|
679 |
+
UIntX2 a1b1 = UIntX2(a.w.v1) * UIntX2(b.w.v1);
|
680 |
+
carryin = false;
|
681 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(a1b1), carryin, &carryout);
|
682 |
+
carryin = carryout;
|
683 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(a1b1 >> UINT_BITS), carryin, &carryout);
|
684 |
+
|
685 |
+
r.d.v23 += a.d.v01 * b.d.v23 + a.d.v23 * b.d.v01;
|
686 |
+
|
687 |
+
return r;
|
688 |
+
}
|
689 |
+
|
690 |
+
|
691 |
+
template <typename UInt, typename UIntX2>
|
692 |
+
uint_x4<UInt,UIntX2> operator*(const uint_x4<UInt,UIntX2>& a,
|
693 |
+
UIntX2 b01)
|
694 |
+
{
|
695 |
+
constexpr auto UINT_BITS = uint_x4<UInt,UIntX2>::UINT_BITS;
|
696 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
697 |
+
bool carryin = false;
|
698 |
+
bool carryout;
|
699 |
+
UIntX2 a0b0 = UIntX2(a.w.v0) * UIntX2(UInt(b01));
|
700 |
+
r.w.v0 = UInt(a0b0);
|
701 |
+
r.w.v1 = UInt(a0b0 >> UINT_BITS);
|
702 |
+
|
703 |
+
UIntX2 a1b0 = UIntX2(a.w.v1) * UIntX2(UInt(b01));
|
704 |
+
r.w.v2 = UInt(a1b0 >> UINT_BITS);
|
705 |
+
r.w.v1 = addwithcarry(r.w.v1, UInt(a1b0), carryin, &carryout);
|
706 |
+
carryin = carryout;
|
707 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
|
708 |
+
carryin = carryout;
|
709 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
710 |
+
|
711 |
+
UIntX2 a0b1 = UIntX2(a.w.v0) * UIntX2(b01 >> UINT_BITS);
|
712 |
+
carryin = false;
|
713 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(a0b1 >> UINT_BITS), carryin, &carryout);
|
714 |
+
carryin = carryout;
|
715 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
716 |
+
|
717 |
+
carryin = false;
|
718 |
+
r.w.v1 = addwithcarry(r.w.v1, UInt(a0b1), carryin, &carryout);
|
719 |
+
carryin = carryout;
|
720 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
|
721 |
+
carryin = carryout;
|
722 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
|
723 |
+
|
724 |
+
UIntX2 a1b1 = UIntX2(a.w.v1) * UIntX2(b01 >> UINT_BITS);
|
725 |
+
carryin = false;
|
726 |
+
r.w.v2 = addwithcarry(r.w.v2, UInt(a1b1), carryin, &carryout);
|
727 |
+
carryin = carryout;
|
728 |
+
r.w.v3 = addwithcarry(r.w.v3, UInt(a1b1 >> UINT_BITS), carryin, &carryout);
|
729 |
+
|
730 |
+
r.d.v23 += a.d.v23 * b01;
|
731 |
+
|
732 |
+
return r;
|
733 |
+
}
|
734 |
+
|
735 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
736 |
+
#if defined(_MSC_VER)
|
737 |
+
#pragma intrinsic(_umul128)
|
738 |
+
#endif
|
739 |
+
|
740 |
+
#if defined(_MSC_VER) || __SIZEOF_INT128__
|
741 |
+
template <typename UInt32>
|
742 |
+
uint_x4<UInt32,uint64_t> operator*(const uint_x4<UInt32,uint64_t>& a,
|
743 |
+
const uint_x4<UInt32,uint64_t>& b)
|
744 |
+
{
|
745 |
+
#if defined(_MSC_VER)
|
746 |
+
uint64_t hi;
|
747 |
+
uint64_t lo = _umul128(a.d.v01, b.d.v01, &hi);
|
748 |
+
#else
|
749 |
+
__uint128_t r = __uint128_t(a.d.v01) * __uint128_t(b.d.v01);
|
750 |
+
uint64_t lo = uint64_t(r);
|
751 |
+
uint64_t hi = r >> 64;
|
752 |
+
#endif
|
753 |
+
hi += a.d.v23 * b.d.v01 + a.d.v01 * b.d.v23;
|
754 |
+
return {hi, lo};
|
755 |
+
}
|
756 |
+
#endif
|
757 |
+
#endif
|
758 |
+
|
759 |
+
|
760 |
+
template <typename UInt, typename UIntX2>
|
761 |
+
uint_x4<UInt,UIntX2> operator+(const uint_x4<UInt,UIntX2>& a,
|
762 |
+
const uint_x4<UInt,UIntX2>& b)
|
763 |
+
{
|
764 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
765 |
+
|
766 |
+
bool carryin = false;
|
767 |
+
bool carryout;
|
768 |
+
r.w.v0 = addwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
|
769 |
+
carryin = carryout;
|
770 |
+
r.w.v1 = addwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
|
771 |
+
carryin = carryout;
|
772 |
+
r.w.v2 = addwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
|
773 |
+
carryin = carryout;
|
774 |
+
r.w.v3 = addwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
|
775 |
+
|
776 |
+
return r;
|
777 |
+
}
|
778 |
+
|
779 |
+
template <typename UInt, typename UIntX2>
|
780 |
+
uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& a,
|
781 |
+
const uint_x4<UInt,UIntX2>& b)
|
782 |
+
{
|
783 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
784 |
+
|
785 |
+
bool carryin = false;
|
786 |
+
bool carryout;
|
787 |
+
r.w.v0 = subwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
|
788 |
+
carryin = carryout;
|
789 |
+
r.w.v1 = subwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
|
790 |
+
carryin = carryout;
|
791 |
+
r.w.v2 = subwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
|
792 |
+
carryin = carryout;
|
793 |
+
r.w.v3 = subwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
|
794 |
+
|
795 |
+
return r;
|
796 |
+
}
|
797 |
+
|
798 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
799 |
+
template <typename UInt32>
|
800 |
+
uint_x4<UInt32,uint64_t> operator+(const uint_x4<UInt32,uint64_t>& a,
|
801 |
+
const uint_x4<UInt32,uint64_t>& b)
|
802 |
+
{
|
803 |
+
uint_x4<UInt32,uint64_t> r = {uint64_t(0u), uint64_t(0u)};
|
804 |
+
|
805 |
+
bool carryin = false;
|
806 |
+
bool carryout;
|
807 |
+
r.d.v01 = addwithcarry(a.d.v01, b.d.v01, carryin, &carryout);
|
808 |
+
carryin = carryout;
|
809 |
+
r.d.v23 = addwithcarry(a.d.v23, b.d.v23, carryin, &carryout);
|
810 |
+
|
811 |
+
return r;
|
812 |
+
}
|
813 |
+
|
814 |
+
template <typename UInt32>
|
815 |
+
uint_x4<UInt32,uint64_t> operator-(const uint_x4<UInt32,uint64_t>& a,
|
816 |
+
const uint_x4<UInt32,uint64_t>& b)
|
817 |
+
{
|
818 |
+
uint_x4<UInt32,uint64_t> r = {uint64_t(0u), uint64_t(0u)};
|
819 |
+
|
820 |
+
bool carryin = false;
|
821 |
+
bool carryout;
|
822 |
+
r.d.v01 = subwithcarry(a.d.v01, b.d.v01, carryin, &carryout);
|
823 |
+
carryin = carryout;
|
824 |
+
r.d.v23 = subwithcarry(a.d.v23, b.d.v23, carryin, &carryout);
|
825 |
+
|
826 |
+
return r;
|
827 |
+
}
|
828 |
+
#endif
|
829 |
+
|
830 |
+
template <typename UInt, typename UIntX2>
|
831 |
+
uint_x4<UInt,UIntX2> operator&(const uint_x4<UInt,UIntX2>& a,
|
832 |
+
const uint_x4<UInt,UIntX2>& b)
|
833 |
+
{
|
834 |
+
return uint_x4<UInt,UIntX2>(a.d.v23 & b.d.v23, a.d.v01 & b.d.v01);
|
835 |
+
}
|
836 |
+
|
837 |
+
template <typename UInt, typename UIntX2>
|
838 |
+
uint_x4<UInt,UIntX2> operator|(const uint_x4<UInt,UIntX2>& a,
|
839 |
+
const uint_x4<UInt,UIntX2>& b)
|
840 |
+
{
|
841 |
+
return uint_x4<UInt,UIntX2>(a.d.v23 | b.d.v23, a.d.v01 | b.d.v01);
|
842 |
+
}
|
843 |
+
|
844 |
+
template <typename UInt, typename UIntX2>
|
845 |
+
uint_x4<UInt,UIntX2> operator^(const uint_x4<UInt,UIntX2>& a,
|
846 |
+
const uint_x4<UInt,UIntX2>& b)
|
847 |
+
{
|
848 |
+
return uint_x4<UInt,UIntX2>(a.d.v23 ^ b.d.v23, a.d.v01 ^ b.d.v01);
|
849 |
+
}
|
850 |
+
|
851 |
+
template <typename UInt, typename UIntX2>
|
852 |
+
uint_x4<UInt,UIntX2> operator~(const uint_x4<UInt,UIntX2>& v)
|
853 |
+
{
|
854 |
+
return uint_x4<UInt,UIntX2>(~v.d.v23, ~v.d.v01);
|
855 |
+
}
|
856 |
+
|
857 |
+
template <typename UInt, typename UIntX2>
|
858 |
+
uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& v)
|
859 |
+
{
|
860 |
+
return uint_x4<UInt,UIntX2>(0UL,0UL) - v;
|
861 |
+
}
|
862 |
+
|
863 |
+
template <typename UInt, typename UIntX2>
|
864 |
+
bool operator==(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
865 |
+
{
|
866 |
+
return (a.d.v01 == b.d.v01) && (a.d.v23 == b.d.v23);
|
867 |
+
}
|
868 |
+
|
869 |
+
template <typename UInt, typename UIntX2>
|
870 |
+
bool operator!=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
871 |
+
{
|
872 |
+
return !operator==(a,b);
|
873 |
+
}
|
874 |
+
|
875 |
+
|
876 |
+
template <typename UInt, typename UIntX2>
|
877 |
+
bool operator<(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
878 |
+
{
|
879 |
+
return (a.d.v23 < b.d.v23)
|
880 |
+
|| ((a.d.v23 == b.d.v23) && (a.d.v01 < b.d.v01));
|
881 |
+
}
|
882 |
+
|
883 |
+
template <typename UInt, typename UIntX2>
|
884 |
+
bool operator>(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
885 |
+
{
|
886 |
+
return operator<(b,a);
|
887 |
+
}
|
888 |
+
|
889 |
+
template <typename UInt, typename UIntX2>
|
890 |
+
bool operator<=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
891 |
+
{
|
892 |
+
return !(operator<(b,a));
|
893 |
+
}
|
894 |
+
|
895 |
+
template <typename UInt, typename UIntX2>
|
896 |
+
bool operator>=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
|
897 |
+
{
|
898 |
+
return !(operator<(a,b));
|
899 |
+
}
|
900 |
+
|
901 |
+
|
902 |
+
|
903 |
+
template <typename UInt, typename UIntX2>
|
904 |
+
uint_x4<UInt,UIntX2> operator<<(const uint_x4<UInt,UIntX2>& v,
|
905 |
+
const bitcount_t shift)
|
906 |
+
{
|
907 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
908 |
+
const bitcount_t bits = uint_x4<UInt,UIntX2>::UINT_BITS;
|
909 |
+
const bitcount_t bitmask = bits - 1;
|
910 |
+
const bitcount_t shiftdiv = shift / bits;
|
911 |
+
const bitcount_t shiftmod = shift & bitmask;
|
912 |
+
|
913 |
+
if (shiftmod) {
|
914 |
+
UInt carryover = 0;
|
915 |
+
#if PCG_LITTLE_ENDIAN
|
916 |
+
for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
|
917 |
+
#else
|
918 |
+
for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
|
919 |
+
--out, --in;
|
920 |
+
#endif
|
921 |
+
r.wa[out] = (v.wa[in] << shiftmod) | carryover;
|
922 |
+
carryover = (v.wa[in] >> (bits - shiftmod));
|
923 |
+
}
|
924 |
+
} else {
|
925 |
+
#if PCG_LITTLE_ENDIAN
|
926 |
+
for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
|
927 |
+
#else
|
928 |
+
for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
|
929 |
+
--out, --in;
|
930 |
+
#endif
|
931 |
+
r.wa[out] = v.wa[in];
|
932 |
+
}
|
933 |
+
}
|
934 |
+
|
935 |
+
return r;
|
936 |
+
}
|
937 |
+
|
938 |
+
template <typename UInt, typename UIntX2>
|
939 |
+
uint_x4<UInt,UIntX2> operator>>(const uint_x4<UInt,UIntX2>& v,
|
940 |
+
const bitcount_t shift)
|
941 |
+
{
|
942 |
+
uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
|
943 |
+
const bitcount_t bits = uint_x4<UInt,UIntX2>::UINT_BITS;
|
944 |
+
const bitcount_t bitmask = bits - 1;
|
945 |
+
const bitcount_t shiftdiv = shift / bits;
|
946 |
+
const bitcount_t shiftmod = shift & bitmask;
|
947 |
+
|
948 |
+
if (shiftmod) {
|
949 |
+
UInt carryover = 0;
|
950 |
+
#if PCG_LITTLE_ENDIAN
|
951 |
+
for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
|
952 |
+
--out, --in;
|
953 |
+
#else
|
954 |
+
for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
|
955 |
+
#endif
|
956 |
+
r.wa[out] = (v.wa[in] >> shiftmod) | carryover;
|
957 |
+
carryover = (v.wa[in] << (bits - shiftmod));
|
958 |
+
}
|
959 |
+
} else {
|
960 |
+
#if PCG_LITTLE_ENDIAN
|
961 |
+
for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
|
962 |
+
--out, --in;
|
963 |
+
#else
|
964 |
+
for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
|
965 |
+
#endif
|
966 |
+
r.wa[out] = v.wa[in];
|
967 |
+
}
|
968 |
+
}
|
969 |
+
|
970 |
+
return r;
|
971 |
+
}
|
972 |
+
|
973 |
+
#if PCG_64BIT_SPECIALIZATIONS
|
974 |
+
template <typename UInt32>
|
975 |
+
uint_x4<UInt32,uint64_t> operator<<(const uint_x4<UInt32,uint64_t>& v,
|
976 |
+
const bitcount_t shift)
|
977 |
+
{
|
978 |
+
constexpr bitcount_t bits2 = uint_x4<UInt32,uint64_t>::UINT_BITS * 2;
|
979 |
+
|
980 |
+
if (shift >= bits2) {
|
981 |
+
return {v.d.v01 << (shift-bits2), uint64_t(0u)};
|
982 |
+
} else {
|
983 |
+
return {shift ? (v.d.v23 << shift) | (v.d.v01 >> (bits2-shift))
|
984 |
+
: v.d.v23,
|
985 |
+
v.d.v01 << shift};
|
986 |
+
}
|
987 |
+
}
|
988 |
+
|
989 |
+
template <typename UInt32>
|
990 |
+
uint_x4<UInt32,uint64_t> operator>>(const uint_x4<UInt32,uint64_t>& v,
|
991 |
+
const bitcount_t shift)
|
992 |
+
{
|
993 |
+
constexpr bitcount_t bits2 = uint_x4<UInt32,uint64_t>::UINT_BITS * 2;
|
994 |
+
|
995 |
+
if (shift >= bits2) {
|
996 |
+
return {uint64_t(0u), v.d.v23 >> (shift-bits2)};
|
997 |
+
} else {
|
998 |
+
return {v.d.v23 >> shift,
|
999 |
+
shift ? (v.d.v01 >> shift) | (v.d.v23 << (bits2-shift))
|
1000 |
+
: v.d.v01};
|
1001 |
+
}
|
1002 |
+
}
|
1003 |
+
#endif
|
1004 |
+
|
1005 |
+
} // namespace pcg_extras
|
1006 |
+
} // namespace arrow_vendored
|
1007 |
+
|
1008 |
+
#endif // PCG_UINT128_HPP_INCLUDED
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/portable-snippets/debug-trap.h
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Debugging assertions and traps
|
2 |
+
* Portable Snippets - https://github.com/nemequ/portable-snippets
|
3 |
+
* Created by Evan Nemerson <[email protected]>
|
4 |
+
*
|
5 |
+
* To the extent possible under law, the authors have waived all
|
6 |
+
* copyright and related or neighboring rights to this code. For
|
7 |
+
* details, see the Creative Commons Zero 1.0 Universal license at
|
8 |
+
* https://creativecommons.org/publicdomain/zero/1.0/
|
9 |
+
*/
|
10 |
+
|
11 |
+
#if !defined(PSNIP_DEBUG_TRAP_H)
|
12 |
+
#define PSNIP_DEBUG_TRAP_H
|
13 |
+
|
14 |
+
#if !defined(PSNIP_NDEBUG) && defined(NDEBUG) && !defined(PSNIP_DEBUG)
|
15 |
+
# define PSNIP_NDEBUG 1
|
16 |
+
#endif
|
17 |
+
|
18 |
+
#if defined(__has_builtin) && !defined(__ibmxl__)
|
19 |
+
# if __has_builtin(__builtin_debugtrap)
|
20 |
+
# define psnip_trap() __builtin_debugtrap()
|
21 |
+
# elif __has_builtin(__debugbreak)
|
22 |
+
# define psnip_trap() __debugbreak()
|
23 |
+
# endif
|
24 |
+
#endif
|
25 |
+
#if !defined(psnip_trap)
|
26 |
+
# if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
27 |
+
# define psnip_trap() __debugbreak()
|
28 |
+
# elif defined(__ARMCC_VERSION)
|
29 |
+
# define psnip_trap() __breakpoint(42)
|
30 |
+
# elif defined(__ibmxl__) || defined(__xlC__)
|
31 |
+
# include <builtins.h>
|
32 |
+
# define psnip_trap() __trap(42)
|
33 |
+
# elif defined(__DMC__) && defined(_M_IX86)
|
34 |
+
static inline void psnip_trap(void) { __asm int 3h; }
|
35 |
+
# elif defined(__i386__) || defined(__x86_64__)
|
36 |
+
static inline void psnip_trap(void) { __asm__ __volatile__("int $03"); }
|
37 |
+
# elif defined(__thumb__)
|
38 |
+
static inline void psnip_trap(void) { __asm__ __volatile__(".inst 0xde01"); }
|
39 |
+
# elif defined(__aarch64__)
|
40 |
+
static inline void psnip_trap(void) { __asm__ __volatile__(".inst 0xd4200000"); }
|
41 |
+
# elif defined(__arm__)
|
42 |
+
static inline void psnip_trap(void) { __asm__ __volatile__(".inst 0xe7f001f0"); }
|
43 |
+
# elif defined (__alpha__) && !defined(__osf__)
|
44 |
+
static inline void psnip_trap(void) { __asm__ __volatile__("bpt"); }
|
45 |
+
# elif defined(_54_)
|
46 |
+
static inline void psnip_trap(void) { __asm__ __volatile__("ESTOP"); }
|
47 |
+
# elif defined(_55_)
|
48 |
+
static inline void psnip_trap(void) { __asm__ __volatile__(";\n .if (.MNEMONIC)\n ESTOP_1\n .else\n ESTOP_1()\n .endif\n NOP"); }
|
49 |
+
# elif defined(_64P_)
|
50 |
+
static inline void psnip_trap(void) { __asm__ __volatile__("SWBP 0"); }
|
51 |
+
# elif defined(_6x_)
|
52 |
+
static inline void psnip_trap(void) { __asm__ __volatile__("NOP\n .word 0x10000000"); }
|
53 |
+
# elif defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 0) && defined(__GNUC__)
|
54 |
+
# define psnip_trap() __builtin_trap()
|
55 |
+
# else
|
56 |
+
# include <signal.h>
|
57 |
+
# if defined(SIGTRAP)
|
58 |
+
# define psnip_trap() raise(SIGTRAP)
|
59 |
+
# else
|
60 |
+
# define psnip_trap() raise(SIGABRT)
|
61 |
+
# endif
|
62 |
+
# endif
|
63 |
+
#endif
|
64 |
+
|
65 |
+
#if defined(HEDLEY_LIKELY)
|
66 |
+
# define PSNIP_DBG_LIKELY(expr) HEDLEY_LIKELY(expr)
|
67 |
+
#elif defined(__GNUC__) && (__GNUC__ >= 3)
|
68 |
+
# define PSNIP_DBG_LIKELY(expr) __builtin_expect(!!(expr), 1)
|
69 |
+
#else
|
70 |
+
# define PSNIP_DBG_LIKELY(expr) (!!(expr))
|
71 |
+
#endif
|
72 |
+
|
73 |
+
#if !defined(PSNIP_NDEBUG) || (PSNIP_NDEBUG == 0)
|
74 |
+
# define psnip_dbg_assert(expr) do { \
|
75 |
+
if (!PSNIP_DBG_LIKELY(expr)) { \
|
76 |
+
psnip_trap(); \
|
77 |
+
} \
|
78 |
+
} while (0)
|
79 |
+
#else
|
80 |
+
# define psnip_dbg_assert(expr)
|
81 |
+
#endif
|
82 |
+
|
83 |
+
#endif /* !defined(PSNIP_DEBUG_TRAP_H) */
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/portable-snippets/safe-math.h
ADDED
@@ -0,0 +1,1072 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Overflow-safe math functions
|
2 |
+
* Portable Snippets - https://github.com/nemequ/portable-snippets
|
3 |
+
* Created by Evan Nemerson <[email protected]>
|
4 |
+
*
|
5 |
+
* To the extent possible under law, the authors have waived all
|
6 |
+
* copyright and related or neighboring rights to this code. For
|
7 |
+
* details, see the Creative Commons Zero 1.0 Universal license at
|
8 |
+
* https://creativecommons.org/publicdomain/zero/1.0/
|
9 |
+
*/
|
10 |
+
|
11 |
+
#if !defined(PSNIP_SAFE_H)
|
12 |
+
#define PSNIP_SAFE_H
|
13 |
+
|
14 |
+
#if !defined(PSNIP_SAFE_FORCE_PORTABLE)
|
15 |
+
# if defined(__has_builtin)
|
16 |
+
# if __has_builtin(__builtin_add_overflow) && !defined(__ibmxl__)
|
17 |
+
# define PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW
|
18 |
+
# endif
|
19 |
+
# elif defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__INTEL_COMPILER)
|
20 |
+
# define PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW
|
21 |
+
# endif
|
22 |
+
# if defined(__has_include)
|
23 |
+
# if __has_include(<intsafe.h>)
|
24 |
+
# define PSNIP_SAFE_HAVE_INTSAFE_H
|
25 |
+
# endif
|
26 |
+
# elif defined(_WIN32)
|
27 |
+
# define PSNIP_SAFE_HAVE_INTSAFE_H
|
28 |
+
# endif
|
29 |
+
#endif /* !defined(PSNIP_SAFE_FORCE_PORTABLE) */
|
30 |
+
|
31 |
+
#if defined(__GNUC__)
|
32 |
+
# define PSNIP_SAFE_LIKELY(expr) __builtin_expect(!!(expr), 1)
|
33 |
+
# define PSNIP_SAFE_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
|
34 |
+
#else
|
35 |
+
# define PSNIP_SAFE_LIKELY(expr) !!(expr)
|
36 |
+
# define PSNIP_SAFE_UNLIKELY(expr) !!(expr)
|
37 |
+
#endif /* defined(__GNUC__) */
|
38 |
+
|
39 |
+
#if !defined(PSNIP_SAFE_STATIC_INLINE)
|
40 |
+
# if defined(__GNUC__)
|
41 |
+
# define PSNIP_SAFE__COMPILER_ATTRIBUTES __attribute__((__unused__))
|
42 |
+
# else
|
43 |
+
# define PSNIP_SAFE__COMPILER_ATTRIBUTES
|
44 |
+
# endif
|
45 |
+
|
46 |
+
# if defined(HEDLEY_INLINE)
|
47 |
+
# define PSNIP_SAFE__INLINE HEDLEY_INLINE
|
48 |
+
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
49 |
+
# define PSNIP_SAFE__INLINE inline
|
50 |
+
# elif defined(__GNUC_STDC_INLINE__)
|
51 |
+
# define PSNIP_SAFE__INLINE __inline__
|
52 |
+
# elif defined(_MSC_VER) && _MSC_VER >= 1200
|
53 |
+
# define PSNIP_SAFE__INLINE __inline
|
54 |
+
# else
|
55 |
+
# define PSNIP_SAFE__INLINE
|
56 |
+
# endif
|
57 |
+
|
58 |
+
# define PSNIP_SAFE__FUNCTION PSNIP_SAFE__COMPILER_ATTRIBUTES static PSNIP_SAFE__INLINE
|
59 |
+
#endif
|
60 |
+
|
61 |
+
// !defined(__cplusplus) added for Solaris support
|
62 |
+
#if !defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
63 |
+
# define psnip_safe_bool _Bool
|
64 |
+
#else
|
65 |
+
# define psnip_safe_bool int
|
66 |
+
#endif
|
67 |
+
|
68 |
+
#if !defined(PSNIP_SAFE_NO_FIXED)
|
69 |
+
/* For maximum portability include the exact-int module from
|
70 |
+
portable snippets. */
|
71 |
+
# if \
|
72 |
+
!defined(psnip_int64_t) || !defined(psnip_uint64_t) || \
|
73 |
+
!defined(psnip_int32_t) || !defined(psnip_uint32_t) || \
|
74 |
+
!defined(psnip_int16_t) || !defined(psnip_uint16_t) || \
|
75 |
+
!defined(psnip_int8_t) || !defined(psnip_uint8_t)
|
76 |
+
# include <stdint.h>
|
77 |
+
# if !defined(psnip_int64_t)
|
78 |
+
# define psnip_int64_t int64_t
|
79 |
+
# endif
|
80 |
+
# if !defined(psnip_uint64_t)
|
81 |
+
# define psnip_uint64_t uint64_t
|
82 |
+
# endif
|
83 |
+
# if !defined(psnip_int32_t)
|
84 |
+
# define psnip_int32_t int32_t
|
85 |
+
# endif
|
86 |
+
# if !defined(psnip_uint32_t)
|
87 |
+
# define psnip_uint32_t uint32_t
|
88 |
+
# endif
|
89 |
+
# if !defined(psnip_int16_t)
|
90 |
+
# define psnip_int16_t int16_t
|
91 |
+
# endif
|
92 |
+
# if !defined(psnip_uint16_t)
|
93 |
+
# define psnip_uint16_t uint16_t
|
94 |
+
# endif
|
95 |
+
# if !defined(psnip_int8_t)
|
96 |
+
# define psnip_int8_t int8_t
|
97 |
+
# endif
|
98 |
+
# if !defined(psnip_uint8_t)
|
99 |
+
# define psnip_uint8_t uint8_t
|
100 |
+
# endif
|
101 |
+
# endif
|
102 |
+
#endif /* !defined(PSNIP_SAFE_NO_FIXED) */
|
103 |
+
#include <limits.h>
|
104 |
+
#include <stdlib.h>
|
105 |
+
|
106 |
+
#if !defined(PSNIP_SAFE_SIZE_MAX)
|
107 |
+
# if defined(__SIZE_MAX__)
|
108 |
+
# define PSNIP_SAFE_SIZE_MAX __SIZE_MAX__
|
109 |
+
# elif defined(PSNIP_EXACT_INT_HAVE_STDINT)
|
110 |
+
# include <stdint.h>
|
111 |
+
# endif
|
112 |
+
#endif
|
113 |
+
|
114 |
+
#if defined(PSNIP_SAFE_SIZE_MAX)
|
115 |
+
# define PSNIP_SAFE__SIZE_MAX_RT PSNIP_SAFE_SIZE_MAX
|
116 |
+
#else
|
117 |
+
# define PSNIP_SAFE__SIZE_MAX_RT (~((size_t) 0))
|
118 |
+
#endif
|
119 |
+
|
120 |
+
#if defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
121 |
+
/* In VS 10, stdint.h and intsafe.h both define (U)INTN_MIN/MAX, which
|
122 |
+
triggers warning C4005 (level 1). */
|
123 |
+
# if defined(_MSC_VER) && (_MSC_VER == 1600)
|
124 |
+
# pragma warning(push)
|
125 |
+
# pragma warning(disable:4005)
|
126 |
+
# endif
|
127 |
+
# include <intsafe.h>
|
128 |
+
# if defined(_MSC_VER) && (_MSC_VER == 1600)
|
129 |
+
# pragma warning(pop)
|
130 |
+
# endif
|
131 |
+
#endif /* defined(PSNIP_SAFE_HAVE_INTSAFE_H) */
|
132 |
+
|
133 |
+
/* If there is a type larger than the one we're concerned with it's
|
134 |
+
* likely much faster to simply promote the operands, perform the
|
135 |
+
* requested operation, verify that the result falls within the
|
136 |
+
* original type, then cast the result back to the original type. */
|
137 |
+
|
138 |
+
#if !defined(PSNIP_SAFE_NO_PROMOTIONS)
|
139 |
+
|
140 |
+
#define PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, op_name, op) \
|
141 |
+
PSNIP_SAFE__FUNCTION psnip_safe_##name##_larger \
|
142 |
+
psnip_safe_larger_##name##_##op_name (T a, T b) { \
|
143 |
+
return ((psnip_safe_##name##_larger) a) op ((psnip_safe_##name##_larger) b); \
|
144 |
+
}
|
145 |
+
|
146 |
+
#define PSNIP_SAFE_DEFINE_LARGER_UNARY_OP(T, name, op_name, op) \
|
147 |
+
PSNIP_SAFE__FUNCTION psnip_safe_##name##_larger \
|
148 |
+
psnip_safe_larger_##name##_##op_name (T value) { \
|
149 |
+
return (op ((psnip_safe_##name##_larger) value)); \
|
150 |
+
}
|
151 |
+
|
152 |
+
#define PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(T, name) \
|
153 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, add, +) \
|
154 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, sub, -) \
|
155 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, mul, *) \
|
156 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, div, /) \
|
157 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, mod, %) \
|
158 |
+
PSNIP_SAFE_DEFINE_LARGER_UNARY_OP (T, name, neg, -)
|
159 |
+
|
160 |
+
#define PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(T, name) \
|
161 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, add, +) \
|
162 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, sub, -) \
|
163 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, mul, *) \
|
164 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, div, /) \
|
165 |
+
PSNIP_SAFE_DEFINE_LARGER_BINARY_OP(T, name, mod, %)
|
166 |
+
|
167 |
+
#define PSNIP_SAFE_IS_LARGER(ORIG_MAX, DEST_MAX) ((DEST_MAX / ORIG_MAX) >= ORIG_MAX)
|
168 |
+
|
169 |
+
#if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__SIZEOF_INT128__) && !defined(__ibmxl__)
|
170 |
+
#define PSNIP_SAFE_HAVE_128
|
171 |
+
typedef __int128 psnip_safe_int128_t;
|
172 |
+
typedef unsigned __int128 psnip_safe_uint128_t;
|
173 |
+
#endif /* defined(__GNUC__) */
|
174 |
+
|
175 |
+
#if !defined(PSNIP_SAFE_NO_FIXED)
|
176 |
+
#define PSNIP_SAFE_HAVE_INT8_LARGER
|
177 |
+
#define PSNIP_SAFE_HAVE_UINT8_LARGER
|
178 |
+
typedef psnip_int16_t psnip_safe_int8_larger;
|
179 |
+
typedef psnip_uint16_t psnip_safe_uint8_larger;
|
180 |
+
|
181 |
+
#define PSNIP_SAFE_HAVE_INT16_LARGER
|
182 |
+
typedef psnip_int32_t psnip_safe_int16_larger;
|
183 |
+
typedef psnip_uint32_t psnip_safe_uint16_larger;
|
184 |
+
|
185 |
+
#define PSNIP_SAFE_HAVE_INT32_LARGER
|
186 |
+
typedef psnip_int64_t psnip_safe_int32_larger;
|
187 |
+
typedef psnip_uint64_t psnip_safe_uint32_larger;
|
188 |
+
|
189 |
+
#if defined(PSNIP_SAFE_HAVE_128)
|
190 |
+
#define PSNIP_SAFE_HAVE_INT64_LARGER
|
191 |
+
typedef psnip_safe_int128_t psnip_safe_int64_larger;
|
192 |
+
typedef psnip_safe_uint128_t psnip_safe_uint64_larger;
|
193 |
+
#endif /* defined(PSNIP_SAFE_HAVE_128) */
|
194 |
+
#endif /* !defined(PSNIP_SAFE_NO_FIXED) */
|
195 |
+
|
196 |
+
#define PSNIP_SAFE_HAVE_LARGER_SCHAR
|
197 |
+
#if PSNIP_SAFE_IS_LARGER(SCHAR_MAX, SHRT_MAX)
|
198 |
+
typedef short psnip_safe_schar_larger;
|
199 |
+
#elif PSNIP_SAFE_IS_LARGER(SCHAR_MAX, INT_MAX)
|
200 |
+
typedef int psnip_safe_schar_larger;
|
201 |
+
#elif PSNIP_SAFE_IS_LARGER(SCHAR_MAX, LONG_MAX)
|
202 |
+
typedef long psnip_safe_schar_larger;
|
203 |
+
#elif PSNIP_SAFE_IS_LARGER(SCHAR_MAX, LLONG_MAX)
|
204 |
+
typedef long long psnip_safe_schar_larger;
|
205 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SCHAR_MAX, 0x7fff)
|
206 |
+
typedef psnip_int16_t psnip_safe_schar_larger;
|
207 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SCHAR_MAX, 0x7fffffffLL)
|
208 |
+
typedef psnip_int32_t psnip_safe_schar_larger;
|
209 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SCHAR_MAX, 0x7fffffffffffffffLL)
|
210 |
+
typedef psnip_int64_t psnip_safe_schar_larger;
|
211 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (SCHAR_MAX <= 0x7fffffffffffffffLL)
|
212 |
+
typedef psnip_safe_int128_t psnip_safe_schar_larger;
|
213 |
+
#else
|
214 |
+
#undef PSNIP_SAFE_HAVE_LARGER_SCHAR
|
215 |
+
#endif
|
216 |
+
|
217 |
+
#define PSNIP_SAFE_HAVE_LARGER_UCHAR
|
218 |
+
#if PSNIP_SAFE_IS_LARGER(UCHAR_MAX, USHRT_MAX)
|
219 |
+
typedef unsigned short psnip_safe_uchar_larger;
|
220 |
+
#elif PSNIP_SAFE_IS_LARGER(UCHAR_MAX, UINT_MAX)
|
221 |
+
typedef unsigned int psnip_safe_uchar_larger;
|
222 |
+
#elif PSNIP_SAFE_IS_LARGER(UCHAR_MAX, ULONG_MAX)
|
223 |
+
typedef unsigned long psnip_safe_uchar_larger;
|
224 |
+
#elif PSNIP_SAFE_IS_LARGER(UCHAR_MAX, ULLONG_MAX)
|
225 |
+
typedef unsigned long long psnip_safe_uchar_larger;
|
226 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UCHAR_MAX, 0xffffU)
|
227 |
+
typedef psnip_uint16_t psnip_safe_uchar_larger;
|
228 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UCHAR_MAX, 0xffffffffUL)
|
229 |
+
typedef psnip_uint32_t psnip_safe_uchar_larger;
|
230 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UCHAR_MAX, 0xffffffffffffffffULL)
|
231 |
+
typedef psnip_uint64_t psnip_safe_uchar_larger;
|
232 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (UCHAR_MAX <= 0xffffffffffffffffULL)
|
233 |
+
typedef psnip_safe_uint128_t psnip_safe_uchar_larger;
|
234 |
+
#else
|
235 |
+
#undef PSNIP_SAFE_HAVE_LARGER_UCHAR
|
236 |
+
#endif
|
237 |
+
|
238 |
+
#if CHAR_MIN == 0 && defined(PSNIP_SAFE_HAVE_LARGER_UCHAR)
|
239 |
+
#define PSNIP_SAFE_HAVE_LARGER_CHAR
|
240 |
+
typedef psnip_safe_uchar_larger psnip_safe_char_larger;
|
241 |
+
#elif CHAR_MIN < 0 && defined(PSNIP_SAFE_HAVE_LARGER_SCHAR)
|
242 |
+
#define PSNIP_SAFE_HAVE_LARGER_CHAR
|
243 |
+
typedef psnip_safe_schar_larger psnip_safe_char_larger;
|
244 |
+
#endif
|
245 |
+
|
246 |
+
#define PSNIP_SAFE_HAVE_LARGER_SHRT
|
247 |
+
#if PSNIP_SAFE_IS_LARGER(SHRT_MAX, INT_MAX)
|
248 |
+
typedef int psnip_safe_short_larger;
|
249 |
+
#elif PSNIP_SAFE_IS_LARGER(SHRT_MAX, LONG_MAX)
|
250 |
+
typedef long psnip_safe_short_larger;
|
251 |
+
#elif PSNIP_SAFE_IS_LARGER(SHRT_MAX, LLONG_MAX)
|
252 |
+
typedef long long psnip_safe_short_larger;
|
253 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SHRT_MAX, 0x7fff)
|
254 |
+
typedef psnip_int16_t psnip_safe_short_larger;
|
255 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SHRT_MAX, 0x7fffffffLL)
|
256 |
+
typedef psnip_int32_t psnip_safe_short_larger;
|
257 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(SHRT_MAX, 0x7fffffffffffffffLL)
|
258 |
+
typedef psnip_int64_t psnip_safe_short_larger;
|
259 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (SHRT_MAX <= 0x7fffffffffffffffLL)
|
260 |
+
typedef psnip_safe_int128_t psnip_safe_short_larger;
|
261 |
+
#else
|
262 |
+
#undef PSNIP_SAFE_HAVE_LARGER_SHRT
|
263 |
+
#endif
|
264 |
+
|
265 |
+
#define PSNIP_SAFE_HAVE_LARGER_USHRT
|
266 |
+
#if PSNIP_SAFE_IS_LARGER(USHRT_MAX, UINT_MAX)
|
267 |
+
typedef unsigned int psnip_safe_ushort_larger;
|
268 |
+
#elif PSNIP_SAFE_IS_LARGER(USHRT_MAX, ULONG_MAX)
|
269 |
+
typedef unsigned long psnip_safe_ushort_larger;
|
270 |
+
#elif PSNIP_SAFE_IS_LARGER(USHRT_MAX, ULLONG_MAX)
|
271 |
+
typedef unsigned long long psnip_safe_ushort_larger;
|
272 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(USHRT_MAX, 0xffff)
|
273 |
+
typedef psnip_uint16_t psnip_safe_ushort_larger;
|
274 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(USHRT_MAX, 0xffffffffUL)
|
275 |
+
typedef psnip_uint32_t psnip_safe_ushort_larger;
|
276 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(USHRT_MAX, 0xffffffffffffffffULL)
|
277 |
+
typedef psnip_uint64_t psnip_safe_ushort_larger;
|
278 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (USHRT_MAX <= 0xffffffffffffffffULL)
|
279 |
+
typedef psnip_safe_uint128_t psnip_safe_ushort_larger;
|
280 |
+
#else
|
281 |
+
#undef PSNIP_SAFE_HAVE_LARGER_USHRT
|
282 |
+
#endif
|
283 |
+
|
284 |
+
#define PSNIP_SAFE_HAVE_LARGER_INT
|
285 |
+
#if PSNIP_SAFE_IS_LARGER(INT_MAX, LONG_MAX)
|
286 |
+
typedef long psnip_safe_int_larger;
|
287 |
+
#elif PSNIP_SAFE_IS_LARGER(INT_MAX, LLONG_MAX)
|
288 |
+
typedef long long psnip_safe_int_larger;
|
289 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(INT_MAX, 0x7fff)
|
290 |
+
typedef psnip_int16_t psnip_safe_int_larger;
|
291 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(INT_MAX, 0x7fffffffLL)
|
292 |
+
typedef psnip_int32_t psnip_safe_int_larger;
|
293 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(INT_MAX, 0x7fffffffffffffffLL)
|
294 |
+
typedef psnip_int64_t psnip_safe_int_larger;
|
295 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (INT_MAX <= 0x7fffffffffffffffLL)
|
296 |
+
typedef psnip_safe_int128_t psnip_safe_int_larger;
|
297 |
+
#else
|
298 |
+
#undef PSNIP_SAFE_HAVE_LARGER_INT
|
299 |
+
#endif
|
300 |
+
|
301 |
+
#define PSNIP_SAFE_HAVE_LARGER_UINT
|
302 |
+
#if PSNIP_SAFE_IS_LARGER(UINT_MAX, ULONG_MAX)
|
303 |
+
typedef unsigned long psnip_safe_uint_larger;
|
304 |
+
#elif PSNIP_SAFE_IS_LARGER(UINT_MAX, ULLONG_MAX)
|
305 |
+
typedef unsigned long long psnip_safe_uint_larger;
|
306 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UINT_MAX, 0xffff)
|
307 |
+
typedef psnip_uint16_t psnip_safe_uint_larger;
|
308 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UINT_MAX, 0xffffffffUL)
|
309 |
+
typedef psnip_uint32_t psnip_safe_uint_larger;
|
310 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(UINT_MAX, 0xffffffffffffffffULL)
|
311 |
+
typedef psnip_uint64_t psnip_safe_uint_larger;
|
312 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (UINT_MAX <= 0xffffffffffffffffULL)
|
313 |
+
typedef psnip_safe_uint128_t psnip_safe_uint_larger;
|
314 |
+
#else
|
315 |
+
#undef PSNIP_SAFE_HAVE_LARGER_UINT
|
316 |
+
#endif
|
317 |
+
|
318 |
+
#define PSNIP_SAFE_HAVE_LARGER_LONG
|
319 |
+
#if PSNIP_SAFE_IS_LARGER(LONG_MAX, LLONG_MAX)
|
320 |
+
typedef long long psnip_safe_long_larger;
|
321 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LONG_MAX, 0x7fff)
|
322 |
+
typedef psnip_int16_t psnip_safe_long_larger;
|
323 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LONG_MAX, 0x7fffffffLL)
|
324 |
+
typedef psnip_int32_t psnip_safe_long_larger;
|
325 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LONG_MAX, 0x7fffffffffffffffLL)
|
326 |
+
typedef psnip_int64_t psnip_safe_long_larger;
|
327 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (LONG_MAX <= 0x7fffffffffffffffLL)
|
328 |
+
typedef psnip_safe_int128_t psnip_safe_long_larger;
|
329 |
+
#else
|
330 |
+
#undef PSNIP_SAFE_HAVE_LARGER_LONG
|
331 |
+
#endif
|
332 |
+
|
333 |
+
#define PSNIP_SAFE_HAVE_LARGER_ULONG
|
334 |
+
#if PSNIP_SAFE_IS_LARGER(ULONG_MAX, ULLONG_MAX)
|
335 |
+
typedef unsigned long long psnip_safe_ulong_larger;
|
336 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULONG_MAX, 0xffff)
|
337 |
+
typedef psnip_uint16_t psnip_safe_ulong_larger;
|
338 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULONG_MAX, 0xffffffffUL)
|
339 |
+
typedef psnip_uint32_t psnip_safe_ulong_larger;
|
340 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULONG_MAX, 0xffffffffffffffffULL)
|
341 |
+
typedef psnip_uint64_t psnip_safe_ulong_larger;
|
342 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (ULONG_MAX <= 0xffffffffffffffffULL)
|
343 |
+
typedef psnip_safe_uint128_t psnip_safe_ulong_larger;
|
344 |
+
#else
|
345 |
+
#undef PSNIP_SAFE_HAVE_LARGER_ULONG
|
346 |
+
#endif
|
347 |
+
|
348 |
+
#define PSNIP_SAFE_HAVE_LARGER_LLONG
|
349 |
+
#if !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LLONG_MAX, 0x7fff)
|
350 |
+
typedef psnip_int16_t psnip_safe_llong_larger;
|
351 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LLONG_MAX, 0x7fffffffLL)
|
352 |
+
typedef psnip_int32_t psnip_safe_llong_larger;
|
353 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(LLONG_MAX, 0x7fffffffffffffffLL)
|
354 |
+
typedef psnip_int64_t psnip_safe_llong_larger;
|
355 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (LLONG_MAX <= 0x7fffffffffffffffLL)
|
356 |
+
typedef psnip_safe_int128_t psnip_safe_llong_larger;
|
357 |
+
#else
|
358 |
+
#undef PSNIP_SAFE_HAVE_LARGER_LLONG
|
359 |
+
#endif
|
360 |
+
|
361 |
+
#define PSNIP_SAFE_HAVE_LARGER_ULLONG
|
362 |
+
#if !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULLONG_MAX, 0xffff)
|
363 |
+
typedef psnip_uint16_t psnip_safe_ullong_larger;
|
364 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULLONG_MAX, 0xffffffffUL)
|
365 |
+
typedef psnip_uint32_t psnip_safe_ullong_larger;
|
366 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(ULLONG_MAX, 0xffffffffffffffffULL)
|
367 |
+
typedef psnip_uint64_t psnip_safe_ullong_larger;
|
368 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (ULLONG_MAX <= 0xffffffffffffffffULL)
|
369 |
+
typedef psnip_safe_uint128_t psnip_safe_ullong_larger;
|
370 |
+
#else
|
371 |
+
#undef PSNIP_SAFE_HAVE_LARGER_ULLONG
|
372 |
+
#endif
|
373 |
+
|
374 |
+
#if defined(PSNIP_SAFE_SIZE_MAX)
|
375 |
+
#define PSNIP_SAFE_HAVE_LARGER_SIZE
|
376 |
+
#if PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, USHRT_MAX)
|
377 |
+
typedef unsigned short psnip_safe_size_larger;
|
378 |
+
#elif PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, UINT_MAX)
|
379 |
+
typedef unsigned int psnip_safe_size_larger;
|
380 |
+
#elif PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, ULONG_MAX)
|
381 |
+
typedef unsigned long psnip_safe_size_larger;
|
382 |
+
#elif PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, ULLONG_MAX)
|
383 |
+
typedef unsigned long long psnip_safe_size_larger;
|
384 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, 0xffff)
|
385 |
+
typedef psnip_uint16_t psnip_safe_size_larger;
|
386 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, 0xffffffffUL)
|
387 |
+
typedef psnip_uint32_t psnip_safe_size_larger;
|
388 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && PSNIP_SAFE_IS_LARGER(PSNIP_SAFE_SIZE_MAX, 0xffffffffffffffffULL)
|
389 |
+
typedef psnip_uint64_t psnip_safe_size_larger;
|
390 |
+
#elif !defined(PSNIP_SAFE_NO_FIXED) && defined(PSNIP_SAFE_HAVE_128) && (PSNIP_SAFE_SIZE_MAX <= 0xffffffffffffffffULL)
|
391 |
+
typedef psnip_safe_uint128_t psnip_safe_size_larger;
|
392 |
+
#else
|
393 |
+
#undef PSNIP_SAFE_HAVE_LARGER_SIZE
|
394 |
+
#endif
|
395 |
+
#endif
|
396 |
+
|
397 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_SCHAR)
|
398 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(signed char, schar)
|
399 |
+
#endif
|
400 |
+
|
401 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_UCHAR)
|
402 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(unsigned char, uchar)
|
403 |
+
#endif
|
404 |
+
|
405 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_CHAR)
|
406 |
+
#if CHAR_MIN == 0
|
407 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(char, char)
|
408 |
+
#else
|
409 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(char, char)
|
410 |
+
#endif
|
411 |
+
#endif
|
412 |
+
|
413 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_SHORT)
|
414 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(short, short)
|
415 |
+
#endif
|
416 |
+
|
417 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_USHORT)
|
418 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(unsigned short, ushort)
|
419 |
+
#endif
|
420 |
+
|
421 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_INT)
|
422 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(int, int)
|
423 |
+
#endif
|
424 |
+
|
425 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_UINT)
|
426 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(unsigned int, uint)
|
427 |
+
#endif
|
428 |
+
|
429 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_LONG)
|
430 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(long, long)
|
431 |
+
#endif
|
432 |
+
|
433 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_ULONG)
|
434 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(unsigned long, ulong)
|
435 |
+
#endif
|
436 |
+
|
437 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_LLONG)
|
438 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(long long, llong)
|
439 |
+
#endif
|
440 |
+
|
441 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_ULLONG)
|
442 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(unsigned long long, ullong)
|
443 |
+
#endif
|
444 |
+
|
445 |
+
#if defined(PSNIP_SAFE_HAVE_LARGER_SIZE)
|
446 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(size_t, size)
|
447 |
+
#endif
|
448 |
+
|
449 |
+
#if !defined(PSNIP_SAFE_NO_FIXED)
|
450 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(psnip_int8_t, int8)
|
451 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(psnip_uint8_t, uint8)
|
452 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(psnip_int16_t, int16)
|
453 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(psnip_uint16_t, uint16)
|
454 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(psnip_int32_t, int32)
|
455 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(psnip_uint32_t, uint32)
|
456 |
+
#if defined(PSNIP_SAFE_HAVE_128)
|
457 |
+
PSNIP_SAFE_DEFINE_LARGER_SIGNED_OPS(psnip_int64_t, int64)
|
458 |
+
PSNIP_SAFE_DEFINE_LARGER_UNSIGNED_OPS(psnip_uint64_t, uint64)
|
459 |
+
#endif
|
460 |
+
#endif
|
461 |
+
|
462 |
+
#endif /* !defined(PSNIP_SAFE_NO_PROMOTIONS) */
|
463 |
+
|
464 |
+
#define PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(T, name, op_name) \
|
465 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
466 |
+
psnip_safe_##name##_##op_name(T* res, T a, T b) { \
|
467 |
+
return !__builtin_##op_name##_overflow(a, b, res); \
|
468 |
+
}
|
469 |
+
|
470 |
+
#define PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(T, name, op_name, min, max) \
|
471 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
472 |
+
psnip_safe_##name##_##op_name(T* res, T a, T b) { \
|
473 |
+
const psnip_safe_##name##_larger r = psnip_safe_larger_##name##_##op_name(a, b); \
|
474 |
+
*res = (T) r; \
|
475 |
+
return (r >= min) && (r <= max); \
|
476 |
+
}
|
477 |
+
|
478 |
+
#define PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(T, name, op_name, max) \
|
479 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
480 |
+
psnip_safe_##name##_##op_name(T* res, T a, T b) { \
|
481 |
+
const psnip_safe_##name##_larger r = psnip_safe_larger_##name##_##op_name(a, b); \
|
482 |
+
*res = (T) r; \
|
483 |
+
return (r <= max); \
|
484 |
+
}
|
485 |
+
|
486 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_ADD(T, name, min, max) \
|
487 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
488 |
+
psnip_safe_##name##_add (T* res, T a, T b) { \
|
489 |
+
psnip_safe_bool r = !( ((b > 0) && (a > (max - b))) || \
|
490 |
+
((b < 0) && (a < (min - b))) ); \
|
491 |
+
if(PSNIP_SAFE_LIKELY(r)) \
|
492 |
+
*res = a + b; \
|
493 |
+
return r; \
|
494 |
+
}
|
495 |
+
|
496 |
+
#define PSNIP_SAFE_DEFINE_UNSIGNED_ADD(T, name, max) \
|
497 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
498 |
+
psnip_safe_##name##_add (T* res, T a, T b) { \
|
499 |
+
*res = (T) (a + b); \
|
500 |
+
return !PSNIP_SAFE_UNLIKELY((b > 0) && (a > (max - b))); \
|
501 |
+
}
|
502 |
+
|
503 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_SUB(T, name, min, max) \
|
504 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
505 |
+
psnip_safe_##name##_sub (T* res, T a, T b) { \
|
506 |
+
psnip_safe_bool r = !((b > 0 && a < (min + b)) || \
|
507 |
+
(b < 0 && a > (max + b))); \
|
508 |
+
if(PSNIP_SAFE_LIKELY(r)) \
|
509 |
+
*res = a - b; \
|
510 |
+
return r; \
|
511 |
+
}
|
512 |
+
|
513 |
+
#define PSNIP_SAFE_DEFINE_UNSIGNED_SUB(T, name, max) \
|
514 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
515 |
+
psnip_safe_##name##_sub (T* res, T a, T b) { \
|
516 |
+
*res = a - b; \
|
517 |
+
return !PSNIP_SAFE_UNLIKELY(b > a); \
|
518 |
+
}
|
519 |
+
|
520 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_MUL(T, name, min, max) \
|
521 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
522 |
+
psnip_safe_##name##_mul (T* res, T a, T b) { \
|
523 |
+
psnip_safe_bool r = 1; \
|
524 |
+
if (a > 0) { \
|
525 |
+
if (b > 0) { \
|
526 |
+
if (a > (max / b)) { \
|
527 |
+
r = 0; \
|
528 |
+
} \
|
529 |
+
} else { \
|
530 |
+
if (b < (min / a)) { \
|
531 |
+
r = 0; \
|
532 |
+
} \
|
533 |
+
} \
|
534 |
+
} else { \
|
535 |
+
if (b > 0) { \
|
536 |
+
if (a < (min / b)) { \
|
537 |
+
r = 0; \
|
538 |
+
} \
|
539 |
+
} else { \
|
540 |
+
if ( (a != 0) && (b < (max / a))) { \
|
541 |
+
r = 0; \
|
542 |
+
} \
|
543 |
+
} \
|
544 |
+
} \
|
545 |
+
if(PSNIP_SAFE_LIKELY(r)) \
|
546 |
+
*res = a * b; \
|
547 |
+
return r; \
|
548 |
+
}
|
549 |
+
|
550 |
+
#define PSNIP_SAFE_DEFINE_UNSIGNED_MUL(T, name, max) \
|
551 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
552 |
+
psnip_safe_##name##_mul (T* res, T a, T b) { \
|
553 |
+
*res = (T) (a * b); \
|
554 |
+
return !PSNIP_SAFE_UNLIKELY((a > 0) && (b > 0) && (a > (max / b))); \
|
555 |
+
}
|
556 |
+
|
557 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_DIV(T, name, min, max) \
|
558 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
559 |
+
psnip_safe_##name##_div (T* res, T a, T b) { \
|
560 |
+
if (PSNIP_SAFE_UNLIKELY(b == 0)) { \
|
561 |
+
*res = 0; \
|
562 |
+
return 0; \
|
563 |
+
} else if (PSNIP_SAFE_UNLIKELY(a == min && b == -1)) { \
|
564 |
+
*res = min; \
|
565 |
+
return 0; \
|
566 |
+
} else { \
|
567 |
+
*res = (T) (a / b); \
|
568 |
+
return 1; \
|
569 |
+
} \
|
570 |
+
}
|
571 |
+
|
572 |
+
#define PSNIP_SAFE_DEFINE_UNSIGNED_DIV(T, name, max) \
|
573 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
574 |
+
psnip_safe_##name##_div (T* res, T a, T b) { \
|
575 |
+
if (PSNIP_SAFE_UNLIKELY(b == 0)) { \
|
576 |
+
*res = 0; \
|
577 |
+
return 0; \
|
578 |
+
} else { \
|
579 |
+
*res = a / b; \
|
580 |
+
return 1; \
|
581 |
+
} \
|
582 |
+
}
|
583 |
+
|
584 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_MOD(T, name, min, max) \
|
585 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
586 |
+
psnip_safe_##name##_mod (T* res, T a, T b) { \
|
587 |
+
if (PSNIP_SAFE_UNLIKELY(b == 0)) { \
|
588 |
+
*res = 0; \
|
589 |
+
return 0; \
|
590 |
+
} else if (PSNIP_SAFE_UNLIKELY(a == min && b == -1)) { \
|
591 |
+
*res = min; \
|
592 |
+
return 0; \
|
593 |
+
} else { \
|
594 |
+
*res = (T) (a % b); \
|
595 |
+
return 1; \
|
596 |
+
} \
|
597 |
+
}
|
598 |
+
|
599 |
+
#define PSNIP_SAFE_DEFINE_UNSIGNED_MOD(T, name, max) \
|
600 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
601 |
+
psnip_safe_##name##_mod (T* res, T a, T b) { \
|
602 |
+
if (PSNIP_SAFE_UNLIKELY(b == 0)) { \
|
603 |
+
*res = 0; \
|
604 |
+
return 0; \
|
605 |
+
} else { \
|
606 |
+
*res = a % b; \
|
607 |
+
return 1; \
|
608 |
+
} \
|
609 |
+
}
|
610 |
+
|
611 |
+
#define PSNIP_SAFE_DEFINE_SIGNED_NEG(T, name, min, max) \
|
612 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
613 |
+
psnip_safe_##name##_neg (T* res, T value) { \
|
614 |
+
psnip_safe_bool r = value != min; \
|
615 |
+
*res = PSNIP_SAFE_LIKELY(r) ? -value : max; \
|
616 |
+
return r; \
|
617 |
+
}
|
618 |
+
|
619 |
+
#define PSNIP_SAFE_DEFINE_INTSAFE(T, name, op, isf) \
|
620 |
+
PSNIP_SAFE__FUNCTION psnip_safe_bool \
|
621 |
+
psnip_safe_##name##_##op (T* res, T a, T b) { \
|
622 |
+
return isf(a, b, res) == S_OK; \
|
623 |
+
}
|
624 |
+
|
625 |
+
#if CHAR_MIN == 0
|
626 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
627 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, add)
|
628 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, sub)
|
629 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, mul)
|
630 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_CHAR)
|
631 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(char, char, add, CHAR_MAX)
|
632 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(char, char, sub, CHAR_MAX)
|
633 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(char, char, mul, CHAR_MAX)
|
634 |
+
#else
|
635 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(char, char, CHAR_MAX)
|
636 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(char, char, CHAR_MAX)
|
637 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(char, char, CHAR_MAX)
|
638 |
+
#endif
|
639 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(char, char, CHAR_MAX)
|
640 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(char, char, CHAR_MAX)
|
641 |
+
#else /* CHAR_MIN != 0 */
|
642 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
643 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, add)
|
644 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, sub)
|
645 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(char, char, mul)
|
646 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_CHAR)
|
647 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(char, char, add, CHAR_MIN, CHAR_MAX)
|
648 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(char, char, sub, CHAR_MIN, CHAR_MAX)
|
649 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(char, char, mul, CHAR_MIN, CHAR_MAX)
|
650 |
+
#else
|
651 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(char, char, CHAR_MIN, CHAR_MAX)
|
652 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(char, char, CHAR_MIN, CHAR_MAX)
|
653 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(char, char, CHAR_MIN, CHAR_MAX)
|
654 |
+
#endif
|
655 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(char, char, CHAR_MIN, CHAR_MAX)
|
656 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(char, char, CHAR_MIN, CHAR_MAX)
|
657 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(char, char, CHAR_MIN, CHAR_MAX)
|
658 |
+
#endif
|
659 |
+
|
660 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
661 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(signed char, schar, add)
|
662 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(signed char, schar, sub)
|
663 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(signed char, schar, mul)
|
664 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_SCHAR)
|
665 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(signed char, schar, add, SCHAR_MIN, SCHAR_MAX)
|
666 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(signed char, schar, sub, SCHAR_MIN, SCHAR_MAX)
|
667 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(signed char, schar, mul, SCHAR_MIN, SCHAR_MAX)
|
668 |
+
#else
|
669 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
670 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
671 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
672 |
+
#endif
|
673 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
674 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
675 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(signed char, schar, SCHAR_MIN, SCHAR_MAX)
|
676 |
+
|
677 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
678 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned char, uchar, add)
|
679 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned char, uchar, sub)
|
680 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned char, uchar, mul)
|
681 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UCHAR)
|
682 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned char, uchar, add, UCHAR_MAX)
|
683 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned char, uchar, sub, UCHAR_MAX)
|
684 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned char, uchar, mul, UCHAR_MAX)
|
685 |
+
#else
|
686 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(unsigned char, uchar, UCHAR_MAX)
|
687 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(unsigned char, uchar, UCHAR_MAX)
|
688 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(unsigned char, uchar, UCHAR_MAX)
|
689 |
+
#endif
|
690 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(unsigned char, uchar, UCHAR_MAX)
|
691 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(unsigned char, uchar, UCHAR_MAX)
|
692 |
+
|
693 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
694 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(short, short, add)
|
695 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(short, short, sub)
|
696 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(short, short, mul)
|
697 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_SHORT)
|
698 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(short, short, add, SHRT_MIN, SHRT_MAX)
|
699 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(short, short, sub, SHRT_MIN, SHRT_MAX)
|
700 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(short, short, mul, SHRT_MIN, SHRT_MAX)
|
701 |
+
#else
|
702 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(short, short, SHRT_MIN, SHRT_MAX)
|
703 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(short, short, SHRT_MIN, SHRT_MAX)
|
704 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(short, short, SHRT_MIN, SHRT_MAX)
|
705 |
+
#endif
|
706 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(short, short, SHRT_MIN, SHRT_MAX)
|
707 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(short, short, SHRT_MIN, SHRT_MAX)
|
708 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(short, short, SHRT_MIN, SHRT_MAX)
|
709 |
+
|
710 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
711 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned short, ushort, add)
|
712 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned short, ushort, sub)
|
713 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned short, ushort, mul)
|
714 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
715 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned short, ushort, add, UShortAdd)
|
716 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned short, ushort, sub, UShortSub)
|
717 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned short, ushort, mul, UShortMult)
|
718 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_USHORT)
|
719 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned short, ushort, add, USHRT_MAX)
|
720 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned short, ushort, sub, USHRT_MAX)
|
721 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned short, ushort, mul, USHRT_MAX)
|
722 |
+
#else
|
723 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(unsigned short, ushort, USHRT_MAX)
|
724 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(unsigned short, ushort, USHRT_MAX)
|
725 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(unsigned short, ushort, USHRT_MAX)
|
726 |
+
#endif
|
727 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(unsigned short, ushort, USHRT_MAX)
|
728 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(unsigned short, ushort, USHRT_MAX)
|
729 |
+
|
730 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
731 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(int, int, add)
|
732 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(int, int, sub)
|
733 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(int, int, mul)
|
734 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_INT)
|
735 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(int, int, add, INT_MIN, INT_MAX)
|
736 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(int, int, sub, INT_MIN, INT_MAX)
|
737 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(int, int, mul, INT_MIN, INT_MAX)
|
738 |
+
#else
|
739 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(int, int, INT_MIN, INT_MAX)
|
740 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(int, int, INT_MIN, INT_MAX)
|
741 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(int, int, INT_MIN, INT_MAX)
|
742 |
+
#endif
|
743 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(int, int, INT_MIN, INT_MAX)
|
744 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(int, int, INT_MIN, INT_MAX)
|
745 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(int, int, INT_MIN, INT_MAX)
|
746 |
+
|
747 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
748 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned int, uint, add)
|
749 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned int, uint, sub)
|
750 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned int, uint, mul)
|
751 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
752 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned int, uint, add, UIntAdd)
|
753 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned int, uint, sub, UIntSub)
|
754 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned int, uint, mul, UIntMult)
|
755 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UINT)
|
756 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned int, uint, add, UINT_MAX)
|
757 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned int, uint, sub, UINT_MAX)
|
758 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned int, uint, mul, UINT_MAX)
|
759 |
+
#else
|
760 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(unsigned int, uint, UINT_MAX)
|
761 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(unsigned int, uint, UINT_MAX)
|
762 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(unsigned int, uint, UINT_MAX)
|
763 |
+
#endif
|
764 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(unsigned int, uint, UINT_MAX)
|
765 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(unsigned int, uint, UINT_MAX)
|
766 |
+
|
767 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
768 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long, long, add)
|
769 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long, long, sub)
|
770 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long, long, mul)
|
771 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_LONG)
|
772 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long, long, add, LONG_MIN, LONG_MAX)
|
773 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long, long, sub, LONG_MIN, LONG_MAX)
|
774 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long, long, mul, LONG_MIN, LONG_MAX)
|
775 |
+
#else
|
776 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(long, long, LONG_MIN, LONG_MAX)
|
777 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(long, long, LONG_MIN, LONG_MAX)
|
778 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(long, long, LONG_MIN, LONG_MAX)
|
779 |
+
#endif
|
780 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(long, long, LONG_MIN, LONG_MAX)
|
781 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(long, long, LONG_MIN, LONG_MAX)
|
782 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(long, long, LONG_MIN, LONG_MAX)
|
783 |
+
|
784 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
785 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long, ulong, add)
|
786 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long, ulong, sub)
|
787 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long, ulong, mul)
|
788 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
789 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long, ulong, add, ULongAdd)
|
790 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long, ulong, sub, ULongSub)
|
791 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long, ulong, mul, ULongMult)
|
792 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_ULONG)
|
793 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long, ulong, add, ULONG_MAX)
|
794 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long, ulong, sub, ULONG_MAX)
|
795 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long, ulong, mul, ULONG_MAX)
|
796 |
+
#else
|
797 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(unsigned long, ulong, ULONG_MAX)
|
798 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(unsigned long, ulong, ULONG_MAX)
|
799 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(unsigned long, ulong, ULONG_MAX)
|
800 |
+
#endif
|
801 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(unsigned long, ulong, ULONG_MAX)
|
802 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(unsigned long, ulong, ULONG_MAX)
|
803 |
+
|
804 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
805 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long long, llong, add)
|
806 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long long, llong, sub)
|
807 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(long long, llong, mul)
|
808 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_LLONG)
|
809 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long long, llong, add, LLONG_MIN, LLONG_MAX)
|
810 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long long, llong, sub, LLONG_MIN, LLONG_MAX)
|
811 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(long long, llong, mul, LLONG_MIN, LLONG_MAX)
|
812 |
+
#else
|
813 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(long long, llong, LLONG_MIN, LLONG_MAX)
|
814 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(long long, llong, LLONG_MIN, LLONG_MAX)
|
815 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(long long, llong, LLONG_MIN, LLONG_MAX)
|
816 |
+
#endif
|
817 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(long long, llong, LLONG_MIN, LLONG_MAX)
|
818 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(long long, llong, LLONG_MIN, LLONG_MAX)
|
819 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(long long, llong, LLONG_MIN, LLONG_MAX)
|
820 |
+
|
821 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
822 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long long, ullong, add)
|
823 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long long, ullong, sub)
|
824 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(unsigned long long, ullong, mul)
|
825 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
826 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long long, ullong, add, ULongLongAdd)
|
827 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long long, ullong, sub, ULongLongSub)
|
828 |
+
PSNIP_SAFE_DEFINE_INTSAFE(unsigned long long, ullong, mul, ULongLongMult)
|
829 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_ULLONG)
|
830 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long long, ullong, add, ULLONG_MAX)
|
831 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long long, ullong, sub, ULLONG_MAX)
|
832 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(unsigned long long, ullong, mul, ULLONG_MAX)
|
833 |
+
#else
|
834 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(unsigned long long, ullong, ULLONG_MAX)
|
835 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(unsigned long long, ullong, ULLONG_MAX)
|
836 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(unsigned long long, ullong, ULLONG_MAX)
|
837 |
+
#endif
|
838 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(unsigned long long, ullong, ULLONG_MAX)
|
839 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(unsigned long long, ullong, ULLONG_MAX)
|
840 |
+
|
841 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
842 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(size_t, size, add)
|
843 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(size_t, size, sub)
|
844 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(size_t, size, mul)
|
845 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H)
|
846 |
+
PSNIP_SAFE_DEFINE_INTSAFE(size_t, size, add, SizeTAdd)
|
847 |
+
PSNIP_SAFE_DEFINE_INTSAFE(size_t, size, sub, SizeTSub)
|
848 |
+
PSNIP_SAFE_DEFINE_INTSAFE(size_t, size, mul, SizeTMult)
|
849 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_SIZE)
|
850 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(size_t, size, add, PSNIP_SAFE__SIZE_MAX_RT)
|
851 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(size_t, size, sub, PSNIP_SAFE__SIZE_MAX_RT)
|
852 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(size_t, size, mul, PSNIP_SAFE__SIZE_MAX_RT)
|
853 |
+
#else
|
854 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(size_t, size, PSNIP_SAFE__SIZE_MAX_RT)
|
855 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(size_t, size, PSNIP_SAFE__SIZE_MAX_RT)
|
856 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(size_t, size, PSNIP_SAFE__SIZE_MAX_RT)
|
857 |
+
#endif
|
858 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(size_t, size, PSNIP_SAFE__SIZE_MAX_RT)
|
859 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(size_t, size, PSNIP_SAFE__SIZE_MAX_RT)
|
860 |
+
|
861 |
+
#if !defined(PSNIP_SAFE_NO_FIXED)
|
862 |
+
|
863 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
864 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int8_t, int8, add)
|
865 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int8_t, int8, sub)
|
866 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int8_t, int8, mul)
|
867 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_INT8)
|
868 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int8_t, int8, add, (-0x7fLL-1), 0x7f)
|
869 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int8_t, int8, sub, (-0x7fLL-1), 0x7f)
|
870 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int8_t, int8, mul, (-0x7fLL-1), 0x7f)
|
871 |
+
#else
|
872 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
873 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
874 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
875 |
+
#endif
|
876 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
877 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
878 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(psnip_int8_t, int8, (-0x7fLL-1), 0x7f)
|
879 |
+
|
880 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
881 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint8_t, uint8, add)
|
882 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint8_t, uint8, sub)
|
883 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint8_t, uint8, mul)
|
884 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UINT8)
|
885 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint8_t, uint8, add, 0xff)
|
886 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint8_t, uint8, sub, 0xff)
|
887 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint8_t, uint8, mul, 0xff)
|
888 |
+
#else
|
889 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(psnip_uint8_t, uint8, 0xff)
|
890 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(psnip_uint8_t, uint8, 0xff)
|
891 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(psnip_uint8_t, uint8, 0xff)
|
892 |
+
#endif
|
893 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(psnip_uint8_t, uint8, 0xff)
|
894 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(psnip_uint8_t, uint8, 0xff)
|
895 |
+
|
896 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
897 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int16_t, int16, add)
|
898 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int16_t, int16, sub)
|
899 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int16_t, int16, mul)
|
900 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_INT16)
|
901 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int16_t, int16, add, (-32767-1), 0x7fff)
|
902 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int16_t, int16, sub, (-32767-1), 0x7fff)
|
903 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int16_t, int16, mul, (-32767-1), 0x7fff)
|
904 |
+
#else
|
905 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
906 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
907 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
908 |
+
#endif
|
909 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
910 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
911 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(psnip_int16_t, int16, (-32767-1), 0x7fff)
|
912 |
+
|
913 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
914 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint16_t, uint16, add)
|
915 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint16_t, uint16, sub)
|
916 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint16_t, uint16, mul)
|
917 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H) && defined(_WIN32)
|
918 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint16_t, uint16, add, UShortAdd)
|
919 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint16_t, uint16, sub, UShortSub)
|
920 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint16_t, uint16, mul, UShortMult)
|
921 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UINT16)
|
922 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint16_t, uint16, add, 0xffff)
|
923 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint16_t, uint16, sub, 0xffff)
|
924 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint16_t, uint16, mul, 0xffff)
|
925 |
+
#else
|
926 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(psnip_uint16_t, uint16, 0xffff)
|
927 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(psnip_uint16_t, uint16, 0xffff)
|
928 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(psnip_uint16_t, uint16, 0xffff)
|
929 |
+
#endif
|
930 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(psnip_uint16_t, uint16, 0xffff)
|
931 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(psnip_uint16_t, uint16, 0xffff)
|
932 |
+
|
933 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
934 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int32_t, int32, add)
|
935 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int32_t, int32, sub)
|
936 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int32_t, int32, mul)
|
937 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_INT32)
|
938 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int32_t, int32, add, (-0x7fffffffLL-1), 0x7fffffffLL)
|
939 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int32_t, int32, sub, (-0x7fffffffLL-1), 0x7fffffffLL)
|
940 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int32_t, int32, mul, (-0x7fffffffLL-1), 0x7fffffffLL)
|
941 |
+
#else
|
942 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
943 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
944 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
945 |
+
#endif
|
946 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
947 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
948 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(psnip_int32_t, int32, (-0x7fffffffLL-1), 0x7fffffffLL)
|
949 |
+
|
950 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
951 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint32_t, uint32, add)
|
952 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint32_t, uint32, sub)
|
953 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint32_t, uint32, mul)
|
954 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H) && defined(_WIN32)
|
955 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint32_t, uint32, add, UIntAdd)
|
956 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint32_t, uint32, sub, UIntSub)
|
957 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint32_t, uint32, mul, UIntMult)
|
958 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UINT32)
|
959 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint32_t, uint32, add, 0xffffffffUL)
|
960 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint32_t, uint32, sub, 0xffffffffUL)
|
961 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint32_t, uint32, mul, 0xffffffffUL)
|
962 |
+
#else
|
963 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(psnip_uint32_t, uint32, 0xffffffffUL)
|
964 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(psnip_uint32_t, uint32, 0xffffffffUL)
|
965 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(psnip_uint32_t, uint32, 0xffffffffUL)
|
966 |
+
#endif
|
967 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(psnip_uint32_t, uint32, 0xffffffffUL)
|
968 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(psnip_uint32_t, uint32, 0xffffffffUL)
|
969 |
+
|
970 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
971 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int64_t, int64, add)
|
972 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int64_t, int64, sub)
|
973 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_int64_t, int64, mul)
|
974 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_INT64)
|
975 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int64_t, int64, add, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
976 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int64_t, int64, sub, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
977 |
+
PSNIP_SAFE_DEFINE_PROMOTED_SIGNED_BINARY_OP(psnip_int64_t, int64, mul, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
978 |
+
#else
|
979 |
+
PSNIP_SAFE_DEFINE_SIGNED_ADD(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
980 |
+
PSNIP_SAFE_DEFINE_SIGNED_SUB(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
981 |
+
PSNIP_SAFE_DEFINE_SIGNED_MUL(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
982 |
+
#endif
|
983 |
+
PSNIP_SAFE_DEFINE_SIGNED_DIV(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
984 |
+
PSNIP_SAFE_DEFINE_SIGNED_MOD(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
985 |
+
PSNIP_SAFE_DEFINE_SIGNED_NEG(psnip_int64_t, int64, (-0x7fffffffffffffffLL-1), 0x7fffffffffffffffLL)
|
986 |
+
|
987 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
988 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint64_t, uint64, add)
|
989 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint64_t, uint64, sub)
|
990 |
+
PSNIP_SAFE_DEFINE_BUILTIN_BINARY_OP(psnip_uint64_t, uint64, mul)
|
991 |
+
#elif defined(PSNIP_SAFE_HAVE_INTSAFE_H) && defined(_WIN32)
|
992 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint64_t, uint64, add, ULongLongAdd)
|
993 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint64_t, uint64, sub, ULongLongSub)
|
994 |
+
PSNIP_SAFE_DEFINE_INTSAFE(psnip_uint64_t, uint64, mul, ULongLongMult)
|
995 |
+
#elif defined(PSNIP_SAFE_HAVE_LARGER_UINT64)
|
996 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint64_t, uint64, add, 0xffffffffffffffffULL)
|
997 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint64_t, uint64, sub, 0xffffffffffffffffULL)
|
998 |
+
PSNIP_SAFE_DEFINE_PROMOTED_UNSIGNED_BINARY_OP(psnip_uint64_t, uint64, mul, 0xffffffffffffffffULL)
|
999 |
+
#else
|
1000 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_ADD(psnip_uint64_t, uint64, 0xffffffffffffffffULL)
|
1001 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_SUB(psnip_uint64_t, uint64, 0xffffffffffffffffULL)
|
1002 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MUL(psnip_uint64_t, uint64, 0xffffffffffffffffULL)
|
1003 |
+
#endif
|
1004 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_DIV(psnip_uint64_t, uint64, 0xffffffffffffffffULL)
|
1005 |
+
PSNIP_SAFE_DEFINE_UNSIGNED_MOD(psnip_uint64_t, uint64, 0xffffffffffffffffULL)
|
1006 |
+
|
1007 |
+
#endif /* !defined(PSNIP_SAFE_NO_FIXED) */
|
1008 |
+
|
1009 |
+
#define PSNIP_SAFE_C11_GENERIC_SELECTION(res, op) \
|
1010 |
+
_Generic((*res), \
|
1011 |
+
char: psnip_safe_char_##op, \
|
1012 |
+
unsigned char: psnip_safe_uchar_##op, \
|
1013 |
+
short: psnip_safe_short_##op, \
|
1014 |
+
unsigned short: psnip_safe_ushort_##op, \
|
1015 |
+
int: psnip_safe_int_##op, \
|
1016 |
+
unsigned int: psnip_safe_uint_##op, \
|
1017 |
+
long: psnip_safe_long_##op, \
|
1018 |
+
unsigned long: psnip_safe_ulong_##op, \
|
1019 |
+
long long: psnip_safe_llong_##op, \
|
1020 |
+
unsigned long long: psnip_safe_ullong_##op)
|
1021 |
+
|
1022 |
+
#define PSNIP_SAFE_C11_GENERIC_BINARY_OP(op, res, a, b) \
|
1023 |
+
PSNIP_SAFE_C11_GENERIC_SELECTION(res, op)(res, a, b)
|
1024 |
+
#define PSNIP_SAFE_C11_GENERIC_UNARY_OP(op, res, v) \
|
1025 |
+
PSNIP_SAFE_C11_GENERIC_SELECTION(res, op)(res, v)
|
1026 |
+
|
1027 |
+
#if defined(PSNIP_SAFE_HAVE_BUILTIN_OVERFLOW)
|
1028 |
+
#define psnip_safe_add(res, a, b) !__builtin_add_overflow(a, b, res)
|
1029 |
+
#define psnip_safe_sub(res, a, b) !__builtin_sub_overflow(a, b, res)
|
1030 |
+
#define psnip_safe_mul(res, a, b) !__builtin_mul_overflow(a, b, res)
|
1031 |
+
#define psnip_safe_div(res, a, b) !__builtin_div_overflow(a, b, res)
|
1032 |
+
#define psnip_safe_mod(res, a, b) !__builtin_mod_overflow(a, b, res)
|
1033 |
+
#define psnip_safe_neg(res, v) PSNIP_SAFE_C11_GENERIC_UNARY_OP (neg, res, v)
|
1034 |
+
|
1035 |
+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
|
1036 |
+
/* The are no fixed-length or size selections because they cause an
|
1037 |
+
* error about _Generic specifying two compatible types. Hopefully
|
1038 |
+
* this doesn't cause problems on exotic platforms, but if it does
|
1039 |
+
* please let me know and I'll try to figure something out. */
|
1040 |
+
|
1041 |
+
#define psnip_safe_add(res, a, b) PSNIP_SAFE_C11_GENERIC_BINARY_OP(add, res, a, b)
|
1042 |
+
#define psnip_safe_sub(res, a, b) PSNIP_SAFE_C11_GENERIC_BINARY_OP(sub, res, a, b)
|
1043 |
+
#define psnip_safe_mul(res, a, b) PSNIP_SAFE_C11_GENERIC_BINARY_OP(mul, res, a, b)
|
1044 |
+
#define psnip_safe_div(res, a, b) PSNIP_SAFE_C11_GENERIC_BINARY_OP(div, res, a, b)
|
1045 |
+
#define psnip_safe_mod(res, a, b) PSNIP_SAFE_C11_GENERIC_BINARY_OP(mod, res, a, b)
|
1046 |
+
#define psnip_safe_neg(res, v) PSNIP_SAFE_C11_GENERIC_UNARY_OP (neg, res, v)
|
1047 |
+
#endif
|
1048 |
+
|
1049 |
+
#if !defined(PSNIP_SAFE_HAVE_BUILTINS) && (defined(PSNIP_SAFE_EMULATE_NATIVE) || defined(PSNIP_BUILTIN_EMULATE_NATIVE))
|
1050 |
+
# define __builtin_sadd_overflow(a, b, res) (!psnip_safe_int_add(res, a, b))
|
1051 |
+
# define __builtin_saddl_overflow(a, b, res) (!psnip_safe_long_add(res, a, b))
|
1052 |
+
# define __builtin_saddll_overflow(a, b, res) (!psnip_safe_llong_add(res, a, b))
|
1053 |
+
# define __builtin_uadd_overflow(a, b, res) (!psnip_safe_uint_add(res, a, b))
|
1054 |
+
# define __builtin_uaddl_overflow(a, b, res) (!psnip_safe_ulong_add(res, a, b))
|
1055 |
+
# define __builtin_uaddll_overflow(a, b, res) (!psnip_safe_ullong_add(res, a, b))
|
1056 |
+
|
1057 |
+
# define __builtin_ssub_overflow(a, b, res) (!psnip_safe_int_sub(res, a, b))
|
1058 |
+
# define __builtin_ssubl_overflow(a, b, res) (!psnip_safe_long_sub(res, a, b))
|
1059 |
+
# define __builtin_ssubll_overflow(a, b, res) (!psnip_safe_llong_sub(res, a, b))
|
1060 |
+
# define __builtin_usub_overflow(a, b, res) (!psnip_safe_uint_sub(res, a, b))
|
1061 |
+
# define __builtin_usubl_overflow(a, b, res) (!psnip_safe_ulong_sub(res, a, b))
|
1062 |
+
# define __builtin_usubll_overflow(a, b, res) (!psnip_safe_ullong_sub(res, a, b))
|
1063 |
+
|
1064 |
+
# define __builtin_smul_overflow(a, b, res) (!psnip_safe_int_mul(res, a, b))
|
1065 |
+
# define __builtin_smull_overflow(a, b, res) (!psnip_safe_long_mul(res, a, b))
|
1066 |
+
# define __builtin_smulll_overflow(a, b, res) (!psnip_safe_llong_mul(res, a, b))
|
1067 |
+
# define __builtin_umul_overflow(a, b, res) (!psnip_safe_uint_mul(res, a, b))
|
1068 |
+
# define __builtin_umull_overflow(a, b, res) (!psnip_safe_ulong_mul(res, a, b))
|
1069 |
+
# define __builtin_umulll_overflow(a, b, res) (!psnip_safe_ullong_mul(res, a, b))
|
1070 |
+
#endif
|
1071 |
+
|
1072 |
+
#endif /* !defined(PSNIP_SAFE_H) */
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/strptime.h
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <time.h>
|
21 |
+
|
22 |
+
#include "arrow/util/visibility.h"
|
23 |
+
|
24 |
+
#ifdef __cplusplus
|
25 |
+
extern "C" {
|
26 |
+
#endif
|
27 |
+
|
28 |
+
// A less featureful implementation of strptime() for platforms lacking
|
29 |
+
// a standard implementation (e.g. Windows).
|
30 |
+
ARROW_EXPORT char* arrow_strptime(const char* __restrict, const char* __restrict,
|
31 |
+
struct tm* __restrict);
|
32 |
+
|
33 |
+
#ifdef __cplusplus
|
34 |
+
} // extern "C"
|
35 |
+
#endif
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/xxhash.h
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#include "arrow/vendored/xxhash/xxhash.h"
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/arrow/vendored/xxhash/xxhash.h
ADDED
The diff for this file is too large to render.
See raw diff
|
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/benchmark_util.h
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <random>
|
21 |
+
#include <string>
|
22 |
+
#include <vector>
|
23 |
+
|
24 |
+
#include "parquet/types.h"
|
25 |
+
|
26 |
+
namespace parquet::benchmark {
|
27 |
+
|
28 |
+
template <typename T>
|
29 |
+
void GenerateBenchmarkData(uint32_t size, uint32_t seed, T* data,
|
30 |
+
std::vector<uint8_t>* heap, uint32_t data_string_length);
|
31 |
+
|
32 |
+
#define _GENERATE_BENCHMARK_DATA_DECL(KLASS) \
|
33 |
+
template <> \
|
34 |
+
void GenerateBenchmarkData(uint32_t size, uint32_t seed, KLASS* data, \
|
35 |
+
std::vector<uint8_t>* heap, uint32_t data_string_length);
|
36 |
+
|
37 |
+
_GENERATE_BENCHMARK_DATA_DECL(int32_t)
|
38 |
+
_GENERATE_BENCHMARK_DATA_DECL(int64_t)
|
39 |
+
_GENERATE_BENCHMARK_DATA_DECL(float)
|
40 |
+
_GENERATE_BENCHMARK_DATA_DECL(double)
|
41 |
+
_GENERATE_BENCHMARK_DATA_DECL(ByteArray)
|
42 |
+
_GENERATE_BENCHMARK_DATA_DECL(FLBA)
|
43 |
+
_GENERATE_BENCHMARK_DATA_DECL(Int96)
|
44 |
+
|
45 |
+
#undef _GENERATE_BENCHMARK_DATA_DECL
|
46 |
+
|
47 |
+
} // namespace parquet::benchmark
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/bloom_filter_reader.h
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pragma once
|
19 |
+
|
20 |
+
#include "arrow/io/interfaces.h"
|
21 |
+
#include "parquet/properties.h"
|
22 |
+
#include "parquet/type_fwd.h"
|
23 |
+
|
24 |
+
namespace parquet {
|
25 |
+
|
26 |
+
class InternalFileDecryptor;
|
27 |
+
class BloomFilter;
|
28 |
+
|
29 |
+
class PARQUET_EXPORT RowGroupBloomFilterReader {
|
30 |
+
public:
|
31 |
+
virtual ~RowGroupBloomFilterReader() = default;
|
32 |
+
|
33 |
+
/// \brief Read bloom filter of a column chunk.
|
34 |
+
///
|
35 |
+
/// \param[in] i column ordinal of the column chunk.
|
36 |
+
/// \returns bloom filter of the column or nullptr if it does not exist.
|
37 |
+
/// \throws ParquetException if the index is out of bound, or read bloom
|
38 |
+
/// filter failed.
|
39 |
+
virtual std::unique_ptr<BloomFilter> GetColumnBloomFilter(int i) = 0;
|
40 |
+
};
|
41 |
+
|
42 |
+
/// \brief Interface for reading the bloom filter for a Parquet file.
|
43 |
+
class PARQUET_EXPORT BloomFilterReader {
|
44 |
+
public:
|
45 |
+
virtual ~BloomFilterReader() = default;
|
46 |
+
|
47 |
+
/// \brief Create a BloomFilterReader instance.
|
48 |
+
/// \returns a BloomFilterReader instance.
|
49 |
+
/// WARNING: The returned BloomFilterReader references to all the input parameters, so
|
50 |
+
/// it must not outlive all of the input parameters. Usually these input parameters
|
51 |
+
/// come from the same ParquetFileReader object, so it must not outlive the reader
|
52 |
+
/// that creates this BloomFilterReader.
|
53 |
+
static std::unique_ptr<BloomFilterReader> Make(
|
54 |
+
std::shared_ptr<::arrow::io::RandomAccessFile> input,
|
55 |
+
std::shared_ptr<FileMetaData> file_metadata, const ReaderProperties& properties,
|
56 |
+
std::shared_ptr<InternalFileDecryptor> file_decryptor = NULLPTR);
|
57 |
+
|
58 |
+
/// \brief Get the bloom filter reader of a specific row group.
|
59 |
+
/// \param[in] i row group ordinal to get bloom filter reader.
|
60 |
+
/// \returns RowGroupBloomFilterReader of the specified row group. A nullptr may or may
|
61 |
+
/// not be returned if the bloom filter for the row group is unavailable. It
|
62 |
+
/// is the caller's responsibility to check the return value of follow-up calls
|
63 |
+
/// to the RowGroupBloomFilterReader.
|
64 |
+
/// \throws ParquetException if the index is out of bound.
|
65 |
+
virtual std::shared_ptr<RowGroupBloomFilterReader> RowGroup(int i) = 0;
|
66 |
+
};
|
67 |
+
|
68 |
+
} // namespace parquet
|
llmeval-env/lib/python3.10/site-packages/pyarrow/include/parquet/column_page.h
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// This module defines an abstract interface for iterating through pages in a
|
19 |
+
// Parquet column chunk within a row group. It could be extended in the future
|
20 |
+
// to iterate through all data pages in all chunks in a file.
|
21 |
+
|
22 |
+
#pragma once
|
23 |
+
|
24 |
+
#include <cstdint>
|
25 |
+
#include <memory>
|
26 |
+
#include <optional>
|
27 |
+
#include <string>
|
28 |
+
|
29 |
+
#include "parquet/statistics.h"
|
30 |
+
#include "parquet/types.h"
|
31 |
+
|
32 |
+
namespace parquet {
|
33 |
+
|
34 |
+
// TODO: Parallel processing is not yet safe because of memory-ownership
|
35 |
+
// semantics (the PageReader may or may not own the memory referenced by a
|
36 |
+
// page)
|
37 |
+
//
|
38 |
+
// TODO(wesm): In the future Parquet implementations may store the crc code
|
39 |
+
// in format::PageHeader. parquet-mr currently does not, so we also skip it
|
40 |
+
// here, both on the read and write path
|
41 |
+
class Page {
|
42 |
+
public:
|
43 |
+
Page(const std::shared_ptr<Buffer>& buffer, PageType::type type)
|
44 |
+
: buffer_(buffer), type_(type) {}
|
45 |
+
|
46 |
+
PageType::type type() const { return type_; }
|
47 |
+
|
48 |
+
std::shared_ptr<Buffer> buffer() const { return buffer_; }
|
49 |
+
|
50 |
+
// @returns: a pointer to the page's data
|
51 |
+
const uint8_t* data() const { return buffer_->data(); }
|
52 |
+
|
53 |
+
// @returns: the total size in bytes of the page's data buffer
|
54 |
+
int32_t size() const { return static_cast<int32_t>(buffer_->size()); }
|
55 |
+
|
56 |
+
private:
|
57 |
+
std::shared_ptr<Buffer> buffer_;
|
58 |
+
PageType::type type_;
|
59 |
+
};
|
60 |
+
|
61 |
+
/// \brief Base type for DataPageV1 and DataPageV2 including common attributes
|
62 |
+
class DataPage : public Page {
|
63 |
+
public:
|
64 |
+
int32_t num_values() const { return num_values_; }
|
65 |
+
Encoding::type encoding() const { return encoding_; }
|
66 |
+
int64_t uncompressed_size() const { return uncompressed_size_; }
|
67 |
+
const EncodedStatistics& statistics() const { return statistics_; }
|
68 |
+
/// Return the row ordinal within the row group to the first row in the data page.
|
69 |
+
/// Currently it is only present from data pages created by ColumnWriter in order
|
70 |
+
/// to collect page index.
|
71 |
+
std::optional<int64_t> first_row_index() const { return first_row_index_; }
|
72 |
+
|
73 |
+
virtual ~DataPage() = default;
|
74 |
+
|
75 |
+
protected:
|
76 |
+
DataPage(PageType::type type, const std::shared_ptr<Buffer>& buffer, int32_t num_values,
|
77 |
+
Encoding::type encoding, int64_t uncompressed_size,
|
78 |
+
const EncodedStatistics& statistics = EncodedStatistics(),
|
79 |
+
std::optional<int64_t> first_row_index = std::nullopt)
|
80 |
+
: Page(buffer, type),
|
81 |
+
num_values_(num_values),
|
82 |
+
encoding_(encoding),
|
83 |
+
uncompressed_size_(uncompressed_size),
|
84 |
+
statistics_(statistics),
|
85 |
+
first_row_index_(std::move(first_row_index)) {}
|
86 |
+
|
87 |
+
int32_t num_values_;
|
88 |
+
Encoding::type encoding_;
|
89 |
+
int64_t uncompressed_size_;
|
90 |
+
EncodedStatistics statistics_;
|
91 |
+
/// Row ordinal within the row group to the first row in the data page.
|
92 |
+
std::optional<int64_t> first_row_index_;
|
93 |
+
};
|
94 |
+
|
95 |
+
class DataPageV1 : public DataPage {
|
96 |
+
public:
|
97 |
+
DataPageV1(const std::shared_ptr<Buffer>& buffer, int32_t num_values,
|
98 |
+
Encoding::type encoding, Encoding::type definition_level_encoding,
|
99 |
+
Encoding::type repetition_level_encoding, int64_t uncompressed_size,
|
100 |
+
const EncodedStatistics& statistics = EncodedStatistics(),
|
101 |
+
std::optional<int64_t> first_row_index = std::nullopt)
|
102 |
+
: DataPage(PageType::DATA_PAGE, buffer, num_values, encoding, uncompressed_size,
|
103 |
+
statistics, std::move(first_row_index)),
|
104 |
+
definition_level_encoding_(definition_level_encoding),
|
105 |
+
repetition_level_encoding_(repetition_level_encoding) {}
|
106 |
+
|
107 |
+
Encoding::type repetition_level_encoding() const { return repetition_level_encoding_; }
|
108 |
+
|
109 |
+
Encoding::type definition_level_encoding() const { return definition_level_encoding_; }
|
110 |
+
|
111 |
+
private:
|
112 |
+
Encoding::type definition_level_encoding_;
|
113 |
+
Encoding::type repetition_level_encoding_;
|
114 |
+
};
|
115 |
+
|
116 |
+
class DataPageV2 : public DataPage {
|
117 |
+
public:
|
118 |
+
DataPageV2(const std::shared_ptr<Buffer>& buffer, int32_t num_values, int32_t num_nulls,
|
119 |
+
int32_t num_rows, Encoding::type encoding,
|
120 |
+
int32_t definition_levels_byte_length, int32_t repetition_levels_byte_length,
|
121 |
+
int64_t uncompressed_size, bool is_compressed = false,
|
122 |
+
const EncodedStatistics& statistics = EncodedStatistics(),
|
123 |
+
std::optional<int64_t> first_row_index = std::nullopt)
|
124 |
+
: DataPage(PageType::DATA_PAGE_V2, buffer, num_values, encoding, uncompressed_size,
|
125 |
+
statistics, std::move(first_row_index)),
|
126 |
+
num_nulls_(num_nulls),
|
127 |
+
num_rows_(num_rows),
|
128 |
+
definition_levels_byte_length_(definition_levels_byte_length),
|
129 |
+
repetition_levels_byte_length_(repetition_levels_byte_length),
|
130 |
+
is_compressed_(is_compressed) {}
|
131 |
+
|
132 |
+
int32_t num_nulls() const { return num_nulls_; }
|
133 |
+
|
134 |
+
int32_t num_rows() const { return num_rows_; }
|
135 |
+
|
136 |
+
int32_t definition_levels_byte_length() const { return definition_levels_byte_length_; }
|
137 |
+
|
138 |
+
int32_t repetition_levels_byte_length() const { return repetition_levels_byte_length_; }
|
139 |
+
|
140 |
+
bool is_compressed() const { return is_compressed_; }
|
141 |
+
|
142 |
+
private:
|
143 |
+
int32_t num_nulls_;
|
144 |
+
int32_t num_rows_;
|
145 |
+
int32_t definition_levels_byte_length_;
|
146 |
+
int32_t repetition_levels_byte_length_;
|
147 |
+
bool is_compressed_;
|
148 |
+
};
|
149 |
+
|
150 |
+
class DictionaryPage : public Page {
|
151 |
+
public:
|
152 |
+
DictionaryPage(const std::shared_ptr<Buffer>& buffer, int32_t num_values,
|
153 |
+
Encoding::type encoding, bool is_sorted = false)
|
154 |
+
: Page(buffer, PageType::DICTIONARY_PAGE),
|
155 |
+
num_values_(num_values),
|
156 |
+
encoding_(encoding),
|
157 |
+
is_sorted_(is_sorted) {}
|
158 |
+
|
159 |
+
int32_t num_values() const { return num_values_; }
|
160 |
+
|
161 |
+
Encoding::type encoding() const { return encoding_; }
|
162 |
+
|
163 |
+
bool is_sorted() const { return is_sorted_; }
|
164 |
+
|
165 |
+
private:
|
166 |
+
int32_t num_values_;
|
167 |
+
Encoding::type encoding_;
|
168 |
+
bool is_sorted_;
|
169 |
+
};
|
170 |
+
|
171 |
+
} // namespace parquet
|