15 #ifndef ROOT7_RHYSD_ARRAY_VIEW_H 16 #define ROOT7_RHYSD_ARRAY_VIEW_H 26 #include <type_traits> 28 #include <initializer_list> 38 static bool const value =
false;
40 template<
class T,
size_t N>
55 template<
size_t... Indices >
57 static constexpr
size_t value[
sizeof...(Indices)] = {Indices...};
60 template<
class IndicesType,
size_t Next>
63 template<
size_t... Indices,
size_t Next>
68 template<
class IndicesType,
size_t Next,
size_t Tail>
71 template<
size_t... Indices,
size_t Next,
size_t Tail>
76 template<
size_t First,
size_t Step,
size_t N,
class =
void>
79 template<
size_t First,
size_t Step,
size_t N>
89 template<
size_t First,
size_t Step,
size_t N>
99 template<
size_t First,
size_t Step,
size_t N>
104 typename
std::enable_if<(N > 1 && N % 2 == 0)>
::type 107 typename detail::make_indices_impl<First, Step, N / 2>::type,
112 template<
size_t First,
size_t Step,
size_t N>
117 typename std::enable_if<(N > 1 && N % 2 == 1)>
::type 120 typename detail::make_indices_impl<First, Step, N / 2>::type,
121 First + N / 2 * Step,
122 First + (N - 1) * Step
126 template<
size_t First,
size_t Last,
size_t Step = 1>
131 ((Last - First) + (Step - 1)) / Step
135 template <
size_t Start,
size_t Last,
size_t Step = 1 >
167 : length_(0), data_(
nullptr)
175 template<
size_type N>
177 : length_(
N), data_(
N > 0 ?
a.data() :
nullptr)
182 template<
size_type N>
186 static_assert(
N > 0,
"Zero-length array is not permitted in ISO C++.");
190 : length_(
v.size()), data_(
v.empty() ?
nullptr :
v.data())
194 : length_(
n), data_(
a)
199 class =
typename std::enable_if<
202 typename std::iterator_traits<InputIterator>::value_type
207 : length_(
std::
distance(start, last)), data_(start)
211 : length_(l.size()), data_(
std::begin(l))
220 constexpr const_iterator
begin() const noexcept
224 constexpr const_iterator
end() const noexcept
226 return data_ + length_;
228 constexpr const_iterator
cbegin() const noexcept
232 constexpr const_iterator
cend() const noexcept
240 const_reverse_iterator
rend()
const 248 const_reverse_iterator
crend()
const 256 constexpr size_type
size() const noexcept
260 constexpr size_type
length() const noexcept
268 constexpr
bool empty() const noexcept
272 constexpr const_reference
operator[](size_type
const n)
const noexcept
276 constexpr const_reference
at(size_type
const n)
const 278 if (n >= length_)
throw std::out_of_range(
"array_view::at()");
281 constexpr const_pointer
data() const noexcept
285 constexpr const_reference
front() const noexcept
289 constexpr const_reference
back() const noexcept
291 return *(data_ + length_ - 1);
301 if (pos >= length_ || pos + length >= length_) {
302 throw std::out_of_range(
"array_view::slice()");
308 if (pos >= length_) {
309 throw std::out_of_range(
"array_view::slice()");
315 if (pos >= length_) {
316 throw std::out_of_range(
"array_view::slice()");
340 if ( start >= end() ||
344 throw std::out_of_range(
"array_view::slice()");
350 if (pos < begin() || pos > end()) {
351 throw std::out_of_range(
"array_view::slice()");
357 if (pos < begin() || pos > end()) {
358 throw std::out_of_range(
"array_view::slice()");
382 template<
class Allocator = std::allocator<T>>
383 auto to_vector(Allocator
const& alloc = Allocator{})
const 384 -> std::vector<T, Allocator>
386 return {begin(), end(), alloc};
396 template<
size_t...
I>
398 ->
std::array<T, sizeof...(
I)>
400 return {{(I < length_ ? *(data_ +
I) : T{} )...}};
411 template<
class ArrayL,
class ArrayR >
413 bool operator_equal_impl(ArrayL
const& lhs,
size_t const lhs_size, ArrayR
const& rhs,
size_t const rhs_size)
415 if (lhs_size != rhs_size) {
419 auto litr = std::begin(lhs);
420 auto ritr = std::begin(rhs);
421 for (; litr != std::end(lhs); ++litr, ++ritr) {
422 if (!(*litr == *ritr)) {
431 template<
class T1,
class T2>
441 class =
typename std::enable_if<
451 template<
class T1,
class T2,
size_t N>
461 class =
typename std::enable_if<
462 is_array<Array>::value
468 return !(lhs == rhs);
474 class =
typename std::enable_if<
475 is_array<Array>::value
487 class =
typename std::enable_if<
488 is_array<Array>::value,
495 return !(rhs == lhs);
502 class =
typename std::enable_if<
513 template<
class T,
size_t N>
527 template<class InputIterator, class Result = array_view<typename std::iterator_traits<InputIterator>::value_type>>
529 Result
make_view(InputIterator begin, InputIterator end)
531 return Result{begin, end};
553 #include <type_traits> 565 static constexpr
int rank = Rank;
566 using reference = ptrdiff_t &;
567 using const_reference =
const ptrdiff_t &;
568 using size_type = size_t;
569 using value_type = ptrdiff_t;
572 std::array<value_type, Rank> m_B;
575 constexpr bounds() noexcept;
577 constexpr bounds(value_type b) noexcept: m_B{{b}} { };
582 reference operator[](size_type i) noexcept {
return m_B[i]; }
584 constexpr const_reference operator[](
585 size_type i)
const noexcept {
return m_B[i]; };
588 bool operator==(
const bounds &rhs)
const noexcept;
590 bool operator!=(
const bounds &rhs)
const noexcept;
592 bounds
operator+(
const index<rank> &rhs)
const noexcept;
594 bounds
operator-(
const index<rank> &rhs)
const noexcept;
596 bounds &operator+=(
const index<rank> &rhs) noexcept;
598 bounds &operator-=(
const index<rank> &rhs) noexcept;
600 constexpr size_type size()
const noexcept;
602 bool contains(
const index<rank> &idx)
const noexcept;
613 static constexpr
int rank = Rank;
614 using reference = ptrdiff_t &;
615 using const_reference =
const ptrdiff_t &;
616 using size_type = size_t;
617 using value_type = ptrdiff_t;
620 constexpr index() noexcept;
622 constexpr index(value_type) noexcept;
624 constexpr index(
const initializer_list<value_type> &) noexcept;
626 constexpr index(
const index &) noexcept;
628 index &
operator=(
const index &) noexcept;
630 reference operator[](size_type component_idx) noexcept;
632 constexpr const_reference operator[](size_type component_idx)
const noexcept;
634 bool operator==(
const index &rhs)
const noexcept;
636 bool operator!=(
const index &rhs)
const noexcept;
638 index
operator+(
const index &rhs)
const noexcept;
640 index
operator-(
const index &rhs)
const noexcept;
642 index &operator+=(
const index &rhs) noexcept;
644 index &operator-=(
const index &rhs) noexcept;
646 index &operator++() noexcept;
648 index operator++(
int) noexcept;
650 index &operator--() noexcept;
652 index operator--(
int) noexcept;
661 template<
typename ValueType,
int Rank = 1>
664 static constexpr
int rank = Rank;
665 using index_type = index<rank>;
666 using bounds_type = bounds<rank>;
667 using size_type =
typename bounds_type::size_type;
668 using value_type = ValueType;
669 using pointer =
typename std::add_pointer_t<value_type>;
670 using reference =
typename std::add_lvalue_reference_t<value_type>;
674 constexpr
explicit array_view(std::vector<ValueType> &cont) noexcept;
676 template<
typename ArrayType>
677 constexpr
explicit array_view(ArrayType &data) noexcept;
679 template<
typename ViewValueType>
682 template<
typename Container>
683 constexpr
array_view(bounds_type bounds, Container &cont) noexcept;
685 constexpr
array_view(bounds_type bounds, pointer data) noexcept;
687 template<
typename ViewValueType>
690 constexpr bounds_type bounds()
const noexcept;
691 constexpr size_type size()
const noexcept;
692 constexpr index_type stride()
const noexcept;
694 constexpr pointer data()
const noexcept;
695 constexpr reference operator[](
const index_type& idx)
const noexcept;
699 #endif // too complex!
constexpr array_view< T > slice_before(check_bound_t, iterator const pos) const
constexpr const_reference operator[](size_type const n) const noexcept
static constexpr check_bound_t check_bound
const_reverse_iterator crbegin() const
value_type const * const_iterator
Double_t distance(const TPoint2 &p1, const TPoint2 &p2)
array_view(std::initializer_list< T > const &l)
constexpr const_iterator end() const noexcept
constexpr array_view< T > slice(check_bound_t, iterator start, iterator last) const
constexpr array_view< T > slice_before(size_type const pos) const
void Step(const gsl_rng *r, void *xp, double step_size)
value_type const * const_pointer
constexpr array_view< T > slice(size_type const pos, size_type const length) const
typename make_indices_< Start, Last, Step >::type make_indices
constexpr size_type length() const noexcept
array_view(InputIterator start, InputIterator last)
array_view(std::array< T, N > const &a) noexcept
array_view(T const (&a)[N]) noexcept
value_type const & reference
constexpr bool operator_equal_impl(ArrayL const &lhs, size_t const lhs_size, ArrayR const &rhs, size_t const rhs_size)
constexpr array_view< T > slice_after(iterator const pos) const
const_reverse_iterator rbegin() const
TTime operator-(const TTime &t1, const TTime &t2)
constexpr const_reference back() const noexcept
constexpr const_iterator begin() const noexcept
constexpr array_view< T > slice_after(check_bound_t, iterator const pos) const
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
constexpr array_view< T > slice(check_bound_t, size_type const pos, size_type const length) const
value_type const * iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr array_view< T > slice_after(size_type const pos) const
ptrdiff_t difference_type
constexpr array_view< T > slice_before(check_bound_t, size_type const pos) const
Double_t length(const TVector2 &v)
constexpr const_iterator cbegin() const noexcept
constexpr array_view() noexcept
constexpr size_type size() const noexcept
constexpr const_pointer data() const noexcept
constexpr const_iterator cend() const noexcept
constexpr array_view< T > slice_before(iterator const pos) const
constexpr array_view< T > slice(iterator start, iterator last) const
constexpr const_reference front() const noexcept
const_pointer const data_
value_type const & const_reference
constexpr size_type max_size() const noexcept
Binding & operator=(OUT(*fun)(void))
const_reverse_iterator rend() const
Bool_t operator==(const TDatime &d1, const TDatime &d2)
constexpr auto make_view(Array const &a) -> array_view< typename Array::value_type >
value_type const * pointer
auto to_array_impl(detail::indices< I... >) const -> std::array< T, sizeof...(I)>
constexpr array_view< T > slice_after(check_bound_t, size_type const pos) const
auto to_vector(Allocator const &alloc=Allocator{}) const -> std::vector< T, Allocator >
std::reverse_iterator< iterator > reverse_iterator
auto to_array() const -> std::array< T, N >
array_view(std::vector< T > const &v) noexcept
constexpr array_view(T const *a, size_type const n) noexcept
constexpr const_reference at(size_type const n) const
constexpr bool empty() const noexcept
const_reverse_iterator crend() const