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 "MRParallelFor.h"
5#include "MRVector.h"
6#include "MRphmap.h"
7#include "MRPch/MRBindingMacros.h"
8#include <variant>
9
10namespace MR
11{
14
15
16namespace detail
17{
18
20template <typename K, typename V>
21struct MR_BIND_IGNORE HashMapSlots : HashMap<K, V>
22{
23 using IteratorAt = typename HashMap<K, V>::const_iterator ( HashMap<K, V>::* )( size_t ) const;
24 static constexpr IteratorAt iteratorAt = &HashMapSlots::iterator_at;
25};
26
27}
28
32template <typename K, typename V>
34{
38 std::variant<Dense, Hash> var;
39
40 [[nodiscard]] static MapOrHashMap createMap( size_t size = 0 );
41 [[nodiscard]] static MapOrHashMap createHashMap( size_t capacity = 0 );
42
43 void setMap( Dense && m MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { var = std::move( m ); }
44 void setHashMap( Hash && m MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { var = std::move( m ); }
45
48 void resizeReserve( size_t denseTotalSize, size_t hashAdditionalCapacity );
49
53
55 template<typename F>
56 void forEach( F && f ) const;
57
60 template<typename F>
61 void forEachParallel( F && f ) const;
62
63 [[nodiscard]] Dense* getMap() MR_LIFETIMEBOUND { return get_if<Dense>( &var ); }
64 [[nodiscard]] const Dense* getMap() const MR_LIFETIMEBOUND { return get_if<Dense>( &var ); }
65
66 [[nodiscard]] Hash* getHashMap() MR_LIFETIMEBOUND { return get_if<Hash>( &var ); }
67 [[nodiscard]] const Hash* getHashMap() const MR_LIFETIMEBOUND { return get_if<Hash>( &var ); }
68
69 void clear();
70};
71
72template <typename K, typename V>
74{
76 res.var = Dense( size );
77 return res;
78}
79
80template <typename K, typename V>
82{
84 Hash hmap;
85 if ( capacity > 0 )
86 hmap.reserve( capacity );
87 res.var = std::move( hmap );
88 return res;
89}
90
91template <typename K, typename V>
92void MapOrHashMap<K,V>::resizeReserve( size_t denseTotalSize, size_t hashAdditionalCapacity )
93{
94 std::visit( overloaded{
95 [denseTotalSize]( Dense& map ) { map.resize( denseTotalSize ); },
96 [hashAdditionalCapacity]( Hash& hashMap ) { hashMap.reserve( hashMap.size() + hashAdditionalCapacity ); }
97 }, var );
98}
99
100template <typename K, typename V>
101void MapOrHashMap<K,V>::pushBack( K key, V val )
102{
103 std::visit( overloaded{
104 [=]( Dense& map ) { assert( key == map.endId() ); map.push_back( val ); },
105 [=]( Hash& hashMap ) { hashMap[key] = val; }
106 }, var );
107}
108
109template <typename K, typename V>
110template <typename F>
111void MapOrHashMap<K,V>::forEach( F && f ) const
112{
113 std::visit( overloaded{
114 [&f]( const Dense& map )
115 {
116 for ( K key( 0 ); key < map.size(); ++key )
117 if ( auto val = map[key] )
118 f( key, val );
119 },
120 [&f]( const Hash& hashMap )
121 {
122 for ( const auto & [ key, val ] : hashMap )
123 f( key, val );
124 }
125 }, var );
126}
127
128template <typename K, typename V>
129template <typename F>
131{
132 std::visit( overloaded{
133 [&f]( const Dense& map )
134 {
135 ParallelFor( map, [&] ( K key )
136 {
137 if ( auto val = map[key] )
138 f( key, val );
139 } );
140 },
141 [&f]( const Hash& hashMap )
142 {
144 auto firstFullFrom = [&hashMap] ( size_t slot )
145 {
146 return slot == 0 ? hashMap.begin() : ++( hashMap.*detail::HashMapSlots<K, V>::iteratorAt )( slot - 1 );
147 };
148 tbb::parallel_for( tbb::blocked_range<size_t>( 0, hashMap.capacity() ), [&] ( const tbb::blocked_range<size_t>& range )
149 {
150 for ( auto it = firstFullFrom( range.begin() ), last = firstFullFrom( range.end() ); it != last; ++it )
151 f( it->first, it->second );
152 } );
153 }
154 }, var );
155}
156
157template <typename K, typename V>
159{
160 std::visit( overloaded{
161 []( Dense& map ) { map.clear(); },
162 []( Hash& hashMap ) { hashMap.clear(); }
163 }, var );
164}
165
166template <typename K, typename V>
167[[nodiscard]] inline V getAt( const MapOrHashMap<K, V> & m, K key, V def = {} )
168{
169 return std::visit( overloaded{
170 [key, def]( const Vector<V, K>& map ) { return getAt( map, key, def ); },
171 [key, def]( const HashMap<K, V>& hashMap ) { return getAt( hashMap, key, def ); }
172 }, m.var );
173}
174
175template <typename K, typename V>
176inline void setAt( MapOrHashMap<K, V> & m, K key, V val )
177{
178 std::visit( overloaded{
179 [key, val]( Vector<V, K>& map ) { map[key] = val; },
180 [key, val]( HashMap<K, V>& hashMap ) { hashMap[key] = val; }
181 }, m.var );
182}
183
184}
#define MR_LIFETIMEBOUND
Definition MRMacros.h:86
#define MR_LIFETIME_CAPTURE_BY_NESTED(x)
Definition MRMacros.h:91
Vector<T> is indexed by size_t.
Definition MRVector.h:24
auto ParallelFor(I begin, I end, F &&f, Cb &&... cb)
Definition MRParallelFor.h:30
void setMap(Dense &&m MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:43
Dense * getMap()
Definition MRMapOrHashMap.h:63
void forEachParallel(F &&f) const
Definition MRMapOrHashMap.h:130
std::variant< Dense, Hash > var
Definition MRMapOrHashMap.h:38
const Hash * getHashMap() const
Definition MRMapOrHashMap.h:67
static MapOrHashMap createMap(size_t size=0)
Definition MRMapOrHashMap.h:73
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:59
Hash * getHashMap()
Definition MRMapOrHashMap.h:66
T getAt(const Buffer< T, I > &bmap, 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:140
static constexpr IteratorAt iteratorAt
Definition MRMapOrHashMap.h:24
void pushBack(K key MR_LIFETIME_CAPTURE_BY_NESTED(this), V val MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:101
void resizeReserve(size_t denseTotalSize, size_t hashAdditionalCapacity)
Definition MRMapOrHashMap.h:92
overloaded(Ts...) -> overloaded< Ts... >
explicit deduction guide (not needed as of C++20, but still needed in Clang)
const Dense * getMap() const
Definition MRMapOrHashMap.h:64
void forEach(F &&f) const
executes given function for all pairs (key, value) with valid value for dense map
Definition MRMapOrHashMap.h:111
typename HashMap< K, V >::const_iterator(HashMap< K, V >::*)(size_t) const IteratorAt
Definition MRMapOrHashMap.h:23
HashMap< K, V > Hash
Definition MRMapOrHashMap.h:36
phmap::flat_hash_map< K, V, Hash, Eq, Allocator< std::pair< const K, V > > > HashMap
Definition MRMeshFwd.h:632
void clear()
Definition MRMapOrHashMap.h:158
Vector< V, K > Dense
Definition MRMapOrHashMap.h:35
void setAt(MapOrHashMap< K, V > &m, K key, V val)
Definition MRMapOrHashMap.h:176
void setHashMap(Hash &&m MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRMapOrHashMap.h:44
static MapOrHashMap createHashMap(size_t capacity=0)
Definition MRMapOrHashMap.h:81
for internal use: namespace detail is not bound to other languages
Definition MRFastInt.h:19
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRMapOrHashMap.h:34
phmap has no public access to a slot of a hash table by its index: take its protected iterator_at() v...
Definition MRMapOrHashMap.h:22
Definition MRMeshFwd.h:822