Skip to content

Commit

Permalink
Refine code for fixed-length types array (#28109)
Browse files Browse the repository at this point in the history
Signed-off-by: Cai Zhang <[email protected]>
  • Loading branch information
xiaocai2333 authored Nov 5, 2023
1 parent 218a988 commit 5c06f29
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions internal/core/src/common/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ class Array {
length_ = field_data.bool_data().data().size();
auto data = new bool[length_];
size_ = length_;
offsets_.reserve(length_);
for (int i = 0; i < length_; ++i) {
data[i] = field_data.bool_data().data(i);
offsets_.push_back(sizeof(bool) * i);
}
data_ = reinterpret_cast<char*>(data);
break;
Expand All @@ -56,11 +54,9 @@ class Array {
length_ = field_data.int_data().data().size();
size_ = length_ * sizeof(int32_t);
data_ = new char[size_];
offsets_.reserve(length_);
for (int i = 0; i < length_; ++i) {
reinterpret_cast<int*>(data_)[i] =
field_data.int_data().data(i);
offsets_.push_back(sizeof(int32_t) * i);
}
break;
}
Expand All @@ -69,11 +65,9 @@ class Array {
length_ = field_data.long_data().data().size();
size_ = length_ * sizeof(int64_t);
data_ = new char[size_];
offsets_.reserve(length_);
for (int i = 0; i < length_; ++i) {
reinterpret_cast<int64_t*>(data_)[i] =
field_data.long_data().data(i);
offsets_.push_back(sizeof(int64_t) * i);
}
break;
}
Expand All @@ -82,11 +76,9 @@ class Array {
length_ = field_data.float_data().data().size();
size_ = length_ * sizeof(float);
data_ = new char[size_];
offsets_.reserve(length_);
for (int i = 0; i < length_; ++i) {
reinterpret_cast<float*>(data_)[i] =
field_data.float_data().data(i);
offsets_.push_back(sizeof(float) * i);
}
break;
}
Expand All @@ -95,11 +87,9 @@ class Array {
length_ = field_data.double_data().data().size();
size_ = length_ * sizeof(double);
data_ = new char[size_];
offsets_.reserve(length_);
for (int i = 0; i < length_; ++i) {
reinterpret_cast<double*>(data_)[i] =
field_data.double_data().data(i);
offsets_.push_back(sizeof(double) * i);
}
break;
}
Expand Down Expand Up @@ -238,11 +228,11 @@ class Array {
index >= 0 && index < length_,
fmt::format(
"index out of range, index={}, length={}", index, length_));
size_t element_length = (index == length_ - 1)
? size_ - offsets_.back()
: offsets_[index + 1] - offsets_[index];
if constexpr (std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view>) {
size_t element_length = (index == length_ - 1)
? size_ - offsets_.back()
: offsets_[index + 1] - offsets_[index];
return T(data_ + offsets_[index], element_length);
}
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, int64_t> ||
Expand Down Expand Up @@ -455,11 +445,12 @@ class ArrayView {
index >= 0 && index < length_,
fmt::format(
"index out of range, index={}, length={}", index, length_));
size_t element_length = (index == length_ - 1)
? size_ - offsets_.back()
: offsets_[index + 1] - offsets_[index];

if constexpr (std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view>) {
size_t element_length = (index == length_ - 1)
? size_ - offsets_.back()
: offsets_[index + 1] - offsets_[index];
return T(data_ + offsets_[index], element_length);
}
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, int64_t> ||
Expand Down

0 comments on commit 5c06f29

Please sign in to comment.