/* * (C) Copyright 2013 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #pragma once #include #include "atlas/array/ArrayViewDefs.h" #include "atlas/array/Range.h" #include "atlas/library/config.h" namespace atlas { namespace array { template class LocalView; template struct Reference { Value& value; operator Value&() { return value; } operator const Value&() const { return value; } template void operator=(const T a) { value = a; } template Reference& operator+(const T a) { value += a; return *this; } template Reference& operator-(const T a) { value -= a; return *this; } Reference& operator--() { --value; return *this; } Reference& operator++() { ++value; return *this; } template bool operator==(const T a) { return value == a; } template bool operator!=(const T a) { return value != a; } friend std::ostream& operator<<(std::ostream& out, const Reference& ref) { out << ref.value; return out; } constexpr int shape(idx_t) { return 0; } constexpr int stride(idx_t) { return 0; } constexpr int rank() { return 0; } }; template struct get_slice_type { using type = typename std::conditional<(Rank == 0), Reference, LocalView>::type; }; //------------------------------------------------------------------------------ namespace helpers { template struct deduce_slice_rank; template struct SliceRank_impl; template <> struct deduce_slice_rank<1> { template static constexpr int apply() { return std::is_base_of::value; } }; template struct SliceRank_impl<1, Args...> { static constexpr int value{deduce_slice_rank<1>::apply()}; }; #define ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(RANK) \ template <> \ struct deduce_slice_rank { \ template \ static constexpr int apply() { \ return std::is_base_of::value + deduce_slice_rank::apply(); \ } \ }; \ template \ struct SliceRank_impl { \ static constexpr int value{deduce_slice_rank::apply()}; \ } ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(2); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(3); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(4); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(5); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(6); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(7); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(8); ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(9); #undef ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION template struct SliceRank { static constexpr int value{SliceRank_impl::value}; }; // template template class ArraySlicer { public: ArraySlicer(View& view): view_(view) {} template struct Slice { using type = typename get_slice_type::value>::type; }; template typename Slice::type apply(const Args... args) const { using slicer_t = Slicer::type, (SliceRank::value == 0)>; static_assert( View::RANK <= sizeof...(Args), "Not enough arguments passed to slice() function. Pehaps you forgot to add a array::Range::all()"); return slicer_t::apply(view_, args...); } private: template struct array { using type = typename std::array::value>; }; template struct Slicer { template static ReturnType apply(View& view, const Args... args) { return ReturnType(view.data() + offset(view, args...), shape(view, args...).data(), strides(view, args...).data()); } }; template struct Slicer { template static ReturnType apply(View& view, const Args... args) { return ReturnType{*(view.data() + offset(view, args...))}; } }; template static idx_t offset_part(View& view, int& i_view, Int idx) { return idx * view.stride(i_view++); } static idx_t offset_part(View& view, int& i_view, Range range) { return range.start() * view.stride(i_view++); } static idx_t offset_part(View& view, int& i_view, RangeAll range) { return range.start() * view.stride(i_view++); } static idx_t offset_part(View& view, int& i_view, RangeTo range) { return range.start() * view.stride(i_view++); } static idx_t offset_part(View& view, int& i_view, RangeFrom range) { return range.start() * view.stride(i_view++); } static idx_t offset_part(View&, int& /*i_view*/, RangeDummy) { return 0; } template static idx_t offset_remaining(View& view, int& i_view, const Int idx, const Ints... next_idx) { const idx_t p = offset_part(view, i_view, idx); return p + offset_remaining(view, i_view, next_idx...); } template static idx_t offset_remaining(View& view, int& i_view, const Int last_idx) { return offset_part(view, i_view, last_idx); } template static idx_t offset(View& view, const Args... args) { int i_view(0); return offset_remaining<0>(view, i_view, args...); } template static void update_shape(View&, Shape&, int& i_view, int& /*i_slice*/, const Int& /*index*/) { // do nothing ++i_view; } template static void update_shape(View&, Shape& shape, int& i_view, int& i_slice, const Range range) { shape[i_slice] = range.end() - range.start(); ++i_slice; ++i_view; } template static void update_shape(View& view, Shape& shape, int& i_view, int& i_slice, const RangeAll range) { shape[i_slice] = range.end(view, i_view) - range.start(); ++i_slice; ++i_view; } template static void update_shape(View& view, Shape& shape, int& i_view, int& i_slice, const RangeFrom range) { shape[i_slice] = range.end(view, i_view) - range.start(); ++i_slice; ++i_view; } template static void update_shape(View&, Shape& shape, int& i_view, int& i_slice, const RangeTo range) { shape[i_slice] = range.end() - range.start(); ++i_slice; ++i_view; } template static void update_shape(View&, Shape& shape, int& /*i_view*/, int& i_slice, const RangeDummy) { shape[i_slice] = 1; ++i_slice; // no update of i_view for dummy-dimension } template static void shape_part(View& view, Shape& shape, int& i_view, int& i_slice, const Int idx, const Ints... next_idx) { update_shape(view, shape, i_view, i_slice, idx); shape_part(view, shape, i_view, i_slice, next_idx...); } template static void shape_part(View& view, Shape& shape, int& i_view, int& i_slice, const Int idx) { update_shape(view, shape, i_view, i_slice, idx); } template static typename array::type shape(View& view, const Args... args) { typename array::type result; int i_slice(0); int i_view(0); shape_part<0>(view, result, i_view, i_slice, args...); return result; } template static void update_strides(View&, Strides&, int& i_view, int& /*i_slice*/, const Int& /*idx*/) { // do nothing ++i_view; } template static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const Range& /*range*/) { strides[i_slice] = view.stride(i_view); ++i_slice; ++i_view; } template static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeFrom& /*range*/) { strides[i_slice] = view.stride(i_view); ++i_slice; ++i_view; } template static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeTo& /*range*/) { strides[i_slice] = view.stride(i_view); ++i_slice; ++i_view; } template static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeAll& /*range*/) { strides[i_slice] = view.stride(i_view); ++i_slice; ++i_view; } template static void update_strides(View& /*view*/, Strides& strides, int& /*i_view*/, int& i_slice, const RangeDummy& /*range*/) { strides[i_slice] = 0; ++i_slice; } template static void strides_part(View& view, Strides& strides, int& i_view, int& i_slice, const Int idx, const Ints... next_idx) { update_strides(view, strides, i_view, i_slice, idx); strides_part(view, strides, i_view, i_slice, next_idx...); } template static void strides_part(View& view, Strides& strides, int& i_view, int& i_slice, const Int idx) { update_strides(view, strides, i_view, i_slice, idx); } template static typename array::type strides(View& view, const Args... args) { typename array::type result; int i_slice(0); int i_view(0); strides_part<0>(view, result, i_view, i_slice, args...); return result; } private: View& view_; }; //------------------------------------------------------------------------------ } // namespace helpers } // namespace array } // namespace atlas