MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRVector.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRMeshFwd.h"
5#include "MRPch/MRBindingMacros.h"
6#include <cassert>
7#include <vector>
8
9namespace MR
10{
11
18template <typename T, typename I>
19class Vector
20{
21public:
22 using value_type = typename std::vector<T>::value_type;
23 using reference = typename std::vector<T>::reference;
24 using const_reference = typename std::vector<T>::const_reference;
25 using iterator = typename std::vector<T>::iterator;
26 using const_iterator = typename std::vector<T>::const_iterator;
27
28 Vector() = default;
29 explicit Vector( size_t size ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> ) : vec_( size ) { }
30 explicit Vector( size_t size, const T & val ) : vec_( size, val ) { }
31 Vector( std::vector<T> && vec ) : vec_( std::move( vec ) ) { }
32 template< class InputIt >
33 Vector( InputIt first, InputIt last ) : vec_( first, last ) { }
34 Vector( std::initializer_list<T> init ) : vec_( init ) { }
35
36 [[nodiscard]] bool operator == ( const Vector & b ) const MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::equality_comparable<T> ) { return vec_ == b.vec_; }
37 [[nodiscard]] bool operator != ( const Vector & b ) const MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::equality_comparable<T> ) { return vec_ != b.vec_; }
38
39 void clear() { vec_.clear(); }
40 [[nodiscard]] bool empty() const { return vec_.empty(); }
41
42 [[nodiscard]] std::size_t size() const { return vec_.size(); }
43
44 void resize( size_t newSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> && std::default_initializable<T> ) { vec_.resize( newSize ); }
45 void resize( size_t newSize, const T & t ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::movable<T> ) { vec_.resize( newSize, t ); }
46
47 // resizes the vector skipping initialization of its elements (more precisely initializing them using ( noInit ) constructor )
48 void resizeNoInit( size_t targetSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::constructible_from<T, NoInit> )
49 {
50 // allocate enough memory
51 reserve( targetSize );
52 // resize without memory access
53 while ( size() < targetSize )
55 // in case initial size was larger
56 resize( targetSize );
57 }
58
59 [[nodiscard]] std::size_t capacity() const { return vec_.capacity(); }
60 void reserve( size_t capacity ) { vec_.reserve( capacity ); }
61
62 [[nodiscard]] const_reference operator[]( I i ) const
63 {
64 assert( i < vec_.size() );
65 return vec_[i];
66 }
67 [[nodiscard]] reference operator[]( I i )
68 {
69 assert( i < vec_.size() );
70 return vec_[i];
71 }
72
74 void resizeWithReserve( size_t newSize ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> )
75 {
76 // 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.
77 resizeWithReserve( newSize, T{} );
78 }
79
81 void resizeWithReserve( size_t newSize, const T & value )
82 {
83 auto reserved = vec_.capacity();
84 if ( reserved > 0 && newSize > reserved )
85 {
86 while ( newSize > reserved )
87 reserved <<= 1;
88 vec_.reserve( reserved );
89 }
90 vec_.resize( newSize, value );
91 }
92
94 void autoResizeSet( I pos, size_t len, T val )
95 {
96 assert( pos );
97 const size_t p = pos;
98 if ( const auto sz = size(); p + len > sz )
99 {
100 resizeWithReserve( p + len, val );
101 if ( p >= sz )
102 return;
103 len = sz - p;
104 }
105 for ( size_t i = 0; i < len; ++i )
106 vec_[ p + i ] = val;
107 }
108 void autoResizeSet( I i, T val ) { autoResizeSet( i, 1, val ); }
109
111 [[nodiscard]] reference autoResizeAt( I i ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> )
112 {
113 if ( i + 1 > size() )
114 resizeWithReserve( i + 1 );
115 return vec_[i];
116 }
117
118 void push_back( const T & t ) { vec_.push_back( t ); }
119 void push_back( T && t ) { vec_.push_back( std::move( t ) ); }
120 void pop_back() { vec_.pop_back(); }
121
122 template<typename... Args>
123 T& emplace_back( Args&&... args ) { return vec_.emplace_back( std::forward<Args>(args)... ); }
124
125 [[nodiscard]] const_reference front() const { return vec_.front(); }
126 [[nodiscard]] reference front() { return vec_.front(); }
127 [[nodiscard]] const_reference back() const { return vec_.back(); }
128 [[nodiscard]] reference back() { return vec_.back(); }
130 [[nodiscard]] I beginId() const { return I( size_t(0) ); }
132 [[nodiscard]] I backId() const { assert( !vec_.empty() ); return I( vec_.size() - 1 ); }
134 [[nodiscard]] I endId() const { return I( vec_.size() ); }
135
136 [[nodiscard]] T* data() { return vec_.data(); }
137 [[nodiscard]] const T* data() const { return vec_.data(); }
138
139 void swap( Vector & b ) { vec_.swap( b.vec_ ); }
140
142 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
143
145 std::vector<T> vec_;
146
147#if defined( MR_PARSING_FOR_PB11_BINDINGS ) || defined( MR_COMPILING_PB11_BINDINGS )
148 static_assert( sizeof(T) > 0 );
149#endif
150};
151
152template <typename T, typename I>
153[[nodiscard]] MR_BIND_IGNORE inline auto begin( const Vector<T, I> & a )
154 { return a.vec_.begin(); }
155
156template <typename T, typename I>
157[[nodiscard]] MR_BIND_IGNORE inline auto begin( Vector<T, I> & a )
158 { return a.vec_.begin(); }
159
160template <typename T, typename I>
161[[nodiscard]] MR_BIND_IGNORE inline auto end( const Vector<T, I> & a )
162 { return a.vec_.end(); }
163
164template <typename T, typename I>
165[[nodiscard]] MR_BIND_IGNORE inline auto end( Vector<T, I> & a )
166 { return a.vec_.end(); }
167
169template <typename T, typename I>
170[[nodiscard]] inline T getAt( const Vector<T, I> & a, I id, T def = {} )
171{
172 return ( id && id < a.size() ) ? a[id] : def;
173}
174
175} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:29
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:20
void pop_back()
Definition MRMesh/MRVector.h:120
Vector()=default
void clear()
Definition MRMesh/MRVector.h:39
reference front()
Definition MRMesh/MRVector.h:126
typename std::vector< T >::iterator iterator
Definition MRMesh/MRVector.h:25
void autoResizeSet(I i, T val)
Definition MRMesh/MRVector.h:108
std::size_t size() const
Definition MRMesh/MRVector.h:42
std::size_t capacity() const
Definition MRMesh/MRVector.h:59
bool operator==(const Vector &b) const
Definition MRMesh/MRVector.h:36
void resizeWithReserve(size_t newSize)
doubles reserved memory until resize(newSize) can be done without reallocation
Definition MRMesh/MRVector.h:74
reference operator[](I i)
Definition MRMesh/MRVector.h:67
typename std::vector< T >::reference reference
Definition MRMesh/MRVector.h:23
void autoResizeSet(I pos, size_t len, T val)
sets elements [pos, pos+len) to given value, adjusting the size of the vector to include new elements
Definition MRMesh/MRVector.h:94
T * data()
Definition MRMesh/MRVector.h:136
Vector(InputIt first, InputIt last)
Definition MRMesh/MRVector.h:33
Vector(std::vector< T > &&vec)
Definition MRMesh/MRVector.h:31
void reserve(size_t capacity)
Definition MRMesh/MRVector.h:60
I beginId() const
returns the identifier of the first element
Definition MRMesh/MRVector.h:130
void resizeNoInit(size_t targetSize)
Definition MRMesh/MRVector.h:48
void push_back(const T &t)
Definition MRMesh/MRVector.h:118
Vector(size_t size)
Definition MRMesh/MRVector.h:29
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRVector.h:142
const_reference operator[](I i) const
Definition MRMesh/MRVector.h:62
I backId() const
returns the identifier of the back() element
Definition MRMesh/MRVector.h:132
Vector(std::initializer_list< T > init)
Definition MRMesh/MRVector.h:34
reference back()
Definition MRMesh/MRVector.h:128
typename std::vector< T >::const_reference const_reference
Definition MRMesh/MRVector.h:24
void resizeWithReserve(size_t newSize, const T &value)
doubles reserved memory until resize(newSize) can be done without reallocation
Definition MRMesh/MRVector.h:81
const T * data() const
Definition MRMesh/MRVector.h:137
typename std::vector< T >::value_type value_type
Definition MRMesh/MRVector.h:22
bool empty() const
Definition MRMesh/MRVector.h:40
bool operator!=(const Vector &b) const
Definition MRMesh/MRVector.h:37
Vector(size_t size, const T &val)
Definition MRMesh/MRVector.h:30
void resize(size_t newSize)
Definition MRMesh/MRVector.h:44
reference autoResizeAt(I i)
this accessor automatically adjusts the size of the vector
Definition MRMesh/MRVector.h:111
typename std::vector< T >::const_iterator const_iterator
Definition MRMesh/MRVector.h:26
std::vector< T > vec_
the user can directly manipulate the vector, anyway she cannot break anything
Definition MRMesh/MRVector.h:145
void resize(size_t newSize, const T &t)
Definition MRMesh/MRVector.h:45
void swap(Vector &b)
Definition MRMesh/MRVector.h:139
const_reference front() const
Definition MRMesh/MRVector.h:125
void push_back(T &&t)
Definition MRMesh/MRVector.h:119
T & emplace_back(Args &&... args)
Definition MRMesh/MRVector.h:123
const_reference back() const
Definition MRMesh/MRVector.h:127
I endId() const
returns backId() + 1
Definition MRMesh/MRVector.h:134
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:286
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:288
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
constexpr NoInit noInit
Definition MRMesh/MRMeshFwd.h:79
I
Definition MRMesh/MRMeshFwd.h:110