SparseVector.hxx
1 // Copyright (C) 2003-2011 Marc DuruflĂ©
2 // Copyright (C) 2001-2011 Vivien Mallet
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_SPARSE_VECTOR_HXX
22 
23 namespace Seldon
24 {
25 
26 
28  template <class T, class Allocator>
29  class Vector<T, VectSparse, Allocator>:
30  public Vector<T, VectFull, Allocator>
31  {
32  // typedef declarations.
33  public:
34  typedef typename Allocator::value_type value_type;
35  typedef typename Allocator::pointer pointer;
36  typedef typename Allocator::const_pointer const_pointer;
37  typedef typename Allocator::reference reference;
38  typedef typename Allocator::const_reference const_reference;
39  typedef typename SeldonDefaultAllocator<VectFull, int>::allocator AllocatorInt;
40 
41  typedef VectSparse storage;
42 
43  // Attributes.
44  private:
46  int* index_;
47 
48  // Methods.
49  public:
50  // Constructor.
51  explicit Vector();
52  explicit Vector(size_t i);
54 
55  // Destructor.
56  ~Vector();
57  void Clear();
58 
59  // Memory management.
60  void Reallocate(size_t i);
61  void ReallocateVector(size_t i);
62  void Resize(size_t i);
63  void ResizeVector(size_t i);
64  void SetData(size_t nz, T* data, int* index);
65  template<class Allocator2>
66  void SetData(Vector<T, VectFull, Allocator2>& data,
67  Vector<int>& index);
68  template<class Allocator2>
69  void SetData(const Vector<T, VectSparse, Allocator2>& V);
70  void Nullify();
71 
72  // Element access and affectation.
73  reference Value(int i);
74 #ifndef SWIG
75  const_reference Value(int i) const;
76 #endif
77  int& Index(int i);
78 #ifndef SWIG
79  int Index(int i) const;
80 #endif
81 
82  value_type operator() (int i);
83  reference Get(int i);
84  reference Val(int i);
85 #ifndef SWIG
86  value_type operator() (int i) const;
87  const_reference Get(int i) const;
88  const_reference Val(int i) const;
90  Allocator>& X);
91 #endif
92  template<class T2, class Alloc2>
93  void Copy(const Vector<T2, VectSparse, Alloc2>& X);
94 
95  // Basic functions.
96  int* GetIndex() const;
97  size_t GetMemorySize() const;
98 
99  // Convenient functions.
100 #ifndef SWIG
101  template <class T0>
102  Vector<T, VectSparse, Allocator>& operator= (const T0& X);
103 #endif
104  void Print() const;
105  void Assemble();
106  template<class T0>
107  void RemoveSmallEntry(const T0& epsilon);
108  void AddInteraction(int i, const T& val);
109  void AddInteractionRow(size_t, int*, T*, bool already_sorted = false);
110  template<class Allocator0>
111  void AddInteractionRow(size_t nb, const Vector<int>& col,
113  bool already_sorted = false);
114 
115  typename ClassComplexType<T>::Treal GetNormInf() const;
116  int GetNormInfIndex() const;
117 
118  // Input/output functions.
119  void Write(string FileName) const;
120  void Write(ostream& FileStream) const;
121  void WriteText(string FileName) const;
122  void WriteText(ostream& FileStream) const;
123  void Read(string FileName);
124  void Read(istream& FileStream);
125  void ReadText(string FileName);
126  void ReadText(istream& FileStream);
127 
128  };
129 
130 #ifndef SWIG
131  template <class T, class Allocator>
132  ostream& operator << (ostream& out,
134 #endif
135 
136 
137 } // namespace Seldon.
138 
139 #define SELDON_FILE_SPARSE_VECTOR_HXX
140 #endif
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Vector< T, VectSparse, Allocator >
Sparse vector class.
Definition: SparseVector.hxx:29
Seldon::VectSparse
Definition: StorageInline.cxx:79
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617