COPASI API  4.16.103
CVector.h
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2002 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #ifndef COPASI_CVector
16 #define COPASI_CVector
17 
18 #include <algorithm> // for std::min
19 #include <iostream>
20 #include <string.h>
21 #include <limits>
22 
23 #include "copasi.h"
25 
26 #undef min
27 #undef max
28 
29 template <typename CType> class CVectorCore;
30 
31 template <typename CType>
32 std::ostream &operator<<(std::ostream &os, const CVectorCore< CType > & A);
33 
34 template <class CType> class CVectorCore
35 {
36 public:
37  typedef CType elementType;
38 
39  // Attributes
40 protected:
41  /**
42  * Size of the vector.
43  */
44  size_t mSize;
45 
46  /**
47  * The array storing the vector elements
48  */
49  CType * mVector;
50 
51  // Operations
52 public:
53  /**
54  * Specific constructor
55  * @param const size_t & size (default: 0)
56  * @param CType * vector (default: NULL)
57  */
58  CVectorCore(const size_t & size = 0,
59  CType * vector = NULL):
60  mSize(size),
61  mVector(vector)
62  {}
63 
64  /**
65  * Copy constructor
66  * @param const CVectorCore< CType > & src
67  */
69  mSize(src.mSize),
70  mVector(src.mVector)
71  {}
72 
73  /**
74  * Destructor.
75  */
77  {}
78 
79  /**
80  * Assignment operator
81  * @param const CType & value
82  */
83  CVectorCore< CType> & operator = (const CType & value)
84  {
85  size_t i;
86  CType * tmp = mVector;
87 
88  for (i = 0; i < mSize; i++, tmp++)
89  {
90  *tmp = value;
91  }
92 
93  return *this;
94  }
95 
96  /**
97  * The number of elements stored in the vector.
98  * @return size_t size
99  */
100  size_t size() const {return mSize;}
101 
102  /**
103  * Retrieve an element of the vector
104  * @param const size_t & row
105  * @return CType & element
106  */
107  inline CType & operator[](const size_t & row)
108  {return *(mVector + row);}
109 
110  /**
111  * Retrieve an element of the vector
112  * @param const size_t & row
113  * @return const CType & element
114  */
115  inline const CType & operator[](const size_t & row) const
116  {return *(mVector + row);}
117 
118  /**
119  * Retrieve a vector element using Fortan style indexing.
120  * @param const size_t & row
121  * @return const CType & element
122  */
123  inline CType & operator()(const size_t & row)
124  {return *(mVector + (row - 1));}
125 
126  /**
127  * Retrieve a vector element using Fortan style indexing.
128  * @param const size_t & row
129  * @return const CType & element
130  */
131  inline const CType & operator()(const size_t & row) const
132  {return *(mVector + (row - 1));}
133 
134  /**
135  * Retrieve the array of the vector elements. This is suitable
136  * for interfacing with clapack routines.
137  * @return CType * array
138  */
139  CType * array() {return mVector;}
140 
141  /**
142  * Retrieve the array of the vector elements. This is suitable
143  * for interfacing with clapack routines.
144  * @return const CType * array
145  */
146  const CType * array() const {return mVector;}
147 
148  /**
149  * Output stream operator
150  * @param ostream & os
151  * @param const CVector< CType > & A
152  * @return ostream & os
153  */
154 #if defined SWIG
155  friend std::ostream &operator << (std::ostream &os,
156  const CVectorCore< CType > & A);
157 #else
158 #if defined _MSC_VER && _MSC_VER < 1201 // 1200 Identifies Visual C++ 6.0
159  friend std::ostream &operator << (std::ostream &os,
160  const CVectorCore< CType > & A);
161 #else
162  friend std::ostream &operator << <> (std::ostream &os,
163  const CVectorCore< CType > & A);
164 #endif // WIN32
165 #endif // SWIG
166 
167  /**
168  * Reorder the elements according to the provided pivots
169  * @param const CVectorCore<size_t> & pivot
170  * @return bool success
171  */
172  bool applyPivot(const CVectorCore<size_t> & pivot)
173  {
174  if (pivot.size() != mSize) return false;
175 
176  bool * pApplied = new bool[mSize];
177  CVectorCore < bool > Applied(mSize, pApplied);
178 
179  Applied = false;
180  CType tmp;
181 
182  size_t i;
183  size_t to;
184  size_t from;
185 
186  for (i = 0; i < mSize; i++)
187  if (!Applied[i])
188  {
189  to = i;
190  from = pivot[i];
191 
192  if (to != from)
193  {
194  tmp = mVector[i];
195 
196  while (from != i)
197  {
198  mVector[to] = mVector[from];
199  Applied[to] = true;
200 
201  to = from;
202  from = pivot[to];
203  }
204 
205  mVector[to] = tmp;
206  }
207 
208  Applied[to] = true;
209  }
210 
211  delete [] pApplied;
212 
213  return true;
214  }
215 };
216 
217 /**
218  * Template class CVector < class CType >
219  * This template class is a simple vector class allowing standard
220  * C-style and fortran style access to the elements. It also supplies
221  * an ostream operator.
222  */
223 template <class CType> class CVector : public CVectorCore< CType >
224 {
225  // Operations
226 public:
227  /**
228  * Default constructor
229  * @param size_t size (default = 0)
230  */
231  CVector(size_t size = 0) :
232  CVectorCore< CType >(0, NULL)
233  {
234  resize(size);
235  }
236 
237  /**
238  * Copy constructor
239  * @param const CVector <CType> & src
240  */
241  CVector(const CVector <CType> & src):
242  CVectorCore< CType >(0, NULL)
243  {
244  copy(src);
245  }
246 
247  /**
248  * Destructor.
249  */
251  {
252  if (CVectorCore< CType >::mVector != NULL)
254  }
255 
256  /**
257  * Assignment operator
258  * @param const CVectorCore <CType> & rhs
259  * @return CVector <CType> & lhs
260  */
262  {
263  copy(rhs);
264 
265  return * this;
266  }
267 
268  /**
269  * Assignment operator
270  * @param const CVector <CType> & rhs
271  * @return CVector <CType> & lhs
272  */
274  {
275  copy(rhs);
276 
277  return * this;
278  }
279 
280  /**
281  * Assignment operator
282  * @param const CType & value
283  */
284  CVector< CType> & operator = (const CType & value)
285  {
286  size_t i;
287  CType * tmp = CVectorCore< CType >::mVector;
288 
289  for (i = 0; i < CVectorCore< CType >::mSize; i++, tmp++)
290  {
291  *tmp = value;
292  }
293 
294  return *this;
295  }
296 
297  /**
298  * Resize the vector. The previous content is lost
299  * @param size_t size
300  */
301  void resize(size_t size, const bool & copy = false)
302  {
303  //TODO: maybe we should only resize if the vector gets bigger
304  //or much smaller?
305  if (size == CVectorCore< CType >::mSize) return;
306 
307  size_t OldSize = CVectorCore< CType >::mSize;
308  CType * OldVector = CVectorCore< CType >::mVector;
309 
310  //TODO: maybe we should only resize if the vector gets bigger
311  //or much smaller?
314 
316  {
317  try
318  {
319  // We need to detect size_t overflow
321  {
323  }
324  else
325  {
327  }
328  }
329 
330  catch (...)
331  {
334  }
335  }
336 
337  if (copy &&
339  OldVector != NULL)
340  {
341  memcpy((void *) CVectorCore< CType >::mVector,
342  (void *) OldVector,
343  std::min(CVectorCore< CType >::mSize, OldSize) * sizeof(CType));
344  }
345 
346  if (OldVector != NULL)
347  {
348  delete [] OldVector;
349  }
350 
351  // Check if allocation failed
352  if (CVectorCore< CType >::mVector == NULL &&
353  size > 0)
354  {
355  CCopasiMessage(CCopasiMessage::EXCEPTION, MCopasiBase + 1, size * sizeof(CType));
356  }
357  }
358 
359 protected:
360  void copy(const CVectorCore <CType> & rhs)
361  {
362  if (this != &rhs)
363  {
364  if (CVectorCore< CType >::mSize != rhs.size())
365  {
366  resize(rhs.size());
367  }
368 
370  {
371  memcpy((void *) CVectorCore< CType >::mVector,
372  (void *) rhs.array(),
373  CVectorCore< CType >::mSize * sizeof(CType));
374  }
375  }
376  }
377 };
378 
379 template <class CType>
380 std::ostream &operator<<(std::ostream &os, const CVectorCore< CType > & A)
381 {
382  os << "(\t";
383 
384  if (A.mSize)
385  {
386  size_t i;
387  CType * tmp = A.mVector;
388  os << *(tmp++);
389 
390  for (i = 1; i < A.mSize; i++)
391  os << "\t" << *(tmp++);
392  }
393 
394  os << "\t)";
395  return os;
396 }
397 
398 #endif // COPASI_CVector
CType * mVector
Definition: CVector.h:49
friend std::ostream & operator<<(std::ostream &os, const CVectorCore< CType > &A)
Definition: CVector.h:380
const CType * array() const
Definition: CVector.h:146
CVectorCore(const size_t &size=0, CType *vector=NULL)
Definition: CVector.h:58
size_t mSize
Definition: CVector.h:44
CType & operator[](const size_t &row)
Definition: CVector.h:107
CVectorCore(const CVectorCore< CType > &src)
Definition: CVector.h:68
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
~CVectorCore()
Definition: CVector.h:76
CType & operator()(const size_t &row)
Definition: CVector.h:123
~CVector()
Definition: CVector.h:250
CVectorCore< CType > & operator=(const CType &value)
Definition: CVector.h:83
CVector< CType > & operator=(const CVectorCore< CType > &rhs)
Definition: CVector.h:261
#define MCopasiBase
const CType & operator[](const size_t &row) const
Definition: CVector.h:115
CVector(const CVector< CType > &src)
Definition: CVector.h:241
size_t size() const
Definition: CVector.h:100
const CType & operator()(const size_t &row) const
Definition: CVector.h:131
#define C_FLOAT64
Definition: copasi.h:92
CType * array()
Definition: CVector.h:139
void copy(const CVectorCore< CType > &rhs)
Definition: CVector.h:360
CVector(size_t size=0)
Definition: CVector.h:231
#define min(a, b)
Definition: f2c.h:175
CType elementType
Definition: CVector.h:37
bool applyPivot(const CVectorCore< size_t > &pivot)
Definition: CVector.h:172
#define max(a, b)
Definition: f2c.h:176