COPASI API  4.16.103
Classes | Functions
CSort.h File Reference
#include <cmath>
#include <algorithm>
#include <functional>
#include "utilities/CVector.h"
Include dependency graph for CSort.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  CompareDefault< RandomAccessIterator >
 
class  CompareDoubleWithNaN
 
class  FSwapBase< IndexType, ReturnType >
 
class  FSwapClass< ClassType, IndexType, ReturnType >
 

Functions

template<typename SwapMethod >
bool applyPartialPivot (const CVector< size_t > &pivot, const size_t &ordered, SwapMethod swap)
 
template<typename SwapMethod >
bool applyPivot (const CVector< size_t > &pivot, SwapMethod swap)
 
template<typename RandomAccessIterator >
void partialSortWithPivot (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, CVector< size_t > &pivot)
 
template<typename RandomAccessIterator , typename LessThanCompare >
void partialSortWithPivot (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, LessThanCompare compare, CVector< size_t > &pivot)
 
template<typename RandomAccessIterator >
void sortWithPivot (RandomAccessIterator first, RandomAccessIterator last, CVector< size_t > &pivot)
 
template<typename RandomAccessIterator , typename LessThanCompare >
void sortWithPivot (RandomAccessIterator first, RandomAccessIterator last, LessThanCompare compare, CVector< size_t > &pivot)
 

Function Documentation

template<typename SwapMethod >
bool applyPartialPivot ( const CVector< size_t > &  pivot,
const size_t &  ordered,
SwapMethod  swap 
)

Partial reordering of the first 'ordered' elements according to the provided pivots. The swap method must be of the form: ReturnType operator() (size_t to, size_t from) where the ReturnType is not used and therefore arbitrary. Objects of type FSwapBase are suitable candidates.

Parameters
constCVector<size_t> & pivot
constsize_t & ordered
SwapMethodswap
Returns
bool success

Definition at line 375 of file CSort.h.

References CVectorCore< CType >::size().

Referenced by COptMethodEP::select(), and COptMethodGA::select().

378 {
379  CVector< bool > Applied(pivot.size());
380  Applied = false;
381 
382  size_t i;
383  size_t to;
384  size_t from;
385 
386  for (i = 0; i < ordered; i++)
387  if (!Applied[i])
388  {
389  to = i;
390  from = pivot[to];
391 
392  while (from != i)
393  {
394  if (to < ordered || from < ordered)
395  {
396  swap(to, from);
397  Applied[to] = true;
398 
399  to = from;
400  }
401 
402  from = pivot[from];
403  }
404 
405  Applied[to] = true;
406  }
407 
408  return true;
409 }
size_t size() const
Definition: CVector.h:100
template<typename SwapMethod >
bool applyPivot ( const CVector< size_t > &  pivot,
SwapMethod  swap 
)

Reorder the elements according to the provided pivots The swap method must be of the form: ReturnType operator() (size_t to, size_t from) where the ReturnType is not used and therefore arbitrary. Objects of type FSwapBase are suitable candidates.

Parameters
constCVector<size_t> & pivot
SwapMethodswap
Returns
bool success

Definition at line 331 of file CSort.h.

References CVectorCore< CType >::size().

333 {
334  CVector< bool > Applied(pivot.size());
335  Applied = false;
336 
337  size_t i, imax = pivot.size();
338  size_t to;
339  size_t from;
340 
341  for (i = 0; i < imax; i++)
342  if (!Applied[i])
343  {
344  to = i;
345  from = pivot[to];
346 
347  while (from != i)
348  {
349  swap(to, from);
350  Applied[to] = true;
351 
352  to = from;
353  from = pivot[to];
354  }
355 
356  Applied[to] = true;
357  }
358 
359  return true;
360 }
size_t size() const
Definition: CVector.h:100
template<typename RandomAccessIterator >
void partialSortWithPivot ( RandomAccessIterator  first,
RandomAccessIterator  middle,
RandomAccessIterator  last,
CVector< size_t > &  pivot 
)

Partial sorting method returning a pivot vector instead of performing the sort. The underlying sorting method is std::partial sort with the operator < used for * comparison .

Parameters
RandomAccessIteratorfirst
RandomAccessIteratormiddle
RandomAccessIteratorlast
CVector<size_t>& pivot

Definition at line 149 of file CSort.h.

Referenced by COptMethodEP::select(), and COptMethodGA::select().

153 {
155 
156  partialSortWithPivot(first, middle, last, Compare, pivot);
157 
158  return;
159 }
void partialSortWithPivot(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, CVector< size_t > &pivot)
Definition: CSort.h:149
template<typename RandomAccessIterator , typename LessThanCompare >
void partialSortWithPivot ( RandomAccessIterator  first,
RandomAccessIterator  middle,
RandomAccessIterator  last,
LessThanCompare  compare,
CVector< size_t > &  pivot 
)

Partial sorting method returning a pivot vector instead of performing the sort. The underlying sorting method is std::partial_sort with the specified compare method used for comparison .

Parameters
RandomAccessIteratorfirst
RandomAccessIteratormiddle
RandomAccessIteratorlast
LessThanComparemethod
CVector<size_t>& pivot

Definition at line 172 of file CSort.h.

References CVectorCore< CType >::array(), and CVector< CType >::resize().

177 {
178  assert(first < middle && middle <= last);
179 
180  // Initialize the two column array to be sorted
182  ToBeSorted.resize(last - first);
183 
184  RandomAccessIterator it;
185  size_t i;
186 
187  typename std::pair<RandomAccessIterator, size_t> * itToBeSorted;
188 
189  for (it = first, i = 0, itToBeSorted = ToBeSorted.array();
190  it != last;
191  ++it, ++i, ++itToBeSorted)
192  {
193  itToBeSorted->first = it;
194  itToBeSorted->second = i;
195  }
196 
197  itToBeSorted = ToBeSorted.array();
198 
199  std::partial_sort(itToBeSorted,
200  itToBeSorted + (middle - first),
201  itToBeSorted + (last - first),
202  compare);
203 
204  // Copy the resulting pivots to the pivot vector.
205  pivot.resize(last - first);
206  CVector<size_t>::elementType *itPivot = pivot.array();
207  CVector<size_t>::elementType *endPivot = itPivot + (last - first);
208 
209  for (; itPivot != endPivot; ++itToBeSorted, ++itPivot)
210  *itPivot = itToBeSorted->second;
211 
212  return;
213 }
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
CType * array()
Definition: CVector.h:139
template<typename RandomAccessIterator >
void sortWithPivot ( RandomAccessIterator  first,
RandomAccessIterator  last,
CVector< size_t > &  pivot 
)

Sorting method returning a pivot vector instead of performing the sort. The underlying sorting method is std::sort with the operator < used for comparison .

Parameters
RandomAccessIteratorfirst
RandomAccessIteratorlast
CVector<size_t>& pivot

Definition at line 77 of file CSort.h.

Referenced by CExperiment::read(), and CEigen::stabilityAnalysis().

80 {
82 
83  sortWithPivot(first, last, Compare, pivot);
84 
85  return;
86 }
void sortWithPivot(RandomAccessIterator first, RandomAccessIterator last, CVector< size_t > &pivot)
Definition: CSort.h:77
template<typename RandomAccessIterator , typename LessThanCompare >
void sortWithPivot ( RandomAccessIterator  first,
RandomAccessIterator  last,
LessThanCompare  compare,
CVector< size_t > &  pivot 
)

Sorting method returning a pivot vector instead of performing the sort. The underlying sorting method is std::sort with the specified compare method used for comparison .

Parameters
RandomAccessIteratorfirst
RandomAccessIteratorlast
LessThanComparemethod
CVector<size_t>& pivot

Definition at line 98 of file CSort.h.

References CVectorCore< CType >::array(), and CVector< CType >::resize().

102 {
103  assert(first < last);
104 
105  // Initialize the two column array to be sorted
107  ToBeSorted.resize(last - first);
108 
109  RandomAccessIterator it;
110  size_t i;
111 
112  typename std::pair<RandomAccessIterator, size_t> * itToBeSorted;
113 
114  for (it = first, i = 0, itToBeSorted = ToBeSorted.array();
115  it != last;
116  ++it, ++i, ++itToBeSorted)
117  {
118  itToBeSorted->first = it;
119  itToBeSorted->second = i;
120  }
121 
122  itToBeSorted = ToBeSorted.array();
123 
124  std::sort(itToBeSorted,
125  itToBeSorted + (last - first),
126  compare);
127 
128  // Copy the resulting pivots to the pivot vector.
129  pivot.resize(last - first);
130  CVector<size_t>::elementType *itPivot = pivot.array();
131  CVector<size_t>::elementType *endPivot = itPivot + (last - first);
132 
133  for (; itPivot != endPivot; ++itToBeSorted, ++itPivot)
134  *itPivot = itToBeSorted->second;
135 
136  return;
137 }
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
CType * array()
Definition: CVector.h:139