cheshirekow  v0.1.0
view.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of fiber.
5  *
6  * fiber is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * fiber is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with fiber. If not, see <http://www.gnu.org/licenses/>.
18  */
25 #ifndef FIBER_VIEW_H_
26 #define FIBER_VIEW_H_
27 
28 
29 namespace fiber {
30 
32 template<typename Scalar, class Exp, Size rows_, Size cols_>
33 class _RView : public _RValue<Scalar, _RView<Scalar, Exp, rows_, cols_> > {
34  protected:
35  Exp const& A_;
38 
39  public:
40  enum {
41  ROWS_ = rows_,
42  COLS_ = cols_,
43  SIZE_ = rows_ * cols_
44  };
45 
46  _RView(Exp const& A, int i, int j)
47  : A_(A), i_(i), j_(j) {
48  assert(i + ROWS_ <= Exp::ROWS_
49  && j + COLS_ <= Exp::COLS_);
50  }
51 
52  Size size() const { return rows_ * cols_; }
53  Size rows() const { return rows_; }
54  Size cols() const { return cols_; }
55 
56  Scalar operator[](Index k) const {
57  assert(k < SIZE_);
58  Index i = k / cols_;
59  Index j = k - i * cols_;
60  return (*this)(i, j);
61  }
62 
63  Scalar operator()(Index i, Index j) const {
64  assert(i < rows_ && j < cols_);
65  return A_(i_ + i, j_ + j);
66  }
67 };
68 
69 template<int rows, int cols, typename Scalar, class Exp>
71  Index i, Index j) {
72  return _RView<Scalar, Exp, rows, cols>(static_cast<Exp const&>(A), i, j);
73 }
74 
75 template<typename Scalar, class Exp>
77  int i) {
78  return _RView<Scalar, Exp, 1, Exp::COLS_>(static_cast<Exp const&>(A), i, 0);
79 }
80 
81 template<int rows, typename Scalar, class Exp>
83  _RValue<Scalar, Exp> const& A, int i) {
84  return _RView<Scalar, Exp, rows, Exp::COLS_>(static_cast<Exp const&>(A),
85  i, 0);
86 }
87 
88 template<typename Scalar, class Exp>
90  _RValue<Scalar, Exp> const& A, int j) {
91  return _RView<Scalar, Exp, Exp::ROWS_, 1>(static_cast<Exp const&>(A), 0, j);
92 }
93 
94 template< int cols, typename Scalar, class Exp>
96  _RValue<Scalar, Exp> const& A, int j) {
97  return _RView<Scalar, Exp, Exp::ROWS_, cols>(static_cast<Exp const&>(A),
98  0, j);
99 }
100 
102 template <typename Scalar, class Exp, int rows_, int cols_>
103 class LView : public _LValue<Scalar, LView<Scalar, Exp, rows_, cols_> > {
104 
105  protected:
106  Exp& A_;
109 
110  public:
113 
114  enum {
115  ROWS_ = rows_,
116  COLS_ = cols_,
117  SIZE_ = rows_ * cols_
118  };
119 
120  LView(Exp& A, Index i, Index j)
121  : A_(A), i_(i), j_(j) {}
122 
123  Size size() const { return rows_ * cols_; }
124  Size rows() const { return rows_; }
125  Size cols() const { return cols_; }
126 
128  Scalar const& operator[](Index k) const {
129  assert(k < SIZE_);
130  Index i = k / cols_;
131  Index j = k - i * cols_;
132  return (*this)(i, j);
133  }
134 
136  Scalar& operator[](Index k) {
137  assert(k < SIZE_);
138  Index i = k / cols_;
139  Index j = k - i * cols_;
140  return (*this)(i, j);
141  }
142 
144  Scalar const& operator()(Index i, Index j) const {
145  return A_(i_ + i, j_ + j);
146  }
147 
149  Scalar& operator()(Index i, Index j) {
150  return A_(i_ + i, j_ + j);
151  }
152 
153  template <class OtherExp>
155  LValueType::operator=(other);
156  return *this;
157  }
158 };
159 
160 template<int rows, int cols, typename Scalar, class Exp>
162  int j) {
163  return LView<Scalar, Exp, rows, cols>(static_cast<Exp&>(A), i, j);
164 }
165 
166 template<typename Scalar, class Exp>
168  return LView<Scalar, Exp, 1, Exp::COLS_>(static_cast<Exp&>(A), i, 0);
169 }
170 
171 template<int rows, typename Scalar, class Exp>
173  int i) {
174  return LView<Scalar, Exp, rows, Exp::COLS_>(static_cast<Exp&>(A), i, 0);
175 }
176 
177 template<typename Scalar, class Exp>
179  int j) {
180  return LView<Scalar, Exp, Exp::ROWS_, 1>(static_cast<Exp&>(A), 0, j);
181 }
182 
183 template<int cols, typename Scalar, class Exp>
185  int j) {
186  return LView<Scalar, Exp, Exp::ROWS_, cols>(static_cast<Exp&>(A), 0, j);
187 }
188 
189 } // namespace fiber
190 
191 #endif // FIBER_VIEW_H_
Scalar const & operator[](Index k) const
return the evaluated i'th element of a vector expression
Definition: view.h:128
Size cols() const
Definition: view.h:125
_RView< Scalar, Exp, rows, Exp::COLS_ > GetRows(_RValue< Scalar, Exp > const &A, int i)
Definition: view.h:82
_RView< Scalar, Exp, Exp::ROWS_, 1 > GetColumn(_RValue< Scalar, Exp > const &A, int j)
Definition: view.h:89
Index j_
Definition: view.h:108
LView< Scalar, Exp, Exp::ROWS_, 1 > Column(_LValue< Scalar, Exp > &A, int j)
Definition: view.h:178
LView(Exp &A, Index i, Index j)
Definition: view.h:120
expression template for subset of a matrix expression
Definition: view.h:33
expression template for rvalues
Definition: rvalue.h:33
Size size() const
Definition: view.h:123
ThisType & operator=(const _RValue< Scalar, OtherExp > &other)
Definition: view.h:154
Scalar operator[](Index k) const
Definition: view.h:56
Index j_
Definition: view.h:37
Size rows() const
Definition: view.h:53
unsigned int Size
Definition: fiber.h:32
Scalar & operator[](Index k)
return the evaluated i'th element of a vector expression
Definition: view.h:136
_RView< Scalar, Exp, rows, cols > View(_RValue< Scalar, Exp > const &A, Index i, Index j)
Definition: view.h:70
Index i_
Definition: view.h:107
Scalar & operator()(Index i, Index j)
return the evaluated (j,i)'th element of a matrix expression
Definition: view.h:149
_RView(Exp const &A, int i, int j)
Definition: view.h:46
Size cols() const
Definition: view.h:54
LView< Scalar, Exp, rows, cols > Block(_LValue< Scalar, Exp > &A, int i, int j)
Definition: view.h:161
Scalar operator()(Index i, Index j) const
Definition: view.h:63
Size size() const
Definition: view.h:52
Exp & A_
Definition: view.h:106
Index i_
Definition: view.h:36
int Index
Definition: fiber.h:31
_LValue< Scalar, Exp > & operator=(_RValue< Scalar, Exp2 > const &B)
Definition: lvalue.h:62
Size rows() const
Definition: view.h:124
_RView< Scalar, Exp, Exp::ROWS_, cols > GetColumns(_RValue< Scalar, Exp > const &A, int j)
Definition: view.h:95
_RView< Scalar, Exp, 1, Exp::COLS_ > GetRow(_RValue< Scalar, Exp > const &A, int i)
Definition: view.h:76
LView< Scalar, Exp, rows, Exp::COLS_ > Rows(_LValue< Scalar, Exp > &A, int i)
Definition: view.h:172
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
expression template for rvalues
Definition: lvalue.h:33
Exp const & A_
underlying matrix expression
Definition: view.h:35
LView< Scalar, Exp, Exp::ROWS_, cols > Columns(_LValue< Scalar, Exp > &A, int j)
Definition: view.h:184
LView< Scalar, Exp, 1, Exp::COLS_ > Row(_LValue< Scalar, Exp > &A, int i)
Definition: view.h:167
_LValue< Scalar, ThisType > LValueType
Definition: view.h:112
expression template for subset of a matrix expression
Definition: view.h:103
Scalar const & operator()(Index i, Index j) const
return the evaluated (j,i)'th element of a matrix expression
Definition: view.h:144
LView< Scalar, Exp, rows_, cols_ > ThisType
Definition: view.h:111