Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
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 <[email protected]>
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
10#ifndef EIGEN_FIXEDSIZEVECTOR_H
11#define EIGEN_FIXEDSIZEVECTOR_H
12
13namespace Eigen {
14
30template <typename T>
32 static const size_t alignment = internal::plain_enum_max(EIGEN_ALIGNOF(T), sizeof(void*));
33
34 public:
35 // Construct a new MaxSizeVector, reserve n elements.
36 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit MaxSizeVector(size_t n)
37 : reserve_(n), size_(0), data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {}
38
39 // Construct a new MaxSizeVector, reserve and resize to n.
40 // Copy the init value to all elements.
41 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n, const T& init)
42 : reserve_(n), size_(n), data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
43 size_t i = 0;
44 EIGEN_TRY {
45 for (; i < size_; ++i) {
46 new (&data_[i]) T(init);
47 }
48 }
49 EIGEN_CATCH(...) {
50 // Construction failed, destruct in reverse order:
51 for (; (i + 1) > 0; --i) {
52 data_[i - 1].~T();
53 }
54 internal::handmade_aligned_free(data_);
55 EIGEN_THROW;
56 }
57 }
58
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~MaxSizeVector() {
60 for (size_t i = size_; i > 0; --i) {
61 data_[i - 1].~T();
62 }
63 internal::handmade_aligned_free(data_);
64 }
65
66 void resize(size_t n) {
67 eigen_assert(n <= reserve_);
68 for (; size_ < n; ++size_) {
69 new (&data_[size_]) T;
70 }
71 for (; size_ > n; --size_) {
72 data_[size_ - 1].~T();
73 }
74 eigen_assert(size_ == n);
75 }
76
77 // Append new elements (up to reserved size).
78 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void push_back(const T& t) {
79 eigen_assert(size_ < reserve_);
80 new (&data_[size_++]) T(t);
81 }
82
83 // For C++03 compatibility this only takes one argument
84 template <class X>
85 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void emplace_back(const X& x) {
86 eigen_assert(size_ < reserve_);
87 new (&data_[size_++]) T(x);
88 }
89
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& operator[](size_t i) const {
91 eigen_assert(i < size_);
92 return data_[i];
93 }
94
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& operator[](size_t i) {
96 eigen_assert(i < size_);
97 return data_[i];
98 }
99
100 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& back() {
101 eigen_assert(size_ > 0);
102 return data_[size_ - 1];
103 }
104
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& back() const {
106 eigen_assert(size_ > 0);
107 return data_[size_ - 1];
108 }
109
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void pop_back() {
111 eigen_assert(size_ > 0);
112 data_[--size_].~T();
113 }
114
115 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t size() const { return size_; }
116
117 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool empty() const { return size_ == 0; }
118
119 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* data() { return data_; }
120
121 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return data_; }
122
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* begin() { return data_; }
124
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* end() { return data_ + size_; }
126
127 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* begin() const { return data_; }
128
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* end() const { return data_ + size_; }
130
131 private:
132 size_t reserve_;
133 size_t size_;
134 T* data_;
135};
136
137} // namespace Eigen
138
139#endif // EIGEN_FIXEDSIZEVECTOR_H
The MaxSizeVector class.
Definition MaxSizeVector.h:31
Namespace containing all symbols from the Eigen library.
Definition Core:137