MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRVector.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRResizeNoInit.h"
5#include "MRPch/MRBindingMacros.h"
6#include <cassert>
7
8namespace MR
9{
10
17template <typename T, typename I>
18class Vector
19{
20public:
21 using value_type = typename std::vector<T>::value_type;
22 using reference = typename std::vector<T>::reference;
23 using const_reference = typename std::vector<T>::const_reference;
24 using iterator = typename std::vector<T>::iterator;
25 using const_iterator = typename std::vector<T>::const_iterator;
26
28 Vector() = default;
29
31 explicit Vector( size_t size ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> ) : vec_( size ) { }
32
34 explicit Vector( size_t size, const T & val ) : vec_( size, val ) { }
35
37 Vector( std::vector<T> && vec ) : vec_( std::move( vec ) ) { }
38
39 template< class InputIt >
40 Vector( InputIt first, InputIt last ) : vec_( first, last ) { }
41
42 Vector( std::initializer_list<T> init ) : vec_( init ) { }
43
44 [[nodiscard]] bool operator == ( const Vector & b ) const MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::equality_comparable<T> ) { return vec_ == b.vec_; }
45 [[nodiscard]] bool operator != ( const Vector & b ) const MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::equality_comparable<T> ) { return vec_ != b.vec_; }
46
47 void clear() { vec_.clear(); }
48
49 [[nodiscard]] bool empty() const { return vec_.empty(); }
50
51 [[nodiscard]] std::size_t size() const { return vec_.size(); }
52
53 void resize( size_t newSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> && std::default_initializable<T> ) { vec_.resize( newSize ); }
54 void resize( size_t newSize, const T & t ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> ) { vec_.resize( newSize, t ); }
55
56 // resizes the vector skipping initialization of its elements (more precisely initializing them using ( noInit ) constructor )
57 void resizeNoInit( size_t targetSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::constructible_from<T, NoInit> ) { MR::resizeNoInit( vec_, targetSize ); }
58
59 [[nodiscard]] std::size_t capacity() const { return vec_.capacity(); }
60
61 void reserve( size_t capacity ) { vec_.reserve( capacity ); }
62
63 [[nodiscard]] const_reference operator[]( I i ) const
64 {
65 assert( i < vec_.size() );
66 return vec_[i];
67 }
68 [[nodiscard]] reference operator[]( I i )
69 {
70 assert( i < vec_.size() );
71 return vec_[i];
72 }
73
75 void resizeWithReserve( size_t newSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> )
76 {
77 // This separate overload is needed as opposed to a `value = T{}` default argument, because if T isn't default-constructible, the parsed chokes on that.
78 resizeWithReserve( newSize, T{} );
79 }
80
82 void resizeWithReserve( size_t newSize, const T & value ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> )
83 {
84 auto reserved = vec_.capacity();
85 if ( reserved > 0 && newSize > reserved )
86 {
87 while ( newSize > reserved )
88 reserved <<= 1;
89 vec_.reserve( reserved );
90 }
91 vec_.resize( newSize, value );
92 }
93
96 void autoResizeSet( I pos, size_t len, T val ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> && std::is_copy_assignable_v<T> )
97 {
98 assert( pos );
99 const size_t p = pos;
100 if ( const auto sz = size(); p + len > sz )
101 {
102 // add new elements with the given value
103 resizeWithReserve( p + len, val );
104 if ( p >= sz )
105 return;
106 // the number of the elements existing before function call to be changed
107 len = sz - p;
108 }
109 // change the value of the elements existing before function call
110 for ( size_t i = 0; i < len; ++i )
111 vec_[ p + i ] = val;
112 }
113
115 void autoResizeSet( I i, T val ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::is_copy_assignable_v<T> )
116 {
117 autoResizeSet( i, 1, val );
118 }
119
121 [[nodiscard]] reference autoResizeAt( I i ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> )
122 {
123 if ( i + 1 > size() )
124 resizeWithReserve( i + 1 );
125 return vec_[i];
126 }
127
128 void push_back( const T & t ) { vec_.push_back( t ); }
129 void push_back( T && t ) { vec_.push_back( std::move( t ) ); }
130 void pop_back() { vec_.pop_back(); }
131
132 template<typename... Args> MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::constructible_from<T, Args &&...> )
133 T& emplace_back( Args&&... args ) { return vec_.emplace_back( std::forward<Args>(args)... ); }
134
135 [[nodiscard]] const_reference front() const { return vec_.front(); }
136 [[nodiscard]] reference front() { return vec_.front(); }
137 [[nodiscard]] const_reference back() const { return vec_.back(); }
138 [[nodiscard]] reference back() { return vec_.back(); }
139
141 [[nodiscard]] I beginId() const { return I( size_t(0) ); }
142
144 [[nodiscard]] I backId() const { assert( !vec_.empty() ); return I( vec_.size() - 1 ); }
145
147 [[nodiscard]] I endId() const { return I( vec_.size() ); }
148
149 [[nodiscard]] T* data() { return vec_.data(); }
150 [[nodiscard]] const T* data() const { return vec_.data(); }
151
152 void swap( Vector & b ) { vec_.swap( b.vec_ ); }
153
155 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
156
158 std::vector<T> vec_;
159
160#if defined( MR_PARSING_FOR_ANY_BINDINGS ) || defined( MR_COMPILING_ANY_BINDINGS )
161 static_assert( sizeof(T) > 0 );
162#endif
163};
164
165template <typename T, typename I>
166[[nodiscard]] MR_BIND_IGNORE inline auto begin( const Vector<T, I> & a )
167 { return a.vec_.begin(); }
168
169template <typename T, typename I>
170[[nodiscard]] MR_BIND_IGNORE inline auto begin( Vector<T, I> & a )
171 { return a.vec_.begin(); }
172
173template <typename T, typename I>
174[[nodiscard]] MR_BIND_IGNORE inline auto end( const Vector<T, I> & a )
175 { return a.vec_.end(); }
176
177template <typename T, typename I>
178[[nodiscard]] MR_BIND_IGNORE inline auto end( Vector<T, I> & a )
179 { return a.vec_.end(); }
180
182template <typename T, typename I>
183[[nodiscard]] inline T getAt( const Vector<T, I> & a, I id, T def = {} )
184{
185 return ( id && id < a.size() ) ? a[id] : def;
186}
187
188} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:19
void pop_back()
Definition MRMesh/MRVector.h:130
Vector()=default
creates empty vector
MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std::constructible_from< T, Args &&... >) T &emplace_back(Args &&... args)
Definition MRMesh/MRVector.h:132
void clear()
Definition MRMesh/MRVector.h:47
reference front()
Definition MRMesh/MRVector.h:136
typename std::vector< T >::iterator iterator
Definition MRMesh/MRVector.h:24
std::size_t size() const
Definition MRMesh/MRVector.h:51
void resizeWithReserve(size_t newSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
doubles reserved memory until resize(newSize) can be done without reallocation
Definition MRMesh/MRVector.h:75
std::size_t capacity() const
Definition MRMesh/MRVector.h:59
void resize(size_t newSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:53
reference autoResizeAt(I i) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
this accessor automatically adjusts the size of the vector
Definition MRMesh/MRVector.h:121
reference operator[](I i)
Definition MRMesh/MRVector.h:68
typename std::vector< T >::reference reference
Definition MRMesh/MRVector.h:22
T * data()
Definition MRMesh/MRVector.h:149
Vector(InputIt first, InputIt last)
Definition MRMesh/MRVector.h:40
Vector(std::vector< T > &&vec)
moves data from the given std::vector<T>
Definition MRMesh/MRVector.h:37
void reserve(size_t capacity)
Definition MRMesh/MRVector.h:61
I beginId() const
returns the identifier of the first element
Definition MRMesh/MRVector.h:141
void push_back(const T &t)
Definition MRMesh/MRVector.h:128
void resizeNoInit(size_t targetSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:57
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRVector.h:155
const_reference operator[](I i) const
Definition MRMesh/MRVector.h:63
I backId() const
returns the identifier of the back() element
Definition MRMesh/MRVector.h:144
void resizeWithReserve(size_t newSize, const T &value) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
doubles reserved memory until resize(newSize, value) can be done without reallocation
Definition MRMesh/MRVector.h:82
Vector(std::initializer_list< T > init)
Definition MRMesh/MRVector.h:42
bool operator!=(const Vector &b) const MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:45
reference back()
Definition MRMesh/MRVector.h:138
typename std::vector< T >::const_reference const_reference
Definition MRMesh/MRVector.h:23
const T * data() const
Definition MRMesh/MRVector.h:150
typename std::vector< T >::value_type value_type
Definition MRMesh/MRVector.h:21
bool operator==(const Vector &b) const MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:44
void autoResizeSet(I i, T val) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
sets the element #i to the given value, adjusting the size of the vector to include new element
Definition MRMesh/MRVector.h:115
bool empty() const
Definition MRMesh/MRVector.h:49
Vector(size_t size, const T &val)
creates a vector with size elements with the given value
Definition MRMesh/MRVector.h:34
typename std::vector< T >::const_iterator const_iterator
Definition MRMesh/MRVector.h:25
std::vector< T > vec_
the user can directly manipulate the vector, anyway she cannot break anything
Definition MRMesh/MRVector.h:158
void swap(Vector &b)
Definition MRMesh/MRVector.h:152
void resize(size_t newSize, const T &t) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:54
void autoResizeSet(I pos, size_t len, T val) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:96
const_reference front() const
Definition MRMesh/MRVector.h:135
Vector(size_t size) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
creates a vector with size elements with default value
Definition MRMesh/MRVector.h:31
void push_back(T &&t)
Definition MRMesh/MRVector.h:129
const_reference back() const
Definition MRMesh/MRVector.h:137
I endId() const
returns backId() + 1
Definition MRMesh/MRVector.h:147
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:308
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:310
Definition MRCameraOrientationPlugin.h:8
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:119
I
Definition MRMesh/MRMeshFwd.h:130
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
void resizeNoInit(std::vector< T > &vec, size_t targetSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T) > 0 &&std
Definition MRResizeNoInit.h:14