MeshLib Documentation
Loading...
Searching...
No Matches
MRBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRNoDefInit.h"
4#include "MRId.h"
5#include <cassert>
6#include <concepts>
7#include <memory>
8#include <type_traits>
9
10namespace MR
11{
12
13template <typename T>
15{
16 T val = 0;
17 constexpr ZeroOnMove() noexcept {}
18 constexpr ZeroOnMove( const ZeroOnMove & ) noexcept = delete;
19 constexpr ZeroOnMove( ZeroOnMove && z ) noexcept : val( z.val ) { z.val = 0; }
20 constexpr ZeroOnMove& operator =( const ZeroOnMove & ) noexcept = delete;
21 constexpr ZeroOnMove& operator =( ZeroOnMove && z ) noexcept { val = z.val; z.val = 0; return * this; }
22};
23
24template <typename T>
25struct NoCtor;
26
27template <typename T>
28concept Trivial = std::is_trivially_constructible_v<T>;
29
30// for trivial types, return the type itself
31template <Trivial T>
32struct NoCtor<T>
33{
34 using type = T;
35};
36
37// for our complex types, return wrapped type with default constructor doing nothing
38template <std::constructible_from<NoInit> T>
39struct NoCtor<T>
40{
42};
43
52template <typename V, typename I>
53class Buffer
54{
55public:
56 using T = typename NoCtor<V>::type;
57 using reference = T&;
58 using const_reference = const T&;
59 using iterator = T*;
60 using const_iterator = const T*;
61
62 Buffer() = default;
63 explicit Buffer( size_t size ) { resize( size ); }
64
65 [[nodiscard]] auto capacity() const { return capacity_.val; }
66 [[nodiscard]] auto size() const { return size_.val; }
67 [[nodiscard]] bool empty() const { return size_.val == 0; }
68
69 void clear() { data_.reset(); capacity_ = {}; size_ = {}; }
70
71 void resize( size_t newSize )
72 {
73 if ( size_.val == newSize )
74 return;
75 if ( newSize > capacity_.val )
76 {
77#if __cpp_lib_smart_ptr_for_overwrite >= 202002L
78 data_ = std::make_unique_for_overwrite<T[]>( capacity_.val = newSize );
79#else
80 data_.reset( new T[capacity_.val = newSize] );
81#endif
82 }
83 size_.val = newSize;
84 }
85
86 [[nodiscard]] const_reference operator[]( I i ) const
87 {
88 assert( i < size_.val );
89 return data_[i];
90 }
91 [[nodiscard]] reference operator[]( I i )
92 {
93 assert( i < size_.val );
94 return data_[i];
95 }
96
97 [[nodiscard]] auto data() { return data_.get(); }
98 [[nodiscard]] auto data() const { return data_.get(); }
99
101 [[nodiscard]] I beginId() const { return I{ size_t(0) }; }
102
104 [[nodiscard]] I backId() const { assert( !empty() ); return I{ size() - 1 }; }
105
107 [[nodiscard]] I endId() const { return I{ size() }; }
108
110 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
111
112private:
113 std::unique_ptr<T[]> data_;
114 ZeroOnMove<size_t> capacity_, size_;
115};
116
118template <typename T, typename I>
119inline T getAt( const Buffer<T, I> & bmap, I key )
120{
121 return key ? T{bmap[key]} : T{};
122}
123
124template <typename T, typename I>
125[[nodiscard]] inline auto begin( const Buffer<T, I> & a )
126 { return a.data(); }
127
128template <typename T, typename I>
129[[nodiscard]] inline auto begin( Buffer<T, I> & a )
130 { return a.data(); }
131
132template <typename T, typename I>
133[[nodiscard]] inline auto end( const Buffer<T, I> & a )
134 { return a.data() + a.size(); }
135
136template <typename T, typename I>
137[[nodiscard]] inline auto end( Buffer<T, I> & a )
138 { return a.data() + a.size(); }
139
141template <typename T, typename I>
142struct BMap
143{
145 size_t tsize = 0;
146};
147
151{
152 UndirectedEdgeBMap e;
153 FaceBMap f;
154 VertBMap v;
155};
156
158template <typename T>
160{
161 BMap<T, T> res;
162 res.b.resize( b.b.size() );
163 res.tsize = a.tsize;
164 for ( T x( 0 ); x < b.b.size(); ++x )
165 {
166 auto bx = b.b[x];
167 if ( bx < a.b.size() ) //invalid bx (=-1) will be casted to size_t(-1)
168 res.b[x] = a.b[bx];
169 }
170 return res;
171}
172
173} // namespace MR
std::vector<V>-like container that is 1) resized without initialization of its elements,...
Definition MRBuffer.h:54
I backId() const
returns the identifier of the back() element
Definition MRBuffer.h:104
void clear()
Definition MRBuffer.h:69
auto size() const
Definition MRBuffer.h:66
auto data()
Definition MRBuffer.h:97
typename NoCtor< V >::type T
Definition MRBuffer.h:56
bool empty() const
Definition MRBuffer.h:67
auto data() const
Definition MRBuffer.h:98
I beginId() const
returns the identifier of the first element
Definition MRBuffer.h:101
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRBuffer.h:110
const_reference operator[](I i) const
Definition MRBuffer.h:86
T & reference
Definition MRBuffer.h:57
void resize(size_t newSize)
Definition MRBuffer.h:71
I endId() const
returns backId() + 1
Definition MRBuffer.h:107
reference operator[](I i)
Definition MRBuffer.h:91
Buffer()=default
const T & const_reference
Definition MRBuffer.h:58
const T * const_iterator
Definition MRBuffer.h:60
auto capacity() const
Definition MRBuffer.h:65
T * iterator
Definition MRBuffer.h:59
Buffer(size_t size)
Definition MRBuffer.h:63
Definition MRBuffer.h:28
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:280
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:282
Definition MRCameraOrientationPlugin.h:8
T getAt(const Buffer< T, I > &bmap, I key)
given some buffer map and a key, returns the value associated with the key, or default value if key i...
Definition MRBuffer.h:119
I
Definition MRMesh/MRMeshFwd.h:88
BMap< T, T > compose(const BMap< T, T > &a, const BMap< T, T > &b)
computes the composition of two mappings x -> a(b(x))
Definition MRBuffer.h:159
flat map: I -> T
Definition MRBuffer.h:143
Buffer< T, I > b
Definition MRBuffer.h:144
size_t tsize
target size, all values inside b must be less than this value
Definition MRBuffer.h:145
T type
Definition MRBuffer.h:34
Definition MRBuffer.h:25
Definition MRNoDefInit.h:11
Definition MRBuffer.h:151
UndirectedEdgeBMap e
Definition MRBuffer.h:152
VertBMap v
Definition MRBuffer.h:154
FaceBMap f
Definition MRBuffer.h:153
Definition MRBuffer.h:15
constexpr ZeroOnMove(ZeroOnMove &&z) noexcept
Definition MRBuffer.h:19
T val
Definition MRBuffer.h:16
constexpr ZeroOnMove(const ZeroOnMove &) noexcept=delete
constexpr ZeroOnMove & operator=(const ZeroOnMove &) noexcept=delete
constexpr ZeroOnMove() noexcept
Definition MRBuffer.h:17