vector/SparseVector.hxx

00001 // Copyright (C) 2003-2009 Marc Duruflé
00002 // Copyright (C) 2001-2009 Vivien Mallet
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 #ifndef SELDON_FILE_SPARSE_VECTOR_HXX
00022 
00023 namespace Seldon
00024 {
00025 
00026 
00028   template <class T, class Allocator>
00029   class Vector<T, VectSparse, Allocator>:
00030     public Vector<T, VectFull, Allocator>
00031   {
00032     // typedef declarations.
00033   public:
00034     typedef typename Allocator::value_type value_type;
00035     typedef typename Allocator::pointer pointer;
00036     typedef typename Allocator::const_pointer const_pointer;
00037     typedef typename Allocator::reference reference;
00038     typedef typename Allocator::const_reference const_reference;
00039 
00040     typedef VectSparse storage;
00041 
00042   protected :
00043     static SELDON_DEFAULT_ALLOCATOR<int> index_allocator_;
00044 
00045     // Attributes.
00046   private:
00048     int* index_;
00049 
00050     // Methods.
00051   public:
00052     // Constructor.
00053     explicit Vector()  throw();
00054     explicit Vector(int i);
00055     Vector(const Vector<T, VectSparse, Allocator>& A);
00056 
00057     // Destructor.
00058     ~Vector();
00059     void Clear();
00060 
00061     // Memory management.
00062     void Reallocate(int i);
00063     void Resize(int i);
00064     void SetData(int nz, T* data, int* index);
00065     template<class Allocator2>
00066     void SetData(Vector<T, VectFull, Allocator2>& data,
00067                  Vector<int>& index);
00068     template<class Allocator2>
00069     void SetData(const Vector<T, VectSparse, Allocator2>& V);
00070     void Nullify();
00071 
00072     // Element access and affectation.
00073     reference Value(int i);
00074 #ifndef SWIG
00075     const_reference Value(int i) const;
00076 #endif
00077     int& Index(int i);
00078 #ifndef SWIG
00079     int Index(int i) const;
00080 #endif
00081     reference operator() (int i);
00082 #ifndef SWIG
00083     value_type operator() (int i) const;
00084 #endif
00085     reference Val(int i);
00086 #ifndef SWIG
00087     const_reference Val(int i) const;
00088     Vector<T, VectSparse, Allocator>& operator= (const Vector<T, VectSparse,
00089                                                  Allocator>& X);
00090 #endif
00091     void Copy(const Vector<T, VectSparse, Allocator>& X);
00092 
00093     // Basic functions.
00094     int* GetIndex() const;
00095 
00096     // Convenient functions.
00097     template <class T0>
00098 #ifndef SWIG
00099     Vector<T, VectSparse, Allocator>& operator= (const T0& X);
00100 #endif
00101     void Print() const;
00102     void Assemble();
00103     template<class T0>
00104     void RemoveSmallEntry(const T0& epsilon);
00105     void AddInteraction(int i, const T& val);
00106     void AddInteractionRow(int, int*, T*, bool already_sorted = false);
00107     template<class Allocator0>
00108     void AddInteractionRow(int nb, Vector<int> col,
00109                            Vector<T, VectFull, Allocator0> val,
00110                            bool already_sorted = false);
00111 
00112     // Input/output functions.
00113     void Write(string FileName) const;
00114     void Write(ostream& FileStream) const;
00115     void WriteText(string FileName) const;
00116     void WriteText(ostream& FileStream) const;
00117     void Read(string FileName);
00118     void Read(istream& FileStream);
00119     void ReadText(string FileName);
00120     void ReadText(istream& FileStream);
00121 
00122   };
00123 
00124 #ifndef SWIG
00125   template <class T, class Allocator>
00126   ostream& operator << (ostream& out,
00127                         const Vector<T, VectSparse, Allocator>& V);
00128 #endif
00129 
00130 
00131   template<class T, class Allocator>
00132   SELDON_DEFAULT_ALLOCATOR<int>
00133   Vector<T, VectSparse, Allocator>::index_allocator_;
00134 
00135 
00136 } // namespace Seldon.
00137 
00138 #define SELDON_FILE_SPARSE_VECTOR_HXX
00139 #endif