MiniVector.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA
3 This file is part of the Open Porous Media project (OPM).
4 OPM is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 OPM is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with OPM. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16#ifndef OPM_GPUISTL_MINIVECTOR_HPP
17#define OPM_GPUISTL_MINIVECTOR_HPP
18
19#include <algorithm>
20#include <array>
21#include <cstddef>
22#include <initializer_list>
23#include <stdexcept>
24#include <type_traits>
25
26#include <opm/common/ErrorMacros.hpp>
27#include <opm/common/utility/gpuDecorators.hpp>
28
39namespace Opm::gpuistl
40{
41
46template <class T, int Dimension>
48{
49 static_assert(Dimension > 0, "Dimension must be positive");
50
51public:
53 using value_type = T;
55 using size_type = std::size_t;
61 using iterator = typename std::array<T, Dimension>::iterator;
63 using const_iterator = typename std::array<T, Dimension>::const_iterator;
64
68 OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible<value_type>::value) = default;
69
74 OPM_HOST_DEVICE constexpr explicit MiniVector(const value_type& value)
75 {
76 data_.fill(value);
77 }
78
91 OPM_HOST_DEVICE MiniVector(std::initializer_list<value_type> init)
92 {
93 if (init.size() != Dimension) {
94 OPM_THROW(std::runtime_error, "Opm::MiniVector – initializer‑list size mismatch");
95 }
96 std::copy_n(init.begin(), Dimension, data_.begin());
97 }
98
102 OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
103 {
104 return data_[idx];
105 }
106
108 OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
109 {
110 return data_[idx];
111 }
112
117 OPM_HOST_DEVICE reference at(size_type idx)
118 {
119 if (idx >= Dimension) {
120 OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
121 }
122 return data_[idx];
123 }
125 OPM_HOST_DEVICE const_reference at(size_type idx) const
126 {
127 if (idx >= Dimension) {
128 OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
129 }
130 return data_[idx];
131 }
132
134 OPM_HOST_DEVICE constexpr iterator begin() noexcept
135 {
136 return data_.begin();
137 }
139 OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
140 {
141 return data_.begin();
142 }
144 OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
145 {
146 return data_.cbegin();
147 }
148
150 OPM_HOST_DEVICE constexpr iterator end() noexcept
151 {
152 return data_.end();
153 }
155 OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
156 {
157 return data_.end();
158 }
160 OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
161 {
162 return data_.cend();
163 }
164
166 OPM_HOST_DEVICE static constexpr size_type size() noexcept
167 {
168 return Dimension;
169 }
170
171
176 OPM_HOST_DEVICE constexpr void fill(const value_type& value)
177 {
178 data_.fill(value);
179 }
180
182 OPM_HOST_DEVICE constexpr bool operator==(const MiniVector& other) const noexcept
183 {
184 return std::equal(data_.begin(), data_.end(), other.data_.begin());
185 }
186
188 OPM_HOST_DEVICE constexpr bool operator!=(const MiniVector& other) const noexcept
189 {
190 return !(*this == other);
191 }
192
193private:
195 std::array<value_type, Dimension> data_ {};
196};
197
198} // namespace Opm::gpuistl
199
200#endif // OPM_GPUISTL_MINIVECTOR_HPP
Definition: MiniVector.hpp:48
T value_type
Element type.
Definition: MiniVector.hpp:53
OPM_HOST_DEVICE reference at(size_type idx)
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:117
OPM_HOST_DEVICE constexpr bool operator!=(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:188
typename std::array< T, Dimension >::const_iterator const_iterator
Immutable iterator.
Definition: MiniVector.hpp:63
std::size_t size_type
Index/size type.
Definition: MiniVector.hpp:55
OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
Definition: MiniVector.hpp:139
OPM_HOST_DEVICE MiniVector(std::initializer_list< value_type > init)
Initializer‑list constructor.
Definition: MiniVector.hpp:91
OPM_HOST_DEVICE constexpr void fill(const value_type &value)
Fill every component with the supplied value.
Definition: MiniVector.hpp:176
OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
Definition: MiniVector.hpp:155
OPM_HOST_DEVICE constexpr iterator begin() noexcept
Definition: MiniVector.hpp:134
const value_type & const_reference
Immutable element reference.
Definition: MiniVector.hpp:59
static OPM_HOST_DEVICE constexpr size_type size() noexcept
Definition: MiniVector.hpp:166
OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
Definition: MiniVector.hpp:102
typename std::array< T, Dimension >::iterator iterator
Mutable iterator.
Definition: MiniVector.hpp:61
OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
Definition: MiniVector.hpp:108
OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
Definition: MiniVector.hpp:160
OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible< value_type >::value)=default
Default‑constructs the MiniVector; elements are value‑initialized.
OPM_HOST_DEVICE constexpr iterator end() noexcept
Definition: MiniVector.hpp:150
OPM_HOST_DEVICE const_reference at(size_type idx) const
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:125
OPM_HOST_DEVICE constexpr bool operator==(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:182
OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
Definition: MiniVector.hpp:144
value_type & reference
Mutable element reference.
Definition: MiniVector.hpp:57
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition: AmgxInterface.hpp:38