STK++ 0.9.13
STK_VisitorsImpl.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: 27 sept. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_VISITORSIMPL_H
37#define STK_VISITORSIMPL_H
38
39#define Idx(size) baseIdx + size - 1
40
41namespace STK
42{
43
44namespace hidden
45{
46template<typename Visitor, typename Derived, bool Orient_, int SizeRows_, int SizeCols_>
48template<typename Visitor, typename Derived, bool Orient_, int SizeRows_, int SizeCols_>
50template<typename Visitor, typename Derived, int SizeRows_, int SizeCols_>
52
53template<typename Visitor, typename Derived, int SizeRows_, int SizeCols_>
54struct VisitorUnrollCol;
55template<typename Visitor, typename Derived, int SizeRows_, int SizeCols_>
56struct VisitorUnrollRow;
57
58// other kind of structure
59template<typename Visitor, typename Derived, int SizeRows_>
61template<typename Visitor, typename Derived, int SizeRows_>
62struct VisitorPointImpl;
63template<typename Visitor, typename Derived, int SizeRows_>
65template<typename Visitor, typename Derived, int orient_>
67template<typename Visitor, typename Derived, int orient_>
69
70
74template<typename Visitor, typename Derived>
75struct VisitorArrayNoUnrollImpl<Visitor, Derived, Arrays::by_col_, UnknownSize, UnknownSize>
76{
77 static void run( Derived const& tab, Visitor& visitor)
78 {
79 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
80 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
81 visitor(tab.elt(i, j), i, j);
82 }
83 static void apply( Derived& tab, Visitor& applier)
84 {
85 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
86 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
87 tab.setValue(i, j, applier());
88 }
89};
90
94template<typename Visitor, typename Derived>
95struct VisitorArrayNoUnrollImpl<Visitor, Derived, Arrays::by_row_, UnknownSize, UnknownSize>
96{
97 static void run( Derived const& tab, Visitor& visitor)
98 {
99 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
100 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
101 visitor(tab.elt(i, j), i, j);
102 }
103 static void apply( Derived& tab, Visitor& applier)
104 {
105 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
106 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
107 tab.setValue(i, j, applier());
108 }
109};
110
114template<typename Visitor, typename Derived, int SizeRows_ , int SizeCols_>
130
134template<typename Visitor, typename Derived, int SizeRows_ , int SizeCols_>
150
154template<typename Visitor, typename Derived, int SizeRows_>
155struct VisitorArrayUnrollImpl<Visitor, Derived, Arrays::by_col_, SizeRows_, 1>
156{
161};
162
165template<typename Visitor, typename Derived, int SizeCols_>
166struct VisitorArrayUnrollImpl<Visitor, Derived, Arrays::by_row_, 1, SizeCols_>
167{
172};
173
178template<typename Visitor, typename Derived>
179struct VisitorArrayUnrollImpl<Visitor, Derived, Arrays::by_col_, 1, 1>
180{
181 inline static void run( Derived const& tab, Visitor& visitor)
182 { visitor(tab.elt(tab.beginRows(), tab.beginCols()), tab.beginRows(), tab.beginCols());}
183 inline static void apply( Derived& tab, Visitor& applier)
184 { tab.setValue(tab.beginRows(), tab.beginCols(), applier());}
185};
186
191template<typename Visitor, typename Derived>
192struct VisitorArrayUnrollImpl<Visitor, Derived, Arrays::by_row_, 1, 1>
193{
194 inline static void run( Derived const& tab, Visitor& visitor)
195 { visitor(tab.elt(tab.beginRows(), tab.beginCols()), tab.beginRows(), tab.beginCols());}
196 inline static void apply( Derived& tab, Visitor& applier)
197 { tab.setValue(tab.beginRows(), tab.beginCols(), applier());}
198};
199
205template<typename Visitor, typename Derived, int SizeRows_>
207{
208 static void run( Derived const& tab, Visitor& visitor)
209 {
211 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
212 visitor(tab.elt(Idx(SizeRows_), j), Idx(SizeRows_), j);
213 }
214 static void apply( Derived& tab, Visitor& applier)
215 {
217 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
218 tab.setValue(Idx(SizeRows_), j, applier());
219 }
220};
221
227template<typename Visitor, typename Derived>
229{
230 static void run( Derived const& tab, Visitor& visitor)
231 {
232 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
233 visitor(tab.elt(tab.beginRows(), j), tab.beginRows(), j);
234 }
235 static void apply( Derived& tab, Visitor& applier)
236 {
237 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
238 tab.setValue(tab.beginRows(), j, applier());
239 }
240};
241
246template<typename Visitor, typename Derived, int SizeCols_>
248{
249 static void run( Derived const& tab, Visitor& visitor)
250 {
252 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
253 visitor(tab.elt(i, Idx(SizeCols_)), i, Idx(SizeCols_));
254 }
255 static void apply( Derived& tab, Visitor& applier)
256 {
258 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
259 tab.setValue(i, Idx(SizeCols_), applier());
260 }
261};
262
267template<typename Visitor, typename Derived>
269{
270 static void run( Derived const& tab, Visitor& visitor)
271 {
272 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
273 visitor(tab.elt(i, tab.beginCols()), i, tab.beginCols());
274 }
275 static void apply( Derived& tab, Visitor& applier)
276 {
277 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
278 tab.setValue(i, tab.beginCols(), applier());
279 }
280};
281
284template<typename Visitor, typename Derived, int SizeRows_, int SizeCols_>
298
301template<typename Visitor, typename Derived, int SizeCols_>
303{
304 inline static void run( Derived const& tab, Visitor& visitor)
306 inline static void apply( Derived& tab, Visitor& applier)
307 { tab.setValue(baseIdx, Idx(SizeCols_), applier());}
308};
309
312template<typename Visitor, typename Derived, int SizeRows_, int SizeCols_>
326
329template<typename Visitor, typename Derived, int SizeRows_>
331{
332 inline static void run( Derived const& tab, Visitor& visitor)
333 { visitor(tab.elt(Idx(SizeRows_), tab.beginCols()), Idx(SizeRows_), tab.beginCols());}
334 inline static void apply( Derived& tab, Visitor& applier)
335 { tab.setValue(Idx(SizeRows_), tab.beginCols(), applier());}
336};
337
340template<typename Visitor, typename Derived>
342{
343 static void run( Derived const& tab, Visitor& visitor)
344 { for(int i = tab.begin(); i < tab.end(); ++i) visitor(tab.elt(i), i, tab.colIdx());}
345 static void apply( Derived& tab, Visitor& applier)
346 { for(int i = tab.begin(); i < tab.end(); ++i) tab.setValue(i, applier());
347 }
348};
349
353template<typename Visitor, typename Derived, int Size_>
355{
356 inline static void run( Derived const& tab, Visitor& visitor)
357 {
359 visitor(tab.elt(tab.begin()+ Size_-1), tab.begin()+ Size_-1, tab.colIdx());
360 }
361 inline static void apply( Derived& tab, Visitor& applier)
362 {
364 tab.setValue(tab.begin()+ Size_-1, applier());
365 }
366};
367
370template<typename Visitor, typename Derived>
371struct VisitorVectorImpl<Visitor, Derived, 1>
372{
373 inline static void run( Derived const& tab, Visitor& visitor)
374 { visitor(tab.elt(tab.begin()), tab.begin(), tab.colIdx());}
375 inline static void apply( Derived& tab, Visitor& applier)
376 { tab.setValue(tab.begin(), applier());}
377};
378
381template<typename Visitor, typename Derived>
383{
384 static void run( Derived const& tab, Visitor& visitor)
385 { for(int j = tab.begin(); j < tab.end(); ++j) visitor(tab.elt(j), tab.rowIdx(),j);}
386 static void apply( Derived& tab, Visitor& applier)
387 { for(int j = tab.begin(); j < tab.end(); ++j) tab.setValue(j, applier());}
388};
389
390
394template<typename Visitor, typename Derived, int Size_>
396{
397 inline static void run( Derived const& tab, Visitor& visitor)
398 {
400 visitor(tab.elt(tab.begin()+ Size_-1), tab.rowIdx(), tab.begin()+ Size_-1);
401 }
402 inline static void apply( Derived& tab, Visitor& applier)
403 {
405 tab.setValue(tab.begin()+ Size_-1, applier());
406 }
407};
408
411template<typename Visitor, typename Derived>
412struct VisitorPointImpl<Visitor, Derived, 1>
413{
414 inline static void run( Derived const& tab, Visitor& visitor)
415 { visitor(tab.elt(tab.begin()), tab.rowIdx(), tab.begin());}
416 inline static void apply( Derived& tab, Visitor& applier)
417 { tab.setValue(tab.begin(), applier());}
418};
419
422template<typename Visitor, typename Derived>
424{
425 static void run( Derived const& tab, Visitor& visitor)
426 { for(int j = tab.begin(); j < tab.end(); ++j) visitor(tab.elt(j), j,j);}
427 static void apply( Derived& tab, Visitor& applier)
428 { for(int j = tab.begin(); j < tab.end(); ++j) tab.setValue(j, applier());
429 }
430};
431
435template<typename Visitor, typename Derived, int Size_>
437{
438 inline static void run( Derived const& tab, Visitor& visitor)
439 {
441 visitor(tab.elt(tab.begin()+ Size_-1), tab.begin()+ Size_-1, tab.begin()+ Size_-1);
442 }
443 inline static void apply( Derived& tab, Visitor& applier)
444 {
446 tab.setValue(tab.begin()+ Size_-1, applier());
447 }
448};
449
452template<typename Visitor, typename Derived>
453struct VisitorDiagonalImpl<Visitor, Derived, 1>
454{
455 inline static void run( Derived const& tab, Visitor& visitor)
456 { visitor(tab.elt(tab.begin()), tab.begin(), tab.begin());}
457 inline static void apply( Derived& tab, Visitor& applier)
458 { tab.setValue(tab.begin(), applier());}
459};
460
463template<typename Visitor, typename Derived>
464struct VisitorUpperImpl<Visitor, Derived, Arrays::by_col_>
465{
466 static void run( Derived const& tab, Visitor& visitor)
467 {
468 for(int j = tab.lastIdxCols(); j >= tab.beginCols(); --j)
469 for(int i = std::min(j, tab.lastIdxRows()); i >= tab.beginRows(); --i)
470 visitor(tab.elt(i, j), i, j);
471 }
472 static void apply( Derived& tab, Visitor& applier)
473 {
474 for(int j = tab.lastIdxCols(); j >= tab.beginCols(); --j)
475 for(int i = std::min(j, tab.lastIdxRows()); i >= tab.beginRows(); --i)
476 tab.setValue(i, j, applier());
477 }
478};
479
482template<typename Visitor, typename Derived>
483struct VisitorUpperImpl<Visitor, Derived, Arrays::by_row_>
484{
485 static void run( Derived const& tab, Visitor& visitor)
486 {
487 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
488 for(int j = std::max(i, tab.beginCols()); j < tab.endCols(); ++j)
489 visitor(tab.elt(i, j), i, j);
490 }
491 static void apply( Derived& tab, Visitor& applier)
492 {
493 for(int i = tab.beginRows(); i < tab.endRows(); ++i)
494 for(int j = std::max(i, tab.beginCols()); j < tab.endCols(); ++j)
495 tab.setValue(i, j, applier());
496 }
497};
498
499
502template<typename Visitor, typename Derived>
503struct VisitorLowerImpl<Visitor, Derived, Arrays::by_col_>
504{
505 static void run( Derived const& tab, Visitor& visitor)
506 {
507 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
508 for(int i = std::max(j, tab.beginRows()); i < tab.endRows(); ++i)
509 visitor(tab.elt(i, j), i, j);
510 }
511 static void apply( Derived& tab, Visitor& applier)
512 {
513 for(int j = tab.beginCols(); j < tab.endCols(); ++j)
514 for(int i = std::max(j, tab.beginRows()); i < tab.endRows(); ++i)
515 tab.setValue(i, j, applier());
516 }
517};
518
521template<typename Visitor, typename Derived>
522struct VisitorLowerImpl<Visitor, Derived, Arrays::by_row_>
523{
524 static void run( Derived const& tab, Visitor& visitor)
525 {
526 for(int i = tab.lastIdxRows(); i >= tab.beginRows(); --i)
527 for(int j = std::min(i, tab.lastIdxCols()); j >= tab.beginCols(); --j)
528 visitor(tab.elt(i, j), i, j);
529 }
530 static void apply( Derived& tab, Visitor& applier)
531 {
532 for(int i = tab.lastIdxRows(); i >= tab.beginRows(); --i)
533 for(int j = std::min(i, tab.lastIdxCols()); j >= tab.beginCols(); --j)
534 tab.setValue(i, j, applier());
535 }
536};
537
542template<typename Visitor, typename Derived>
544{
545 inline static void run( Derived const& tab, Visitor& visitor)
546 { visitor(tab.elt(), tab.beginRows(), tab.beginCols());}
547 inline static void apply( Derived& tab, Visitor& applier)
548 { tab.setValue(tab.beginRows(), tab.beginCols(), applier());}
549};
550
551} // namespace hidden
552
553} // namespace STK
554
555#undef Idx
556
557#endif /* STK_VISITORSIMPL_H */
#define Idx(size)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
const int baseIdx
base index of the containers created in STK++.
The namespace STK is the main domain space of the Statistical ToolKit project.
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
A VisitorDiagonalImpl allow to unroll the visit of a Diagonal tab if possible.
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
specialization for the general case with 1 row and 1 column arrays.
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
A VisitorPointImpl allow to unroll the visit of a row-vector if possible.
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
unroll the column baseIdx + SizeCols_ -1
static void run(Derived const &tab, Visitor &visitor)
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
unroll the row baseIdx + SizeRows_ -1
static void run(Derived const &tab, Visitor &visitor)
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void run(Derived const &tab, Visitor &visitor)
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)
A visitor Vector allow to unroll the visit of a vector if possible.
static void apply(Derived &tab, Visitor &applier)
static void run(Derived const &tab, Visitor &visitor)