Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DerThorsten committed Dec 16, 2024
1 parent 3d61e48 commit 26ace9d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 113 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ if(USE_LARGE_INT_PLACEHOLDERS)
add_compile_definitions(SPARROW_USE_LARGE_INT_PLACEHOLDERS)
endif()

if (DISABLE_LARGE_INTEGER_DECIMALS)
add_compile_definitions(SPARROW_DISABLE_LARGE_INTEGER_DECIMALS)
endif()

# Build
# =====

Expand Down
2 changes: 0 additions & 2 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ dependencies:
# For now, we use HowardHinnant/date as a replacement if we are compiling with libc++.
# TODO: remove this once libc++ has full support for P0355R7.
- howardhinnant_date
# for large integers
- boost
# Documentation
- doxygen
- graphviz
1 change: 0 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ SET(EXAMPLES
typed_array_high_level.cpp
record_batch_example.cpp
builder_example.cpp
# decimal.cpp
)

# custom target to run all examples
Expand Down
236 changes: 131 additions & 105 deletions include/sparrow/utils/decimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,136 +14,162 @@

namespace sparrow
{




template<class T>
class decimal
{
public:
using integer_type = T;

decimal() requires(!is_int_placeholder_v<T>)
: m_value(0)
, m_scale()
{
}
decimal() requires(is_int_placeholder_v<T>)
: m_value()
, m_scale()
{
}

decimal() requires(!is_int_placeholder_v<T>);
decimal() requires(is_int_placeholder_v<T>);
decimal(T value, int scale);

decimal(T value, int scale)
: m_value(value)
, m_scale(scale)
{
}
bool operator==(const decimal& other) const;
bool operator!=(const decimal& other) const;
explicit operator float() const requires(!is_int_placeholder_v<T>);
explicit operator double() const requires(!is_int_placeholder_v<T>);
explicit operator long double() const requires(!is_int_placeholder_v<T>);

// convert to string
explicit operator std::string() const requires(!is_int_placeholder_v<T>);

const T & storage() const;

int scale() const;

private:
template<class FLOAT_TYPE>
FLOAT_TYPE convert_to_floating_point() const requires(!is_int_placeholder_v<T>);

bool operator==(const decimal& other) const
{
return m_value == other.m_value && m_scale == other.m_scale;
}

// operator !=
bool operator!=(const decimal& other) const
{
return !(*this == other);
}
T m_value;
//int m_precision;
int m_scale;
};

template <typename T>
constexpr bool is_decimal_v = mpl::is_type_instance_of_v<T, decimal>;

template <typename T>
decimal<T>::decimal() requires(!is_int_placeholder_v<T>)
: m_value(0)
, m_scale()
{
}

template <typename T>
decimal<T>::decimal() requires(is_int_placeholder_v<T>)
: m_value()
, m_scale()
{
}

template <typename T>
decimal<T>::decimal(T value, int scale)
: m_value(value)
, m_scale(scale)
{
}

template <typename T>
bool decimal<T>::operator==(const decimal& other) const
{
return m_value == other.m_value && m_scale == other.m_scale;
}

template <typename T>
bool decimal<T>::operator!=(const decimal& other) const
{
return !(*this == other);
}

template <typename T>
decimal<T>::operator float() const requires(!is_int_placeholder_v<T>)
{
return convert_to_floating_point<float>();
}

explicit operator float() const requires(!is_int_placeholder_v<T>)
template <typename T>
decimal<T>::operator double() const requires(!is_int_placeholder_v<T>)
{
return convert_to_floating_point<double>();
}

template <typename T>
decimal<T>::operator long double() const requires(!is_int_placeholder_v<T>)
{
return convert_to_floating_point<long double>();
}

template <typename T>
decimal<T>::operator std::string() const requires(!is_int_placeholder_v<T>)
{
std::stringstream ss;
ss << m_value;
std::string result = ss.str();
if( m_scale == 0 )
{
return convert_to_floating_point<float>();
return result;
}
explicit operator double() const requires(!is_int_placeholder_v<T>)
if(result[0] == '0')
{
return convert_to_floating_point<double>();
return "0";
}
explicit operator long double() const requires(!is_int_placeholder_v<T>)
// remove - (we handle it later)
if(result[0] == '-')
{
return convert_to_floating_point<long double>();
result = result.substr(1);
}

// convert to string
explicit operator std::string() const requires(!is_int_placeholder_v<T>)
{
std::stringstream ss;
ss << m_value;
std::string result = ss.str();
if( m_scale == 0 )
{
return result;
}
if(result[0] == '0')
{
return "0";
}
// remove - (we handle it later)
if(result[0] == '-')
{
result = result.substr(1);
}

if (m_scale > 0)
{
if (result.length() <= static_cast<std::size_t>(m_scale)) {
result.insert(0, std::string(static_cast<std::size_t>(m_scale) + 1 - result.length(), '0')); // Pad with leading zeros
}
std::size_t int_part_len = result.length() - static_cast<std::size_t>(m_scale);
std::string int_part = result.substr(0, int_part_len);
std::string frac_part = result.substr(int_part_len);
result = int_part + "." + frac_part;
}
else
{
result += std::string(static_cast<std::size_t>(-m_scale), '0'); // Append zeros
}
// handle negative values
if(m_value < 0)
{
result.insert(0, 1, '-');
if (m_scale > 0)
{
if (result.length() <= static_cast<std::size_t>(m_scale)) {
result.insert(0, std::string(static_cast<std::size_t>(m_scale) + 1 - result.length(), '0')); // Pad with leading zeros
}
return result;
std::size_t int_part_len = result.length() - static_cast<std::size_t>(m_scale);
std::string int_part = result.substr(0, int_part_len);
std::string frac_part = result.substr(int_part_len);
result = int_part + "." + frac_part;
}


const T & storage() const
else
{
return m_value;
result += std::string(static_cast<std::size_t>(-m_scale), '0'); // Append zeros
}

int scale() const
// handle negative values
if(m_value < 0)
{
return m_scale;
result.insert(0, 1, '-');
}

private:
template<class FLOAT_TYPE>
FLOAT_TYPE convert_to_floating_point() const requires(!is_int_placeholder_v<T>)
{
using to_type = FLOAT_TYPE;
if constexpr( std::is_same_v<T, int256_t> )
{
// danger zone
auto val = static_cast<int128_t>(m_value);
return static_cast<to_type>(val) / static_cast<to_type>(std::pow(10, m_scale));
}
else
{
return static_cast<to_type>(m_value) / static_cast<to_type>(std::pow(10, m_scale));
}
}


T m_value;
//int m_precision;
int m_scale;
};
return result;
}

template <typename T>
constexpr bool is_decimal_v = mpl::is_type_instance_of_v<T, decimal>;
const T & decimal<T>::storage() const
{
return m_value;
}

template <typename T>
int decimal<T>::scale() const
{
return m_scale;
}

template <typename T>
template<class FLOAT_TYPE>
FLOAT_TYPE decimal<T>::convert_to_floating_point() const requires(!is_int_placeholder_v<T>)
{
using to_type = FLOAT_TYPE;
if constexpr( std::is_same_v<T, int256_t> )
{
// danger zone
auto val = static_cast<int128_t>(m_value);
return static_cast<to_type>(val) / static_cast<to_type>(std::pow(10, m_scale));
}
else
{
return static_cast<to_type>(m_value) / static_cast<to_type>(std::pow(10, m_scale));
}
}

} // namespace sparrow
1 change: 0 additions & 1 deletion src/arrow_interface/arrow_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace sparrow
return const_cast<T*>(static_cast<const T*>(ptr));
}




// get the bit width for fixed width binary from format
Expand Down

0 comments on commit 26ace9d

Please sign in to comment.