OpenLB 1.7
Loading...
Searching...
No Matches
typeIndexedContainers.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021-22 Adrian Kummerlaender, Julius Jessberger
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef TYPE_INDEXED_CONTAINERS_H
25#define TYPE_INDEXED_CONTAINERS_H
26
27#include <vector>
28#include <optional>
29#include <tuple>
30
31#include "core/meta.h"
32#include "utilities/typeMap.h"
33
34namespace olb {
35
36namespace utilities {
37
38template<typename KEYS, typename VALUE>
40private:
41 std::array<VALUE,KEYS::size> _index;
42
43public:
44 template<typename TYPE>
45 static constexpr bool provides() {
46 return KEYS::template contains<TYPE>();
47 }
48
49 template<typename TYPE>
50 const VALUE& get() const {
51 return _index[KEYS::template index<TYPE>()];
52 }
53 template<typename TYPE>
54 VALUE& get() {
55 return _index[KEYS::template index<TYPE>()];
56 }
57
58 template<typename TYPE>
59 void set(VALUE value) {
60 _index[KEYS::template index<TYPE>()] = value;
61 }
62
63};
64
66
71template<typename VALUE, typename CONTEXT=void>
73private:
74 std::vector<std::optional<VALUE>> _index;
75
76 static std::size_t next_index();
77 template <typename TYPE>
78 static std::size_t get_index();
79
80public:
82 _index.resize(1);
83 }
84
85 template<typename TYPE>
86 bool provides() const;
87
88 template<typename TYPE>
89 std::size_t index() const;
90
91 template<typename TYPE>
92 const VALUE& get() const;
93 template<typename TYPE>
94 VALUE& get();
95
96 template<typename TYPE>
97 void set(VALUE value);
98
99};
100
101template <typename VALUE, typename CONTEXT>
103{
104 std::size_t curr;
105 #ifdef PARALLEL_MODE_OMP
106 #pragma omp critical
107 #endif
108 {
109 static std::size_t index = 0;
110 curr = index++;
111 }
112 return curr;
113}
114
115template <typename VALUE, typename CONTEXT>
116template <typename TYPE>
117std::size_t TypeIndexedMap<VALUE,CONTEXT>::get_index()
118{
119 static std::size_t index = next_index();
120 return index;
121}
122
123template <typename VALUE, typename CONTEXT>
124template <typename TYPE>
126{
127 const std::size_t index = get_index<TYPE>();
128 if (index >= _index.size()) {
129 return false;
130 } else {
131 return _index[index].has_value();
132 }
133}
134
135template <typename VALUE, typename CONTEXT>
136template <typename TYPE>
138{
139 return get_index<TYPE>();
140}
141
142template <typename VALUE, typename CONTEXT>
143template <typename TYPE>
145{
146 const std::size_t index = get_index<TYPE>();
147 if (auto& value = _index[index]) {
148 return value.value();
149 } else {
150 throw std::out_of_range("VALUE for TYPE doesn't exist as it was not set.");
151 }
152}
153
154template <typename VALUE, typename CONTEXT>
155template <typename TYPE>
157{
158 const std::size_t index = get_index<TYPE>();
159 if (auto& value = _index[index]) {
160 return value.value();
161 } else {
162 throw std::out_of_range("VALUE for TYPE doesn't exist as it was not set.");
163 }
164}
165
166template <typename VALUE, typename CONTEXT>
167template <typename TYPE>
169{
170 const std::size_t index = get_index<TYPE>();
171 if (index >= _index.size()) {
172 _index.resize(2*index);
173 }
174 _index[index] = value;
175}
176
177
179
182template <typename MAP>
185 using tuple_t = typename MAP::values_t::template decompose_into<std::tuple>;
186 using map_t = MAP;
188
192
193 template <typename... KEYS>
194 struct generator {
195 template <typename F>
197 // Don't use std::make_tuple here due to weird incompatability with CUDA 11.7+
198 return std::tuple<decltype(f(meta::id<KEYS>{}))...>(f(meta::id<KEYS>{})...);
199 }
200 };
201
202 template <typename F>
204 tuple{typename MAP::keys_t::template decompose_into<generator>{}(f)} { }
205
206 template <typename KEY>
207 using value_t = typename MAP::template value<KEY>;
208
210 template <typename KEY>
212 return MAP::keys_t::template contains<KEY>();
213 }
214
216 template <unsigned I>
217 constexpr auto& get() any_platform {
218 return std::get<I>(tuple);
219 }
221 template <unsigned I>
222 constexpr const auto& get() const any_platform {
223 return std::get<I>(tuple);
224 }
225
227 template <typename KEY>
229 return get<(MAP::keys_t::template index<KEY>())>();
230 }
232 template <typename KEY>
233 constexpr const auto& get(meta::id<KEY> = meta::id<KEY>{}) const any_platform {
234 return get<(MAP::keys_t::template index<KEY>())>();
235 }
236
238 template <typename KEY>
239 constexpr void set(typename MAP::template value<KEY> value) any_platform {
240 get<KEY>() = value;
241 }
242
243 template <typename KEY>
244 constexpr void set(meta::id<KEY>, typename MAP::template value<KEY> value) any_platform {
245 get<KEY>() = value;
246 }
247
249 template <typename F>
250 constexpr void for_each(F f) any_platform {
251 MAP::keys_t::for_each([&](auto key) {
252 f(key, get(key));
253 });
254 }
255
257 template <typename F>
258 constexpr auto exchange_values(F f) any_platform {
260 typename MAP::keys_t,
261 typename MAP::keys_t::template map_to_callable_result<F>
262 >>(f);
263 }
264
265};
266
267template <typename MAP>
269 typename MAP::template map_values<std::shared_ptr>
270>;
271
272}
273
274}
275
276#endif
(Time) efficient mapping between TYPEs and VALUEs
Top level namespace for all of OpenLB.
#define any_platform
Define preprocessor macros for device-side functions, constant storage.
Definition platform.h:78
Identity type to pass non-constructible types as value.
Definition meta.h:79
Mapping between KEYs and instances of type VALUEs.
constexpr void set(meta::id< KEY >, typename MAP::template value< KEY > value) any_platform
constexpr auto & get() any_platform
Access Ith element.
constexpr void set(typename MAP::template value< KEY > value) any_platform
Set value assigned to KEY of MAP.
constexpr bool contains(meta::id< KEY >=meta::id< KEY >{}) any_platform
Return true iff MAP contains KEY.
constexpr void for_each(F f) any_platform
Calls f(name, value) for all pairs.
typename MAP::template value< KEY > value_t
constexpr const auto & get() const any_platform
Access Ith element.
TypeIndexedTuple() any_platform=default
constexpr auto exchange_values(F f) any_platform
Returns new TypeIndexedTuple with same keys but values generated by f(key)
constexpr auto & get(meta::id< KEY >=meta::id< KEY >{}) any_platform
Access by KEY of MAP.
constexpr const auto & get(meta::id< KEY >=meta::id< KEY >{}) const any_platform
Access by KEY of MAP.
typename MAP::values_t::template decompose_into< std::tuple > tuple_t
Storage of values in MAP-order.