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();
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     reference Get(int i);
00129 #ifndef SWIG
00130     const_reference operator() (int i) const;
00131     const_reference Get(int i) const;
00132     Vector<T, VectFull, Allocator>& operator= (const Vector<T, VectFull,
00133                                                Allocator>& X);
00134 #endif
00135     void Copy(const Vector<T, VectFull, Allocator>& X);
00136     Vector<T, VectFull, Allocator> Copy() const;
00137     void Append(const T& x);
00138     template<class T0>
00139     void PushBack(const T0& x);
00140     template<class Allocator0>
00141     void PushBack(const Vector<T, VectFull, Allocator0>& X);
00142 
00143     // Basic functions.
00144     int GetDataSize();
00145 
00146     // Convenient functions.
00147     void Zero();
00148     void Fill();
00149     template <class T0>
00150     void Fill(const T0& x);
00151 #ifndef SWIG
00152     template <class T0>
00153     Vector<T, VectFull, Allocator>& operator= (const T0& X);
00154 #endif
00155     template <class T0>
00156     Vector<T, VectFull, Allocator>& operator*= (const T0& X);
00157     void FillRand();
00158     void Print() const;
00159 
00160     // Norms.
00161     value_type GetNormInf() const;
00162     int GetNormInfIndex() const;
00163 
00164     // Input/output functions.
00165     void Write(string FileName, bool with_size = true) const;
00166     void Write(ostream& FileStream, bool with_size = true) const;
00167     void WriteText(string FileName) const;
00168     void WriteText(ostream& FileStream) const;
00169 #ifdef SELDON_WITH_HDF5
00170     void WriteHDF5(string FileName, string group_name,
00171                    string dataset_name) const;
00172 #endif
00173     void Read(string FileName, bool with_size = true);
00174     void Read(istream& FileStream, bool with_size = true);
00175     void ReadText(string FileName);
00176     void ReadText(istream& FileStream);
00177   };
00178 
00179 #ifndef SWIG
00180   template <class T, class Storage, class Allocator>
00181   ostream& operator << (ostream& out,
00182                         const Vector<T, Storage, Allocator>& V);
00183 #endif
00184 
00185 
00186 } // namespace Seldon.
00187 
00188 #define SELDON_FILE_VECTOR_HXX
00189 #endif