Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
MaxSizeVector.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_FIXEDSIZEVECTOR_H
12#define EIGEN_FIXEDSIZEVECTOR_H
13
14namespace Eigen {
15
31template <typename T>
32class MaxSizeVector {
33 static const size_t alignment = internal::plain_enum_max(EIGEN_ALIGNOF(T), sizeof(void*));
34
35 public:
36 // Construct a new MaxSizeVector, reserve n elements.
37 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit MaxSizeVector(size_t n)
38 : m_reserve(n), m_size(0), m_data(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {}
39
40 // Construct a new MaxSizeVector, reserve and resize to n.
41 // Copy the init value to all elements.
42 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n, const T& init)
43 : m_reserve(n), m_size(n), m_data(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
44 size_t i = 0;
45 EIGEN_TRY {
46 for (; i < m_size; ++i) {
47 new (&m_data[i]) T(init);
48 }
49 }
50 EIGEN_CATCH(...) {
51 // Construction failed, destruct in reverse order:
52 for (; i > 0; --i) {
53 m_data[i - 1].~T();
54 }
55 internal::handmade_aligned_free(m_data);
56 EIGEN_THROW;
57 }
58 }
59
60 // The vector owns its buffer, so a member-wise copy would free it twice.
61 MaxSizeVector(const MaxSizeVector&) = delete;
62 MaxSizeVector& operator=(const MaxSizeVector&) = delete;
63
64 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~MaxSizeVector() {
65 for (size_t i = m_size; i > 0; --i) {
66 m_data[i - 1].~T();
67 }
68 internal::handmade_aligned_free(m_data);
69 }
70
71 void resize(size_t n) {
72 eigen_assert(n <= m_reserve);
73 for (; m_size < n; ++m_size) {
74 new (&m_data[m_size]) T;
75 }
76 for (; m_size > n; --m_size) {
77 m_data[m_size - 1].~T();
78 }
79 eigen_assert(m_size == n);
80 }
81
82 // Append new elements (up to reserved size).
83 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void push_back(const T& t) {
84 eigen_assert(m_size < m_reserve);
85 new (&m_data[m_size]) T(t);
86 ++m_size;
87 }
88
89 template <class X>
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void emplace_back(const X& x) {
91 eigen_assert(m_size < m_reserve);
92 new (&m_data[m_size]) T(x);
93 ++m_size;
94 }
95
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& operator[](size_t i) const {
97 eigen_assert(i < m_size);
98 return m_data[i];
99 }
100
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& operator[](size_t i) {
102 eigen_assert(i < m_size);
103 return m_data[i];
104 }
105
106 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& back() {
107 eigen_assert(m_size > 0);
108 return m_data[m_size - 1];
109 }
110
111 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& back() const {
112 eigen_assert(m_size > 0);
113 return m_data[m_size - 1];
114 }
115
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void pop_back() {
117 eigen_assert(m_size > 0);
118 m_data[--m_size].~T();
119 }
120
121 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t size() const { return m_size; }
122
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool empty() const { return m_size == 0; }
124
125 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
126
127 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
128
129 EIGEN_DEVICE_FUNC constexpr T* begin() { return m_data; }
130
131 EIGEN_DEVICE_FUNC constexpr T* end() { return m_data + m_size; }
132
133 EIGEN_DEVICE_FUNC constexpr const T* begin() const { return m_data; }
134
135 EIGEN_DEVICE_FUNC constexpr const T* end() const { return m_data + m_size; }
136
137 private:
138 size_t m_reserve;
139 size_t m_size;
140 T* m_data;
141};
142
143} // namespace Eigen
144
145#endif // EIGEN_FIXEDSIZEVECTOR_H