Skip to content

Commit

Permalink
Slicing interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jfalcou authored Jun 21, 2024
1 parent 96d8f49 commit ae5df40
Show file tree
Hide file tree
Showing 26 changed files with 651 additions and 240 deletions.
2 changes: 1 addition & 1 deletion include/kwk/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
#include <kwk/concepts/range.hpp>
#include <kwk/concepts/axis.hpp>
#include <kwk/concepts/values.hpp>
// #include <kwk/concept/slicer.hpp>
#include <kwk/concepts/slicer.hpp>
2 changes: 1 addition & 1 deletion include/kwk/concepts/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//==================================================================================================
#pragma once

#include <concepts>
#include <cstddef>

namespace kwk::concepts
{
Expand Down
52 changes: 52 additions & 0 deletions include/kwk/concepts/slicer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//======================================================================================================================
/*
KIWAKU - Containers Well Made
Copyright : KIWAKU Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//======================================================================================================================
#pragma once

#include <kwk/concepts/extent.hpp>
#include <kwk/concepts/values.hpp>
#include <kwk/detail/stdfix.hpp>

namespace kwk::concepts
{
template<typename S>
inline constexpr bool is_contiguous = concepts::joker<S> || concepts::extremum <S> || std::integral<S>;

template<typename S>
requires( requires { S::has_step; } )
inline constexpr bool is_contiguous<S> = !S::has_step;

//====================================================================================================================
//! @brief slicer specifier concept
//!
//! A **KIWAKU** Slicer is a type which instances can be used to compute a sub-volume of a container.
//====================================================================================================================
template<typename S>
concept slicer = requires(S const& s)
{
{ origin (shape<_>{} , s) };
{ reindex (shape<_>{} , s, kumi::index<0>) };
{ reshape (shape<_>{} , s, kumi::index<0>) };
{ restride(stride<_>{}, s, kumi::index<0>) };
};


//====================================================================================================================
//! @brief slicer specifier with unit step concept
//!
//! A kwk::unit_slicer is a type which fulfills kwk::slicer and doesn't break the contiguity of the dimension it
//! slices.
//====================================================================================================================
template<typename S>
concept unit_slicer = slicer<S> && is_contiguous<S>;

template<typename S>
concept interval_slicer = slicer<S> && requires(S) { typename S::kwk_is_interval; };

template<typename S>
concept unit_interval_slicer = interval_slicer<S> && is_contiguous<S>;
}
39 changes: 27 additions & 12 deletions include/kwk/container/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <kwk/algorithm/algos/for_each.hpp>
#include <kwk/container/pick.hpp>
#include <kwk/utility/traits/reachable.hpp>
#include <kwk/detail/container/builder.hpp>
#include <kwk/detail/memory/block.hpp>
#include <type_traits>
Expand All @@ -29,8 +30,9 @@ namespace kwk
using shape_type = typename access_t::shape_type;
using container_kind = decltype(Tag);

static constexpr std::int32_t static_order = access_t::static_order;
static constexpr bool has_label = meta_t::has_label;
static constexpr std::int32_t static_order = access_t::static_order;
static constexpr bool has_label = meta_t::has_label;
static constexpr bool preserve_reachability = Builder::preserve_reachability;

constexpr container( container_kind ) noexcept
: meta_t{}, data_t{}, access_t{}
Expand All @@ -44,10 +46,10 @@ namespace kwk
: meta_t{ params }, data_t{ params }, access_t{ params }
{}

static constexpr auto kind() noexcept { return Tag; }
constexpr std::int32_t order() const noexcept { return this->shape().order(); }
constexpr auto numel() const noexcept { return this->shape().numel(); }
constexpr bool empty() const noexcept { return this->size() == 0; }
static constexpr auto kind() noexcept { return Tag; }
static constexpr auto order() noexcept { return static_order; }
constexpr auto numel() const noexcept { return this->shape().numel(); }
constexpr bool empty() const noexcept { return this->size() == 0; }

using meta_t::label;

Expand Down Expand Up @@ -112,29 +114,42 @@ namespace kwk
}

template<kumi::sized_product_type<static_order> Pos>
const_reference operator()(Pos p) const noexcept
decltype(auto) operator()(Pos p) const noexcept
{
return kumi::apply([&](auto... i) -> const_reference { return (*this)(i...); }, p);
return kumi::apply([&](auto... i) -> decltype(auto) { return (*this)(i...); }, p);
}

template<kumi::sized_product_type<static_order> Pos>
reference operator()(Pos p) noexcept
decltype(auto) operator()(Pos p) noexcept
{
return kumi::apply([&](auto... i) -> reference { return (*this)(i...); }, p);
return kumi::apply([&](auto... i) -> decltype(auto) { return (*this)(i...); }, p);
}

template<std::integral... Is>
requires(sizeof...(Is) == static_order) const_reference operator()(Is... is) const noexcept
requires(sizeof...(Is) == static_order) decltype(auto) operator()(Is... is) const noexcept
{
return data(static_cast<data_t const&>(*this))[ access_t::index(is...) ];
}

template<std::integral... Is>
requires(sizeof...(Is) == static_order) reference operator()(Is... is) noexcept
requires(sizeof...(Is) == static_order) decltype(auto) operator()(Is... is) noexcept
{
return data(static_cast<data_t&>(*this))[ access_t::index(is...) ];
}

template<concepts::slicer... Slicers>
requires( (sizeof...(Slicers) <= static_order) && (!std::integral<Slicers> || ...))
auto operator()(Slicers... slice) const noexcept
{
auto shp = this->shape();
auto str = compress<sizeof...(slice)>(this->stride());

if constexpr(preserve_reachability && kwk::preserve_reachability<Slicers...>)
return make_view(source = this->get_data() + origin(shp, slice...), shp(slice...), str(slice...));
else
return make_view( source = this->get_data() + origin(shp, slice...), shp(slice...), str(slice...), unreachable);
}

constexpr auto get_data() const noexcept { return data(static_cast<data_t const&>(*this)); }
constexpr auto get_data() noexcept { return data(static_cast<data_t&>(*this)); }
};
Expand Down
14 changes: 7 additions & 7 deletions include/kwk/container/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <kwk/container/container.hpp>
#include <kwk/concepts/slicer.hpp>
#include <kwk/detail/raberu.hpp>
#include <type_traits>

Expand Down Expand Up @@ -54,7 +55,8 @@ namespace kwk
KWK_TRIVIAL constexpr table() : parent{kwk::table_} {}

/// Construct a table from a list of options
KWK_TRIVIAL constexpr table(rbr::concepts::option auto const&... opts) : table{rbr::settings{opts...}} {}
KWK_TRIVIAL constexpr table(rbr::concepts::option auto const&... opts)
: parent{rbr::settings{kwk::table_,opts...}} {}

/// Construct a table from a settings descriptor
KWK_TRIVIAL constexpr table(rbr::concepts::settings auto const& opts)
Expand Down Expand Up @@ -98,6 +100,7 @@ namespace kwk
//==============================================================================================
//! @}
//==============================================================================================
using parent::operator();
};

//================================================================================================
Expand All @@ -121,12 +124,9 @@ namespace kwk
//! @}
//================================================================================================

/// Type helper
template<typename... Settings> struct make_table
{
using type = table<__::builder<rbr::settings(table_,Settings{}...)>>;
};
template<rbr::concepts::option... Os> auto make_table(Os const&... os) { return table{os...}; };

/// Type helper
template<typename... Settings>
using make_table_t = typename make_table<Settings...>::type;
using make_table_t = table<__::builder<rbr::settings(table_,Settings{}...)>>;
}
13 changes: 6 additions & 7 deletions include/kwk/container/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <kwk/container/container.hpp>
#include <kwk/concepts/slicer.hpp>
#include <kwk/detail/raberu.hpp>
#include <type_traits>

Expand Down Expand Up @@ -54,7 +55,8 @@ namespace kwk
KWK_TRIVIAL constexpr view() : parent{kwk::view_} {}

/// Construct a view from a list of options
KWK_TRIVIAL constexpr view(rbr::concepts::option auto const&... opts) : view{rbr::settings{opts...}} {}
KWK_TRIVIAL constexpr view(rbr::concepts::option auto const&... opts)
: parent{rbr::settings{kwk::view_,opts...}} {}

/// Construct a view from a settings descriptor
KWK_TRIVIAL constexpr view(rbr::concepts::settings auto const& opts)
Expand All @@ -79,6 +81,7 @@ namespace kwk
//==============================================================================================
//! @}
//==============================================================================================
using parent::operator();
};

//================================================================================================
Expand All @@ -103,13 +106,9 @@ namespace kwk
//================================================================================================
//! @}
//================================================================================================
template<rbr::concepts::option... Os> auto make_view(Os const&... os) { return view{os...}; };

/// Type helper
template<typename... Settings> struct make_view
{
using type = view<__::builder<rbr::settings(view_,Settings{}...)>>;
};

template<typename... Settings>
using make_view_t = typename make_view<Settings...>::type;
using make_view_t = view<__::builder<rbr::settings(view_,Settings{}...)>>;
}
1 change: 1 addition & 0 deletions include/kwk/detail/algorithm/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <kwk/detail/abi.hpp>
#include <kwk/detail/kumi.hpp>
#include <kwk/utility/fixed.hpp>
#include <cstddef>
#include <utility>

Expand Down
Loading

0 comments on commit ae5df40

Please sign in to comment.