Skip to content

Commit

Permalink
Union (#238)
Browse files Browse the repository at this point in the history
Implemented union layouts
  • Loading branch information
DerThorsten authored Oct 16, 2024
1 parent 3708666 commit ad70c5d
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@
# Clangd cache
.cache
CMakeUserPresets.json

# MacOS
.DS_Store
38 changes: 13 additions & 25 deletions include/sparrow/layout/array_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "sparrow/layout/layout_iterator.hpp"
#include "sparrow/utils/nullable.hpp"
#include "sparrow/utils/iterator.hpp"
#include "sparrow/utils/crtp_base.hpp"

namespace sparrow
{
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace sparrow
* implements comme interface for arrays with a bitmap.
*/
template <class D>
class array_crtp_base
class array_crtp_base : public crtp_base<D>
{
public:
using self_type = array_crtp_base<D>;
Expand Down Expand Up @@ -122,8 +123,6 @@ namespace sparrow
const_bitmap_iterator bitmap_begin() const;
const_bitmap_iterator bitmap_end() const;

derived_type& derived_cast();
const derived_type& derived_cast() const;

private:

Expand Down Expand Up @@ -155,33 +154,33 @@ namespace sparrow
template <class D>
auto array_crtp_base<D>::operator[](size_type i) -> reference
{
SPARROW_ASSERT_TRUE(i < derived_cast().size());
SPARROW_ASSERT_TRUE(i < this->derived_cast().size());
return reference(
inner_reference(derived_cast().value(i)),
derived_cast().has_value(i)
inner_reference(this->derived_cast().value(i)),
this->derived_cast().has_value(i)
);
}

template <class D>
auto array_crtp_base<D>::operator[](size_type i) const -> const_reference
{
SPARROW_ASSERT_TRUE(i < derived_cast().size());
SPARROW_ASSERT_TRUE(i < this->derived_cast().size());
return const_reference(
inner_const_reference(derived_cast().value(i)),
derived_cast().has_value(i)
inner_const_reference(this->derived_cast().value(i)),
this->derived_cast().has_value(i)
);
}

template <class D>
auto array_crtp_base<D>::begin() -> iterator
{
return iterator(derived_cast().value_begin(), derived_cast().bitmap_begin());
return iterator(this->derived_cast().value_begin(), this->derived_cast().bitmap_begin());
}

template <class D>
auto array_crtp_base<D>::end() -> iterator
{
return iterator(derived_cast().value_end(), derived_cast().bitmap_end());
return iterator(this->derived_cast().value_end(), this->derived_cast().bitmap_end());
}

template <class D>
Expand All @@ -199,13 +198,13 @@ namespace sparrow
template <class D>
auto array_crtp_base<D>::cbegin() const -> const_iterator
{
return const_iterator(derived_cast().value_cbegin(), derived_cast().bitmap_begin());
return const_iterator(this->derived_cast().value_cbegin(), this->derived_cast().bitmap_begin());
}

template <class D>
auto array_crtp_base<D>::cend() const -> const_iterator
{
return const_iterator(derived_cast().value_cend(), derived_cast().bitmap_end());
return const_iterator(this->derived_cast().value_cend(), this->derived_cast().bitmap_end());
}

template <class D>
Expand All @@ -217,7 +216,7 @@ namespace sparrow
template <class D>
auto array_crtp_base<D>::values() const -> const_value_range
{
return const_value_range(derived_cast().value_cbegin(), derived_cast().value_cend());
return const_value_range(this->derived_cast().value_cbegin(), this->derived_cast().value_cend());
}

template <class D>
Expand Down Expand Up @@ -292,17 +291,6 @@ namespace sparrow
return sparrow::next(bitmap_begin(), size());
}

template <class D>
auto array_crtp_base<D>::derived_cast() -> derived_type&
{
return *static_cast<derived_type*>(this);
}

template <class D>
auto array_crtp_base<D>::derived_cast() const -> const derived_type&
{
return *static_cast<const derived_type*>(this);
}

template <class D>
auto array_crtp_base<D>::make_bitmap() -> bitmap_type
Expand Down
5 changes: 5 additions & 0 deletions include/sparrow/layout/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "sparrow/layout/run_end_encoded_layout/run_end_encoded_array.hpp"
#include "sparrow/layout/list_layout/list_array.hpp"
#include "sparrow/layout/struct_layout/struct_array.hpp"
#include "sparrow/layout/union_array.hpp"
#include "sparrow/types/data_traits.hpp"

namespace sparrow
Expand Down Expand Up @@ -105,6 +106,10 @@ namespace sparrow
return func(unwrap_array<fixed_sized_list_array>(ar));
case data_type::STRUCT:
return func(unwrap_array<struct_array>(ar));
case data_type::DENSE_UNION:
return func(unwrap_array<dense_union_array>(ar));
case data_type::SPARSE_UNION:
return func(unwrap_array<sparse_union_array>(ar));
default:
throw std::invalid_argument("array type not supported");
}
Expand Down
56 changes: 44 additions & 12 deletions include/sparrow/layout/layout_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,65 @@

namespace sparrow::detail
{

template<class LAYOUT_TYPE>
class layout_functor_base
{
public:
using layout_type = LAYOUT_TYPE;
constexpr layout_functor_base() = default;
constexpr layout_functor_base& operator=(layout_functor_base&&) = default;
constexpr layout_functor_base(const layout_functor_base&) = default;
constexpr layout_functor_base(layout_functor_base&&) = default;
constexpr layout_functor_base& operator=(const layout_functor_base&) = default;

constexpr layout_functor_base(layout_type * layout)
: p_layout(layout)
{
}

protected:
layout_type * p_layout = nullptr;
};


// Functor to get the value of the layout at index i.
//
// This is usefull to create a iterator over the values of a layout.
// This functor will be passed to the functor_index_iterator.
template<class LAYOUT_TYPE, class VALUE_TYPE>
class layout_value_functor
class layout_value_functor : public layout_functor_base<LAYOUT_TYPE>
{
public:
using layout_type = LAYOUT_TYPE;
using base_type = layout_functor_base<LAYOUT_TYPE>;
using base_type::base_type;
using base_type::operator=;
using value_type = VALUE_TYPE;
constexpr layout_value_functor() = default;
constexpr layout_value_functor& operator=(layout_value_functor&&) = default;
constexpr layout_value_functor(const layout_value_functor&) = default;
constexpr layout_value_functor(layout_value_functor&&) = default;
constexpr layout_value_functor& operator=(const layout_value_functor&) = default;

constexpr layout_value_functor(layout_type * layout)
: p_layout(layout)
value_type operator()(std::size_t i) const
{
return this->p_layout->value(i);
}
};


// Functor to get the optional-value of the layout at index i.
//
// This is usefull to create a iterator over the nullable-values of a layout.
// This functor will be passed to the functor_index_iterator.
template<class LAYOUT_TYPE, class VALUE_TYPE>
class layout_bracket_functor : public layout_functor_base<LAYOUT_TYPE>
{
public:
using base_type = layout_functor_base<LAYOUT_TYPE>;
using base_type::base_type;
using base_type::operator=;
using value_type = VALUE_TYPE;

value_type operator()(std::size_t i) const
{
return p_layout->value(i);
return this->p_layout->operator[](i);
}
private:
layout_type * p_layout = nullptr;
};

}; // namespace sparrow::detail
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ namespace sparrow
array_traits::const_reference
>
{
using array_ptr_type = std::conditional_t<CONST, const run_end_encoded_array *, run_end_encoded_array*>;

private:
using array_ptr_type = std::conditional_t<CONST, const run_end_encoded_array *, run_end_encoded_array*>;
public:

run_encoded_array_iterator() = default;
run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index);

private:

bool equal(const run_encoded_array_iterator& rhs) const;
Expand Down
Loading

0 comments on commit ad70c5d

Please sign in to comment.