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
90 [[nodiscard]] const_reference operator[]( I i ) const MR_LIFETIMEBOUND
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
116private:
117 std::unique_ptr<T[]> data_;
118 ZeroOnMove<size_t> capacity_, size_;
119};
120
122template <typename T, typename I>
123inline T getAt( const Buffer<T, I> & bmap MR_LIFETIMEBOUND_NESTED, I key, T def = {} )
124{
125 return key ? T{bmap[key]} : def;
126}
127
128template <typename T, typename I>
129[[nodiscard]] inline auto begin( const Buffer<T, I> & a )
130 { return a.data(); }
131
132template <typename T, typename I>
133[[nodiscard]] inline auto begin( Buffer<T, I> & a )
134 { return a.data(); }
135
136template <typename T, typename I>
137[[nodiscard]] inline auto end( const Buffer<T, I> & a )
138 { return a.data() + a.size(); }
139
140template <typename T, typename I>
141[[nodiscard]] inline auto end( Buffer<T, I> & a )
142 { return a.data() + a.size(); }
143
145template <typename T, typename I>
146struct BMap
147{
149 size_t tsize = 0;
150};
151
155{
156 UndirectedEdgeBMap e;
157 FaceBMap f;
158 VertBMap v;
159};
160
162template <typename T>
164{
165 BMap<T, T> res;
166 res.b.resize( b.b.size() );
167 res.tsize = a.tsize;
168 for ( T x( 0 ); x < b.b.size(); ++x )
169 {
170 auto bx = b.b[x];
171 if ( bx < a.b.size() )
172 res.b[x] = a.b[bx];
173 }
174 return res;
175}
176
177}
#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
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
MR_BIND_IGNORE_PY auto begin(const BitSet &a)
Definition MRBitSet.h:395
UndirectedEdgeBMap e
Definition MRBuffer.h:156
Buffer< T, I > b
Definition MRBuffer.h:148
I backId() const
returns the identifier of the back() element
Definition MRBuffer.h:108
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:158
auto size() const
Definition MRBuffer.h:70
FaceBMap f
Definition MRBuffer.h:157
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:123
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
size_t tsize
target size, all values inside b must be less than this value
Definition MRBuffer.h:149
auto data() const MR_LIFETIMEBOUND
Definition MRBuffer.h:102
T & reference
Definition MRBuffer.h:61
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:163
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:147
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:155
Definition MRBuffer.h:19