MaxSizeVector.h
Go to the documentation of this file.
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 
10 #ifndef EIGEN_FIXEDSIZEVECTOR_H
11 #define EIGEN_FIXEDSIZEVECTOR_H
12 
13 namespace Eigen {
14 
30 template <typename T>
32  static const size_t alignment = EIGEN_PLAIN_ENUM_MAX(EIGEN_ALIGNOF(T), sizeof(void*));
33  public:
34  // Construct a new MaxSizeVector, reserve n elements.
36  explicit MaxSizeVector(size_t n)
37  : reserve_(n), size_(0),
38  data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
39  }
40 
41  // Construct a new MaxSizeVector, reserve and resize to n.
42  // Copy the init value to all elements.
44  MaxSizeVector(size_t n, const T& init)
45  : reserve_(n), size_(n),
46  data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
47  size_t i = 0;
48  EIGEN_TRY
49  {
50  for(; i < size_; ++i) { new (&data_[i]) T(init); }
51  }
52  EIGEN_CATCH(...)
53  {
54  // Construction failed, destruct in reverse order:
55  for(; (i+1) > 0; --i) { data_[i-1].~T(); }
58  }
59  }
60 
63  for (size_t i = size_; i > 0; --i) {
64  data_[i-1].~T();
65  }
67  }
68 
69  void resize(size_t n) {
71  for (; size_ < n; ++size_) {
72  new (&data_[size_]) T;
73  }
74  for (; size_ > n; --size_) {
75  data_[size_-1].~T();
76  }
77  eigen_assert(size_ == n);
78  }
79 
80  // Append new elements (up to reserved size).
82  void push_back(const T& t) {
84  new (&data_[size_++]) T(t);
85  }
86 
87  // For C++03 compatibility this only takes one argument
88  template<class X>
90  void emplace_back(const X& x) {
92  new (&data_[size_++]) T(x);
93  }
94 
95 
97  const T& operator[] (size_t i) const {
98  eigen_assert(i < size_);
99  return data_[i];
100  }
101 
103  T& operator[] (size_t i) {
104  eigen_assert(i < size_);
105  return data_[i];
106  }
107 
109  T& back() {
110  eigen_assert(size_ > 0);
111  return data_[size_ - 1];
112  }
113 
115  const T& back() const {
116  eigen_assert(size_ > 0);
117  return data_[size_ - 1];
118  }
119 
121  void pop_back() {
122  eigen_assert(size_ > 0);
123  data_[--size_].~T();
124  }
125 
127  size_t size() const { return size_; }
128 
130  bool empty() const { return size_ == 0; }
131 
133  T* data() { return data_; }
134 
136  const T* data() const { return data_; }
137 
139  T* begin() { return data_; }
140 
142  T* end() { return data_ + size_; }
143 
145  const T* begin() const { return data_; }
146 
148  const T* end() const { return data_ + size_; }
149 
150  private:
151  size_t reserve_;
152  size_t size_;
154 };
155 
156 } // namespace Eigen
157 
158 #endif // EIGEN_FIXEDSIZEVECTOR_H
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::MaxSizeVector::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
Definition: MaxSizeVector.h:133
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_CATCH
#define EIGEN_CATCH(X)
Definition: Macros.h:1407
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::MaxSizeVector::back
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE T & back() const
Definition: MaxSizeVector.h:115
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
Eigen::MaxSizeVector::alignment
static const size_t alignment
Definition: MaxSizeVector.h:32
X
#define X
Definition: icosphere.cpp:20
Eigen::MaxSizeVector::end
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE T * end() const
Definition: MaxSizeVector.h:148
Eigen::internal::handmade_aligned_malloc
EIGEN_DEVICE_FUNC void * handmade_aligned_malloc(std::size_t size, std::size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition: Memory.h:100
Eigen::MaxSizeVector::pop_back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void pop_back()
Definition: MaxSizeVector.h:121
Eigen::MaxSizeVector::MaxSizeVector
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n, const T &init)
Definition: MaxSizeVector.h:44
Eigen::MaxSizeVector::begin
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE T * begin() const
Definition: MaxSizeVector.h:145
EIGEN_THROW
#define EIGEN_THROW
Definition: Macros.h:1404
Eigen::MaxSizeVector::begin
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * begin()
Definition: MaxSizeVector.h:139
Eigen::MaxSizeVector::~MaxSizeVector
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~MaxSizeVector()
Definition: MaxSizeVector.h:62
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::MaxSizeVector::size
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t size() const
Definition: MaxSizeVector.h:127
EIGEN_TRY
#define EIGEN_TRY
Definition: Macros.h:1406
Eigen::internal::handmade_aligned_free
EIGEN_DEVICE_FUNC void handmade_aligned_free(void *ptr)
Definition: Memory.h:114
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::MaxSizeVector::emplace_back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void emplace_back(const X &x)
Definition: MaxSizeVector.h:90
Eigen::Triplet< double >
Eigen::MaxSizeVector::data_
T * data_
Definition: MaxSizeVector.h:153
Eigen::MaxSizeVector::push_back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void push_back(const T &t)
Definition: MaxSizeVector.h:82
Eigen::MaxSizeVector::reserve_
size_t reserve_
Definition: MaxSizeVector.h:151
Eigen::MaxSizeVector::end
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * end()
Definition: MaxSizeVector.h:142
Eigen::MaxSizeVector::size_
size_t size_
Definition: MaxSizeVector.h:152
Eigen::MaxSizeVector::operator[]
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE T & operator[](size_t i) const
Definition: MaxSizeVector.h:97
EIGEN_PLAIN_ENUM_MAX
#define EIGEN_PLAIN_ENUM_MAX(a, b)
Definition: Macros.h:1289
Eigen::MaxSizeVector::MaxSizeVector
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n)
Definition: MaxSizeVector.h:36
Eigen::MaxSizeVector::empty
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool empty() const
Definition: MaxSizeVector.h:130
internal
Definition: BandTriangularSolver.h:13
Eigen::MaxSizeVector::resize
void resize(size_t n)
Definition: MaxSizeVector.h:69
align_3::t
Point2 t(10, 10)
init
Definition: TutorialInplaceLU.cpp:2
Eigen::MaxSizeVector::back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition: MaxSizeVector.h:109
Eigen::MaxSizeVector::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE T * data() const
Definition: MaxSizeVector.h:136
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::MaxSizeVector
The MaxSizeVector class.
Definition: MaxSizeVector.h:31


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:59