Skip to content

Commit

Permalink
Merge pull request #197 from rdeioris/ReadDoubleVector
Browse files Browse the repository at this point in the history
Implemented ReadDoubleVector instead of ReadDoubleArray for crate::CrateDataTypeId::CRATE_DATA_TYPE_DOUBLE_VECTOR
  • Loading branch information
syoyo authored Oct 24, 2024
2 parents 8e7af44 + 7b0b498 commit 0693cae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/crate-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,11 @@ bool CrateReader::ReadDoubleArray(bool is_compressed, std::vector<double> *d) {
length = size_t(n);
}

if (length == 0) {
d->clear();
return true;
}

if (length > _config.maxArrayElements) {
PUSH_ERROR_AND_RETURN_TAG(kTag, "Too many array elements.");
}
Expand Down Expand Up @@ -760,6 +765,40 @@ bool CrateReader::ReadDoubleArray(bool is_compressed, std::vector<double> *d) {
}
}

bool CrateReader::ReadDoubleVector(std::vector<double> *d) {
size_t length;

uint64_t n;
if (!_sr->read8(&n)) {
_err += "Failed to read the number of array elements.\n";
return false;
}

length = size_t(n);

if (length == 0) {
d->clear();
return true;
}

if (length > _config.maxArrayElements) {
PUSH_ERROR_AND_RETURN_TAG(kTag, "Too many array elements.");
}

CHECK_MEMORY_USAGE(length * sizeof(double));

d->resize(length);

// TODO(syoyo): Zero-copy
if (!_sr->read(sizeof(double) * length, sizeof(double) * length,
reinterpret_cast<uint8_t *>(d->data()))) {
_err += "Failed to read double vector data.\n";
return false;
}

return true;
}

bool CrateReader::ReadTimeSamples(value::TimeSamples *d) {

// Layout
Expand Down Expand Up @@ -4113,7 +4152,7 @@ bool CrateReader::UnpackValueRep(const crate::ValueRep &rep,
}
case crate::CrateDataTypeId::CRATE_DATA_TYPE_DOUBLE_VECTOR: {
std::vector<double> v;
if (!ReadDoubleArray(rep.IsCompressed(), &v)) {
if (!ReadDoubleVector(&v)) {
_err += "Failed to read DoubleVector value\n";
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/crate-reader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ class CrateReader {
bool ReadFloatArray(bool is_compressed, std::vector<float> *d);
bool ReadDoubleArray(bool is_compressed, std::vector<double> *d);

bool ReadDoubleVector(std::vector<double> *d);

// template <class T>
// struct IsIntType {
// static const bool value =
Expand Down

0 comments on commit 0693cae

Please sign in to comment.