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 
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
51  ~MaxSizeVector() {
52  for (size_t i = 0; i < size_; ++i) {
53  data_[i].~T();
54  }
55  internal::aligned_free(data_);
56  }
57 
58  // Append new elements (up to reserved size).
59  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
60  void push_back(const T& t) {
61  eigen_assert(size_ < reserve_);
62  data_[size_++] = t;
63  }
64 
65  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
66  const T& operator[] (size_t i) const {
67  eigen_assert(i < size_);
68  return data_[i];
69  }
70 
71  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
72  T& operator[] (size_t i) {
73  eigen_assert(i < size_);
74  return data_[i];
75  }
76 
77  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
78  T& back() {
79  eigen_assert(size_ > 0);
80  return data_[size_ - 1];
81  }
82 
83  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
84  const T& back() const {
85  eigen_assert(size_ > 0);
86  return data_[size_ - 1];
87  }
88 
89  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
90  void pop_back() {
91  // NOTE: This does not destroy the value at the end the way
92  // std::vector's version of pop_back() does. That happens when
93  // the Vector is destroyed.
94  eigen_assert(size_ > 0);
95  size_--;
96  }
97 
98  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
99  size_t size() const { return size_; }
100 
101  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
102  bool empty() const { return size_ == 0; }
103 
104  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
105  T* data() { return data_; }
106 
107  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
108  const T* data() const { return data_; }
109 
110  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
111  T* begin() { return data_; }
112 
113  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
114  T* end() { return data_ + size_; }
115 
116  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
117  const T* begin() const { return data_; }
118 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
120  const T* end() const { return data_ + size_; }
121 
122  private:
123  size_t reserve_;
124  size_t size_;
125  T* data_;
126 };
127 
128 } // namespace Eigen
129 
130 #endif // EIGEN_FIXEDSIZEVECTOR_H
Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45
The MaxSizeVector class.
Definition: MaxSizeVector.h:31