NGen
Loading...
Searching...
No Matches
variable.hpp
1#ifndef NGEN_IO_MDFRAME_VARIABLE_HPP
2#define NGEN_IO_MDFRAME_VARIABLE_HPP
3
4#include "mdarray.hpp"
5#include "traits.hpp"
6#include "dimension.hpp"
7#include <initializer_list>
8#include <functional>
9
10#include "visitors.hpp"
11
12namespace ngen {
13
14namespace detail {
15
23template<typename... SupportedTypes>
24struct variable {
25 using size_type = std::size_t;
26
32 using types = traits::type_list<SupportedTypes...>;
33
40 using mdarray_variant = typename types::template variant_container<ngen::mdarray>;
41
43
44 // Hasher for variables
45 struct hash
46 {
47 std::size_t operator()(const variable& v) const noexcept
48 {
49 return std::hash<std::string>{}(v.m_name);
50 }
51
52 static std::size_t apply(const variable& d) noexcept
53 {
54 return variable::hash{}(d);
55 }
56 };
57
61 variable() noexcept
62 : m_name()
63 , m_data()
64 , m_dimensions() {};
65
72 template<typename T, typename types::template enable_if_supports<T, bool> = true>
73 static variable make(const std::string& name, const std::vector<dimension>& dimensions)
74 {
75 variable var;
76 var.m_name = name;
78
79 std::vector<size_type> dsizes;
80 dsizes.reserve(dimensions.size());
81 for (const auto& d : dimensions) {
82 dsizes.push_back(d.size());
83 }
84
85 var.m_data = mdarray<T>{dsizes};
86 return var;
87 }
88
96 template<typename T, typename types::template enable_if_supports<T, bool> = true>
98 {
99 this->m_data = data;
100 }
101
109 template<typename T, typename types::template enable_if_supports<T, bool> = true>
111 {
112 this->m_data = std::move(data);
113 }
114
120 const mdarray_variant& values() const noexcept
121 {
122 return this->m_data;
123 }
124
125 template<typename T, typename types::template enable_if_supports<T, bool> = true>
126 const mdarray<T>& values() const noexcept
127 {
128 return boost::get<mdarray<T>>(this->m_data);
129 }
130
141 bool operator==(const variable& rhs) const
142 {
143 return this->m_name == rhs.m_name;
144 }
145
151 const std::string& name() const noexcept {
152 return this->m_name;
153 }
154
160 std::vector<std::string> dimensions() const noexcept {
161 std::vector<std::string> names;
162 names.reserve(this->m_dimensions.size());
163 for (const dimension& dim : this->m_dimensions) {
164 names.push_back(dim.name());
165 }
166
167 return names;
168 }
169
177 size_type size() const noexcept {
178 return boost::apply_visitor(visitors::mdarray_size{}, this->m_data);
179 }
180
188 size_type rank() const noexcept {
189 return boost::apply_visitor(visitors::mdarray_rank{}, this->m_data);
190 }
191
201 template<typename T, typename types::template enable_if_supports<T, bool> = true>
202 void insert(boost::span<const size_type> index, T value)
203 {
204 // bind arguments to operator()
205 auto visitor = std::bind(
207 std::placeholders::_1,
208 index,
209 value
210 );
211
212 boost::apply_visitor(visitor, this->m_data);
213 }
214
225 element_type at(const boost::span<const size_type> index)
226 {
227 auto visitor = std::bind(
229 std::placeholders::_1,
230 index
231 );
232
233 return boost::apply_visitor(visitor, this->m_data);
234 }
235
236 template<typename T, typename types::template enable_if_supports<T, bool> = true>
237 T at(boost::span<const size_type> index)
238 {
239 auto visitor = std::bind(
241 std::placeholders::_1,
242 index
243 );
244
245 element_type result = boost::apply_visitor(visitor, this->m_data);
246 T value = boost::get<T>(result);
247 return value;
248 }
249
250 boost::span<const size_type> shape() const noexcept
251 {
252 return boost::apply_visitor(visitors::mdarray_shape{}, this->m_data);
253 }
254
255 private:
256 // Name of this variable
257 mutable std::string m_name;
258
259 // multi-dimensional vector associated with this variable
261
262 // References to dimensions that this variable spans
263 std::vector<dimension> m_dimensions;
264};
265
266} // namespace detail
267
268} // namespace ngen
269
270#endif // NGEN_IO_MDFRAME_VARIABLE_HPP
Definition mdarray.hpp:13
Definition DomainLayer.hpp:9
Dimension Key.
Definition dimension.hpp:19
Definition variable.hpp:46
static std::size_t apply(const variable &d) noexcept
Definition variable.hpp:52
std::size_t operator()(const variable &v) const noexcept
Definition variable.hpp:47
Variable Key.
Definition variable.hpp:24
T at(boost::span< const size_type > index)
Definition variable.hpp:237
variable & set_data(mdarray< T > &&data)
Assign an mdarray to this variable.
Definition variable.hpp:110
variable() noexcept
Constructs an empty variable.
Definition variable.hpp:61
void insert(boost::span< const size_type > index, T value)
Construct and insert a mdvalue into the backing mdarray.
Definition variable.hpp:202
bool operator==(const variable &rhs) const
Equality operator to check equality between two variables.
Definition variable.hpp:141
typename types::variant_scalar element_type
Definition variable.hpp:42
static variable make(const std::string &name, const std::vector< dimension > &dimensions)
Constructs a named variable spanned over the given dimensions.
Definition variable.hpp:73
size_type size() const noexcept
Get the size of this variable.
Definition variable.hpp:177
element_type at(const boost::span< const size_type > index)
Access value at a given index.
Definition variable.hpp:225
std::vector< std::string > dimensions() const noexcept
Get the names of all dimensions associated with this variable.
Definition variable.hpp:160
variable & set_data(const mdarray< T > &data)
Assign an mdarray to this variable.
Definition variable.hpp:97
typename types::template variant_container< ngen::mdarray > mdarray_variant
A boost::variant type consisting of mdarrays of the support types, i.e.
Definition variable.hpp:40
const mdarray_variant & values() const noexcept
Get the values of this variable.
Definition variable.hpp:120
std::string m_name
Definition variable.hpp:257
boost::span< const size_type > shape() const noexcept
Definition variable.hpp:250
std::size_t size_type
Definition variable.hpp:25
mdarray_variant m_data
Definition variable.hpp:260
const std::string & name() const noexcept
Get the name of this variable.
Definition variable.hpp:151
size_type rank() const noexcept
Get the rank of this variable.
Definition variable.hpp:188
const mdarray< T > & values() const noexcept
Definition variable.hpp:126
std::vector< dimension > m_dimensions
Definition variable.hpp:263
mdarray visitor for indexed access
Definition visitors.hpp:73
mdarray visitor for inserting a value
Definition visitors.hpp:57
mdarray visitor for retrieving the rank of the mdarray
Definition visitors.hpp:31
mdarray visitor for retrieving the shape of the mdarray
Definition visitors.hpp:44
mdarray visitor for retrieving the size of the mdarray
Definition visitors.hpp:18
Definition traits.hpp:62
boost::variant< Ts... > variant_scalar
Provides a type alias for a boost::variant containing the types of this type list.
Definition traits.hpp:71