From 26ace9d7640cc5a2d9aa437034177ea4190e215f Mon Sep 17 00:00:00 2001 From: DerThorsten Date: Mon, 16 Dec 2024 07:49:31 +0100 Subject: [PATCH] cleanup --- CMakeLists.txt | 4 - environment-dev.yml | 2 - examples/CMakeLists.txt | 1 - include/sparrow/utils/decimal.hpp | 236 +++++++++++++++------------- src/arrow_interface/arrow_array.cpp | 1 - 5 files changed, 131 insertions(+), 113 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e20ae9260..96a310995 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 # ===== diff --git a/environment-dev.yml b/environment-dev.yml index 374178387..6c5cb4835 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -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 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7133f4f0f..abcb0efc8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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 diff --git a/include/sparrow/utils/decimal.hpp b/include/sparrow/utils/decimal.hpp index 9bc077f80..640833337 100644 --- a/include/sparrow/utils/decimal.hpp +++ b/include/sparrow/utils/decimal.hpp @@ -14,136 +14,162 @@ namespace sparrow { - - - - template class decimal { public: using integer_type = T; - decimal() requires(!is_int_placeholder_v) - : m_value(0) - , m_scale() - { - } - decimal() requires(is_int_placeholder_v) - : m_value() - , m_scale() - { - } - + decimal() requires(!is_int_placeholder_v); + decimal() requires(is_int_placeholder_v); + 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); + explicit operator double() const requires(!is_int_placeholder_v); + explicit operator long double() const requires(!is_int_placeholder_v); + // convert to string + explicit operator std::string() const requires(!is_int_placeholder_v); + + const T & storage() const; + + int scale() const; + + private: + template + FLOAT_TYPE convert_to_floating_point() const requires(!is_int_placeholder_v); - 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 + constexpr bool is_decimal_v = mpl::is_type_instance_of_v; + + template + decimal::decimal() requires(!is_int_placeholder_v) + : m_value(0) + , m_scale() + { + } + + template + decimal::decimal() requires(is_int_placeholder_v) + : m_value() + , m_scale() + { + } + + template + decimal::decimal(T value, int scale) + : m_value(value) + , m_scale(scale) + { + } + + template + bool decimal::operator==(const decimal& other) const + { + return m_value == other.m_value && m_scale == other.m_scale; + } + + template + bool decimal::operator!=(const decimal& other) const + { + return !(*this == other); + } + + template + decimal::operator float() const requires(!is_int_placeholder_v) + { + return convert_to_floating_point(); + } - explicit operator float() const requires(!is_int_placeholder_v) + template + decimal::operator double() const requires(!is_int_placeholder_v) + { + return convert_to_floating_point(); + } + + template + decimal::operator long double() const requires(!is_int_placeholder_v) + { + return convert_to_floating_point(); + } + + template + decimal::operator std::string() const requires(!is_int_placeholder_v) + { + std::stringstream ss; + ss << m_value; + std::string result = ss.str(); + if( m_scale == 0 ) { - return convert_to_floating_point(); + return result; } - explicit operator double() const requires(!is_int_placeholder_v) + if(result[0] == '0') { - return convert_to_floating_point(); + return "0"; } - explicit operator long double() const requires(!is_int_placeholder_v) + // remove - (we handle it later) + if(result[0] == '-') { - return convert_to_floating_point(); + result = result.substr(1); } - // convert to string - explicit operator std::string() const requires(!is_int_placeholder_v) - { - 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(m_scale)) { - result.insert(0, std::string(static_cast(m_scale) + 1 - result.length(), '0')); // Pad with leading zeros - } - std::size_t int_part_len = result.length() - static_cast(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(-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(m_scale)) { + result.insert(0, std::string(static_cast(m_scale) + 1 - result.length(), '0')); // Pad with leading zeros } - return result; + std::size_t int_part_len = result.length() - static_cast(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(-m_scale), '0'); // Append zeros } - - int scale() const + // handle negative values + if(m_value < 0) { - return m_scale; + result.insert(0, 1, '-'); } - - private: - template - FLOAT_TYPE convert_to_floating_point() const requires(!is_int_placeholder_v) - { - using to_type = FLOAT_TYPE; - if constexpr( std::is_same_v ) - { - // danger zone - auto val = static_cast(m_value); - return static_cast(val) / static_cast(std::pow(10, m_scale)); - } - else - { - return static_cast(m_value) / static_cast(std::pow(10, m_scale)); - } - } - - - T m_value; - //int m_precision; - int m_scale; - }; + return result; + } template - constexpr bool is_decimal_v = mpl::is_type_instance_of_v; + const T & decimal::storage() const + { + return m_value; + } + + template + int decimal::scale() const + { + return m_scale; + } + + template + template + FLOAT_TYPE decimal::convert_to_floating_point() const requires(!is_int_placeholder_v) + { + using to_type = FLOAT_TYPE; + if constexpr( std::is_same_v ) + { + // danger zone + auto val = static_cast(m_value); + return static_cast(val) / static_cast(std::pow(10, m_scale)); + } + else + { + return static_cast(m_value) / static_cast(std::pow(10, m_scale)); + } + } } // namespace sparrow \ No newline at end of file diff --git a/src/arrow_interface/arrow_array.cpp b/src/arrow_interface/arrow_array.cpp index 3d683a64a..74301a710 100644 --- a/src/arrow_interface/arrow_array.cpp +++ b/src/arrow_interface/arrow_array.cpp @@ -47,7 +47,6 @@ namespace sparrow return const_cast(static_cast(ptr)); } - // get the bit width for fixed width binary from format