emlabcpp
modern opinionated embedded C++ library
bounded_view.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../bounded.h"
27 #include "../view.h"
28 
29 namespace emlabcpp
30 {
31 template < typename Iterator, bounded_derived SizeType >
32 class bounded_view : public view< Iterator >
33 {
34 public:
35  using size_type = SizeType;
36  using iterator = Iterator;
37 
38  static constexpr std::size_t min = size_type::min_val;
39  static constexpr std::size_t max = size_type::max_val;
40 
41 private:
43  : view< Iterator >( beg, end )
44  {
45  }
46 
47 public:
48  template < typename, bounded_derived >
49  friend class bounded_view;
50 
51  template < bounded_derived OtherSize >
52  requires( OtherSize::min_val >= min && OtherSize::max_val <= max )
53  bounded_view( bounded_view< Iterator, OtherSize > const& other )
54  : bounded_view( std::begin( other ), std::end( other ) )
55  {
56  }
57 
58  template < typename Container >
60  range_container< Container > && static_sized< Container > &&
61  std::tuple_size_v< Container > <= max && std::tuple_size_v< Container > >= min )
62  explicit bounded_view( Container& cont )
63  : bounded_view( std::begin( cont ), std::end( cont ) )
64  {
65  }
66 
67  static std::optional< bounded_view > make( view< Iterator > v )
68  {
69  if ( std::size( v ) < min )
70  return {};
71  if ( std::size( v ) > max )
72  return {};
73  return { bounded_view( std::begin( v ), std::end( v ) ) };
74  }
75 
76  template < std::size_t n >
77  requires( n <= min )
79  {
80  return { this->begin(), this->begin() + n };
81  }
82 
83  template < std::size_t n >
84  requires( n <= min )
85  [[nodiscard]] bounded_view< iterator, bounded< std::size_t, min - n, max - n > >
86  offset() const
87  {
88  return { this->begin() + n, this->end() };
89  }
90 
91  template < typename OffsetSizeType >
92  std::optional< bounded_view< iterator, OffsetSizeType > > opt_offset( std::size_t offset )
93  {
94  auto new_beg = this->begin() + offset;
95  if ( new_beg + OffsetSizeType::min_val > this->end() )
96  return {};
97  auto new_end = new_beg + OffsetSizeType::max_val;
98  if ( new_end <= this->end() )
99  return { bounded_view< iterator, OffsetSizeType >{ new_beg, new_end } };
100  return { bounded_view< iterator, OffsetSizeType >{ new_beg, this->end() } };
101  }
102 };
103 
104 } // namespace emlabcpp
Definition: bounded_view.h:33
requires(range_container< Container > &&static_sized< Container > &&std::tuple_size_v< Container ><=max &&std::tuple_size_v< Container > >=min) explicit bounded_view(Container &cont)
Definition: bounded_view.h:59
static constexpr std::size_t min
Definition: bounded_view.h:38
static constexpr std::size_t max
Definition: bounded_view.h:39
std::optional< bounded_view< iterator, OffsetSizeType > > opt_offset(std::size_t offset)
Definition: bounded_view.h:92
OtherSize const std::end(other))
Definition: bounded_view.h:54
static std::optional< bounded_view > make(view< Iterator > v)
Definition: bounded_view.h:67
Iterator iterator
Definition: bounded_view.h:36
requires(OtherSize::min_val >=min &&OtherSize::max_val<=max) bounded_view(bounded_view< Iterator
requires(n<=min) bounded_view< iterator
friend class bounded_view
Definition: bounded_view.h:49
SizeType size_type
Definition: bounded_view.h:35
bounded< std::size_t, min - n, max - n > offset() const
Definition: bounded_view.h:86
OtherSize const & other
Definition: bounded_view.h:54
bounded< std::size_t, n, n > first() const
Definition: bounded_view.h:78
The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-t...
Definition: bounded.h:44
Generic class to represent view of some container.
Definition: view.h:41
constexpr EndIterator end() const
Past the end iterator.
Definition: view.h:110
constexpr Iterator begin() const
Start of the dataset iterator.
Definition: view.h:104
MIT License.
Definition: impl.h:31