Dune Core Modules (unstable)

propertymap.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_PROPERTYMAP_HH
6#define DUNE_PROPERTYMAP_HH
7
8#include <cstddef>
9#include <iterator>
10#include <type_traits>
11
12namespace Dune
13{
14
15 template<class PM>
16 struct PropertyMapTraits
17 {
21 typedef typename PM::KeyType KeyType;
25 typedef typename PM::ValueType ValueType;
29 typedef typename PM::Reference Reference;
33 typedef typename PM::Category Category;
34 };
35
38 {};
39
42 {};
43
50 {};
51
57 {};
58
59 template<class T>
60 struct PropertyMapTraits<T*>
61 {
62 typedef T ValueType;
63 typedef ValueType& Reference;
64 typedef std::ptrdiff_t KeyType;
65 typedef LvaluePropertyMapTag Category;
66 };
67
68
69 template<class T>
70 struct PropertyMapTraits<const T*>
71 {
72 typedef T ValueType;
73 typedef const ValueType& Reference;
74 typedef std::ptrdiff_t KeyType;
75 typedef LvaluePropertyMapTag Category;
76 };
77
78 template<class Reference, class PropertyMap>
79 struct RAPropertyMapHelper
80 {};
81
82 template<class Reference, class PropertyMap, class Key>
83 inline Reference
84 get(const RAPropertyMapHelper<Reference,PropertyMap>& pmap,
85 const Key& key)
86 {
87 return static_cast<const PropertyMap&>(pmap)[key];
88 }
89
90 template<class Reference, class PropertyMap, class Key, class Value>
91 inline void
92 put(const RAPropertyMapHelper<Reference,PropertyMap>& pmap,
93 const Key& key, const Value& value)
94 {
95 static_assert(std::is_convertible<typename PropertyMap::Category,WritablePropertyMapTag>::value,
96 "WritablePropertyMapTag required!");
97 static_cast<const PropertyMap&>(pmap)[key] = value;
98 }
99
103 template<class RAI, class IM,
104 class T = typename std::iterator_traits<RAI>::value_type,
105 class R = typename std::iterator_traits<RAI>::reference>
107 : public RAPropertyMapHelper<R,IteratorPropertyMap<RAI,IM,T,R> >
108 {
109 public:
114
120 typedef IM IndexMap;
121
125 typedef typename IndexMap::KeyType KeyType;
126
130 typedef T ValueType;
131
135 typedef R Reference;
136
141
150 const IndexMap& im=IndexMap())
151 : iter_(iter), indexMap_(im)
152 {}
153
156 : iter_(), indexMap_()
157 {}
158
160 inline Reference operator[](KeyType key) const
161 {
162 return *(iter_ + get(indexMap_, key));
163 }
164
165 private:
169 IndexMap indexMap_;
170 };
171
176 template<typename T>
178 : RAPropertyMapHelper<typename T::value_type::second_type&,
179 AssociativePropertyMap<T> >
180 {
184 typedef T UniqueAssociativeContainer;
185
189 typedef typename UniqueAssociativeContainer::value_type::first_type
190 KeyType;
191
195 typedef typename UniqueAssociativeContainer::value_type::second_type
196 ValueType;
197
201 typedef ValueType& Reference;
202
207
210 : map_(0)
211 {}
212
214 inline AssociativePropertyMap(UniqueAssociativeContainer& map)
215 : map_(&map)
216 {}
217
222 inline Reference operator[](KeyType key) const
223 {
224 return map_->find(key)->second;
225 }
226 private:
227 UniqueAssociativeContainer* map_;
228 };
229
234 template<typename T>
236 : RAPropertyMapHelper<const typename T::value_type::second_type&,
237 ConstAssociativePropertyMap<T> >
238 {
242 typedef T UniqueAssociativeContainer;
243
247 typedef typename UniqueAssociativeContainer::value_type::first_type
248 KeyType;
249
253 typedef typename UniqueAssociativeContainer::value_type::second_type
254 ValueType;
255
259 typedef const ValueType& Reference;
260
265
268 : map_(0)
269 {}
270
272 inline ConstAssociativePropertyMap(const UniqueAssociativeContainer& map)
273 : map_(&map)
274 {}
275
280 inline Reference operator[](KeyType key) const
281 {
282 return map_->find(key)->second;
283 }
284 private:
285 const UniqueAssociativeContainer* map_;
286 };
287
292 : public RAPropertyMapHelper<std::size_t, IdentityMap>
293 {
295 typedef std::size_t KeyType;
296
298 typedef std::size_t ValueType;
299
301 typedef std::size_t Reference;
302
305
306 inline ValueType operator[](const KeyType& key) const
307 {
308 return key;
309 }
310 };
311
312
318 template<typename T, typename C>
320 {
324 typedef T Tag;
329 typedef C Container;
330 };
331
332}
333
334#endif
An adapter to turn an unique associative container into a property map.
Definition: propertymap.hh:180
An adaptor to turn an unique associative container into a property map.
Definition: propertymap.hh:238
Adapter to turn a random access iterator into a property map.
Definition: propertymap.hh:108
IndexMap::KeyType KeyType
The key type of the property map.
Definition: propertymap.hh:125
R Reference
The reference type of the property map.
Definition: propertymap.hh:135
T ValueType
The value type of the property map.
Definition: propertymap.hh:130
Reference operator[](KeyType key) const
Access the a value by reference.
Definition: propertymap.hh:160
IteratorPropertyMap(RandomAccessIterator iter, const IndexMap &im=IndexMap())
Constructor.
Definition: propertymap.hh:149
IteratorPropertyMap()
Constructor.
Definition: propertymap.hh:155
IM IndexMap
The type of the index map.
Definition: propertymap.hh:120
RAI RandomAccessIterator
The type of the random access iterator.
Definition: propertymap.hh:113
LvaluePropertyMapTag Category
The category of this property map.
Definition: propertymap.hh:140
Dune namespace.
Definition: alignedallocator.hh:13
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
A property map that applies the identity function to integers.
Definition: propertymap.hh:293
std::size_t ValueType
The value type of the map.
Definition: propertymap.hh:298
std::size_t KeyType
The key type of the map.
Definition: propertymap.hh:295
std::size_t Reference
The reference type of the map.
Definition: propertymap.hh:301
ReadablePropertyMapTag Category
The category of the map.
Definition: propertymap.hh:304
Tag for the category of lvalue property maps.
Definition: propertymap.hh:57
Selector for the property map type.
Definition: propertymap.hh:320
T Tag
the tag identifying the property.
Definition: propertymap.hh:324
C Container
The container type to whose entries the properties are attached.
Definition: propertymap.hh:329
Tag for the category of readable and writable property maps.
Definition: propertymap.hh:50
Tag for the category of readable property maps.
Definition: propertymap.hh:38
Tag for the category of writable property maps.
Definition: propertymap.hh:42
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)