BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_Set.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
13#ifndef BELFEM_CL_SET_HPP
14#define BELFEM_CL_SET_HPP
15
16#include <unordered_set>
17#include <string>
18
19#include "assert.hpp"
20
21namespace belfem
22{
23//------------------------------------------------------------------------------
24
31 template< typename Key >
32 class Set
33 {
34 std::unordered_set< Key > mSet;
35
36//------------------------------------------------------------------------------
37 public:
38//------------------------------------------------------------------------------
39
40 using iterator = typename std::unordered_set< Key >::iterator;
41 using const_iterator = typename std::unordered_set< Key >::const_iterator;
42
46 Set() = default;
47
48//------------------------------------------------------------------------------
49
53 Set( std::initializer_list< Key > aInit ) : mSet( aInit )
54 {
55 }
56
57//------------------------------------------------------------------------------
58
62 template< typename InputIt >
63 Set( InputIt aFirst, InputIt aLast ) : mSet( aFirst, aLast )
64 {
65 }
66
67//------------------------------------------------------------------------------
68
72 Set( const Set< Key > & aSet ) = default;
73
74//------------------------------------------------------------------------------
75
79 Set( Set< Key > && aSet ) noexcept = default;
80
81//------------------------------------------------------------------------------
82
86 Set< Key > & operator=( const Set< Key > & aSet ) = default;
87
88//------------------------------------------------------------------------------
89
93 Set< Key > & operator=( Set< Key > && aSet ) noexcept = default;
94
95//------------------------------------------------------------------------------
96
100 ~Set() = default;
101
102//------------------------------------------------------------------------------
103
107 void
109 {
110 mSet.clear();
111 }
112
113//------------------------------------------------------------------------------
114
118 size_t
119 size() const
120 {
121 return mSet.size();
122 }
123
124//------------------------------------------------------------------------------
125
129 bool
130 empty() const
131 {
132 return mSet.empty();
133 }
134
135//------------------------------------------------------------------------------
136
141 std::pair< iterator, bool >
142 insert( const Key & aKey )
143 {
144 return mSet.insert( aKey );
145 }
146
147//------------------------------------------------------------------------------
148
152 std::pair< iterator, bool >
153 insert( Key && aKey )
154 {
155 return mSet.insert( std::move( aKey ) );
156 }
157
158//------------------------------------------------------------------------------
159
163 template< typename InputIt >
164 void
165 insert( InputIt aFirst, InputIt aLast )
166 {
167 mSet.insert( aFirst, aLast );
168 }
169
170//------------------------------------------------------------------------------
171
175 template< typename... Args >
176 std::pair< iterator, bool >
177 emplace( Args&&... args )
178 {
179 return mSet.emplace( std::forward< Args >( args )... );
180 }
181
182//------------------------------------------------------------------------------
183
187 bool
188 key_exists( const Key & aKey ) const
189 {
190 return mSet.find( aKey ) != mSet.end();
191 }
192
193//------------------------------------------------------------------------------
194
198 bool
199 contains( const Key & aKey ) const
200 {
201 return this->key_exists( aKey );
202 }
203
204//------------------------------------------------------------------------------
205
209 size_t
210 count( const Key & aKey ) const
211 {
212 return mSet.count( aKey );
213 }
214
215//------------------------------------------------------------------------------
216
221 find( const Key & aKey )
222 {
223 return mSet.find( aKey );
224 }
225
226//------------------------------------------------------------------------------
227
232 find( const Key & aKey ) const
233 {
234 return mSet.find( aKey );
235 }
236
237//------------------------------------------------------------------------------
238
243 size_t
244 erase( const Key & aKey )
245 {
246 return mSet.erase( aKey );
247 }
248
249//------------------------------------------------------------------------------
250
256 {
257 return mSet.erase( aPos );
258 }
259
260//------------------------------------------------------------------------------
261
267 {
268 return mSet.erase( aFirst, aLast );
269 }
270
271//------------------------------------------------------------------------------
272
276 void
277 reserve( size_t n )
278 {
279 mSet.reserve( n );
280 }
281
282//------------------------------------------------------------------------------
283
289 {
290 return mSet.begin();
291 }
292
293//------------------------------------------------------------------------------
294
299 begin() const
300 {
301 return mSet.begin();
302 }
303
304//------------------------------------------------------------------------------
305
311 {
312 return mSet.end();
313 }
314
315//------------------------------------------------------------------------------
316
321 end() const
322 {
323 return mSet.end();
324 }
325
326//------------------------------------------------------------------------------
327
331 std::unordered_set< Key > &
333 {
334 return mSet;
335 }
336
337//------------------------------------------------------------------------------
338
342 const std::unordered_set< Key > &
343 set_data() const
344 {
345 return mSet;
346 }
347
348//------------------------------------------------------------------------------
349
353 void
354 swap( Set< Key > & aOther )
355 {
356 mSet.swap( aOther.mSet );
357 }
358
359//------------------------------------------------------------------------------
360
366 operator|( const Set< Key > & aOther ) const
367 {
368 Set< Key > tResult( *this );
369 tResult.insert( aOther.begin(), aOther.end() );
370 return tResult;
371 }
372
373//------------------------------------------------------------------------------
374
380 operator&( const Set< Key > & aOther ) const
381 {
382 Set< Key > tResult;
383 for ( const auto & tKey : mSet )
384 {
385 if ( aOther.contains( tKey ) )
386 {
387 tResult.insert( tKey );
388 }
389 }
390 return tResult;
391 }
392
393//------------------------------------------------------------------------------
394
400 operator-( const Set< Key > & aOther ) const
401 {
402 Set< Key > tResult;
403 for ( const auto & tKey : mSet )
404 {
405 if ( !aOther.contains( tKey ) )
406 {
407 tResult.insert( tKey );
408 }
409 }
410 return tResult;
411 }
412
413//------------------------------------------------------------------------------
414
420 operator^( const Set< Key > & aOther ) const
421 {
422 Set< Key > tResult;
423
424 // Add elements from this set not in other
425 for ( const auto & tKey : mSet )
426 {
427 if ( !aOther.contains( tKey ) )
428 {
429 tResult.insert( tKey );
430 }
431 }
432
433 // Add elements from other set not in this
434 for ( const auto & tKey : aOther )
435 {
436 if ( !this->contains( tKey ) )
437 {
438 tResult.insert( tKey );
439 }
440 }
441
442 return tResult;
443 }
444
445//------------------------------------------------------------------------------
446
450 bool
451 is_subset_of( const Set< Key > & aOther ) const
452 {
453 if ( this->size() > aOther.size() )
454 {
455 return false;
456 }
457
458 for ( const auto & tKey : mSet )
459 {
460 if ( !aOther.contains( tKey ) )
461 {
462 return false;
463 }
464 }
465 return true;
466 }
467
468//------------------------------------------------------------------------------
469
473 bool
474 is_superset_of( const Set< Key > & aOther ) const
475 {
476 return aOther.is_subset_of( *this );
477 }
478
479//------------------------------------------------------------------------------
480
484 bool
485 operator==( const Set< Key > & aOther ) const
486 {
487 if ( this->size() != aOther.size() )
488 {
489 return false;
490 }
491
492 for ( const auto & tKey : mSet )
493 {
494 if ( !aOther.contains( tKey ) )
495 {
496 return false;
497 }
498 }
499 return true;
500 }
501
502//------------------------------------------------------------------------------
503
507 bool
508 operator!=( const Set< Key > & aOther ) const
509 {
510 return !( *this == aOther );
511 }
512
513//------------------------------------------------------------------------------
514 };
515
516//------------------------------------------------------------------------------
517} // namespace belfem
518
519#endif // BELFEM_CL_SET_HPP
Hash set with set operations.
Definition cl_Set.hpp:33
void clear()
Clear the set.
Definition cl_Set.hpp:108
size_t erase(const Key &aKey)
Erase a key from the set.
Definition cl_Set.hpp:244
Set< Key > operator-(const Set< Key > &aOther) const
Set difference operation.
Definition cl_Set.hpp:400
Set< Key > operator|(const Set< Key > &aOther) const
Set union operation.
Definition cl_Set.hpp:366
bool operator!=(const Set< Key > &aOther) const
Inequality operator.
Definition cl_Set.hpp:508
size_t size() const
Returns the size of the set.
Definition cl_Set.hpp:119
const_iterator end() const
Get end iterator (const).
Definition cl_Set.hpp:321
void swap(Set< Key > &aOther)
Swap contents with another set.
Definition cl_Set.hpp:354
void reserve(size_t n)
Reserve space for at least n elements.
Definition cl_Set.hpp:277
bool empty() const
Check if set is empty.
Definition cl_Set.hpp:130
Set< Key > & operator=(const Set< Key > &aSet)=default
Copy assignment operator.
Set(InputIt aFirst, InputIt aLast)
Constructor from iterators.
Definition cl_Set.hpp:63
std::unordered_set< Key > & set_data()
Expose the underlying container.
Definition cl_Set.hpp:332
Set(std::initializer_list< Key > aInit)
Constructor with initializer list.
Definition cl_Set.hpp:53
Set< Key > operator^(const Set< Key > &aOther) const
Set symmetric difference operation.
Definition cl_Set.hpp:420
void insert(InputIt aFirst, InputIt aLast)
Insert a range of elements.
Definition cl_Set.hpp:165
size_t count(const Key &aKey) const
Count occurrences of key (0 or 1 for set).
Definition cl_Set.hpp:210
Set()=default
Empty constructor.
std::pair< iterator, bool > insert(const Key &aKey)
Insert a key into the set.
Definition cl_Set.hpp:142
Set< Key > & operator=(Set< Key > &&aSet) noexcept=default
Move assignment operator.
typename std::unordered_set< Key >::iterator iterator
Definition cl_Set.hpp:40
const_iterator find(const Key &aKey) const
Find an element (const version).
Definition cl_Set.hpp:232
const std::unordered_set< Key > & set_data() const
Expose the underlying container (const).
Definition cl_Set.hpp:343
bool is_superset_of(const Set< Key > &aOther) const
Check if this is a superset of another set.
Definition cl_Set.hpp:474
iterator erase(const_iterator aFirst, const_iterator aLast)
Erase a range of elements.
Definition cl_Set.hpp:266
const_iterator begin() const
Get begin iterator (const).
Definition cl_Set.hpp:299
bool contains(const Key &aKey) const
Alternative name for key_exists (more set-like).
Definition cl_Set.hpp:199
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a key (construct in-place).
Definition cl_Set.hpp:177
iterator end()
Get end iterator.
Definition cl_Set.hpp:310
Set< Key > operator&(const Set< Key > &aOther) const
Set intersection operation.
Definition cl_Set.hpp:380
iterator find(const Key &aKey)
Find an element.
Definition cl_Set.hpp:221
bool is_subset_of(const Set< Key > &aOther) const
Check if this is a subset of another set.
Definition cl_Set.hpp:451
iterator begin()
Get begin iterator.
Definition cl_Set.hpp:288
bool operator==(const Set< Key > &aOther) const
Equality operator.
Definition cl_Set.hpp:485
Set(const Set< Key > &aSet)=default
Copy constructor.
bool key_exists(const Key &aKey) const
Check if a key exists in the set.
Definition cl_Set.hpp:188
~Set()=default
Destructor.
Set(Set< Key > &&aSet) noexcept=default
Move constructor.
iterator erase(const_iterator aPos)
Erase an element by iterator.
Definition cl_Set.hpp:255
typename std::unordered_set< Key >::const_iterator const_iterator
Definition cl_Set.hpp:41
std::pair< iterator, bool > insert(Key &&aKey)
Insert a key using move semantics.
Definition cl_Set.hpp:153
USER GUIDES:
Definition cl_Capacitor.cpp:16