Home | History | Annotate | Download | only in Core
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #ifndef EIGEN_VECTORBLOCK_H
     12 #define EIGEN_VECTORBLOCK_H
     13 
     14 namespace Eigen {
     15 
     16 namespace internal {
     17 template<typename VectorType, int Size>
     18 struct traits<VectorBlock<VectorType, Size> >
     19   : public traits<Block<VectorType,
     20                      traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
     21                      traits<VectorType>::Flags & RowMajorBit ? Size : 1> >
     22 {
     23 };
     24 }
     25 
     26 /** \class VectorBlock
     27   * \ingroup Core_Module
     28   *
     29   * \brief Expression of a fixed-size or dynamic-size sub-vector
     30   *
     31   * \tparam VectorType the type of the object in which we are taking a sub-vector
     32   * \tparam Size size of the sub-vector we are taking at compile time (optional)
     33   *
     34   * This class represents an expression of either a fixed-size or dynamic-size sub-vector.
     35   * It is the return type of DenseBase::segment(Index,Index) and DenseBase::segment<int>(Index) and
     36   * most of the time this is the only way it is used.
     37   *
     38   * However, if you want to directly maniputate sub-vector expressions,
     39   * for instance if you want to write a function returning such an expression, you
     40   * will need to use this class.
     41   *
     42   * Here is an example illustrating the dynamic case:
     43   * \include class_VectorBlock.cpp
     44   * Output: \verbinclude class_VectorBlock.out
     45   *
     46   * \note Even though this expression has dynamic size, in the case where \a VectorType
     47   * has fixed size, this expression inherits a fixed maximal size which means that evaluating
     48   * it does not cause a dynamic memory allocation.
     49   *
     50   * Here is an example illustrating the fixed-size case:
     51   * \include class_FixedVectorBlock.cpp
     52   * Output: \verbinclude class_FixedVectorBlock.out
     53   *
     54   * \sa class Block, DenseBase::segment(Index,Index,Index,Index), DenseBase::segment(Index,Index)
     55   */
     56 template<typename VectorType, int Size> class VectorBlock
     57   : public Block<VectorType,
     58                      internal::traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
     59                      internal::traits<VectorType>::Flags & RowMajorBit ? Size : 1>
     60 {
     61     typedef Block<VectorType,
     62                      internal::traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
     63                      internal::traits<VectorType>::Flags & RowMajorBit ? Size : 1> Base;
     64     enum {
     65       IsColVector = !(internal::traits<VectorType>::Flags & RowMajorBit)
     66     };
     67   public:
     68     EIGEN_DENSE_PUBLIC_INTERFACE(VectorBlock)
     69 
     70     using Base::operator=;
     71 
     72     /** Dynamic-size constructor
     73       */
     74     EIGEN_DEVICE_FUNC
     75     inline VectorBlock(VectorType& vector, Index start, Index size)
     76       : Base(vector,
     77              IsColVector ? start : 0, IsColVector ? 0 : start,
     78              IsColVector ? size  : 1, IsColVector ? 1 : size)
     79     {
     80       EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorBlock);
     81     }
     82 
     83     /** Fixed-size constructor
     84       */
     85     EIGEN_DEVICE_FUNC
     86     inline VectorBlock(VectorType& vector, Index start)
     87       : Base(vector, IsColVector ? start : 0, IsColVector ? 0 : start)
     88     {
     89       EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorBlock);
     90     }
     91 };
     92 
     93 
     94 } // end namespace Eigen
     95 
     96 #endif // EIGEN_VECTORBLOCK_H
     97