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  public:
33  // Construct a new MaxSizeVector, reserve n elements.
34  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
35  explicit MaxSizeVector(size_t n)
36  : reserve_(n), size_(0),
37  data_(static_cast<T*>(internal::aligned_malloc(n * sizeof(T)))) {
38  for (size_t i = 0; i < n; ++i) { new (&data_[i]) T; }
39  }
40 
41  // Construct a new MaxSizeVector, reserve and resize to n.
42  // Copy the init value to all elements.
43  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
44  MaxSizeVector(size_t n, const T& init)
45  : reserve_(n), size_(n),
46  data_(static_cast<T*>(internal::aligned_malloc(n * sizeof(T)))) {
47  for (size_t i = 0; i < n; ++i) { new (&data_[i]) T(init); }
48  }
49 
50  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
52  for (size_t i = 0; i < size_; ++i) {
53  data_[i].~T();
54  }
56  }
57 
58  void resize(size_t n) {
59  eigen_assert(n <= reserve_);
60  for (size_t i = size_; i < n; ++i) {
61  new (&data_[i]) T;
62  }
63  for (size_t i = n; i < size_; ++i) {
64  data_[i].~T();
65  }
66  size_ = n;
67  }
68 
69  // Append new elements (up to reserved size).
70  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
71  void push_back(const T& t) {
73  data_[size_++] = t;
74  }
75 
76  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
77  const T& operator[] (size_t i) const {
78  eigen_assert(i < size_);
79  return data_[i];
80  }
81 
82  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
83  T& operator[] (size_t i) {
84  eigen_assert(i < size_);
85  return data_[i];
86  }
87 
88  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
89  T& back() {
90  eigen_assert(size_ > 0);
91  return data_[size_ - 1];
92  }
93 
94  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
95  const T& back() const {
96  eigen_assert(size_ > 0);
97  return data_[size_ - 1];
98  }
99 
100  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
101  void pop_back() {
102  // NOTE: This does not destroy the value at the end the way
103  // std::vector's version of pop_back() does. That happens when
104  // the Vector is destroyed.
105  eigen_assert(size_ > 0);
106  size_--;
107  }
108 
109  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
110  size_t size() const { return size_; }
111 
112  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
113  bool empty() const { return size_ == 0; }
114 
115  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
116  T* data() { return data_; }
117 
118  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
119  const T* data() const { return data_; }
120 
121  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
122  T* begin() { return data_; }
123 
124  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
125  T* end() { return data_ + size_; }
126 
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
128  const T* begin() const { return data_; }
129 
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
131  const T* end() const { return data_ + size_; }
132 
133  private:
134  size_t reserve_;
135  size_t size_;
137 };
138 
139 } // namespace Eigen
140 
141 #endif // EIGEN_FIXEDSIZEVECTOR_H
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
EIGEN_DEVICE_FUNC void * aligned_malloc(std::size_t size)
Definition: Memory.h:153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * end()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n)
Definition: MaxSizeVector.h:35
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & back() const
Definition: MaxSizeVector.h:95
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void push_back(const T &t)
Definition: MaxSizeVector.h:71
int n
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T * begin() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition: MaxSizeVector.h:89
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T * end() const
EIGEN_DEVICE_FUNC void aligned_free(void *ptr)
Definition: Memory.h:174
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T * data() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & operator[](size_t i) const
Definition: MaxSizeVector.h:77
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool empty() const
void resize(size_t n)
Definition: MaxSizeVector.h:58
#define eigen_assert(x)
Definition: Macros.h:579
Eigen::Triplet< double > T
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * begin()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t size() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void pop_back()
The MaxSizeVector class.
Definition: MaxSizeVector.h:31
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~MaxSizeVector()
Definition: MaxSizeVector.h:51
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaxSizeVector(size_t n, const T &init)
Definition: MaxSizeVector.h:44
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:59