Eigen  5.0.1
 
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 + 1) > 0; --i) {
53 m_data[i - 1].~T();
54 }
55 internal::handmade_aligned_free(m_data);
56 EIGEN_THROW;
57 }
58 }
59
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~MaxSizeVector() {
61 for (size_t i = m_size; i > 0; --i) {
62 m_data[i - 1].~T();
63 }
64 internal::handmade_aligned_free(m_data);
65 }
66
67 void resize(size_t n) {
68 eigen_assert(n <= m_reserve);
69 for (; m_size < n; ++m_size) {
70 new (&m_data[m_size]) T;
71 }
72 for (; m_size > n; --m_size) {
73 m_data[m_size - 1].~T();
74 }
75 eigen_assert(m_size == n);
76 }
77
78 // Append new elements (up to reserved size).
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void push_back(const T& t) {
80 eigen_assert(m_size < m_reserve);
81 new (&m_data[m_size++]) T(t);
82 }
83
84 template <class X>
85 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void emplace_back(const X& x) {
86 eigen_assert(m_size < m_reserve);
87 new (&m_data[m_size++]) T(x);
88 }
89
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& operator[](size_t i) const {
91 eigen_assert(i < m_size);
92 return m_data[i];
93 }
94
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& operator[](size_t i) {
96 eigen_assert(i < m_size);
97 return m_data[i];
98 }
99
100 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& back() {
101 eigen_assert(m_size > 0);
102 return m_data[m_size - 1];
103 }
104
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& back() const {
106 eigen_assert(m_size > 0);
107 return m_data[m_size - 1];
108 }
109
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void pop_back() {
111 eigen_assert(m_size > 0);
112 m_data[--m_size].~T();
113 }
114
115 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t size() const { return m_size; }
116
117 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool empty() const { return m_size == 0; }
118
119 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
120
121 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
122
123 EIGEN_DEVICE_FUNC constexpr T* begin() { return m_data; }
124
125 EIGEN_DEVICE_FUNC constexpr T* end() { return m_data + m_size; }
126
127 EIGEN_DEVICE_FUNC constexpr const T* begin() const { return m_data; }
128
129 EIGEN_DEVICE_FUNC constexpr const T* end() const { return m_data + m_size; }
130
131 private:
132 size_t m_reserve;
133 size_t m_size;
134 T* m_data;
135};
136
137} // namespace Eigen
138
139#endif // EIGEN_FIXEDSIZEVECTOR_H