scnlib  0.1.2
FormattedinputformodernC++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
span.h
Go to the documentation of this file.
1 // Copyright 2017-2019 Elias Kosunen
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This file is a part of scnlib:
16 // https://github.com/eliaskosunen/scnlib
17 
18 #ifndef SCN_DETAIL_SPAN_H
19 #define SCN_DETAIL_SPAN_H
20 
21 #include "config.h"
22 
23 #include <iterator>
24 
25 namespace scn {
27 
32  template <typename T>
33  class span {
34  public:
35  using element_type = T;
37  using index_type = std::size_t;
38  using ssize_type = std::ptrdiff_t;
39  using difference_type = std::ptrdiff_t;
40  using pointer = T*;
41  using const_pointer = T*;
42  using reference = T&;
43  using iterator = pointer;
45  using reverse_iterator = std::reverse_iterator<iterator>;
46  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
47 
48  SCN_CONSTEXPR span() noexcept = default;
49  SCN_CONSTEXPR span(pointer ptr, index_type count) noexcept
50  : m_ptr(ptr), m_size(count)
51  {
52  }
54  : span(first, static_cast<index_type>(last - first))
55  {
56  }
57 
58  SCN_CONSTEXPR iterator begin() const noexcept
59  {
60  return m_ptr;
61  }
62  SCN_CONSTEXPR iterator end() const noexcept
63  {
64  return m_ptr + m_size;
65  }
67  {
68  return reverse_iterator{end()};
69  }
71  {
72  return reverse_iterator{begin()};
73  }
74 
76  {
77  return m_ptr;
78  }
80  {
81  return m_ptr + m_size;
82  }
84  {
85  return reverse_iterator{cend()};
86  }
88  {
89  return reverse_iterator{cbegin()};
90  }
91 
93  {
94  SCN_EXPECT(size() > i);
95  return *(m_ptr + i);
96  }
97 
98  SCN_CONSTEXPR pointer data() const noexcept
99  {
100  return m_ptr;
101  }
102  SCN_CONSTEXPR index_type size() const noexcept
103  {
104  return m_size;
105  }
106  SCN_CONSTEXPR ssize_type ssize() const noexcept
107  {
108  return static_cast<std::ptrdiff_t>(size());
109  }
110 
112  {
113  SCN_EXPECT(size() >= n);
114  return span<T>(data(), data() + n);
115  }
117  {
118  SCN_EXPECT(size() >= n);
119  return span<T>(data() + size() - n, data() + size());
120  }
122  {
123  SCN_EXPECT(size() >= off);
124  return span<T>(data() + off, size() - off);
125  }
127  difference_type count) const
128  {
129  SCN_EXPECT(size() > off + count);
130  SCN_EXPECT(count > 0);
131  return span<T>(data() + off, count);
132  }
133 
135  {
136  return {m_ptr, m_size};
137  }
138 
139  private:
140  pointer m_ptr{nullptr};
141  index_type m_size{0};
142  };
143 
144  template <typename T>
145  SCN_CONSTEXPR span<T> make_span(T* ptr, std::size_t count) noexcept
146  {
147  return span<T>(ptr, count);
148  }
149  template <typename T>
150  SCN_CONSTEXPR span<T> make_span(T* first, T* last) noexcept
151  {
152  return span<T>(first, last);
153  }
154  template <typename T>
156  {
157  using std::begin;
158  using std::end;
160  std::addressof(*begin(container)),
161  std::addressof(*(end(container) - 1)) + 1);
162  }
163 
165 } // namespace scn
166 
167 #endif // SCN_DETAIL_SPAN_H
std::size_t index_type
Definition: span.h:37
SCN_CONSTEXPR reverse_iterator rbegin() const noexcept
Definition: span.h:66
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: span.h:46
SCN_CONSTEXPR const_iterator cbegin() const noexcept
Definition: span.h:75
std::ptrdiff_t ssize_type
Definition: span.h:38
const value_type * pointer
Definition: span.h:40
SCN_CONSTEXPR span< typename std::add_const< T >::type > as_const() const
Definition: span.h:134
#define SCN_END_NAMESPACE
Definition: config.h:376
SCN_CONSTEXPR span< T > first(index_type n) const
Definition: span.h:111
SCN_CONSTEXPR const_reverse_iterator crbegin() const noexcept
Definition: span.h:83
const value_type & reference
Definition: span.h:42
const value_type element_type
Definition: span.h:35
std::reverse_iterator< iterator > reverse_iterator
Definition: span.h:45
SCN_CONSTEXPR span< T > last(index_type n) const
Definition: span.h:116
SCN_CONSTEXPR iterator begin() const noexcept
Definition: span.h:58
SCN_CONSTEXPR pointer data() const noexcept
Definition: span.h:98
SCN_CONSTEXPR span< T > make_span(T *ptr, std::size_t count) noexcept
Definition: span.h:145
SCN_CONSTEXPR span< T > subspan(index_type off, difference_type count) const
Definition: span.h:126
std::ptrdiff_t difference_type
Definition: span.h:39
SCN_CONSTEXPR reference operator[](index_type i) const noexcept
Definition: span.h:92
SCN_CONSTEXPR span(pointer first, pointer last) noexcept
Definition: span.h:53
const_pointer const_iterator
Definition: span.h:44
SCN_CONSTEXPR const_reverse_iterator crend() const noexcept
Definition: span.h:87
#define SCN_BEGIN_NAMESPACE
Definition: config.h:375
SCN_CONSTEXPR iterator end() const noexcept
Definition: span.h:62
SCN_CONSTEXPR const_iterator cend() const noexcept
Definition: span.h:79
SCN_CONSTEXPR span() noexcept=default
SCN_CONSTEXPR reverse_iterator rend() const noexcept
Definition: span.h:70
SCN_CONSTEXPR ssize_type ssize() const noexcept
Definition: span.h:106
SCN_CONSTEXPR span< T > subspan(index_type off) const
Definition: span.h:121
SCN_CONSTEXPR index_type size() const noexcept
Definition: span.h:102
#define SCN_CONSTEXPR
Definition: config.h:226
const value_type * const_pointer
Definition: span.h:41
typename std::remove_cv< T >::type value_type
Definition: span.h:36
#define SCN_EXPECT(cond)
Definition: config.h:371
A view over a contiguous range.
Definition: span.h:33