MeshLib Documentation
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, const T & value = T() )
75 {
76 auto reserved = vec_.capacity();
77 if ( reserved > 0 && newSize > reserved )
78 {
79 while ( newSize > reserved )
80 reserved <<= 1;
81 vec_.reserve( reserved );
82 }
83 vec_.resize( newSize, value );
84 }
85
87 void autoResizeSet( I pos, size_t len, T val )
88 {
89 assert( pos );
90 const size_t p = pos;
91 if ( const auto sz = size(); p + len > sz )
92 {
93 resizeWithReserve( p + len, val );
94 if ( p >= sz )
95 return;
96 len = sz - p;
97 }
98 for ( size_t i = 0; i < len; ++i )
99 vec_[ p + i ] = val;
100 }
101 void autoResizeSet( I i, T val ) { autoResizeSet( i, 1, val ); }
102
104 [[nodiscard]] reference autoResizeAt( I i ) MR_REQUIRES_IF_SUPPORTED( sizeof(T)>0 && std::default_initializable<T> )
105 {
106 if ( i + 1 > size() )
107 resizeWithReserve( i + 1 );
108 return vec_[i];
109 }
110
111 void push_back( const T & t ) { vec_.push_back( t ); }
112 void push_back( T && t ) { vec_.push_back( std::move( t ) ); }
113 void pop_back() { vec_.pop_back(); }
114
115 template<typename... Args>
116 T& emplace_back( Args&&... args ) { return vec_.emplace_back( std::forward<Args>(args)... ); }
117
118 [[nodiscard]] const_reference front() const { return vec_.front(); }
119 [[nodiscard]] reference front() { return vec_.front(); }
120 [[nodiscard]] const_reference back() const { return vec_.back(); }
121 [[nodiscard]] reference back() { return vec_.back(); }
123 [[nodiscard]] I beginId() const { return I( size_t(0) ); }
125 [[nodiscard]] I backId() const { assert( !vec_.empty() ); return I( vec_.size() - 1 ); }
127 [[nodiscard]] I endId() const { return I( vec_.size() ); }
128
129 [[nodiscard]] T* data() { return vec_.data(); }
130 [[nodiscard]] const T* data() const { return vec_.data(); }
131
132 void swap( Vector & b ) { vec_.swap( b.vec_ ); }
133
135 [[nodiscard]] size_t heapBytes() const { return capacity() * sizeof(T); }
136
138 std::vector<T> vec_;
139
140#if defined( MR_PARSING_FOR_PB11_BINDINGS ) || defined( MR_COMPILING_PB11_BINDINGS )
141 static_assert( sizeof(T) > 0 );
142#endif
143};
144
145template <typename T, typename I>
146[[nodiscard]] MR_BIND_IGNORE inline auto begin( const Vector<T, I> & a )
147 { return a.vec_.begin(); }
148
149template <typename T, typename I>
150[[nodiscard]] MR_BIND_IGNORE inline auto begin( Vector<T, I> & a )
151 { return a.vec_.begin(); }
152
153template <typename T, typename I>
154[[nodiscard]] MR_BIND_IGNORE inline auto end( const Vector<T, I> & a )
155 { return a.vec_.end(); }
156
157template <typename T, typename I>
158[[nodiscard]] MR_BIND_IGNORE inline auto end( Vector<T, I> & a )
159 { return a.vec_.end(); }
160
162template <typename T, typename I>
163[[nodiscard]] inline T getAt( const Vector<T, I> & a, I id, T def = {} )
164{
165 return ( id && id < a.size() ) ? a[id] : def;
166}
167
168} // 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:113
Vector()=default
void clear()
Definition MRMesh/MRVector.h:39
reference front()
Definition MRMesh/MRVector.h:119
typename std::vector< T >::iterator iterator
Definition MRMesh/MRVector.h:25
void autoResizeSet(I i, T val)
Definition MRMesh/MRVector.h:101
std::size_t size() const
Definition MRMesh/MRVector.h:42
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:44
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:104
reference operator[](I i)
Definition MRMesh/MRVector.h:67
void resizeWithReserve(size_t newSize, const T &value=T())
doubles reserved memory until resize(newSize) can be done without reallocation
Definition MRMesh/MRVector.h:74
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:87
T * data()
Definition MRMesh/MRVector.h:129
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:123
void push_back(const T &t)
Definition MRMesh/MRVector.h:111
void resizeNoInit(size_t targetSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:48
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRVector.h:135
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:125
Vector(std::initializer_list< T > init)
Definition MRMesh/MRVector.h:34
bool operator!=(const Vector &b) const MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:37
reference back()
Definition MRMesh/MRVector.h:121
typename std::vector< T >::const_reference const_reference
Definition MRMesh/MRVector.h:24
const T * data() const
Definition MRMesh/MRVector.h:130
typename std::vector< T >::value_type value_type
Definition MRMesh/MRVector.h:22
bool operator==(const Vector &b) const MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:36
bool empty() const
Definition MRMesh/MRVector.h:40
Vector(size_t size, const T &val)
Definition MRMesh/MRVector.h:30
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:138
void swap(Vector &b)
Definition MRMesh/MRVector.h:132
void resize(size_t newSize, const T &t) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:45
const_reference front() const
Definition MRMesh/MRVector.h:118
Vector(size_t size) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:29
void push_back(T &&t)
Definition MRMesh/MRVector.h:112
T & emplace_back(Args &&... args)
Definition MRMesh/MRVector.h:116
const_reference back() const
Definition MRMesh/MRVector.h:120
I endId() const
returns backId() + 1
Definition MRMesh/MRVector.h:127
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:280
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:282
Definition MRCameraOrientationPlugin.h:8
T getAt(const Buffer< T, I > &bmap, I key)
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:57
I
Definition MRMesh/MRMeshFwd.h:88