STK++ 0.9.13
STK_ArrayBaseAssign.h File Reference

In this file we implement the copy and assign methods. More...

Include dependency graph for STK_ArrayBaseAssign.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  STK
 The namespace STK is the main domain space of the Statistical ToolKit project.
 

Macros

#define IS_VALID_ASSIGN(lhs, rhs)
 

Detailed Description

In this file we implement the copy and assign methods.

Definition in file STK_ArrayBaseAssign.h.

Macro Definition Documentation

◆ IS_VALID_ASSIGN

#define IS_VALID_ASSIGN (   lhs,
  rhs 
)
Value:
( ( ( lhs==Arrays::array2D_ || lhs==Arrays::square_) \
&& \
( rhs==Arrays::array2D_ || rhs==Arrays::square_ \
|| rhs==Arrays::diagonal_ || rhs==Arrays::number_ \
|| rhs==Arrays::lower_triangular_ || rhs==Arrays::upper_triangular_ \
|| rhs==Arrays::lower_symmetric_ || rhs==Arrays::upper_symmetric_ \
|| rhs==Arrays::symmetric_ \
) \
) \
|| \
( ( lhs==Arrays::array2D_) \
&& \
(rhs==Arrays::vector_ || rhs==Arrays::point_|| rhs==Arrays::number_) \
) \
|| \
( lhs==Arrays::lower_triangular_ && rhs==Arrays::lower_triangular_) \
|| \
( lhs==Arrays::upper_triangular_ && rhs==Arrays::upper_triangular_) \
|| \
( ( lhs==Arrays::diagonal_ || lhs==Arrays::vector_ || lhs==Arrays::point_) \
&& \
(rhs==Arrays::diagonal_ || rhs==Arrays::vector_ || rhs==Arrays::point_) \
) \
|| \
( lhs==Arrays::number_ && rhs==Arrays::number_) \
)

Definition at line 42 of file STK_ArrayBaseAssign.h.

70{
71
72/* @brief assign rhs to this **/
73template<class Derived>
74template<class Rhs>
75inline Derived& ArrayBase<Derived>::assign(ExprBase<Rhs> const& rhs)
76{
77 enum
78 {
79 rhs_structure_ = hidden::Traits<Rhs>::structure_
80 , rhs_orient_ = hidden::Traits<Rhs>::orient_
81 , rhs_sizeRows_ = hidden::Traits<Rhs>::sizeRows_
82 , rhs_sizeCols_ = hidden::Traits<Rhs>::sizeCols_
83 , is_valid_ = IS_VALID_ASSIGN((Arrays::Structure)structure_, (Arrays::Structure)rhs_structure_)
84 };
85 STK_STATIC_ASSERT(is_valid_,YOU_TRIED_TO_ASSIGN_A_NOT_COMPATIBLE_ARRAY);
86 // check if assignment is possible
87 if (structure_ == int(Arrays::square_) && rhs.cols() != rhs.rows() )
88 { STKRUNTIME_ERROR_2ARG(ArrayBase::assign,Arrays::structureToString((Arrays::Structure)structure_),Arrays::structureToString((Arrays::Structure)rhs_structure_),is not permited);}
89 // choose the correct way to resize this if necessary
90 hidden::resizeSelector<Derived, Rhs, rhs_structure_>::run(this->asDerived(), rhs.asDerived());
91 // choose the correct way to copy
92 hidden::CopycatSelector<Derived, Rhs, rhs_orient_>::run(this->asDerived(), rhs.asDerived());
93 return this->asDerived();
94}
95
96/* @return the matrix or vector obtained by setting this constant*/
97template<class Derived>
98inline Derived& ArrayBase<Derived>::operator=( Type const& value) { return setValue(value);}
99/* @return the matrix or vector obtained by evaluating this expression */
100template<class Derived>
101inline Derived& ArrayBase<Derived>::operator=( Derived const& rhs) { return assign(rhs);}
102/* @return the matrix or vector obtained by evaluating this expression */
103template<class Derived>
104template<typename Rhs>
105inline Derived& ArrayBase<Derived>::operator=( ExprBase<Rhs> const& rhs)
106{ return assign(rhs.asDerived());}
107
108//------------------------------------------------
109// Rhs array
110template<class Derived>
111template<typename Rhs>
112inline Derived& ArrayBase<Derived>::operator+=( ExprBase<Rhs> const& rhs)
113{
114 enum { orient_ = hidden::Traits<Derived>::orient_
115 , RStructure_ = hidden::Traits<Rhs>::structure_
116 };
117 typedef typename hidden::OperatorSelector<Derived, Rhs, Arrays::sumOp_>::Result Res;
118 hidden::CopycatSelector<Derived, Res, orient_>::run(this->asDerived(), this->asDerived() + rhs.asDerived());
119 return this->asDerived();
120}
121template<class Derived>
122template<typename Rhs>
123inline Derived& ArrayBase<Derived>::operator-=( ExprBase<Rhs> const& rhs)
124{
125 enum { orient_ = hidden::Traits<Derived>::orient_
126 , RStructure_ = hidden::Traits<Rhs>::structure_
127 };
128 typedef typename hidden::OperatorSelector<Derived, Rhs, Arrays::differenceOp_>::Result Res;
129 hidden::CopycatSelector<Derived, Res, orient_>::run(this->asDerived(), this->asDerived() - rhs.asDerived());
130 return this->asDerived();
131}
132
133template<class Derived>
134template<typename Rhs>
135inline Derived& ArrayBase<Derived>::operator/=( ExprBase<Rhs> const& rhs)
136{
137 enum { orient_ = hidden::Traits<Derived>::orient_
138 , RStructure_ = hidden::Traits<Rhs>::structure_
139 };
140 typedef typename hidden::OperatorSelector<Derived, Rhs, Arrays::divisionOp_>::Result Res;
141 hidden::CopycatSelector<Derived, Res, orient_>::run(this->asDerived(), this->asDerived() / rhs.asDerived());
142 return this->asDerived();
143}
144// TODO: implement direct call to CopyCat
145template<class Derived>
146template<typename Rhs>
147inline Derived& ArrayBase<Derived>::operator*=( ExprBase<Rhs> const& rhs)
148{
149 enum { orient_ = hidden::Traits<Derived>::orient_
150 , RStructure_ = hidden::Traits<Rhs>::structure_
151 };
152 //typedef typename ProductProductType<Derived, Rhs>::ProductType Res;
153// typedef BinaryOperator< ProductOp<Type, typename hidden::Traits<Rhs>::Type>, Derived, Rhs> Res;
154// hidden::CopycatSelector<Derived, Res, orient_>::run(this->asDerived(), this->asDerived() * rhs.asDerived());
155 this->asDerived() = this->asDerived() * rhs.asDerived();
156 return this->asDerived();
157}
158
159template<class Derived>
160template<typename Rhs>
161inline Derived& ArrayBase<Derived>::operator%=( ExprBase<Rhs> const& rhs)
162{
163 enum { orient_ = hidden::Traits<Derived>::orient_
164 , RStructure_ = hidden::Traits<Rhs>::structure_
165 };
166 typedef typename hidden::OperatorSelector<Derived, Rhs, Arrays::moduloOp_>::Result Res;
167 hidden::CopycatSelector<Derived, Res, orient_>::run(this->asDerived(), this->asDerived() % rhs.asDerived());
168 return this->asDerived();
169}
170
171//------------------------------------------------
172// rhs value
173template<class Derived>
174inline Derived& ArrayBase<Derived>::operator+=( Type const& value)
175{
176 enum { orient_ = hidden::Traits<Derived>::orient_};
177 typedef UnaryOperator<SumWithOp<Type>, Derived> Rhs;
178 hidden::CopycatSelector<Derived, Rhs, orient_>::run(this->asDerived(), this->asDerived() + value);
179 return this->asDerived();
180}
181template<class Derived>
182inline Derived& ArrayBase<Derived>::operator-=( Type const& value)
183{
184 enum { orient_ = hidden::Traits<Derived>::orient_};
185 typedef UnaryOperator<SumWithOp<Type>, Derived> Rhs;
186 hidden::CopycatSelector<Derived, Rhs, orient_>::run(this->asDerived(), this->asDerived() + (-value));
187 return this->asDerived();
188}
189template<class Derived>
190inline Derived& ArrayBase<Derived>::operator*=( Type const& value)
191{
192 enum { orient_ = hidden::Traits<Derived>::orient_};
193 typedef UnaryOperator<ProductWithOp<Type>, Derived> Rhs;
194 hidden::CopycatSelector<Derived, Rhs, orient_>::run(this->asDerived(), this->asDerived() * value);
195 return this->asDerived();
196}
197template<class Derived>
198inline Derived& ArrayBase<Derived>::operator/=( Type const& value)
199{
200 enum { orient_ = hidden::Traits<Derived>::orient_};
201 typedef UnaryOperator<DivisionWithOp<Type>, Derived> Rhs;
202 hidden::CopycatSelector<Derived, Rhs, orient_>::run(this->asDerived(), this->asDerived() / value);
203 return this->asDerived();
204}
205template<class Derived>
206inline Derived& ArrayBase<Derived>::operator%=( Type const& value)
207{
208 enum { orient_ = hidden::Traits<Derived>::orient_};
209 typedef UnaryOperator<ModuloWithOp<Type>, Derived> Rhs;
210 hidden::CopycatSelector<Derived, Rhs, orient_>::run(this->asDerived(), this->asDerived() % value);
211 return this->asDerived();
212}
213
214/* overwrite @c this with @c rhs.
215 * @note this method does not take care of the possibility of overlapping
216 * @param rhs the array to copy
217 **/
218template<class Derived>
219template<class Rhs>
220inline Derived& ArrayBase<Derived>::copy( ExprBase<Rhs> const& rhs)
221{
223 // check
224 if (this->sizeRows() != rhs.sizeRows())
225 { STKRUNTIME_ERROR_2ARG(ArrayBase<Derived>::copy,this->sizeRows(), rhs.sizeRows(),sizeRows are not the sames);}
226 if (this->sizeCols() != rhs.sizeCols())
227 { STKRUNTIME_ERROR_2ARG(ArrayBase<Derived>::copy,this->sizeCols(), rhs.sizeCols(),sizeCols are not the sames);}
228 // copy. TODO: Use iterators
229 //this->asDerived().reserve(this->sizeRows(), this->sizeCols());
230 for ( int jRhs=rhs.beginCols(), jLhs=this->beginCols(); jRhs<rhs.endCols(); jLhs++, jRhs++)
231 for ( int iRhs=rhs.beginRows(), iLhs=this->beginRows(); iRhs<rhs.endRows(); iLhs++, iRhs++)
232 {
233 setValue(iLhs, jLhs, rhs.elt(iRhs, jRhs));
234 }
235 // return this
236 return this->asDerived();
237}
238
239} // namespace STK
240
241#undef IS_VALID_ASSIGN
242
243#endif /* STK_ARRAYBASEASSIGN_H */
#define IS_VALID_ASSIGN(lhs, rhs)
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
#define STK_STATIC_ASSERT(COND, MSG)
#define STK_STATIC_ASSERT_DENSE_ONLY(EXPR)