MeshLib C++ Docs
Loading...
Searching...
No Matches
MRBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRNoDefInit.h"
5#include "MRId.h"
6#include <cassert>
7#include <concepts>
8#include <memory>
9#include <type_traits>
10
11namespace MR
12{
15
16
17template <typename T>
19{
20 T val = 0;
21 constexpr ZeroOnMove() noexcept {}
22 constexpr ZeroOnMove( const ZeroOnMove & ) noexcept = delete;
23 constexpr ZeroOnMove( ZeroOnMove && z ) noexcept : val( z.val ) { z.val = 0; }
24 constexpr ZeroOnMove& operator =( const ZeroOnMove & ) noexcept = delete;
25 constexpr ZeroOnMove& operator =( ZeroOnMove && z ) noexcept { val = z.val; z.val = 0; return * this; }
26};
27
28template <typename T>
29struct NoCtor;
30
31template <typename T>
32concept Trivial = std::is_trivially_constructible_v<T>;
33
35template <Trivial T>
36struct NoCtor<T>
37{
38 using type = T;
39};
40
42template <std::constructible_from<NoInit> T>
43struct NoCtor<T>
44{
46};
47
56template <typename V, typename I>
57class Buffer
58{
59public:
60 using T = typename NoCtor<V>::type;
61 using reference = T&;
62 using const_reference = const T&;
63 using iterator = T*;
64 using const_iterator = const T*;
65
66 Buffer() = default;
67 explicit Buffer( size_t size ) { resize( size ); }
68
69 [[nodiscard]] auto capacity() const { return capacity_.val; }
70 [[nodiscard]] auto size() const { return size_.val; }
71 [[nodiscard]] bool empty() const { return size_.val == 0; }
72
73 void clear() { data_.reset(); capacity_ = {}; size_ = {}; }
74
75 void resize( size_t newSize )
76 {
77 if ( size_.val == newSize )
78 return;
79 if ( newSize > capacity_.val )
80 {
81#if __cpp_lib_smart_ptr_for_overwrite >= 202002L
82 data_ = std::make_unique_for_overwrite<T[]>( capacity_.val = newSize );
83#else
84 data_.reset( new T[capacity_.val = newSize] );
85#endif
86 }
87 size_.val = newSize;
88 }
89
91 {
92 assert( i < size_.val );
93 return data_[i];
94 }
96 {
97 assert( i < size_.val );
98 return data_[i];
99 }
100
101 [[nodiscard]] auto data() MR_LIFETIMEBOUND { return data_.get(); }
102 [[nodiscard]] auto data() const MR_LIFETIMEBOUND { return data_.get(); }
103
105 [[nodiscard]] I beginId() const { return I{ size_t(0) }; }
106
108 [[nodiscard]] I backId() const { assert( !empty() ); return I{ size() - 1 }; }
109
111 [[nodiscard]] I endId() const { return I{ size() }; }
112
114 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
115
116 [[nodiscard]] friend auto begin( const Buffer & a ) { return a.data(); }
117 [[nodiscard]] friend auto begin( Buffer & a ) { return a.data(); }
118 [[nodiscard]] friend auto end( const Buffer & a ) { return a.data() + a.size(); }
119 [[nodiscard]] friend auto end( Buffer & a ) { return a.data() + a.size(); }
120
121private:
122 std::unique_ptr<T[]> data_;
123 ZeroOnMove<size_t> capacity_, size_;
124};
125
127template <typename T, typename I>
128inline T getAt( const Buffer<T, I> & bmap MR_LIFETIMEBOUND_NESTED, I key, T def = {} )
129{
130 return key ? T{bmap[key]} : def;
131}
132
134template <typename T, typename I>
135struct BMap
136{
138 size_t tsize = 0;
139};
140
144{
145 UndirectedEdgeBMap e;
146 FaceBMap f;
147 VertBMap v;
148};
149
151template <typename T>
153{
154 BMap<T, T> res;
155 res.b.resize( b.b.size() );
156 res.tsize = a.tsize;
157 for ( T x( 0 ); x < b.b.size(); ++x )
158 {
159 auto bx = b.b[x];
160 if ( bx < a.b.size() )
161 res.b[x] = a.b[bx];
162 }
163 return res;
164}
165
166}
#define MR_LIFETIMEBOUND
Definition MRMacros.h:81
#define MR_LIFETIMEBOUND_NESTED
Definition MRMacros.h:84
std::vector<V>-like container that is 1) resized without initialization of its elements,...
Definition MRBuffer.h:58
Definition MRBuffer.h:32
UndirectedEdgeBMap e
Definition MRBuffer.h:145
Buffer< LeafId, LeafId > b
Definition MRBuffer.h:137
I backId() const
returns the identifier of the back() element
Definition MRBuffer.h:108
friend auto begin(Buffer &a)
Definition MRBuffer.h:117
constexpr ZeroOnMove(ZeroOnMove &&z) noexcept
Definition MRBuffer.h:23
T val
Definition MRBuffer.h:20
void clear()
Definition MRBuffer.h:73
VertBMap v
Definition MRBuffer.h:147
auto size() const
Definition MRBuffer.h:70
FaceBMap f
Definition MRBuffer.h:146
friend auto begin(const Buffer &a)
Definition MRBuffer.h:116
T type
Definition MRBuffer.h:38
typename NoCtor< V >::type T
Definition MRBuffer.h:60
bool empty() const
Definition MRBuffer.h:71
const_reference operator[](I i) const MR_LIFETIMEBOUND
Definition MRBuffer.h:90
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:128
constexpr ZeroOnMove(const ZeroOnMove &) noexcept=delete
I beginId() const
returns the identifier of the first element
Definition MRBuffer.h:105
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRBuffer.h:114
auto data() MR_LIFETIMEBOUND
Definition MRBuffer.h:101
friend auto end(const Buffer &a)
Definition MRBuffer.h:118
class MRMESH_CLASS I
Definition MRMeshFwd.h:137
size_t tsize
Definition MRBuffer.h:138
auto data() const MR_LIFETIMEBOUND
Definition MRBuffer.h:102
T & reference
Definition MRBuffer.h:61
friend auto end(Buffer &a)
Definition MRBuffer.h:119
void resize(size_t newSize)
Definition MRBuffer.h:75
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:152
I endId() const
returns backId() + 1
Definition MRBuffer.h:111
constexpr ZeroOnMove & operator=(const ZeroOnMove &) noexcept=delete
Buffer()=default
const T & const_reference
Definition MRBuffer.h:62
const T * const_iterator
Definition MRBuffer.h:64
reference operator[](I i) MR_LIFETIMEBOUND
Definition MRBuffer.h:95
constexpr ZeroOnMove() noexcept
Definition MRBuffer.h:21
auto capacity() const
Definition MRBuffer.h:69
T * iterator
Definition MRBuffer.h:63
Buffer(size_t size)
Definition MRBuffer.h:67
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
flat map: I -> T
Definition MRBuffer.h:136
Definition MRBuffer.h:29
this class is similar to T, but does not make default initialization of the fields for best performan...
Definition MRNoDefInit.h:14
Definition MRBuffer.h:144
Definition MRBuffer.h:19