/*--------------------------------------------------------------------*/
/*     Copyright (C) 2004-2016  Serge Iovleff, Université Lille 1, Inria

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the
    Free Software Foundation, Inc.,
    59 Temple Place,
    Suite 330,
    Boston, MA 02111-1307
    USA

    Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
*/

/*
 * Project: stkpp::Arrays
 * Purpose:  Define the SArray2DPoint class.
 * Author:   Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
 *
 **/

/** @file STK_SArray2DPoint.h
  * @brief A SArray2DPoint is a one dimensional horizontal container
 **/

#ifndef STK_SARRAY2DPOINT_H
#define STK_SARRAY2DPOINT_H

#include "STK_SArray1D.h"

#include "STK_IArray2D.h"
#include "STK_IArray2DSlicers.h"
#include "STK_IArray2DModifiers.h"

namespace STK
{

template<typename> class SArray2DPoint;
template<typename> class SArray2DVector;

/** @ingroup Arrays
  * @brief final class for a Real horizontal container.
  * A Point is a row oriented 1D container of Real.
  */
typedef SArray2DPoint<Real>   SPointX;
typedef SArray2DPoint<double> SPointXd;
typedef SArray2DPoint<int>    SPointXi;

namespace hidden
{
/** @ingroup hidden
 *  @brief Specialization of the Traits class for SArray2DPoint class.
 **/
template<class Type_>
struct Traits< SArray2DPoint<Type_> >
{
  typedef SArray2DPoint<Type_>  Row;
  typedef SArray2DVector<Type_> Col;
  typedef SArray2DPoint<Type_>  SubRow;
  typedef SArray2DVector<Type_> SubCol;
  typedef SArray2DPoint<Type_>  SubArray;
  typedef SArray2DPoint<Type_>  SubVector;

  typedef Type_                Type;
  typedef typename RemoveConst<Type>::Type const& TypeConst;

  enum
  {
    structure_ = Arrays::point_,
    orient_    = Arrays::by_col_,
    sizeRows_  = 1,
    sizeCols_  = UnknownSize,
    size_      = UnknownSize,
    storage_   = Arrays::sparse_
  };
  typedef SArray1D<Type, UnknownSize, UnknownSize> ColVector;
};

} // namespace hidden

/** @ingroup Arrays
 *  @brief template one dimensional horizontal Array.
 *
 * An SArray2DPoint is an implementation of the interface IArray2D.
 * It's a one dimensional row-vector and is refered as a point.
 *
 *  By default the index of the first element is 0 but this can be
 *  modified using the appropriate constructor or using the method @c shift.
 **/
template<class Type_>
class SArray2DPoint: public IArray2D< SArray2DPoint<Type_> >
{
  public:
    typedef IArray2D< SArray2DPoint<Type_> > Base;
    typedef ArrayBase< SArray2DPoint<Type_> > LowBase;

    typedef typename hidden::Traits< SArray2DPoint<Type_> >::Row Row;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::Col Col;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::SubRow SubRow;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::SubCol SubCol;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::SubVector SubVector;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::SubArray SubArray;

    typedef typename hidden::Traits< SArray2DPoint<Type_> >::Type Type;
    typedef typename hidden::Traits< SArray2DPoint<Type_> >::TypeConst TypeConst;

    enum
    {
      structure_ = hidden::Traits< SArray2DPoint<Type_> >::structure_,
      orient_    = hidden::Traits< SArray2DPoint<Type_> >::orient_,
      sizeCols_  = hidden::Traits< SArray2DPoint<Type_> >::sizeCols_,
      sizeRows_  = hidden::Traits< SArray2DPoint<Type_> >::sizeRows_,
      size_      = hidden::Traits< SArray2DPoint<Type_> >::size_,
      storage_   = hidden::Traits< SArray2DPoint<Type_> >::storage_
    };

    /** Default constructor */
    SArray2DPoint(): Base(Range(1), Range()) {}
    /** constructor with specified range.
     *  @param J range of the container
     **/
    SArray2DPoint( Range const& J): Base(Range(1), J) {}
    /** constructor with specified range, initialization with a constant.
     *  @param J range of the container
     *  @param v initial value of the container
     **/
    SArray2DPoint( Range const& J, Type const& v): Base(Range(1), J)
    { LowBase::setValue(v);}
    /** Copy constructor
     *  @param T the container to copy
     *  @param ref true if this is a wrapper of T
     **/
    SArray2DPoint( SArray2DPoint const& T, bool ref =false)
               : Base(T, ref)
    {}
    /** constructor by reference, ref_=1.
     *  @param T the container to wrap
     *  @param J the columns range to wrap
     **/
    SArray2DPoint( SArray2DPoint const& T, Range const& J)
               : Base(T, T.rows(), J) {}
    /** constructor by reference, ref_=1.
     *  @param T the container to wrap
     *  @param J the range of the data to wrap
     *  @param row the index of the row to wrap
     **/
    template<class OtherArray>
    SArray2DPoint( IArray2D<OtherArray> const& T, Range const& J, int row)
               : Base(T, Range(row, 1), J)
    {}
    /** Copy constructor using an expression.
     *  @param T the container to wrap
     **/
    template<class OtherDerived>
    SArray2DPoint( ExprBase<OtherDerived> const& T): Base(Range(1), Range())
    { LowBase::operator=(T);}
    /** constructor by reference, ref_=1.
     *  @param p_data a pointer on the data to wrap
     *  @param J the range of the data to wrap
     *  @param row the index of the row to wrap
     **/
     SArray2DPoint( Type** p_data, Range const& J, int row)
                : Base(p_data, Range(row, 1), J) {}
    /** destructor. */
    ~SArray2DPoint() {}
    /** @return a constant reference on the jth element
     *  @param j index of the element (const)
     **/
    Type const & elt1Impl(int const& j) const { return this->elt(this->beginRows(), j);}
    /** @return a reference on the jth element
     *  @param j index of the element
     **/
    inline Type& elt1Impl(int const& j) { return this->elt(this->beginRows(), j);}
    /** New first indexes for the object.
     *  @param cbeg the index of the first column to set
     **/
    void shift1D(int const& cbeg) { Base::shift(this->beginRows(), cbeg);}
    /**  Resize the container.
     *  @param J the range to set to the container
     **/
    SArray2DPoint<Type>& resize1D(Range const& J)
    { Base::resize(this->rows(), J); return *this;}
    /** Set value at position j
     *  @param j,v position and value to set
     **/
    void setValue1D( int j, TypeConst v)
    { this->setValue(this->beginRows(), j, v);}

    /** Add n elements to the container.
     *  @param n number of elements to add
     **/
    void pushBack( int n=1) { Base::pushBackCols(n);}
    /** Delete n elts at the pos index to the container.
     *  @param pos, n position and number of elements to delete
    **/
    void erase( int pos, int n=1)
    { Base::eraseCols(pos, n);}
    /** Insert n elts at the position pos of the container. The bound
     *  end_ should be modified at the very end of the insertion as pos
     *  can be a reference to it.
     *  @param pos, n position and number of elements to insert
     **/
    void insertElt( int pos, int n =1)
    { Base::insertCols(pos, n);}
    /** operator = : overwrite the CArray with the Right hand side T.
     *  @param T the container to copy
     **/
    template<class Rhs>
    SArray2DPoint& operator=(ExprBase<Rhs> const& T) { return LowBase::operator=(T);}
    /** operator = : overwrite the CArray with the Right hand side T.
     *  @param T the container to copy
     **/
    SArray2DPoint& operator=(const SArray2DPoint &T) { return LowBase::assign(T);}
    /** set the container to a constant value.
     *  @param v the value to set
     **/
    SArray2DPoint& operator=(Type const& v) { return LowBase::setValue(v);}
};

} // namespace STK

#endif // STK_SARRAY2DPOINT_H
