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@inria.fr>
5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@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
14namespace Eigen {
15
16/** \class VectorBlock
17  * \ingroup Core_Module
18  *
19  * \brief Expression of a fixed-size or dynamic-size sub-vector
20  *
21  * \param VectorType the type of the object in which we are taking a sub-vector
22  * \param Size size of the sub-vector we are taking at compile time (optional)
23  *
24  * This class represents an expression of either a fixed-size or dynamic-size sub-vector.
25  * It is the return type of DenseBase::segment(Index,Index) and DenseBase::segment<int>(Index) and
26  * most of the time this is the only way it is used.
27  *
28  * However, if you want to directly maniputate sub-vector expressions,
29  * for instance if you want to write a function returning such an expression, you
30  * will need to use this class.
31  *
32  * Here is an example illustrating the dynamic case:
33  * \include class_VectorBlock.cpp
34  * Output: \verbinclude class_VectorBlock.out
35  *
36  * \note Even though this expression has dynamic size, in the case where \a VectorType
37  * has fixed size, this expression inherits a fixed maximal size which means that evaluating
38  * it does not cause a dynamic memory allocation.
39  *
40  * Here is an example illustrating the fixed-size case:
41  * \include class_FixedVectorBlock.cpp
42  * Output: \verbinclude class_FixedVectorBlock.out
43  *
44  * \sa class Block, DenseBase::segment(Index,Index,Index,Index), DenseBase::segment(Index,Index)
45  */
46
47namespace internal {
48template<typename VectorType, int Size>
49struct traits<VectorBlock<VectorType, Size> >
50  : public traits<Block<VectorType,
51                     traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
52                     traits<VectorType>::Flags & RowMajorBit ? Size : 1> >
53{
54};
55}
56
57template<typename VectorType, int Size> class VectorBlock
58  : public Block<VectorType,
59                     internal::traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
60                     internal::traits<VectorType>::Flags & RowMajorBit ? Size : 1>
61{
62    typedef Block<VectorType,
63                     internal::traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
64                     internal::traits<VectorType>::Flags & RowMajorBit ? Size : 1> Base;
65    enum {
66      IsColVector = !(internal::traits<VectorType>::Flags & RowMajorBit)
67    };
68  public:
69    EIGEN_DENSE_PUBLIC_INTERFACE(VectorBlock)
70
71    using Base::operator=;
72
73    /** Dynamic-size constructor
74      */
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    inline VectorBlock(VectorType& vector, Index start)
86      : Base(vector, IsColVector ? start : 0, IsColVector ? 0 : start)
87    {
88      EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorBlock);
89    }
90};
91
92
93} // end namespace Eigen
94
95#endif // EIGEN_VECTORBLOCK_H
96