STK++ 0.9.13
STK_ArrayBaseInitializer.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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: 28 nov. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_ARRAYBASEINITIALIZER_H
37#define STK_ARRAYBASEINITIALIZER_H
38
39namespace STK
40{
47template<typename Derived>
49{
50 public:
51 typedef typename Derived::Type Type;
53 ArrayInitializer( Derived& array, Type const& s)
54 : array_(array), row_(array.beginRows()), col_(array.beginCols())
55 { // check if there is space
56 if (array.empty())
58 toFirstElt();
59 array_.elt(row_, col_) = s;
60 }
63 {
64 toNextElt();
65 array_.elt(row_, col_) = s;
66 return *this;
67 }
70
71 protected:
77 {
78 // loop over the rows, until we get existing columns
79 while (array_.rangeColsInRow(row_).size() <= 0)
80 { row_++;
81 if (row_ >= array_.endRows())
83 }
84 col_ = array_.rangeColsInRow(row_).begin();
85 }
87 void toNextElt()
88 {
89 // for the current row go to first available column if necessary
90 if (col_ < array_.rangeColsInRow(row_).begin())
91 { col_ = array_.rangeColsInRow(row_).begin();}
92 else // otherwise just increment
93 { col_++;}
94 // check if we need to go to next row
95 if (col_ > array_.rangeColsInRow(row_).lastIdx())
96 {
97 row_++;
98 while ( (array_.rangeColsInRow(row_).size() <= 0) && (row_ <= array_.lastIdxRows()))
99 { row_++;}
100 if (row_ > array_.lastIdxRows())
102 col_ = array_.rangeColsInRow(row_).begin();
103 }
104 }
105 protected:
106 Derived& array_; // target
107 int row_; // current row
108 int col_; // current col
109};
110
111/* Convenient operator to set the coefficients of a matrix.
112 *
113 * The coefficients must be provided in a row major order and exactly match
114 * the size of the matrix. Otherwise an assertion is raised.
115 */
116template<typename Derived>
118{ return ArrayInitializer<Derived>(this->asDerived(), s);}
119
120// @sa operator<<(const Type&) */
121//template<typename Derived>
122//template<typename Rhs>
123//ArrayInitializer<Derived>
124//ArrayBase<Derived>::operator<<(const ArrayBase<Rhs>& other)
125//{ return ArrayInitializer<Derived>(this->asDerived(), other);}
126
127} // namespace STK
128
129#endif /* STK_ARRAYBASEINITIALIZER_H */
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
ArrayInitializer< Derived > operator<<(Type const &s)
Convenient operator to set the coefficients of a matrix.
Helper class used by the comma initializer operator.
ArrayInitializer & operator,(Type const &s)
inserts a scalar value in the target matrix
ArrayInitializer(Derived &array, Type const &s)
Constructor.
void toFirstElt()
Set (row_, col_) to the first element of the array.
void toNextElt()
Compute the next element.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The namespace STK is the main domain space of the Statistical ToolKit project.