Refine code for fixed-length types array (#28108)

Signed-off-by: Cai Zhang <cai.zhang@zilliz.com>
This commit is contained in:
cai.zhang 2023-11-03 00:40:14 +08:00 committed by GitHub
parent dc89730a50
commit fc2df9514f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}
@ -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> ||
@ -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> ||