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 "MRAllocator.h"
5#include "MRFinally.h"
6#include "MRNoDefInit.h"
7#include "MRId.h"
8#include <cassert>
9#include <concepts>
10#include <memory>
11#include <type_traits>
12
13namespace MR
14{
17
18
19template <typename T>
21{
22 T val = 0;
23 constexpr ZeroOnMove() noexcept {}
24 constexpr ZeroOnMove( const ZeroOnMove & ) noexcept = delete;
25 constexpr ZeroOnMove( ZeroOnMove && z ) noexcept : val( z.val ) { z.val = 0; }
26 constexpr ZeroOnMove& operator =( const ZeroOnMove & ) noexcept = delete;
27 constexpr ZeroOnMove& operator =( ZeroOnMove && z ) noexcept { val = z.val; z.val = 0; return * this; }
28};
29
30template <typename T>
31struct NoCtor;
32
33template <typename T>
34concept Trivial = std::is_trivially_constructible_v<T>;
35
37template <Trivial T>
38struct NoCtor<T>
39{
40 using type = T;
41};
42
44template <std::constructible_from<NoInit> T>
45struct NoCtor<T>
46{
48};
49
58template <typename V, typename I>
59class Buffer
60{
61public:
62 using T = typename NoCtor<V>::type;
63 using reference = T&;
64 using const_reference = const T&;
65 using iterator = T*;
66 using const_iterator = const T*;
67
68 Buffer() = default;
69 explicit Buffer( size_t size ) { resize( size ); }
70
71 [[nodiscard]] auto capacity() const { return capacity_.val; }
72 [[nodiscard]] auto size() const { return size_.val; }
73 [[nodiscard]] bool empty() const { return size_.val == 0; }
74
75 void clear() { data_.reset(); capacity_ = {}; size_ = {}; }
76
77 void resize( size_t newSize )
78 {
79 if ( size_.val == newSize )
80 return;
81 if ( newSize > capacity_.val )
82 {
83 clear();
84 T * p = Allocator<T>().allocate( newSize );
85 {
86 bool ok = false;
87 MR_FINALLY { if ( !ok ) Allocator<T>().deallocate( p, newSize ); };
88 std::uninitialized_default_construct_n( p, newSize );
89 ok = true;
90 }
91 data_ = std::unique_ptr<T[], Deleter>( p, Deleter{ newSize } );
92 capacity_.val = newSize;
93 }
94 size_.val = newSize;
95 }
96
97 [[nodiscard]] const_reference operator[]( I i ) const MR_LIFETIMEBOUND
98 {
99 assert( i < size_.val );
100 return data_[i];
101 }
103 {
104 assert( i < size_.val );
105 return data_[i];
106 }
107
108 [[nodiscard]] auto data() MR_LIFETIMEBOUND { return data_.get(); }
109 [[nodiscard]] auto data() const MR_LIFETIMEBOUND { return data_.get(); }
110
112 [[nodiscard]] I beginId() const { return I{ size_t(0) }; }
113
115 [[nodiscard]] I backId() const { assert( !empty() ); return I{ size() - 1 }; }
116
118 [[nodiscard]] I endId() const { return I{ size() }; }
119
121 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
122
123 [[nodiscard]] friend auto begin( const Buffer & a ) { return a.data(); }
124 [[nodiscard]] friend auto begin( Buffer & a ) { return a.data(); }
125 [[nodiscard]] friend auto end( const Buffer & a ) { return a.data() + a.size(); }
126 [[nodiscard]] friend auto end( Buffer & a ) { return a.data() + a.size(); }
127
128private:
129 struct Deleter
130 {
131 size_t n = 0;
132 void operator()( T * p ) const { std::destroy_n( p, n ); Allocator<T>().deallocate( p, n ); }
133 };
134 std::unique_ptr<T[], Deleter> data_;
135 ZeroOnMove<size_t> capacity_, size_;
136};
137
139template <typename T, typename I>
140inline T getAt( const Buffer<T, I> & bmap MR_LIFETIMEBOUND_NESTED, I key, T def = {} )
141{
142 return key ? T{bmap[key]} : def;
143}
144
146template <typename T, typename I>
147struct BMap
148{
150 size_t tsize = 0;
151};
152
156{
157 UndirectedEdgeBMap e;
158 FaceBMap f;
159 VertBMap v;
160};
161
163template <typename T>
165{
166 BMap<T, T> res;
167 res.b.resize( b.b.size() );
168 res.tsize = a.tsize;
169 for ( T x( 0 ); x < b.b.size(); ++x )
170 {
171 auto bx = b.b[x];
172 if ( bx < a.b.size() )
173 res.b[x] = a.b[bx];
174 }
175 return res;
176}
177
178}
#define MR_FINALLY
Definition MRFinally.h:10
#define MR_LIFETIMEBOUND
Definition MRMacros.h:86
#define MR_LIFETIMEBOUND_NESTED
Definition MRMacros.h:89
std::vector<V>-like container that is 1) resized without initialization of its elements,...
Definition MRBuffer.h:60
Definition MRBuffer.h:34
UndirectedEdgeBMap e
Definition MRBuffer.h:157
Buffer< LeafId, LeafId > b
Definition MRBuffer.h:149
I backId() const
returns the identifier of the back() element
Definition MRBuffer.h:115
friend auto begin(Buffer &a)
Definition MRBuffer.h:124
constexpr ZeroOnMove(ZeroOnMove &&z) noexcept
Definition MRBuffer.h:25
T val
Definition MRBuffer.h:22
void clear()
Definition MRBuffer.h:75
VertBMap v
Definition MRBuffer.h:159
void deallocate(T *p, std::size_t) noexcept
Definition MRAllocator.h:70
T * allocate(std::size_t n)
Definition MRAllocator.h:59
auto size() const
Definition MRBuffer.h:72
auto data()
Definition MRBuffer.h:108
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
FaceBMap f
Definition MRBuffer.h:158
void operator()(T *p) const
Definition MRBuffer.h:132
friend auto begin(const Buffer &a)
Definition MRBuffer.h:123
T type
Definition MRBuffer.h:40
typename NoCtor< V >::type T
Definition MRBuffer.h:62
bool empty() const
Definition MRBuffer.h:73
auto data() const
Definition MRBuffer.h:109
constexpr ZeroOnMove(const ZeroOnMove &) noexcept=delete
I beginId() const
returns the identifier of the first element
Definition MRBuffer.h:112
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRBuffer.h:121
friend auto end(const Buffer &a)
Definition MRBuffer.h:125
size_t tsize
Definition MRBuffer.h:150
const_reference operator[](I i) const
Definition MRBuffer.h:97
T & reference
Definition MRBuffer.h:63
friend auto end(Buffer &a)
Definition MRBuffer.h:126
void resize(size_t newSize)
Definition MRBuffer.h:77
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:164
I endId() const
returns backId() + 1
Definition MRBuffer.h:118
reference operator[](I i)
Definition MRBuffer.h:102
constexpr ZeroOnMove & operator=(const ZeroOnMove &) noexcept=delete
Buffer()=default
const T & const_reference
Definition MRBuffer.h:64
const T * const_iterator
Definition MRBuffer.h:66
constexpr ZeroOnMove() noexcept
Definition MRBuffer.h:23
auto capacity() const
Definition MRBuffer.h:71
T * iterator
Definition MRBuffer.h:65
Buffer(size_t size)
Definition MRBuffer.h:69
size_t n
Definition MRBuffer.h:131
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRAllocator.h:47
flat map: I -> T
Definition MRBuffer.h:148
Definition MRBuffer.h:31
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:156
Definition MRBuffer.h:21