All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 #ifndef __VECTOR_HPP__
35 #define __VECTOR_HPP__
36 
37 
38 #include <iostream>
39 #include <openfluid/dllexport.hpp>
41 
42 
43 namespace openfluid { namespace core {
44 
45 
46 /**
47  Template class for vector data
48 */
49 template <class T>
51 {
52  protected :
53  T* m_Data;
54  unsigned long m_Size;
55 
56  bool allocate(unsigned long Size);
57 
58  void init();
59 
60  static void copy(const Vector& Source, Vector& Dest);
61 
62  public :
63 
64  /**
65  Default constructor, creates an empty vector
66  */
67  Vector();
68 
69  /**
70  Copy constructor
71  */
72  Vector(const Vector &Vector);
73 
74  /**
75  Constructor, creates a vector containing Size elements
76  */
77  Vector(unsigned long Size);
78 
79  /**
80  Constructor, creates a vector containing Size elements, initialized with value InitValue
81  */
82  Vector(unsigned long Size, T InitValue);
83 
84  /**
85  Constructor, creates a vector of size Size, containing Data
86  */
87  Vector(T* Data, unsigned long Size);
88 
89  /**
90  Destructor
91  */
92  ~Vector();
93 
94  /**
95  Returns the size of the vector
96  @return size of the vector
97  */
98  unsigned long getSize() const { return m_Size; };
99 
100  /**
101  Returns the size of the vector
102  @return size of the vector
103  */
104  unsigned long size() const { return getSize(); };
105 
106  /**
107  Returns a pointer to the content of the vector (like C arrays)
108  */
109  T* getData() const { return m_Data; };
110 
111  /**
112  Sets data for the vector from Data and with size Size
113  */
114  void setData(T* Data, unsigned long Size);
115 
116  /**
117  Returns the element of the vector for index Index
118  */
119  T getElement(unsigned long Index) const;
120 
121  /**
122  Returns the element of the vector for index Index
123  */
124  inline T at(unsigned long Index) const { return getElement(Index); };
125 
126  /**
127  Returns the element of the vector for index Index
128  */
129  inline T get(unsigned long Index) const { return getElement(Index); };
130 
131 
132  /**
133  Sets a new value for element at the given index
134  */
135  void setElement(unsigned long Index, T Element);
136 
137  /**
138  Sets a new value for element at the given index
139  */
140  inline void set(unsigned long Index, T Element) { setElement(Index,Element); };
141 
142 
143  /**
144  Operator to set a new value for element given between []
145  */
146  T& operator[](unsigned long Index);
147 
148  /**
149  Allocation operator
150  */
151  Vector<T>& operator = (const Vector &A);
152 
153  /**
154  Fills the vector with given value
155  */
156  void fill(const T& Val);
157 
158 
159  /**
160  Clears the vector (empty and size is 0)
161  */
162  void clear();
163 
164 };
165 
166 // =====================================================================
167 // =====================================================================
168 
169 template <class T>
171 {
172  init();
173 }
174 
175 
176 // =====================================================================
177 // =====================================================================
178 
179 template <class T>
181 {
182  init();
183 
184  if (!allocate(A.m_Size)) throw openfluid::base::FrameworkException("Vector::Vector(const Vector)","Cannot allocate memory");
185 
186  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
187 
188 }
189 
190 
191 // =====================================================================
192 // =====================================================================
193 
194 template <class T>
195 Vector<T>::Vector(unsigned long Size)
196 {
197  init();
198 
199  if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(Size)","Cannot allocate memory");
200 }
201 
202 
203 
204 // =====================================================================
205 // =====================================================================
206 template <class T>
207 Vector<T>::Vector(unsigned long Size, T InitValue)
208 {
209  init();
210 
211 
212  if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(Size,T)","Cannot allocate memory");
213 
214 
215  if (m_Data != NULL)
216  {
217  unsigned long i;
218  for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
219  }
220 
221 }
222 
223 
224 // =====================================================================
225 // =====================================================================
226 
227 
228 template <class T>
229 Vector<T>::Vector(T* Data, unsigned long Size)
230 {
231  init();
232 
233  if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(T*,Size)","Cannot allocate memory");
234 
235  std::copy(Data, Data + Size, m_Data);
236 
237 }
238 
239 
240 // =====================================================================
241 // =====================================================================
242 
243 template <class T>
245 {
246  if (m_Data != NULL) clear();
247 }
248 
249 
250 // =====================================================================
251 // =====================================================================
252 
253 template <class T>
254 bool Vector<T>::allocate(unsigned long Size)
255 {
256 
257  if (Size > 0)
258  {
259  m_Data = new T[Size];
260  if (m_Data != NULL) m_Size = Size;
261  else
262  {
263  return false;
264  }
265  }
266 
267  return true;
268 
269 
270 }
271 
272 // =====================================================================
273 // =====================================================================
274 
275 template <class T>
276 void Vector<T>::setData(T* Data, unsigned long Size)
277 {
278  clear();
279 
280  if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::setData","Cannot allocate memory");
281 
282  if (m_Data != NULL) std::copy(Data, Data + Size, m_Data);
283 
284 }
285 
286 
287 // =====================================================================
288 // =====================================================================
289 
290 template <class T>
291 T Vector<T>::getElement(unsigned long Index) const
292 {
293  if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::getElement","element access range error");
294  return m_Data[Index];
295 }
296 
297 
298 // =====================================================================
299 // =====================================================================
300 
301 template <class T>
302 void Vector<T>::setElement(unsigned long Index, T Element)
303 {
304  if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::setElement","element access range error");
305  m_Data[Index] = Element;
306 }
307 
308 
309 // =====================================================================
310 // =====================================================================
311 
312 
313 template <class T>
314 T& Vector<T>::operator[](unsigned long Index)
315 {
316  if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::operator[]","element access range error");
317  return m_Data[Index];
318 }
319 
320 // =====================================================================
321 // =====================================================================
322 
323 template <class T>
325 {
326 
327  if (this == &A) return *this; // in case somebody tries assign array to itself
328 
329  clear();
330 
331  allocate(A.m_Size);
332  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
333 
334  return *this;
335 }
336 
337 // =====================================================================
338 // =====================================================================
339 
340 
341 template <class T>
343 {
344  m_Data = NULL;
345  m_Size = 0;
346 }
347 
348 
349 // =====================================================================
350 // =====================================================================
351 
352 
353 template <class T>
354 void Vector<T>::fill(const T& Val)
355 {
356  std::fill(m_Data, m_Data + m_Size,Val);
357 }
358 
359 
360 // =====================================================================
361 // =====================================================================
362 
363 
364 template <class T>
366 {
367  delete [] m_Data;
368  init();
369 }
370 
371 // =====================================================================
372 // =====================================================================
373 
374 template <class T>
375 void Vector<T>::copy(const Vector& Source, Vector& Dest)
376 {
377  Dest.clear;
378  Dest.allocate(Source.m_Size);
379  for (unsigned long i = 0; i < Source.m_Size;i++)
380  {
381  Dest.m_Data[i] = Source.m_Data[i];
382  }
383 }
384 
385 
386 
387 } }
388 
389 
390 #endif
void fill(const T &Val)
Definition: Vector.hpp:354
void setData(T *Data, unsigned long Size)
Definition: Vector.hpp:276
unsigned long size() const
Definition: Vector.hpp:104
unsigned long getSize() const
Definition: Vector.hpp:98
void setElement(unsigned long Index, T Element)
Definition: Vector.hpp:302
static void copy(const Vector &Source, Vector &Dest)
Definition: Vector.hpp:375
void clear()
Definition: Vector.hpp:365
~Vector()
Definition: Vector.hpp:244
Vector< T > & operator=(const Vector &A)
Definition: Vector.hpp:324
Definition: FrameworkException.hpp:49
T & operator[](unsigned long Index)
Definition: Vector.hpp:314
bool allocate(unsigned long Size)
Definition: Vector.hpp:254
void init()
Definition: Vector.hpp:342
T * m_Data
Definition: Vector.hpp:53
T getElement(unsigned long Index) const
Definition: Vector.hpp:291
void set(unsigned long Index, T Element)
Definition: Vector.hpp:140
T at(unsigned long Index) const
Definition: Vector.hpp:124
unsigned long m_Size
Definition: Vector.hpp:54
Vector()
Definition: Vector.hpp:170
T * getData() const
Definition: Vector.hpp:109
Definition: Vector.hpp:50
#define DLLEXPORT
Definition: dllexport.hpp:51