Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:14:27

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_FIXEDSIZEVECTOR_H
0011 #define EIGEN_FIXEDSIZEVECTOR_H
0012 
0013 namespace Eigen {
0014 
0015 /** \class MaxSizeVector
0016   * \ingroup Core
0017   *
0018   * \brief The MaxSizeVector class.
0019   *
0020   * The %MaxSizeVector provides a subset of std::vector functionality.
0021   *
0022   * The goal is to provide basic std::vector operations when using
0023   * std::vector is not an option (e.g. on GPU or when compiling using
0024   * FMA/AVX, as this can cause either compilation failures or illegal
0025   * instruction failures).
0026   *
0027   * Beware: The constructors are not API compatible with these of
0028   * std::vector.
0029   */
0030 template <typename T>
0031 class MaxSizeVector {
0032   static const size_t alignment = EIGEN_PLAIN_ENUM_MAX(EIGEN_ALIGNOF(T), sizeof(void*));
0033  public:
0034   // Construct a new MaxSizeVector, reserve n elements.
0035   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0036   explicit MaxSizeVector(size_t n)
0037       : reserve_(n), size_(0),
0038         data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
0039   }
0040 
0041   // Construct a new MaxSizeVector, reserve and resize to n.
0042   // Copy the init value to all elements.
0043   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0044   MaxSizeVector(size_t n, const T& init)
0045       : reserve_(n), size_(n),
0046         data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
0047     size_t i = 0;
0048     EIGEN_TRY
0049     {
0050       for(; i < size_; ++i) { new (&data_[i]) T(init); }
0051     }
0052     EIGEN_CATCH(...)
0053     {
0054       // Construction failed, destruct in reverse order:
0055       for(; (i+1) > 0; --i) { data_[i-1].~T(); }
0056       internal::handmade_aligned_free(data_);
0057       EIGEN_THROW;
0058     }
0059   }
0060 
0061   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0062   ~MaxSizeVector() {
0063     for (size_t i = size_; i > 0; --i) {
0064       data_[i-1].~T();
0065     }
0066     internal::handmade_aligned_free(data_);
0067   }
0068 
0069   void resize(size_t n) {
0070     eigen_assert(n <= reserve_);
0071     for (; size_ < n; ++size_) {
0072       new (&data_[size_]) T;
0073     }
0074     for (; size_ > n; --size_) {
0075       data_[size_-1].~T();
0076     }
0077     eigen_assert(size_ == n);
0078   }
0079 
0080   // Append new elements (up to reserved size).
0081   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0082   void push_back(const T& t) {
0083     eigen_assert(size_ < reserve_);
0084     new (&data_[size_++]) T(t);
0085   }
0086 
0087   // For C++03 compatibility this only takes one argument
0088   template<class X>
0089   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0090   void emplace_back(const X& x) {
0091     eigen_assert(size_ < reserve_);
0092     new (&data_[size_++]) T(x);
0093   }
0094 
0095 
0096   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0097   const T& operator[] (size_t i) const {
0098     eigen_assert(i < size_);
0099     return data_[i];
0100   }
0101 
0102   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0103   T& operator[] (size_t i) {
0104     eigen_assert(i < size_);
0105     return data_[i];
0106   }
0107 
0108   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0109   T& back() {
0110     eigen_assert(size_ > 0);
0111     return data_[size_ - 1];
0112   }
0113 
0114   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0115   const T& back() const {
0116     eigen_assert(size_ > 0);
0117     return data_[size_ - 1];
0118   }
0119 
0120   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0121   void pop_back() {
0122     eigen_assert(size_ > 0);
0123     data_[--size_].~T();
0124   }
0125 
0126   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0127   size_t size() const { return size_; }
0128 
0129   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0130   bool empty() const { return size_ == 0; }
0131 
0132   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0133   T* data() { return data_; }
0134 
0135   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0136   const T* data() const { return data_; }
0137 
0138   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0139   T* begin() { return data_; }
0140 
0141   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0142   T* end() { return data_ + size_; }
0143 
0144   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0145   const T* begin() const { return data_; }
0146 
0147   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0148   const T* end() const { return data_ + size_; }
0149 
0150  private:
0151   size_t reserve_;
0152   size_t size_;
0153   T* data_;
0154 };
0155 
0156 }  // namespace Eigen
0157 
0158 #endif  // EIGEN_FIXEDSIZEVECTOR_H