Skip to content

Commit

Permalink
Implement typed_array (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET authored Apr 19, 2024
1 parent bef873a commit 3f996ab
Show file tree
Hide file tree
Showing 15 changed files with 1,098 additions and 20 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,22 @@ endif()
# =====

set(SPARROW_HEADERS
${SPARROW_INCLUDE_DIR}/sparrow/algorithm.hpp
${SPARROW_INCLUDE_DIR}/sparrow/allocator.hpp
${SPARROW_INCLUDE_DIR}/sparrow/array_data.hpp
${SPARROW_INCLUDE_DIR}/sparrow/buffer.hpp
${SPARROW_INCLUDE_DIR}/sparrow/buffer_view.hpp
${SPARROW_INCLUDE_DIR}/sparrow/contracts.hpp
${SPARROW_INCLUDE_DIR}/sparrow/data_type.hpp
${SPARROW_INCLUDE_DIR}/sparrow/config.hpp
${SPARROW_INCLUDE_DIR}/sparrow/contracts.hpp
${SPARROW_INCLUDE_DIR}/sparrow/data_traits.hpp
${SPARROW_INCLUDE_DIR}/sparrow/data_type.hpp
${SPARROW_INCLUDE_DIR}/sparrow/dynamic_bitset.hpp
${SPARROW_INCLUDE_DIR}/sparrow/fixed_size_layout.hpp
${SPARROW_INCLUDE_DIR}/sparrow/iterator.hpp
${SPARROW_INCLUDE_DIR}/sparrow/memory.hpp
${SPARROW_INCLUDE_DIR}/sparrow/mp_utils.hpp
${SPARROW_INCLUDE_DIR}/sparrow/sparrow_version.hpp
${SPARROW_INCLUDE_DIR}/sparrow/typed_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/variable_size_binary_layout.hpp

${SPARROW_INCLUDE_DIR}/sparrow/details/3rdparty/float16_t.hpp
Expand Down
126 changes: 126 additions & 0 deletions include/sparrow/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2024 Man Group Operations Limited

Check notice on line 1 in include/sparrow/algorithm.hpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on include/sparrow/algorithm.hpp

File include/sparrow/algorithm.hpp does not conform to Custom style guidelines. (lines 17)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <algorithm>
#include <compare>
#include <concepts>
#include "sparrow/config.hpp"

namespace sparrow
{
#if COMPILING_WITH_APPLE_CLANG

template <typename T>
concept OrdCategory = std::same_as<T, std::strong_ordering> || std::same_as<T, std::weak_ordering>
|| std::same_as<T, std::partial_ordering>;

template <class R1, class R2, class Cmp>
concept LexicographicalComparable = requires(const R1& r1, const R2& r2, Cmp comp) {
{ r1.cbegin() } -> std::input_or_output_iterator;
{ r2.cbegin() } -> std::input_or_output_iterator;
OrdCategory<decltype(comp(*r1.cbegin(), *r2.cbegin()))>;
};

template <class R1, class R2, class Cmp>
requires LexicographicalComparable<R1, R2, Cmp>
constexpr auto lexicographical_compare_three_way_non_std(const R1& range1, const R2& range2, Cmp comp)
-> decltype(comp(*range1.cbegin(), *range1.cbegin()))
{
auto iter_1 = range1.cbegin();
const auto end_1 = range1.cend();
auto iter_2 = range2.cbegin();
const auto end_2 = range2.cend();

while (true)
{
if (iter_1 == end_1)
{
return iter_2 == end_2 ? std::strong_ordering::equal : std::strong_ordering::less;
}

if (iter_2 == end_2)
{
return std::strong_ordering::greater;
}

if (const auto result = comp(*iter_1, *iter_2); result != 0)
{
return result;
}

++iter_1;
++iter_2;
}
}
#endif

template <class R1, class R2, class Cmp>
constexpr auto lexicographical_compare_three_way(const R1& range1, const R2& range2, Cmp comp)
-> decltype(comp(*range1.cbegin(), *range2.cbegin()))
{
#if COMPILING_WITH_APPLE_CLANG
return lexicographical_compare_three_way_non_std(range1, range2, comp);
#else
return std::lexicographical_compare_three_way(
range1.cbegin(),
range1.cend(),
range2.cbegin(),
range2.cend(),
comp
);
#endif
}

#if COMPILING_WITH_APPLE_CLANG
struct compare_three_way
{
template <class T, class U>
constexpr auto operator()(const T& t, const U& u) const noexcept -> std::partial_ordering
{
if (t < u)
{
return std::partial_ordering::less;
}
if (u < t)
{
return std::partial_ordering::greater;
}
return std::partial_ordering::equivalent;
}
};
#endif

template <class R1, class R2>
constexpr auto lexicographical_compare_three_way(const R1& r1, const R2& r2) -> std::partial_ordering
{
return lexicographical_compare_three_way<R1, R2>(
r1,
r2,
#if COMPILING_WITH_APPLE_CLANG
compare_three_way {}
#else
std::compare_three_way{}
#endif
);
}

template <class R1, class R2>
constexpr auto lexicographical_compare(const R1& r1, const R2& r2) -> bool
{
return lexicographical_compare_three_way(r1, r2) == std::strong_ordering::less;
}

} // namespace sparrow
22 changes: 16 additions & 6 deletions include/sparrow/array_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ namespace sparrow
bool operator==(const reference_proxy_base<D>& lhs, std::nullopt_t);

template <class D1, class D2>
std::strong_ordering operator<=>(const reference_proxy_base<D1>& lhs, const reference_proxy_base<D2>& rhs);
auto operator<=>(const reference_proxy_base<D1>& lhs, const reference_proxy_base<D2>& rhs);

template <class D, not_ref_proxy T>
std::strong_ordering operator<=>(const reference_proxy_base<D>& lhs, const T& rhs);
std::partial_ordering operator<=>(const reference_proxy_base<D>& lhs, const T& rhs);

template <class D>
std::strong_ordering operator<=>(const reference_proxy_base<D>& lhs, std::nullopt_t);
Expand Down Expand Up @@ -282,17 +282,27 @@ namespace sparrow
}

template <class D1, class D2>
std::strong_ordering operator<=>(const reference_proxy_base<D1>& lhs, const reference_proxy_base<D2>& rhs)
auto operator<=>(const reference_proxy_base<D1>& lhs, const reference_proxy_base<D2>& rhs)
{
const D1& dlhs = lhs.derived_cast();
const D2& drhs = rhs.derived_cast();
return (dlhs && drhs) ? (dlhs.value() <=> drhs.value()) : (dlhs.has_value() <=> drhs.has_value());

using TOrdering = decltype(dlhs.value() <=> drhs.value());
if (dlhs && drhs)
{
return dlhs.value() <=> drhs.value();
}
return TOrdering(dlhs.has_value() <=> drhs.has_value());
}

template <class D, not_ref_proxy T>
std::strong_ordering operator<=>(const reference_proxy_base<D>& lhs, const T& rhs)
std::partial_ordering operator<=>(const reference_proxy_base<D>& lhs, const T& rhs)
{
return lhs.derived_cast() ? (lhs.derived_cast().value() <=> rhs) : std::strong_ordering::less;
if (lhs.derived_cast())
{
return lhs.derived_cast().value() <=> rhs;
}
return std::partial_ordering::less;
}

template <class D>
Expand Down
26 changes: 26 additions & 0 deletions include/sparrow/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 Man Group Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#if defined(__apple_build_version__)
#define COMPILING_WITH_APPLE_CLANG 1
#else
#define COMPILING_WITH_APPLE_CLANG 0
#endif

consteval bool is_apple_compiler()
{
return static_cast<bool>(COMPILING_WITH_APPLE_CLANG);
}
10 changes: 5 additions & 5 deletions include/sparrow/data_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include "data_type.hpp"
#include "fixed_size_layout.hpp"
#include "variable_size_binary_layout.hpp"
#include "sparrow/data_type.hpp"
#include "sparrow/fixed_size_layout.hpp"
#include "sparrow/variable_size_binary_layout.hpp"

namespace sparrow
{
Expand Down Expand Up @@ -114,15 +114,15 @@ namespace sparrow
{
static constexpr data_type type_id = data_type::STRING;
using value_type = std::string;
using default_layout = variable_size_binary_layout<value_type, std::string_view, std::string_view>; // FIXME: this is incorrect, change when we have the right types
using default_layout = variable_size_binary_layout<value_type, std::string_view, const std::string_view>; // FIXME: this is incorrect, change when we have the right types
};

template <>
struct arrow_traits<std::vector<byte_t>>
{
static constexpr data_type type_id = data_type::STRING;
using value_type = std::vector<byte_t>;
using default_layout = variable_size_binary_layout<value_type, std::span<byte_t>, std::span<byte_t>>; // FIXME: this is incorrect, change when we have the right types
using default_layout = variable_size_binary_layout<value_type, std::span<byte_t>, const std::span<byte_t>>; // FIXME: this is incorrect, change when we have the right types
};

namespace predicate
Expand Down
14 changes: 13 additions & 1 deletion include/sparrow/details/3rdparty/float16_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
// PLEASE UPDATE THIS COMMENT IF YOU REPLACE THIS
// SEE README.md for rational
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Modification from the original:
// - Added of the <=> operator
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef FLOAT16_T_HPP_INCLUDED_OSDIJSALKJS8OU4LKJAFSOIUASFD98U3LJKASFOIJFFDDDDDF
#define FLOAT16_T_HPP_INCLUDED_OSDIJSALKJS8OU4LKJAFSOIUASFD98U3LJKASFOIJFFDDDDDF
//
// inspired by:
// https://github.com/acgessler/half_float
// https://github.com/x448/float16
//
#include <compare>
#include <cstdint>
#include <limits>
#include <iostream>
Expand Down Expand Up @@ -786,6 +789,15 @@ namespace numeric
return !( lhs == rhs );
}

// Added by Alexis Placet from Quantstack
constexpr std::partial_ordering operator <=> ( float16_t lhs, float16_t rhs ) noexcept
{
if ( lhs < rhs ) return std::partial_ordering::less;
if ( lhs > rhs ) return std::partial_ordering::greater;
if ( lhs == rhs ) return std::partial_ordering::equivalent;
return std::partial_ordering::unordered;
}

template<typename CharT, class Traits>
std::basic_ostream<CharT, Traits>& operator << ( std::basic_ostream<CharT, Traits>& os, float16_t const& f )
{
Expand Down
2 changes: 1 addition & 1 deletion include/sparrow/fixed_size_layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace sparrow
using self_type = fixed_size_layout<T>;
using inner_value_type = T;
using inner_reference = inner_value_type&;
using inner_const_reference = const inner_reference;
using inner_const_reference = const inner_value_type&;
using bitmap_type = array_data::bitmap_type;
using bitmap_reference = typename bitmap_type::reference;
using bitmap_const_reference = typename bitmap_type::const_reference;
Expand Down
Loading

0 comments on commit 3f996ab

Please sign in to comment.