vector/Vector.hxx

00001 // Copyright (C) 2001-2009 Vivien Mallet
00002 //
00003 // This file is part of the linear-algebra library Seldon,
00004 // http://seldon.sourceforge.net/.
00005 //
00006 // Seldon is free software; you can redistribute it and/or modify it under the
00007 // terms of the GNU Lesser General Public License as published by the Free
00008 // Software Foundation; either version 2.1 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00018 
00019 
00020 #ifndef SELDON_FILE_VECTOR_HXX
00021 
00022 #include "../share/Common.hxx"
00023 #include "../share/Properties.hxx"
00024 #include "../share/Storage.hxx"
00025 #include "../share/Errors.hxx"
00026 #include "../share/Allocator.hxx"
00027 
00028 namespace Seldon
00029 {
00030 
00031 
00033 
00037   template <class T, class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00038   class Vector_Base
00039   {
00040     // typdef declarations.
00041   public:
00042     typedef typename Allocator::value_type value_type;
00043     typedef typename Allocator::pointer pointer;
00044     typedef typename Allocator::const_pointer const_pointer;
00045     typedef typename Allocator::reference reference;
00046     typedef typename Allocator::const_reference const_reference;
00047 
00048     // Static attributes.
00049   protected:
00050     static Allocator vect_allocator_;
00051 
00052     // Attributes.
00053   protected:
00054     // Number of elements.
00055     int m_;
00056     // Pointer to stored elements.
00057     pointer data_;
00058 
00059     // Methods.
00060   public:
00061     // Constructors.
00062     Vector_Base();
00063     explicit Vector_Base(int i);
00064     Vector_Base(const Vector_Base<T, Allocator>& A);
00065 
00066     // Destructor.
00067     ~Vector_Base();
00068 
00069     // Basic methods.
00070     int GetM() const;
00071     int GetLength() const;
00072     int GetSize() const;
00073     pointer GetData() const;
00074     const_pointer GetDataConst() const;
00075     void* GetDataVoid() const;
00076     const void* GetDataConstVoid() const;
00077 
00078   };
00079 
00080 
00081   // Vector allocator.
00082   template <class T, class Allocator>
00083   Allocator Vector_Base<T, Allocator>::vect_allocator_;
00084 
00085 
00087 
00090   template <class T, class Allocator>
00091   class Vector<T, VectFull, Allocator>: public Vector_Base<T, Allocator>
00092   {
00093     // typedef declaration.
00094   public:
00095     typedef typename Allocator::value_type value_type;
00096     typedef typename Allocator::pointer pointer;
00097     typedef typename Allocator::const_pointer const_pointer;
00098     typedef typename Allocator::reference reference;
00099     typedef typename Allocator::const_reference const_reference;
00100 
00101     typedef VectFull storage;
00102 
00103     // Attributes.
00104   private:
00105 
00106     // Methods.
00107   public:
00108     // Constructor.
00109     explicit Vector()  throw();
00110     explicit Vector(int i);
00111     Vector(int i, pointer data);
00112     Vector(const Vector<T, VectFull, Allocator>& A);
00113 
00114     // Destructor.
00115     ~Vector();
00116     void Clear();
00117 
00118     // Memory management.
00119     void Reallocate(int i);
00120     void Resize(int i);
00121     void SetData(int i, pointer data);
00122     template <class Allocator0>
00123     void SetData(const Vector<T, VectFull, Allocator0>& V);
00124     void Nullify();
00125 
00126     // Element access and affectation.
00127     reference operator() (int i);
00128 #ifndef SWIG
00129     const_reference operator() (int i) const;
00130     Vector<T, VectFull, Allocator>& operator= (const Vector<T, VectFull,
00131                                                Allocator>& X);
00132 #endif
00133     void Copy(const Vector<T, VectFull, Allocator>& X);
00134     void Append(const T& x);
00135     template<class T0>
00136     void PushBack(const T0& x);
00137     template<class Allocator0>
00138     void PushBack(const Vector<T, VectFull, Allocator0>& X);
00139 
00140     // Basic functions.
00141     int GetDataSize();
00142 
00143     // Convenient functions.
00144     void Zero();
00145     void Fill();
00146     template <class T0>
00147     void Fill(const T0& x);
00148 #ifndef SWIG
00149     template <class T0>
00150     Vector<T, VectFull, Allocator>& operator= (const T0& X);
00151 #endif
00152     template <class T0>
00153     Vector<T, VectFull, Allocator>& operator*= (const T0& X);
00154     void FillRand();
00155     void Print() const;
00156 
00157     // Norms.
00158     value_type GetNormInf() const;
00159     int GetNormInfIndex() const;
00160 
00161     // Input/output functions.
00162     void Write(string FileName, bool with_size = true) const;
00163     void Write(ostream& FileStream, bool with_size = true) const;
00164     void WriteText(string FileName) const;
00165     void WriteText(ostream& FileStream) const;
00166     void Read(string FileName, bool with_size = true);
00167     void Read(istream& FileStream, bool with_size = true);
00168     void ReadText(string FileName);
00169     void ReadText(istream& FileStream);
00170   };
00171 
00172 #ifndef SWIG
00173   template <class T, class Storage, class Allocator>
00174   ostream& operator << (ostream& out,
00175                         const Vector<T, Storage, Allocator>& V);
00176 #endif
00177 
00178 
00179 } // namespace Seldon.
00180 
00181 #define SELDON_FILE_VECTOR_HXX
00182 #endif