STK++ 0.9.13
STK_SlicingAccessors.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Arrays
27 * created on: 21 nov. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_SLICINGACCESSORS_H
37#define STK_SLICINGACCESSORS_H
38
39namespace STK
40{
41template< typename Array> class RowAccessor;
42template< typename Array> class ColAccessor;
43// only for vectors/points/diagonal expressions
44template< typename Array, int Size_> class SubVectorAccessor;
45// only for array expressions
46template< typename Array, int SizeRows_, int SizeCols_> class SubAccessor;
47
48namespace hidden
49{
50
54template<typename Lhs>
56{
57 enum
58 {
59 structure_ = ( Lhs::structure_ != int(Arrays::vector_) && Lhs::structure_ != int(Arrays::number_) )
60 ? Arrays::point_ : Arrays::number_,
61 orient_ = Lhs::orient_,
62 sizeRows_ = 1,
63 sizeCols_ = Lhs::sizeCols_,
64 storage_ = Lhs::storage_
65 };
66 typedef typename Lhs::Type Type;
67 typedef typename Lhs::TypeConst TypeConst;
68};
69
70} // namespace hidden
71
86template< typename Lhs>
87class RowAccessor: public ExprBase< RowAccessor< Lhs> >, public TRef<1>
88{
89 public:
91 typedef typename hidden::Traits< RowAccessor< Lhs> >::Type Type;
92 typedef typename hidden::Traits< RowAccessor< Lhs> >::TypeConst TypeConst;
93 enum
94 {
95 structure_ = hidden::Traits< RowAccessor<Lhs> >::structure_,
97 sizeRows_ = hidden::Traits< RowAccessor<Lhs> >::sizeRows_,
98 sizeCols_ = hidden::Traits< RowAccessor<Lhs> >::sizeCols_,
99 storage_ = hidden::Traits< RowAccessor<Lhs> >::storage_
100 };
104 typedef TRange<sizeCols_> ColRange; // will not be used
105
107 inline RowAccessor( Lhs& lhs, int i): Base(), lhs_(lhs), i_(i), rows_(i_, 1), cols_(lhs_.rangeColsInRow(i_)) {}
108// /** Copy constructor */
109// inline RowAccessor( RowAccessor& row, bool ref = true)
110// : Base(), lhs_(row.lhs_), i_(row.i_), rows_(row.rows_), cols_(row.cols_) {}
112 inline RowRange const& rowsImpl() const { return rows_;}
114 inline ColRange const& colsImpl() const { return cols_;}
116 inline Lhs const& lhs() const { return lhs_;}
120 inline TypeConst elt2Impl(int i, int j) const
121 {
122#ifdef STK_BOUNDS_CHECK
123 if (i != i_) { STKRUNTIME_ERROR_2ARG(RowOperatorBase::elt2Impl,i,j,row index is not valid);}
124#endif
125 return (lhs().elt(i_, j));
126 }
130 inline TypeConst elt1Impl(int j) const { return (lhs().elt(i_, j));}
132 inline TypeConst elt0Impl() const { return (lhs().elt());}
133
137 inline Type& elt2Impl(int i, int j)
138 {
139#ifdef STK_BOUNDS_CHECK
140 if (i != i_) { STKRUNTIME_ERROR_2ARG(RowOperatorBase::elt2Impl,i,j,row index is not valid);}
141#endif
142 return (lhs().elt(i_, j));
143 }
147 inline Type& elt1Impl(int j)
148 { return (lhs().elt(i_, j));}
150 inline Type& elt0Impl() { return (lhs().elt());}
151
152 protected:
154 int i_;
157};
158
159
160namespace hidden
161{
165template<typename Lhs>
167{
168 enum
169 {
170 structure_ = ( Lhs::structure_ != int(Arrays::point_) && Lhs::structure_ != int(Arrays::number_) )
171 ? Arrays::vector_ : Arrays::number_,
172 orient_ = Lhs::orient_,
173 sizeRows_ = Lhs::sizeRows_,
174 sizeCols_ = 1,
175 storage_ = Lhs::storage_
176 };
177 typedef typename Lhs::Type Type;
178 typedef typename Lhs::TypeConst TypeConst;
179};
180
181} // namespace hidden
182
183
198template< typename Lhs>
199class ColAccessor: public ExprBase< ColAccessor< Lhs> >, public TRef<1>
200{
201 public:
203 typedef typename hidden::Traits< ColAccessor<Lhs> >::Type Type;
204 typedef typename hidden::Traits< ColAccessor<Lhs> >::TypeConst TypeConst;
205 enum
206 {
207 structure_ = hidden::Traits< ColAccessor<Lhs> >::structure_,
209 sizeRows_ = hidden::Traits< ColAccessor<Lhs> >::sizeRows_,
210 sizeCols_ = hidden::Traits< ColAccessor<Lhs> >::sizeCols_,
211 storage_ = hidden::Traits< ColAccessor<Lhs> >::storage_
212 };
217
219 inline ColAccessor( Lhs& lhs, int j)
220 : Base(), lhs_(lhs), j_(j), rows_(lhs_.rangeRowsInCol(j_)), cols_(j_,1) {}
221// /** Copy constructor */
222// inline ColAccessor( ColAccessor& col, bool ref = true)
223// : Base(), lhs_(col.lhs_), j_(col.j_), rows_(col.rows_), cols_(col.cols_) {}
225 inline RowRange const& rowsImpl() const { return rows_;}
227 inline ColRange const& colsImpl() const { return cols_;}
228
230 inline Lhs const& lhs() const { return lhs_; }
234 inline TypeConst elt2Impl(int i, int j) const
235 {
236#ifdef STK_BOUNDS_CHECK
237 if (j != j_) { STKRUNTIME_ERROR_2ARG(ColOperatorBase::elt2Impl,i,j,column index is not valid);}
238#endif
239 return (lhs_.elt(i, j_));
240 }
244 inline TypeConst elt1Impl(int i) const { return (lhs_.elt(i, j_));}
246 inline TypeConst elt0Impl() const { return (lhs_.elt(j_));}
247
251 inline Type& elt2Impl(int i, int j)
252 {
253#ifdef STK_BOUNDS_CHECK
254 if (j != j_) { STKRUNTIME_ERROR_2ARG(ColOperatorBase::elt2Impl,i,j,column index is not valid);}
255#endif
256 return (lhs_.elt(i, j_));
257 }
261 inline Type& elt1Impl(int i) { return (lhs_.elt(i, j_));}
263 inline Type& elt0Impl() { return (lhs_.elt(j_));}
264
265 protected:
267 const int j_;
270};
271
272
273namespace hidden
274{
278template<typename Lhs, int Size_>
280{
281 enum
282 {
283 structure_ = Lhs::structure_,
284 orient_ = Lhs::orient_,
285 sizeRows_ = (structure_ == int(Arrays::point_)) ? 1 : Size_,
286 sizeCols_ = (structure_ == int(Arrays::vector_)) ? 1 : Size_,
287 storage_ = Lhs::storage_
288 };
291 typedef typename Lhs::Type Type;
292 typedef typename Lhs::TypeConst TypeConst;
293};
294
295} // namespace hidden
296
297// forward declaration
298template< typename Lhs, int Size_, int Structure_>
300
316template< typename Lhs, int Size_>
318 , hidden::Traits< SubVectorAccessor<Lhs, Size_> >::structure_
319 >
320 , public TRef<1>
321{
322 public:
325 enum
326 {
332 };
334
339
341 inline SubVectorAccessor( Lhs& lhs, TRange<Size_> const& I): Base(I), lhs_(lhs) {}
343 inline Lhs const& lhs() const { return lhs_;}
347 inline TypeConst elt2Impl(int i, int j) const { return (lhs_.elt2Impl(i, j));}
351 inline TypeConst elt1Impl(int i) const { return (lhs_.elt1Impl(i));}
353 inline TypeConst elt0Impl() const { return (lhs_.elt());}
354
358 inline Type& elt2Impl(int i, int j) { return (lhs_.elt2Impl(i, j));}
362 inline Type& elt1Impl(int i) { return (lhs_.elt1Impl(i));}
364 inline Type& elt0Impl() { return (lhs_.elt());}
365
366 protected:
368};
369
370
375template< typename Lhs, int Size_>
376class SubVectorAccessorBase<Lhs, Size_, Arrays::point_>: public ExprBase< SubVectorAccessor<Lhs, Size_> >
377{
378 public:
381 enum
382 {
385 };
391 inline SubVectorAccessorBase( ColRange const& J): Base(), cols_(J) {}
393 inline RowRange const& rowsImpl() const { return this->asDerived().lhs().rows();}
395 inline ColRange const& colsImpl() const { return cols_;}
396
397 protected:
399};
400
405template< typename Lhs, int Size_>
406class SubVectorAccessorBase<Lhs, Size_, Arrays::vector_ >: public ExprBase< SubVectorAccessor<Lhs, Size_> >
407{
408 public:
411 enum
412 {
415 };
421 inline SubVectorAccessorBase( RowRange const& I): Base(), rows_(I) {}
423 inline RowRange const& rowsImpl() const { return rows_;}
425 inline ColRange const& colsImpl() const { return this->asDerived().lhs().cols();}
426
427 protected:
429};
430
435template< typename Lhs, int Size_>
436class SubVectorAccessorBase<Lhs, Size_, Arrays::diagonal_ >: public ExprBase< SubVectorAccessor<Lhs, Size_> >
437{
438 public:
441 enum
442 {
445 };
451 inline SubVectorAccessorBase( RowRange const& I): Base(), range_(I) {}
453 inline RowRange const& rowsImpl() const { return range_;}
455 inline ColRange const& colsImpl() const { return range_;}
456
457 protected:
459};
460
461
462
463namespace hidden
464{
468template<typename Lhs, int SizeRows_, int SizeCols_>
470{
471 enum
472 {
473 structure_ = Lhs::structure_,
474 orient_ = Lhs::orient_,
475 sizeRows_ = SizeRows_,
476 sizeCols_ = SizeCols_,
477 storage_ = Lhs::storage_
478 };
479 typedef typename Lhs::Type Type;
480 typedef typename Lhs::TypeConst TypeConst;
481};
482
483} // namespace hidden
484
485
500template< typename Lhs, int SizeRows_, int SizeCols_>
501class SubAccessor: public ExprBase< SubAccessor<Lhs, SizeRows_, SizeCols_> >
502 , public TRef<1>
503{
504 public:
507 enum
508 {
514 };
516
521
523 inline SubAccessor( Lhs& lhs, RowRange const& I, ColRange const& J)
524 : Base(), lhs_(lhs), rows_(I), cols_(J) {}
526 inline RowRange const& rowsImpl() const { return rows_;}
528 inline ColRange const& colsImpl() const { return cols_;}
530 inline Lhs const& lhs() const { return lhs_;}
531
535 inline TypeConst elt2Impl(int i, int j) const { return (lhs_.elt2Impl(i, j));}
539 inline TypeConst elt1Impl(int i) const { return (lhs_.elt1Impl(i));}
541 inline TypeConst elt0Impl() const { return (lhs_.elt());}
542
546 inline Type& elt2Impl(int i, int j) { return (lhs_.elt2Impl(i, j));}
550 inline Type& elt1Impl(int i) { return (lhs_.elt1Impl(i));}
552 inline Type& elt0Impl() { return (lhs_.elt());}
553
554 protected:
558};
559
560} // namespace STK
561
562#endif /* STK_SLICINGACCESSORS_H */
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
Generic expression when the column of an expression is accessed.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
Type & elt2Impl(int i, int j)
hidden::Traits< ColAccessor< Lhs > >::TypeConst TypeConst
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
RowRange const & rowsImpl() const
ExprBase< ColAccessor< Lhs > > Base
ColRange const & colsImpl() const
TypeConst elt1Impl(int i) const
TypeConst elt0Impl() const
access to the element
TypeConst elt2Impl(int i, int j) const
ColAccessor(Lhs &lhs, int j)
constructor
Lhs const & lhs() const
hidden::Traits< ColAccessor< Lhs > >::Type Type
Type & elt0Impl()
access to the element
base class for template evaluation expressions and visitors.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Generic expression when the row of an expression is accessed.
hidden::Traits< RowAccessor< Lhs > >::Type Type
TypeConst elt1Impl(int j) const
Lhs const & lhs() const
RowRange const & rowsImpl() const
ColRange const & colsImpl() const
Type & elt0Impl()
accesses to the element
ExprBase< RowAccessor< Lhs > > Base
TypeConst elt0Impl() const
accesses to the element
hidden::Traits< RowAccessor< Lhs > >::TypeConst TypeConst
Type & elt2Impl(int i, int j)
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
RowAccessor(Lhs &lhs, int i)
constructor
TypeConst elt2Impl(int i, int j) const
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
Generic expression when the sub-part of an expression is accessed.
RowRange const & rowsImpl() const
ExprBase< SubAccessor< Lhs, SizeRows_, SizeCols_ > > Base
TypeConst elt0Impl() const
accesses to the element
hidden::Traits< SubAccessor< Lhs, SizeRows_, SizeCols_ > >::Type Type
ColRange const & colsImpl() const
TypeConst elt2Impl(int i, int j) const
Type & elt2Impl(int i, int j)
Lhs const & lhs() const
TypeConst elt1Impl(int i) const
Type & elt0Impl()
accesses to the element
SubAccessor(Lhs &lhs, RowRange const &I, ColRange const &J)
constructor
hidden::Traits< SubAccessor< Lhs, SizeRows_, SizeCols_ > >::TypeConst TypeConst
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
Type & elt2Impl(int i, int j)
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TypeConst elt1Impl(int i) const
Type & elt0Impl()
accesses to the element
TypeConst elt2Impl(int i, int j) const
SubVectorAccessor(Lhs &lhs, TRange< Size_ > const &I)
constructor
hidden::Traits< SubVectorAccessor< Lhs, Size_ > >::Type Type
hidden::Traits< SubVectorAccessor< Lhs, Size_ > >::TypeConst TypeConst
TypeConst elt0Impl() const
accesses to the element
SubVectorOperatorBase< Lhs, Size_, structure_ > Base
@ number_
(1,1) matrix/vector/array/expression (like a number)
@ point_
row oriented vector/array/expression
@ vector_
column oriented vector/array/expression
The namespace STK is the main domain space of the Statistical ToolKit project.
Base class for all referencing containers.
ColOperator< SubVectorOperator< Lhs, Size_ > > Col
RowOperator< SubVectorOperator< Lhs, Size_ > > Row