Skip to content

Commit

Permalink
Use a macro for cases
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 24, 2017
1 parent 1cc43ce commit 535f9ff
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions cpp/src/arrow/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,34 +367,25 @@ DecimalArray::DecimalArray(const std::shared_ptr<DataType>& type, int64_t length
std::make_shared<ArrayData>(type, length, std::move(buffers), null_count, offset));
}

const uint8_t* DecimalArray::GetValue(int64_t i) const {
return raw_values_ + (i + data_->offset) * byte_width();
}
#define DECIMAL_TO_STRING_CASE(bits, bytes, precision, scale) \
case bits: { \
decimal::Decimal##bits value; \
decimal::FromBytes((bytes), &value); \
return decimal::ToString(value, (precision), (scale)); \
}

std::string DecimalArray::FormatValue(int64_t i) const {
const auto& type_ = static_cast<const DecimalType&>(*type());
const int precision = type_.precision();
const int scale = type_.scale();
const int byte_width = type_.byte_width();
const uint8_t* bytes = raw_values_ + (i + data_->offset) * byte_width;
switch (byte_width) {
case 4: {
decimal::Decimal32 value;
decimal::FromBytes(bytes, &value);
return decimal::ToString(value, precision, scale);
}
case 8: {
decimal::Decimal64 value;
decimal::FromBytes(bytes, &value);
return decimal::ToString(value, precision, scale);
}
case 16: {
decimal::Decimal128 value;
decimal::FromBytes(bytes, &value);
return decimal::ToString(value, precision, scale);
}
const int bit_width = type_.bit_width();
const uint8_t* bytes = GetValue(i);
switch (bit_width) {
DECIMAL_TO_STRING_CASE(32, bytes, precision, scale)
DECIMAL_TO_STRING_CASE(64, bytes, precision, scale)
DECIMAL_TO_STRING_CASE(128, bytes, precision, scale)
default: {
DCHECK(false) << "Invalid byte width: " << byte_width;
DCHECK(false) << "Invalid bit width: " << bit_width;
return "";
}
}
Expand Down

0 comments on commit 535f9ff

Please sign in to comment.