Vector.hxx
1 // Copyright (C) 2001-2009 Vivien Mallet
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_VECTOR_HXX
21 
22 #include "../share/Common.hxx"
23 #include "../share/Properties.hxx"
24 #include "../share/Storage.hxx"
25 #include "../share/Errors.hxx"
26 #include "../share/Allocator.hxx"
27 #include "VectorExpression.hxx"
28 
29 namespace Seldon
30 {
31 
32 
34 
38  template <class T, class Allocator
39  = typename SeldonDefaultAllocator<VectFull, T>::allocator>
40  class Vector_Base :
41  public VectorExpression<T, Vector<T, VectFull, Allocator> >
42  {
43  // typdef declarations.
44  public:
45  typedef typename Allocator::value_type value_type;
46  typedef typename Allocator::pointer pointer;
47  typedef typename Allocator::const_pointer const_pointer;
48  typedef typename Allocator::reference reference;
49  typedef typename Allocator::const_reference const_reference;
50  typedef Allocator allocator;
51 
52  // Attributes.
53  protected:
54  // Number of elements.
55  size_t m_;
56  // Pointer to stored elements.
57  pointer data_;
58 
59  // Methods.
60  public:
61  // Constructors.
62  Vector_Base();
63  explicit Vector_Base(size_t i);
64  Vector_Base(const Vector_Base<T, Allocator>& A);
65 
66  // Destructor.
67  ~Vector_Base();
68  void Clear();
69 
70  // Basic methods.
71  long GetM() const;
72  size_t GetLength() const;
73  size_t GetSize() const;
74  size_t GetMemorySize() const;
75  pointer GetData() const;
76  const_pointer GetDataConst() const;
77  void* GetDataVoid() const;
78  const void* GetDataConstVoid() const;
79 
80  };
81 
82 
84 
87  template <class T, class Allocator>
88  class Vector<T, VectFull, Allocator>: public Vector_Base<T, Allocator>
89  {
90  // typedef declaration.
91  public:
92  typedef typename Allocator::value_type value_type;
93  typedef typename Allocator::pointer pointer;
94  typedef typename Allocator::const_pointer const_pointer;
95  typedef typename Allocator::reference reference;
96  typedef typename Allocator::const_reference const_reference;
97 
98  typedef VectFull storage;
99 
100  // Attributes.
101  private:
102 
103  // Methods.
104  public:
105  // Constructor.
106  explicit Vector();
107  explicit Vector(size_t i);
108  Vector(size_t i, pointer data);
110 
111  // Destructor.
112  ~Vector();
113  void Clear();
114 
115  // Memory management.
116  void Reallocate(size_t i);
117  void ReallocateVector(size_t i);
118  void Resize(size_t i);
119  void ResizeVector(size_t i);
120  void SetData(size_t i, pointer data);
121  template <class Allocator0>
122  void SetData(const Vector<T, VectFull, Allocator0>& V);
123  void Nullify();
124 
125  // Element access and affectation.
126  reference operator() (long i);
127  reference Get(long i);
128 #ifndef SWIG
129  const_reference operator() (long i) const;
130  const_reference Get(long i) const;
131  Vector<T, VectFull, Allocator>& operator= (const Vector<T, VectFull,
132  Allocator>& X);
133 #endif
134  void Copy(const Vector<T, VectFull, Allocator>& X);
135  Vector<T, VectFull, Allocator> Copy() const;
136  void Append(const T& x);
137 
138  template<class T0>
139  void PushBack(const T0& x);
140 
141  template<class Allocator0>
142  void PushBack(const Vector<T, VectFull, Allocator0>& X);
143 
144  // Basic functions.
145  size_t GetDataSize();
146 
147  // Convenient functions.
148  void Zero();
149  void Fill();
150  template <class T0>
151  void Fill(const T0& x);
152 #ifndef SWIG
153  Vector<T, VectFull, Allocator>& operator =(const T& X);
154 #endif
155 
156  template <class T0>
157  Vector<T, VectFull, Allocator>& operator *=(const T0& X);
158 
159  template<class E>
161 
162  template<class E>
164 
165  template<class E>
167 
168  void FillRand();
169  void Print() const;
170 
171  // Norms.
172  typename ClassComplexType<T>::Treal GetNormInf() const;
173  long GetNormInfIndex() const;
174 
175  // Input/output functions.
176  void Write(string FileName, bool with_size = true) const;
177  void Write(ostream& FileStream, bool with_size = true) const;
178  void WriteText(string FileName) const;
179  void WriteText(ostream& FileStream) const;
180 #ifdef SELDON_WITH_HDF5
181  void WriteHDF5(string FileName, string group_name,
182  string dataset_name) const;
183 #endif
184  void Read(string FileName, bool with_size = true);
185  void Read(istream& FileStream, bool with_size = true);
186  void ReadText(string FileName);
187  void ReadText(istream& FileStream);
188  };
189 
190 #ifndef SWIG
191  template <class T, class Storage, class Allocator>
192  ostream& operator << (ostream& out,
194 #endif
195 
196 
197 } // namespace Seldon.
198 
199 #define SELDON_FILE_VECTOR_HXX
200 #endif
Seldon::Vector_Base::GetM
long GetM() const
Returns the number of elements.
Definition: VectorInline.cxx:131
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::VectorExpression
Expression between vectors.
Definition: VectorExpression.hxx:8
Seldon::Vector_Base::GetSize
size_t GetSize() const
Returns the number of elements stored.
Definition: VectorInline.cxx:153
Seldon::Vector_Base::GetDataConstVoid
const void * GetDataConstVoid() const
Returns a pointer of type "const void*" to the data array (data_).
Definition: VectorInline.cxx:212
Seldon::VectFull
Definition: StorageInline.cxx:74
Seldon::Vector_Base
Base structure for all vectors.
Definition: SeldonHeader.hxx:201
Seldon::Vector_Base::Clear
void Clear()
Releases memory used by the vector.
Definition: VectorInline.cxx:94
Seldon::Vector_Base::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: VectorInline.cxx:165
Seldon::Vector< T, VectFull, Allocator >
Full vector class.
Definition: Vector.hxx:88
Seldon::Vector_Base::GetLength
size_t GetLength() const
Returns the number of elements.
Definition: VectorInline.cxx:142
Seldon::Vector_Base::GetDataVoid
void * GetDataVoid() const
Returns a pointer of type "void*" to the data array (data_).
Definition: VectorInline.cxx:201
Seldon::Vector_Base::~Vector_Base
~Vector_Base()
Destructor.
Definition: VectorInline.cxx:86
Seldon::Vector_Base::Vector_Base
Vector_Base()
Default constructor.
Definition: VectorInline.cxx:45
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
Seldon::Vector_Base::GetDataConst
const_pointer GetDataConst() const
Returns a const pointer to data_ (stored data).
Definition: VectorInline.cxx:189
Seldon::Vector_Base::GetData
pointer GetData() const
Returns a pointer to data_ (stored data).
Definition: VectorInline.cxx:177