MaxSizeVector.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_FIXEDSIZEVECTOR_H
00011 #define EIGEN_FIXEDSIZEVECTOR_H
00012 
00013 namespace Eigen {
00014 
00030 template <typename T>
00031 class MaxSizeVector {
00032  public:
00033   // Construct a new MaxSizeVector, reserve n elements.
00034   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00035   explicit MaxSizeVector(size_t n)
00036       : reserve_(n), size_(0),
00037         data_(static_cast<T*>(internal::aligned_malloc(n * sizeof(T)))) {
00038     for (size_t i = 0; i < n; ++i) { new (&data_[i]) T; }
00039   }
00040 
00041   // Construct a new MaxSizeVector, reserve and resize to n.
00042   // Copy the init value to all elements.
00043   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00044   MaxSizeVector(size_t n, const T& init)
00045       : reserve_(n), size_(n),
00046         data_(static_cast<T*>(internal::aligned_malloc(n * sizeof(T)))) {
00047     for (size_t i = 0; i < n; ++i) { new (&data_[i]) T(init); }
00048   }
00049 
00050   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00051   ~MaxSizeVector() {
00052     for (size_t i = 0; i < size_; ++i) {
00053       data_[i].~T();
00054     }
00055     internal::aligned_free(data_);
00056   }
00057 
00058   void resize(size_t n) {
00059     eigen_assert(n <= reserve_);
00060     for (size_t i = size_; i < n; ++i) {
00061       new (&data_[i]) T;
00062     }
00063     for (size_t i = n; i < size_; ++i) {
00064       data_[i].~T();
00065     }
00066     size_ = n;
00067   }
00068 
00069   // Append new elements (up to reserved size).
00070   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00071   void push_back(const T& t) {
00072     eigen_assert(size_ < reserve_);
00073     data_[size_++] = t;
00074   }
00075 
00076   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00077   const T& operator[] (size_t i) const {
00078     eigen_assert(i < size_);
00079     return data_[i];
00080   }
00081 
00082   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00083   T& operator[] (size_t i) {
00084     eigen_assert(i < size_);
00085     return data_[i];
00086   }
00087 
00088   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00089   T& back() {
00090     eigen_assert(size_ > 0);
00091     return data_[size_ - 1];
00092   }
00093 
00094   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00095   const T& back() const {
00096     eigen_assert(size_ > 0);
00097     return data_[size_ - 1];
00098   }
00099 
00100   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00101   void pop_back() {
00102     // NOTE: This does not destroy the value at the end the way
00103     // std::vector's version of pop_back() does.  That happens when
00104     // the Vector is destroyed.
00105     eigen_assert(size_ > 0);
00106     size_--;
00107   }
00108 
00109   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00110   size_t size() const { return size_; }
00111 
00112   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00113   bool empty() const { return size_ == 0; }
00114 
00115   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00116   T* data() { return data_; }
00117 
00118   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00119   const T* data() const { return data_; }
00120 
00121   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00122   T* begin() { return data_; }
00123 
00124   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00125   T* end() { return data_ + size_; }
00126 
00127   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00128   const T* begin() const { return data_; }
00129 
00130   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00131   const T* end() const { return data_ + size_; }
00132 
00133  private:
00134   size_t reserve_;
00135   size_t size_;
00136   T* data_;
00137 };
00138 
00139 }  // namespace Eigen
00140 
00141 #endif  // EIGEN_FIXEDSIZEVECTOR_H
 All Classes Functions Variables Typedefs Enumerator