BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_Map.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_CL_MAP_HPP
13#define BELFEM_CL_MAP_HPP
14
15#include <unordered_map>
16
17
18#include <string>
19
20#include "typedefs.hpp"
21#include "assert.hpp"
22
23namespace belfem
24{
25//------------------------------------------------------------------------------
26
27 namespace map
28 {
29 template < typename T >
30 std::string
31 KeyToString( const T & aKey )
32 {
33 return "unknown";
34 }
35
36 template <>
37 inline std::string
38 KeyToString( const std::string & aKey )
39 {
40 return aKey;
41 }
42
43 template <>
44 inline std::string
45 KeyToString( const int & aKey )
46 {
47 return std::to_string( aKey );
48 }
49
50 template <>
51 inline std::string
52 KeyToString( const unsigned int & aKey )
53 {
54 return std::to_string( aKey );
55 }
56
57 template <>
58 inline std::string
59 KeyToString( const long unsigned int & aKey )
60 {
61 return std::to_string( aKey );
62 }
63 }
64
65//------------------------------------------------------------------------------
66
73 template< typename Key, typename Value >
74 class Map
75 {
76
77 std::unordered_map< Key, Value > mMap;
78
79//------------------------------------------------------------------------------
80 public:
81//------------------------------------------------------------------------------
82
83 using const_iterator = typename std::unordered_map< Key, Value >::const_iterator ;
84
88 explicit Map() = default;
89
90//------------------------------------------------------------------------------
91
95 Map( const Map< Key, Value > & aMap ) = default;
96
97//------------------------------------------------------------------------------
98
102 Map(Map<Key, Value>&& other) noexcept = default;
103
104//------------------------------------------------------------------------------
105
109 Map<Key, Value>& operator=(const Map<Key, Value>& other) = default;
110
111//------------------------------------------------------------------------------
112
116 Map<Key, Value>& operator=(Map<Key, Value>&& other) noexcept = default;
117
118//------------------------------------------------------------------------------
119
123 ~Map() = default;
124
125//------------------------------------------------------------------------------
126
130 void
132 {
133 mMap.clear();
134 }
135
136//------------------------------------------------------------------------------
137
141 size_t
142 size() const
143 {
144 return mMap.size();
145 }
146
147//------------------------------------------------------------------------------
148
152 Value &
153 operator[]( const Key & aKey )
154 {
155 return mMap[ aKey ];
156 }
157
158//------------------------------------------------------------------------------
159
160 bool
161 key_exists( const Key & aKey ) const
162 {
163 return mMap.find( aKey ) != mMap.end();
164 }
165
166//------------------------------------------------------------------------------
167
168 auto
169 begin() const -> decltype( mMap.begin() )
170 {
171 return mMap.begin();
172 }
173
174 auto
175 end() const -> decltype( mMap.end() )
176 {
177 return mMap.end();
178 }
179
180 auto
181 find ( const Key & tKey ) const -> decltype( mMap.find(tKey) )
182 {
183 return mMap.find( tKey );
184 }
185
186 auto
187 empty() const -> decltype( mMap.empty() )
188 {
189 return mMap.empty();
190 }
191
192 auto
193 get_entry( const index_t aIndex ) const -> decltype( * mMap.begin() )
194 {
195 BELFEM_ASSERT( aIndex < mMap.size(),
196 "Map::get_entry() index %lu out of bounds ( must be < %lu )",
197 ( long unsigned int ) aIndex,
198 ( long unsigned int ) mMap.size() );
199
200 auto tEntry = mMap.begin();
201 std::advance( tEntry, aIndex );
202
203 const auto & aEntry = * tEntry;
204 return aEntry;
205 }
206
207//------------------------------------------------------------------------------
208
209 void
210 erase_key( const Key & aKey )
211 {
212 // remove key from map
213 mMap.erase( aKey );
214 }
215//------------------------------------------------------------------------------
216
217 // expose the data container
218 auto
219 map_data() -> decltype( mMap ) &
220 {
221 return mMap ;
222 }
223
224//------------------------------------------------------------------------------
225
229 Value &
230 operator()( const Key & aKey )
231 {
232 // check if key exists
233 auto tIterator = mMap.find( aKey );
234
235#if !defined( NDEBUG ) || defined( DEBUG )
236 BELFEM_ASSERT( tIterator != mMap.end(),
237 "Key %s not found in map.",
238 map::KeyToString( aKey ).c_str() );
239
240#else
241 BELFEM_ERROR( tIterator != mMap.end(),
242 "Key not found in map." );
243#endif
244
245 return tIterator->second;
246 }
247
248//------------------------------------------------------------------------------
249
253 const Value &
254 operator()( const Key & aKey ) const
255 {
256// check if key exists
257 auto tIterator = mMap.find( aKey );
258
259#if !defined( NDEBUG ) || defined( DEBUG )
260
261 BELFEM_ASSERT( tIterator != mMap.end(),
262 "Key %s not found in map.",
263 map::KeyToString( aKey ).c_str() );
264
265#else
266 BELFEM_ERROR( tIterator != mMap.end(),
267 "Key not found in map." );
268#endif
269
270 return tIterator->second;
271 }
272 };
273//------------------------------------------------------------------------------
274} /* namespace belfem */
275#endif //BELFEM_CL_MAP_HPP
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
void clear()
clear map
Definition cl_Map.hpp:131
auto empty() const -> decltype(mMap.empty())
Definition cl_Map.hpp:187
Value & operator()(const Key &aKey)
find operator
Definition cl_Map.hpp:230
auto get_entry(const index_t aIndex) const -> decltype(*mMap.begin())
Definition cl_Map.hpp:193
bool key_exists(const Key &aKey) const
Definition cl_Map.hpp:161
~Map()=default
default destructor
Map()=default
empty constructor
typename std::unordered_map< Key, Value >::const_iterator const_iterator
Definition cl_Map.hpp:83
const Value & operator()(const Key &aKey) const
find operator ( const variant )
Definition cl_Map.hpp:254
Map(const Map< Key, Value > &aMap)=default
copy constructor
Map< Key, Value > & operator=(Map< Key, Value > &&other) noexcept=default
Move assignment operator constructor.
Value & operator[](const Key &aKey)
insert operator
Definition cl_Map.hpp:153
auto end() const -> decltype(mMap.end())
Definition cl_Map.hpp:175
Map(Map< Key, Value > &&other) noexcept=default
move constructor
Map< Key, Value > & operator=(const Map< Key, Value > &other)=default
Copy assignment operator.
auto begin() const -> decltype(mMap.begin())
Definition cl_Map.hpp:169
size_t size() const
returns the size of the map
Definition cl_Map.hpp:142
auto map_data() -> decltype(mMap) &
Definition cl_Map.hpp:219
auto find(const Key &tKey) const -> decltype(mMap.find(tKey))
Definition cl_Map.hpp:181
void erase_key(const Key &aKey)
Definition cl_Map.hpp:210
Definition cl_Map.hpp:28
std::string KeyToString(const T &aKey)
Definition cl_Map.hpp:31
USER GUIDES:
Definition cl_Capacitor.cpp:16
uint32_t index_t
Definition typedefs.hpp:52