rows.hpp 916 Bytes
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
2
#pragma once

3
4
#include <mln/core/rangev3/range_traits.hpp>

Edwin Carlinet's avatar
Edwin Carlinet committed
5
6
7
8
9
10
11
12
13
14
#include <range/v3/view/single.hpp>

namespace mln::ranges
{

  /// \function rows
  /// \brief Provide unified way to iterate row-wise over uni-dimensional and multi-dimensional ranges.
  ///
  /// \p rows(rng) returns \p rng.rows() if \p rng is multi-dimensional, \p {rng} otherwise.

15
  template <class R, typename = std::enable_if_t<is_segmented_range_v<std::remove_reference_t<R>>>>
Michaël Roynard's avatar
Michaël Roynard committed
16
  decltype(auto) rows(R&& rng) // decltype for perfect forwarding
Edwin Carlinet's avatar
Edwin Carlinet committed
17
  {
18
    static_assert(std::is_lvalue_reference<R>(), "Cannot apply on a temporary range.");
Edwin Carlinet's avatar
Edwin Carlinet committed
19
20
21
    return rng.rows();
  }

22
  template <class R, typename = std::enable_if_t<!is_segmented_range_v<std::remove_reference_t<R>>>>
Edwin Carlinet's avatar
Edwin Carlinet committed
23
24
  auto rows(R&& rng)
  {
Michaël Roynard's avatar
Michaël Roynard committed
25
    static_assert(::ranges::Range<R>(), "The input is not a range.");
Edwin Carlinet's avatar
Edwin Carlinet committed
26
27
    return ::ranges::single_view(std::forward<R>(rng));
  }
Michaël Roynard's avatar
Michaël Roynard committed
28
} // namespace mln::ranges