28 #include "graphar/result.h"
30 #define REGULAR_SEPARATOR "_"
45 Array() : data_(
nullptr), size_(0) {}
46 Array(
const T* data,
size_t size) : data_(data), size_(size) {}
49 Array& operator=(
const Array& other) =
default;
53 const T& operator[](
size_t index)
const {
return data_[index]; }
55 const T* data()
const {
return data_; }
57 size_t size()
const {
return size_; }
64 bool empty()
const {
return size_ == 0; }
66 void swap(
Array& other) {
67 std::swap(data_, other.data_);
68 std::swap(size_, other.size_);
71 const T* begin()
const {
return data_; }
73 const T* end()
const {
return data_ + size_; }
81 class Array<std::string_view> final {
83 using ValueType = std::string_view;
87 const int32_t* offsets_;
92 explicit iterator(
const int32_t* offsets,
const uint8_t* data,
size_t index)
93 : offsets_(offsets), data_(data), index_(index) {}
95 const std::string_view operator*()
const {
96 return std::string_view(
97 reinterpret_cast<const char*
>(data_ + offsets_[index_]),
98 offsets_[index_ + 1] - offsets_[index_]);
101 iterator& operator++() {
106 iterator operator++(
int) {
return iterator(offsets_, data_, index_++); }
108 iterator operator+(
size_t n) {
109 return iterator(offsets_, data_, index_ + n);
112 bool operator==(
const iterator& other)
const {
113 return index_ == other.index_;
115 bool operator!=(
const iterator& other)
const {
116 return index_ != other.index_;
119 Array() : offsets_(nullptr), data_(nullptr), size_(0) {}
120 explicit Array(
const int32_t* offsets,
const uint8_t* data,
size_t size)
121 : offsets_(offsets), data_(data), size_(size) {}
123 const std::string_view operator[](
size_t index)
const {
124 return std::string_view(
125 reinterpret_cast<const char*
>(data_ + offsets_[index]),
126 offsets_[index + 1] - offsets_[index]);
129 const int32_t* offsets()
const {
return offsets_; }
130 const uint8_t* data()
const {
return data_; }
132 size_t size()
const {
return size_; }
140 bool empty()
const {
return size_ == 0; }
142 void swap(Array& other) {
143 std::swap(offsets_, other.offsets_);
144 std::swap(data_, other.data_);
145 std::swap(size_, other.size_);
148 const iterator begin()
const {
return iterator(offsets_, data_, 0); }
149 const iterator end()
const {
return iterator(offsets_, data_, size_); }
152 const int32_t* offsets_;
153 const uint8_t* data_;
157 using Int32Array = Array<int32_t>;
158 using Int64Array = Array<int64_t>;
159 using FloatArray = Array<float>;
160 using DoubleArray = Array<double>;
161 using StringArray = Array<std::string_view>;
165 namespace graphar::util {
169 : edge_chunk_nums_(std::move(edge_chunk_nums)) {}
170 IdType IndexPairToGlobalChunkIndex(IdType vertex_chunk_index,
171 IdType edge_chunk_index) {
172 IdType global_edge_chunk_index = 0;
173 for (IdType i = 0; i < vertex_chunk_index; ++i) {
174 global_edge_chunk_index += edge_chunk_nums_[i];
176 return global_edge_chunk_index + edge_chunk_index;
180 std::pair<IdType, IdType> GlobalChunkIndexToIndexPair(IdType global_index) {
181 std::pair<IdType, IdType> index_pair(0, 0);
182 for (
size_t i = 0; i < edge_chunk_nums_.size(); ++i) {
183 if (global_index < edge_chunk_nums_[i]) {
184 index_pair.first =
static_cast<IdType
>(i);
185 index_pair.second = global_index;
188 global_index -= edge_chunk_nums_[i];
194 std::vector<IdType> edge_chunk_nums_;
197 static inline IdType IndexPairToGlobalChunkIndex(
198 const std::vector<IdType>& edge_chunk_nums, IdType vertex_chunk_index,
199 IdType edge_chunk_index) {
200 IdType global_edge_chunk_index = 0;
201 for (IdType i = 0; i < vertex_chunk_index; ++i) {
202 global_edge_chunk_index += edge_chunk_nums[i];
204 return global_edge_chunk_index + edge_chunk_index;
208 static inline std::pair<IdType, IdType> GlobalChunkIndexToIndexPair(
209 const std::vector<IdType>& edge_chunk_nums, IdType global_index) {
210 std::pair<IdType, IdType> index_pair(0, 0);
211 for (
size_t i = 0; i < edge_chunk_nums.size(); ++i) {
212 if (global_index < edge_chunk_nums[i]) {
213 index_pair.first =
static_cast<IdType
>(i);
214 index_pair.second = global_index;
217 global_index -= edge_chunk_nums[i];
222 std::shared_ptr<arrow::ChunkedArray> GetArrowColumnByName(
223 std::shared_ptr<arrow::Table>
const& table,
const std::string& name);
225 std::shared_ptr<arrow::Array> GetArrowArrayByChunkIndex(
226 std::shared_ptr<arrow::ChunkedArray>
const& chunk_array,
227 int64_t chunk_index);
229 Result<const void*> GetArrowArrayData(
230 std::shared_ptr<arrow::Array>
const& array);
232 static inline std::string ConcatStringWithDelimiter(
233 const std::vector<std::string>& str_vec,
const std::string& delimiter) {
234 return std::accumulate(
235 std::begin(str_vec), std::end(str_vec), std::string(),
236 [&delimiter](
const std::string& ss,
const std::string& s) {
237 return ss.empty() ? s : ss + delimiter + s;
241 template <
typename T>
243 inline static T Value(
const void* data, int64_t offset) {
244 return reinterpret_cast<const T*
>(data)[offset];
250 static std::string Value(
const void* data, int64_t offset);