span.h Source File

CPP API: span.h Source File
span.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_UTILS_SPAN_H
21 #define MIO_UTILS_SPAN_H
22 
23 #include <utility>
24 #include <cstddef>
25 
26 namespace mio
27 {
28 
32 template <class T>
33 class Span
34 {
35 public:
39  Span() = default;
40 
45  template <class Cont>
46  Span(const Cont& c)
47  : m_ptr(c.size() == 0 ? nullptr : c.data())
48  , m_size(c.size())
49  {
50  }
51 
55  template <size_t N>
56  Span(T (&p)[N])
57  : m_ptr(p)
58  , m_size(N)
59  {
60  }
61 
65  Span(const T* ptr, size_t size)
66  : m_ptr(ptr)
67  , m_size(size)
68  {
69  }
70 
74  const T* get_ptr() const
75  {
76  return m_ptr;
77  }
78 
82  const T* begin() const
83  {
84  return m_ptr;
85  }
86 
90  const T* end() const
91  {
92  return m_ptr + m_size;
93  }
94 
98  size_t size() const
99  {
100  return m_size;
101  }
102 
103 private:
104  const T* m_ptr = nullptr;
105  size_t m_size = 0;
106 };
107 
108 } // namespace mio
109 
110 #endif // MIO_UTILS_SPAN_H
a reference to any contigiuous array of objects.
Definition: span.h:34
const T * begin() const
get an iterator to the first element.
Definition: span.h:82
Span(const T *ptr, size_t size)
construct from ptr and size.
Definition: span.h:65
const T * end() const
get an iterator to one past the last element.
Definition: span.h:90
Span()=default
empty span.
size_t m_size
Definition: span.h:105
Span(const Cont &c)
construct from a container.
Definition: span.h:46
const T * m_ptr
Definition: span.h:104
size_t size() const
get the number of elements in the array
Definition: span.h:98
const T * get_ptr() const
get the adress of the beginning of array.
Definition: span.h:74
Span(T(&p)[N])
construct from a c array.
Definition: span.h:56
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30