STK++ 0.9.13
STK_ICArray.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::Array
27 * created on: 10 août 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
40#ifndef STK_ICARRAY_H
41#define STK_ICARRAY_H
42
43#include "STK_ArrayBase.h"
44#include "STK_IContainer2D.h"
45#include "STK_ITContainer2D.h"
48
49namespace STK
50{
51// forward declaration of all CArray classes
52template< typename Type, int SizeRows_, int SizeCols_, bool Orient_> class CArray;
53template< typename Type, int Size_, bool Orient_> class CArraySquare;
54template< typename Type, int SizeCols_, bool Orient_> class CArrayPoint;
55template< typename Type, int SizeRows_, bool Orient_> class CArrayVector;
56template< typename Type, bool Orient_> class CArrayNumber;
57
58namespace hidden
59{
66template<typename Derived, int SizeRows, int SizeCols>
94
98template<typename Derived, int Size>
108
109}
124template<class Derived>
125
126class ICArray: public ArrayBase<Derived>
127{
128 public:
131
136
137 enum
138 {
144 };
149
150 protected:
158 ICArray( int sizeRows, int sizeCols): Base(), allocator_(sizeRows, sizeCols) {}
163 ICArray( int sizeRows, int sizeCols, Type const& value): Base(), allocator_(sizeRows, sizeCols, value) {}
168 ICArray( Derived const& T, bool ref = false): Base(), allocator_(T.allocator_, ref) {}
173 ICArray( Type* const& q, int sizeRows, int sizeCols): Base(), allocator_(q, sizeRows, sizeCols){}
178 template<class OtherAllocator>
180 : Base(), allocator_(allocator.asDerived(), I, J)
181 {}
185 template< class OtherAllocator>
187 : Base(), allocator_(allocator.asDerived(), true)
188 {}
191 public:
193 inline ColRange const& colsImpl() const { return allocator_.cols();};
195 inline RowRange const& rowsImpl() const { return allocator_.rows();}
196
198 void clear() { allocator_.clear();}
199
201 bool empty() const { return allocator_.empty();}
203 bool isRef() const { return allocator_.isRef();}
204
206 inline Allocator const& allocator() const { return allocator_;}
208 inline Type* const& p_data() const { return allocator_.p_data();}
209
211 inline Type& elt2Impl( int i, int j) { return allocator_.elt(i, j);}
213 inline TypeConst elt2Impl( int i, int j) const { return allocator_.elt(i, j);}
214
216 inline Type& elt1Impl( int j) { return allocator_.elt(j);}
218 inline TypeConst elt1Impl( int j) const{ return allocator_.elt(j);}
219
221 inline Type& elt0Impl() { return allocator_.elt();}
223 inline TypeConst elt0Impl() const { return allocator_.elt();}
224
226 inline void setValueImpl( int j, TypeConst value) { allocator_.elt(j) = value ;}
228 inline void setValueImpl( int i, int j, TypeConst value) { allocator_.elt(i,j) = value ;}
229
230 // overloaded operators
234 inline Type& operator()(int i, int j)
235 {
236#ifdef STK_BOUNDS_CHECK
237 if (this->beginRows() > i) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, beginRows() > i);}
238 if (this->endRows() <= i) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, endRows() <= i);}
239 if (this->beginCols() > j) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, beginCols() > j);}
240 if (this->endCols() <= j) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, endCols() <= j);}
241#endif
242 return this->elt(i,j);
243 }
247 inline TypeConst operator()(int i, int j) const
248 {
249#ifdef STK_BOUNDS_CHECK
250 if (this->beginRows() > i) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, beginRows() > i);}
251 if (this->endRows() <= i) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, endRows() <= i);}
252 if (this->beginCols() > j) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, beginCols() > j);}
253 if (this->endCols() <= j) { STKOUT_OF_RANGE_2ARG(ICArray::operator(), i, j, endCols() <= j);}
254#endif
255 return this->elt(i,j);
256 }
257
261 inline Type& operator[](int i)
262 {
264#ifdef STK_BOUNDS_CHECK
265 if (this->asDerived().begin() > i) { STKOUT_OF_RANGE_1ARG(ICArray::operator[], i, begin() > i);}
266 if (this->asDerived().end() <= i) { STKOUT_OF_RANGE_1ARG(ICArray::operator[], i, end() <= i);}
267#endif
268 return this->elt(i);
269 }
273 inline TypeConst operator[](int i) const
274 {
276#ifdef STK_BOUNDS_CHECK
277 if (this->asDerived().begin() > i) { STKOUT_OF_RANGE_1ARG(ICArray::operator[], i, begin() > i);}
278 if (this->asDerived().end() <= i) { STKOUT_OF_RANGE_1ARG(ICArray::operator[], i, end() <= i);}
279#endif
280 return this->elt(i);
281 }
282
284 inline Type& operator()() { return this->elt();}
286 inline TypeConst operator()() const { return this->elt();}
287
288 // row operators
297 template<int Size_>
299 { return typename hidden::CSlice<Derived, 1, Size_>::Result( allocator_.row( i, J));}
303 template<int Size_>
305 { return row(i, J);}
309 template<int Size_>
312
313 // col operators
322 template<int Size_ >
324 { return typename hidden::CSlice<Derived, Size_, 1>::Result( allocator_.col( I, j));}
328 template<int Size_>
334 template<int Size_>
336 { return col(I, j);}
337
338 // sub operators
342 template<int Size>
351 template<int Size>
353 {
355 return sub(I);
356 }
361 template<int OtherRows_, int OtherCols_>
367 template<int OtherRows_, int OtherCols_>
370
372 void swap(int i, int j) { std::swap(this->elt(i), this->elt(j)); }
374 void swapCols(int pos1, int pos2)
375 {
376#ifdef STK_BOUNDS_CHECK
377 if (this->beginCols() > pos1)
379 if (this->endCols() <= pos1)
381 if (this->beginCols() > pos2)
383 if (this->endCols() <= pos2)
385#endif
386 // swap allocator
387 allocator_.swapCols(pos1, pos2);
388 }
390 void swapRows(int pos1, int pos2)
391 {
392#ifdef STK_BOUNDS_CHECK
393 if (this->beginRows() > pos1)
395 if (this->endRows() <= pos1)
397 if (this->beginRows() > pos2)
399 if (this->endRows() <= pos2)
401#endif
402 // swap allocator
403 allocator_.swapRows(pos1, pos2);
404 }
408 void exchange(Derived& T) { allocator_.exchange(T.allocator_);}
412 void move(Derived const& T) { allocator_.move(T.allocator_);}
416 Derived& shift(int beginRows, int beginCols)
417 {
421 ,YOU_CANNOT_USED_THIS_METHOD_WITH_THIS_KIND_OF_ARRAY);
422 if (!hidden::CheckShift<Derived, structure_>::shift(this->asDerived(), beginRows, beginCols)) return this->asDerived();
423 if (this->isRef())
424 { STKRUNTIME_ERROR_2ARG(ICArray::shift,beginRows,beginCols,cannot operate on reference);}
425 allocator_.shift(beginRows, beginCols);
426 return this->asDerived();
427 }
433 Derived& shift(int firstIdx)
434 {
435 if (!hidden::CheckShift<Derived, structure_>::shift(this->asDerived(), firstIdx)) return this->asDerived();
436 if (this->isRef())
438 allocator_.shift(firstIdx);
439 return this->asDerived();
440 }
444 Derived& resize(Range const& I, Range const& J)
445 {
447 ||(structure_ == (int)Arrays::square_)
448 ||(structure_ == (int)Arrays::diagonal_)
454 ,YOU_CANNOT_USED_THIS_METHOD_WITH_THIS_KIND_OF_ARRAY);
457 if (!hidden::CheckShift<Derived, structure_>::resize(this->asDerived(), I, J)) return this->asDerived();
458 if (this->isRef())
460 allocator_.resize(I.size(), J.size()).shift(I.begin(), J.begin());
461 return this->asDerived();
462 }
468 template<int Size_>
469 Derived& resize(TRange<Size_> const& I)
470 {
471 if (!hidden::CheckShift<Derived, structure_>::resize(this->asDerived(), I)) return this->asDerived();
472 if (this->isRef())
474 allocator_.resize(I.size()).shift(I.begin());
475 return this->asDerived();
476 }
482 Derived& resize(int size)
483 {
484 if (!hidden::CheckShift<Derived, structure_>::resize(this->asDerived(), size)) return this->asDerived();
485 if (this->isRef())
487 allocator_.resize(size);
488 return this->asDerived();
489 }
490};
491
492} // namespace STK
493
494#endif /* STK_ICARRAY_H */
In this file we define the base class for Arrays.
In this file we define the CAllocator class.
In this file we define the IContainer2D interface classes.
In this file we define the ITContainer2D interface class.
#define STKOUT_OF_RANGE_1ARG(Where, Arg, Error)
Definition STK_Macros.h:93
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
#define STKOUT_OF_RANGE_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:102
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
In this file we define the class MemAllocator.
#define STK_STATIC_ASSERT(COND, MSG)
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
base class for template arrays.
TypeConst operator()(int i, int j) const
hidden::CSlice< Derived, Size_, sizeCols_ >::Result row(TRange< Size_ > const &I) const
implement the row operator using a reference on a range of rows of the allocator
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
Derived & resize(Range const &I, Range const &J)
resize the Array.
bool empty() const
Derived & shift(int beginRows, int beginCols)
shift the Array.
Derived & resize(int size)
Resize the vector/point/diagonal/square array.
ICArray(Type *const &q, int sizeRows, int sizeCols)
wrapper constructor for 0 based C-Array.
TypeConst elt2Impl(int i, int j) const
implement the writable element accessor
hidden::CSlice< Derived, 1, sizeCols_ >::Result row(int i) const
implement the row operator using a reference on the row of the allocator
Allocator allocator_
allocator of the memory
hidden::Traits< Derived >::Allocator Allocator
ColRange const & colsImpl() const
void setValueImpl(int j, TypeConst value)
implement setValue for vector/point/diagonal arrays
ICArray(int sizeRows, int sizeCols, Type const &value)
constructor with specified sizes and value.
TypeConst operator[](int i) const
TypeConst elt1Impl(int j) const
implement the writable element accessor for vector/point/diagonal arrays
ICArray(Derived const &T, bool ref=false)
copy or wrapper constructor.
Derived & resize(TRange< Size_ > const &I)
Resize the vector/point/diagonal/square array.
ArrayBase< Derived > Base
hidden::CSlice< Derived, 1, Size_ >::Result row(int i, TRange< Size_ > const &J) const
implement the row operator using a reference on the row of the allocator
TypeConst operator()() const
void swapCols(int pos1, int pos2)
hidden::CSlice< Derived, sizeRows_, Size_ >::Result col(TRange< Size_ > const &J) const
implement the col operator using a reference on a range of columns of the allocator
Allocator const & allocator() const
Get a constant reference on the main allocator.
hidden::CSlice< Derived, Size_, 1 >::Result col(TRange< Size_ > const &I, int j) const
implement the col operator using a reference on the column of the allocator
void exchange(Derived &T)
exchange this with T.
hidden::Traits< Derived >::Type Type
Type & operator[](int i)
void move(Derived const &T)
move T to this.
hidden::CSlice< Derived, OtherRows_, OtherCols_ >::Result operator()(TRange< OtherRows_ > const &I, TRange< OtherCols_ > const &J) const
Type & elt0Impl()
implement the const element accessor for number arrays
ICArray(ITContainer2D< OtherAllocator > const &allocator, Range const &I, Range const &J)
constructor by reference, ref_=1.
ICArray()
default constructor.
void swapRows(int pos1, int pos2)
hidden::CSlice< Derived, 1, Size_ >::Result operator()(int i, TRange< Size_ > const &J) const
RowRange const & rowsImpl() const
bool isRef() const
TypeConst elt0Impl() const
implement the writable element accessor for number arrays
void swap(int i, int j)
swap two elements: only for vectors an points.
~ICArray()
destructor
hidden::Traits< Derived >::TypeConst TypeConst
hidden::CSliceDispatcher< Derived, Size >::Result sub(TRange< Size > const &J) const
implement the sub operator for 1D arrays using a reference on the row/column of the allocator
Type *const & p_data() const
Type & elt1Impl(int j)
implement the const element accessor for vector/point/diagonal arrays
hidden::Traits< Derived >::Row Row
Derived & shift(int firstIdx)
shift the Array.
hidden::Traits< Derived >::Col Col
void setValueImpl(int i, int j, TypeConst value)
implement setValue for vector/point/diagonal arrays
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
Type & elt2Impl(int i, int j)
implement the const element accessor
hidden::CSliceDispatcher< Derived, Size >::Result operator[](TRange< Size > const &I) const
hidden::CSlice< Derived, OtherRows_, OtherCols_ >::Result sub(TRange< OtherRows_ > const &I, TRange< OtherCols_ > const &J) const
implement the sub operator for 2D arrays using references on a range of rows and columns of the alloc...
void clear()
clear all allocated memory .
ICArray(ITContainer2D< OtherAllocator > const &allocator)
constructor by reference, ref_=1.
Type & operator()()
hidden::CSlice< Derived, Size_, 1 >::Result operator()(TRange< Size_ > const &I, int j) const
Type & operator()(int i, int j)
ICArray(int sizeRows, int sizeCols)
constructor with specified sizes.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
@ array2D_
general matrix/array/expression
@ lower_symmetric_
lower symmetric matrix/array/expression
@ lower_triangular_
lower triangular matrix/array/expression
@ symmetric_
symmetric matrix/array/expression
@ upper_triangular_
upper triangular matrix/array/expression
@ diagonal_
diagonal matrix/array/expression
@ upper_symmetric_
upper symmetric matrix/array/expression
@ vector_
column oriented vector/array/expression
@ square_
square matrix/array/expression
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
The namespace STK is the main domain space of the Statistical ToolKit project.
helper allowing to disambiguate SubVector access
If< structure_==(int) Arrays::vector_, typenameCSlice< Derived, Size, 1 >::Result, typenameCSlice< Derived, 1, Size >::Result >::Result Result
The traits struct CSlice allows to disambiguate return type of the col/row/sub operators for CArray f...
Definition STK_ICArray.h:68
If<(isNumber_), CArrayNumber< Type, orient_ >, typenameIf< isVector_, CArrayVector< Type, SizeRows, orient_ >, typenameIf< isPoint_, CArrayPoint< Type, SizeCols, orient_ >, typenameIf< isSquare_, CArraySquare< Type, SizeRows, orient_ >, CArray< Type, SizeRows, SizeCols, orient_ > >::Result >::Result >::Result >::Result Result
Definition STK_ICArray.h:92
Traits< Derived >::Type Type
Definition STK_ICArray.h:69
Utility class allowing to know if in an assignment the destination must be resized or shifted.