flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
array_view.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef ARRAY_VIEW_H
20#define ARRAY_VIEW_H
21
22#include "error.hpp"
23#include <cstddef>
24#include <ostream>
25#include <string>
26
27namespace flexiblesusy {
28
71template <typename ElementType>
73{
74public:
75 using Element_t = ElementType;
76 using Index_t = std::ptrdiff_t;
81
82 constexpr Dynamic_array_view() noexcept = default;
83 constexpr Dynamic_array_view(Pointer_t p, Index_t l) noexcept
84 : ptr(p), len(l) {}
85 constexpr Dynamic_array_view(Pointer_t first, Pointer_t last) noexcept
86 : ptr(first), len(std::distance(first, last)) {}
87 template <std::size_t N>
88 constexpr Dynamic_array_view(ElementType (&arr)[N]) noexcept
89 : ptr(&arr[0]), len(N) {}
90
91 constexpr Pointer_t data() const noexcept { return ptr; }
92 constexpr bool empty() const noexcept { return size() == 0; }
93 constexpr Index_t size() const noexcept { return len; }
94
95 Iterator_t begin() const noexcept { return ptr; }
96 Iterator_t end() const noexcept { return ptr + len; }
97
98 Const_iterator_t cbegin() const noexcept { return ptr; }
99 Const_iterator_t cend() const noexcept { return ptr + len; }
100
102 check_range(idx);
103 return ptr[idx];
104 }
105
107 check_range(idx);
108 return ptr[idx];
109 }
110
111private:
112 Pointer_t ptr{nullptr};
114
115 void check_range(Index_t idx) const {
116 if (idx < 0 || idx >= len) {
117 throw OutOfBoundsError(
118 "Dynamic_array_view index " + std::to_string(idx)
119 + " out of range [0, " + std::to_string(len) + ").");
120 }
121 }
122};
123
124template <typename ElementType>
125constexpr Dynamic_array_view<ElementType> make_dynamic_array_view(ElementType* ptr, std::ptrdiff_t len)
126{
127 return Dynamic_array_view<ElementType>(ptr, len);
128}
129
130template <typename ElementType>
131constexpr Dynamic_array_view<ElementType> make_dynamic_array_view(ElementType* first, ElementType* last)
132{
133 return Dynamic_array_view<ElementType>(first, last);
134}
135
136template <typename ElementType, std::size_t N>
138{
139 return Dynamic_array_view<ElementType>(arr, N);
140}
141
142template <typename ElementType>
143std::ostream& operator<<(std::ostream& ostr, const Dynamic_array_view<ElementType>& av)
144{
145 if (av.empty()) {
146 return ostr;
147 }
148
149 ostr << "[";
150
151 for (typename Dynamic_array_view<ElementType>::Index_t i = 0; i < av.size(); i++) {
152 ostr << av[i];
153 if (i < av.size() - 1) {
154 ostr << ", ";
155 }
156 }
157
158 ostr << "]";
159
160 return ostr;
161}
162
163} // namespace flexiblesusy
164
165#endif
Iterator_t end() const noexcept
Definition: array_view.hpp:96
constexpr Index_t size() const noexcept
Definition: array_view.hpp:93
Reference_t operator[](Index_t idx)
Definition: array_view.hpp:101
constexpr Dynamic_array_view(Pointer_t first, Pointer_t last) noexcept
Definition: array_view.hpp:85
constexpr Dynamic_array_view(ElementType(&arr)[N]) noexcept
Definition: array_view.hpp:88
Iterator_t begin() const noexcept
Definition: array_view.hpp:95
Const_iterator_t cend() const noexcept
Definition: array_view.hpp:99
void check_range(Index_t idx) const
Definition: array_view.hpp:115
Element_t operator[](Index_t idx) const
Definition: array_view.hpp:106
constexpr Pointer_t data() const noexcept
Definition: array_view.hpp:91
Const_iterator_t cbegin() const noexcept
Definition: array_view.hpp:98
constexpr bool empty() const noexcept
Definition: array_view.hpp:92
constexpr Dynamic_array_view() noexcept=default
Out of bounds access.
Definition: error.hpp:208
constexpr Dynamic_array_view< ElementType > make_dynamic_array_view(ElementType *ptr, std::ptrdiff_t len)
Definition: array_view.hpp:125
std::string to_string(char a)
std::ostream & operator<<(std::ostream &ostr, const Dynamic_array_view< ElementType > &av)
Definition: array_view.hpp:143