MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMapOrHashMap.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRVector.h"
5#include "MRphmap.h"
6#include <variant>
7
8namespace MR
9{
12
13
17template <typename K, typename V>
19{
21 using Hash = HashMap<K, V>;
23 std::variant<Dense, Hash> var;
24
25 [[nodiscard]] static MapOrHashMap createMap( size_t size = 0 );
26 [[nodiscard]] static MapOrHashMap createHashMap( size_t capacity = 0 );
27
28 void setMap( Dense && m MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { var = std::move( m ); }
29 void setHashMap( Hash && m MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { var = std::move( m ); }
30
33 void resizeReserve( size_t denseTotalSize, size_t hashAdditionalCapacity );
34
38
40 template<typename F>
41 void forEach( F && f ) const;
42
43 [[nodiscard]] Dense* getMap() MR_LIFETIMEBOUND { return get_if<Dense>( &var ); }
44 [[nodiscard]] const Dense* getMap() const MR_LIFETIMEBOUND { return get_if<Dense>( &var ); }
45
46 [[nodiscard]] Hash* getHashMap() MR_LIFETIMEBOUND { return get_if<Hash>( &var ); }
47 [[nodiscard]] const Hash* getHashMap() const MR_LIFETIMEBOUND { return get_if<Hash>( &var ); }
48
49 void clear();
50};
51
52template <typename K, typename V>
54{
56 res.var = Dense( size );
57 return res;
58}
59
60template <typename K, typename V>
62{
64 Hash hmap;
65 if ( capacity > 0 )
66 hmap.reserve( capacity );
67 res.var = std::move( hmap );
68 return res;
69}
70
71template <typename K, typename V>
72void MapOrHashMap<K,V>::resizeReserve( size_t denseTotalSize, size_t hashAdditionalCapacity )
73{
74 std::visit( overloaded{
75 [denseTotalSize]( Dense& map ) { map.resize( denseTotalSize ); },
76 [hashAdditionalCapacity]( Hash& hashMap ) { hashMap.reserve( hashMap.size() + hashAdditionalCapacity ); }
77 }, var );
78}
79
80template <typename K, typename V>
81void MapOrHashMap<K,V>::pushBack( K key, V val )
82{
83 std::visit( overloaded{
84 [=]( Dense& map ) { assert( key == map.endId() ); map.push_back( val ); },
85 [=]( Hash& hashMap ) { hashMap[key] = val; }
86 }, var );
87}
88
89template <typename K, typename V>
90template <typename F>
91void MapOrHashMap<K,V>::forEach( F && f ) const
92{
93 std::visit( overloaded{
94 [&f]( const Dense& map )
95 {
96 for ( K key( 0 ); key < map.size(); ++key )
97 if ( auto val = map[key] )
98 f( key, val );
99 },
100 [&f]( const Hash& hashMap )
101 {
102 for ( const auto & [ key, val ] : hashMap )
103 f( key, val );
104 }
105 }, var );
106}
107
108template <typename K, typename V>
110{
111 std::visit( overloaded{
112 []( Dense& map ) { map.clear(); },
113 []( Hash& hashMap ) { hashMap.clear(); }
114 }, var );
115}
116
117template <typename K, typename V>
118[[nodiscard]] inline V getAt( const MapOrHashMap<K, V> & m, K key, V def = {} )
119{
120 return std::visit( overloaded{
121 [key, def]( const Vector<V, K>& map ) { return getAt( map, key, def ); },
122 [key, def]( const HashMap<K, V>& hashMap ) { return getAt( hashMap, key, def ); }
123 }, m.var );
124}
125
126template <typename K, typename V>
127inline void setAt( MapOrHashMap<K, V> & m, K key, V val )
128{
129 std::visit( overloaded{
130 [key, val]( Vector<V, K>& map ) { map[key] = val; },
131 [key, val]( HashMap<K, V>& hashMap ) { hashMap[key] = val; }
132 }, m.var );
133}
134
135}
#define MR_LIFETIMEBOUND
Definition MRMacros.h:81
#define MR_LIFETIME_CAPTURE_BY_NESTED(x)
Definition MRMacros.h:86
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
void setMap(Dense &&m MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:28
const Hash * getHashMap() const MR_LIFETIMEBOUND
Definition MRMapOrHashMap.h:47
std::variant< Dense, Hash > var
default construction will select dense map
Definition MRMapOrHashMap.h:23
static MapOrHashMap createMap(size_t size=0)
Definition MRMapOrHashMap.h:53
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
void pushBack(K key MR_LIFETIME_CAPTURE_BY_NESTED(this), V val MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:81
Dense * getMap() MR_LIFETIMEBOUND
Definition MRMapOrHashMap.h:43
T getAt(const Buffer< T, I > &bmap MR_LIFETIMEBOUND_NESTED, I key, T def={})
given some buffer map and a key, returns the value associated with the key, or default value if key i...
Definition MRBuffer.h:123
void resizeReserve(size_t denseTotalSize, size_t hashAdditionalCapacity)
Definition MRMapOrHashMap.h:72
void forEach(F &&f) const
executes given function for all pairs (key, value) with valid value for dense map
Definition MRMapOrHashMap.h:91
HashMap< K, V > Hash
Definition MRMapOrHashMap.h:21
void clear()
Definition MRMapOrHashMap.h:109
const Dense * getMap() const MR_LIFETIMEBOUND
Definition MRMapOrHashMap.h:44
Hash * getHashMap() MR_LIFETIMEBOUND
Definition MRMapOrHashMap.h:46
void setAt(MapOrHashMap< K, V > &m, K key, V val)
Definition MRMapOrHashMap.h:127
void setHashMap(Hash &&m MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:29
static MapOrHashMap createHashMap(size_t capacity=0)
Definition MRMapOrHashMap.h:61
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMapOrHashMap.h:19