STK++ 1.0
STK_ITContainer1D.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::Sdk::TContainer
00027  * Purpose:  Define the Interface 1D templated Container class.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00041 #ifndef STK_ITCONTAINER1D_H
00042 #define STK_ITCONTAINER1D_H
00043 
00044 #include "STK_IContainerBase.h"
00045 #include "STK_IContainer1D.h"
00046 #include "../../STKernel/include/STK_Exceptions.h"
00047 
00048 namespace STK
00049 {
00075 template <class TYPE, class TContainer1D>
00076 class ITContainer1D : public IContainerBase<TContainer1D>
00077                     , virtual public IContainer1D
00078 {
00079   protected:
00083     ITContainer1D( Range const& I = Range())
00084                  : IContainerBase<TContainer1D>(IContainerBase<TContainer1D>::_1D_)
00085                  , range_(I)
00086     { this->ranges_.push_back(&range_);}
00087 
00091     ITContainer1D( const ITContainer1D& T)
00092                  : IContainerBase<TContainer1D>(IContainerBase<TContainer1D>::_1D_)
00093                  , range_(T.range_)
00094     { this->ranges_.push_back(&range_);}
00095 
00096   public:
00098     typedef TYPE Type;
00100     virtual ~ITContainer1D() { ;}
00101 
00105     inline Integer const& first() const { return range_.first();}
00106 
00110     inline Integer const& last() const { return range_.last();}
00111 
00115     inline Integer const& size() const { return range_.size();}
00116 
00120     inline Range const& range() const { return range_;}
00121 
00126     inline TYPE& elt(Integer const& pos)
00127     { return this->asLeaf().elt(pos);}
00128 
00133     inline TYPE const& elt(Integer const& pos) const
00134     { return this->asLeaf().elt(pos);}
00135 
00140     inline TContainer1D elt(Range const& I) const
00141     { return this->asLeaf().elt(I);}
00142 
00147     inline TYPE& operator[](Integer const& pos)
00148     { return this->asLeaf().elt(pos);}
00149 
00154     inline TYPE const& operator[](Integer const& pos) const
00155     { return this->asLeaf().elt(pos);}
00156 
00161     inline TContainer1D operator[](Range const& I) const
00162     { return this->asLeaf().elt(I);}
00163 
00169     void swap(Integer const& pos1, Integer const& pos2)
00170     {
00171 #ifdef STK_BOUNDS_CHECK
00172       // check indices
00173       if (pos1<this->first())
00174       { throw out_of_range("ITContainer1D::swap(pos1, pos2) "
00175                                 "pos1<this->first()");
00176       }
00177       if (pos1>this->last())
00178       { throw out_of_range("ITContainer1D::swap(pos1, pos2) "
00179                                 "pos1>this->last()");
00180       }
00181       if (pos2<this->first())
00182       { throw out_of_range("ITContainer1D::swap(pos1, pos2) "
00183                                 "pos2<this->first()");
00184       }
00185       if (pos2>this->last())
00186       { throw out_of_range("ITContainer1D::swap(pos1, pos2) "
00187                                 "pos2>this->last()");
00188       }
00189 #endif
00190       // swap
00191       TYPE aux(this->asLeaf().elt(pos1));
00192       this->asLeaf().elt(pos1) = this->asLeaf().elt(pos2);
00193       this->asLeaf().elt(pos2) = aux;
00194     }
00195 
00200     inline TYPE& at(Integer const& pos)
00201     {
00202       if ((pos<this->first()))
00203       {
00204         throw out_of_range("ITContainer1D::at(pos) "
00205                                 "pos<this->first()");
00206       }
00207       if ((pos>this->last()))
00208       { throw out_of_range("ITContainer1D::at(pos) "
00209                                 "pos>this->last()");
00210       }
00211       return this->asLeaf().elt(pos);
00212     }
00213 
00218     inline TYPE const& at(Integer const& pos) const
00219     {
00220       if (pos<this->first())
00221       {
00222         throw out_of_range("ITContainer1D::at(pos) "
00223                                 "pos<this->first()");
00224       }
00225       if (pos>this->last())
00226       { throw out_of_range("ITContainer1D::at(pos) "
00227                                 "pos>this->last()");
00228       }
00229       return this->asLeaf().elt(pos);
00230     }
00231 
00235     inline TYPE& front()
00236     { return this->asLeaf().elt(this->first()); }
00237 
00241     inline TYPE const& front() const
00242     { return this->asLeaf().elt(this->first()); }
00243 
00247     inline TYPE& back()
00248     { return this->asLeaf().elt(this->last()); }
00249 
00253     inline TYPE const& back() const
00254     { return this->asLeaf().elt(this->last()); }
00255 
00259     inline void push_back(TYPE const& v)
00260     {
00261       this->asLeaf().pushBack();
00262       this->asLeaf().elt(this->last()) = v;
00263     }
00264 
00268 //    inline void push_front(TYPE const& v)
00269 //    { insert(Range(this->first(), this->first()), v);}
00270 
00275 //    inline void insert(Range const& I, TYPE const& v)
00276 //    {
00277 //      this->insertElt(I.first(), I.size());
00278 //      for (Integer i=I.first(); i<=I.last(); i++)
00279 //        this->asLeaf().elt(i) = v;
00280 //    }
00281 
00282   protected:
00284     Range range_;
00285 
00289     inline void setRange(Range const& I = Range())
00290     { range_ = I;}
00291 
00295     inline void incRange(Integer const& inc =1)
00296     { range_.inc(inc);}
00297 
00301     inline void incFirst(Integer const& inc = 1)
00302     { range_.incFirst(inc);}
00303 
00307     inline void decFirst(Integer const& inc = 1)
00308     { range_.decFirst(inc);}
00309 
00313     inline void incLast(Integer const& inc =1)
00314     { range_.incLast(inc);}
00315 
00319     inline void decLast(Integer const& inc =1)
00320     { range_.decLast(inc);}
00321 };
00322 
00323 } // namespace STK
00324 
00325 #endif
00326 // STK_ITCONTAINER1D_H