COPASI API  4.16.103
CPermutation.cpp
Go to the documentation of this file.
1 // Copyright (C) 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 #include "copasi.h"
7 
8 #include "CPermutation.h"
9 #include "CRandom.h"
10 
11 // static
13 
15  mpRandom(NULL),
16  mVector(),
17  mpNext(NULL),
18  mpBeyond(NULL)
19 {};
20 
21 CPermutation::CPermutation(CRandom * pRandom, const size_t & size) :
22  mpRandom(pRandom),
23  mVector(size),
24  mpNext(NULL),
25  mpBeyond(NULL)
26 {
27  init();
28 };
29 
31  mpRandom(src.mpRandom),
32  mVector(src.mVector),
33  mpNext(NULL),
34  mpBeyond(NULL)
35 {
36  if (src.mpNext != NULL)
37  {
38  mpNext = mVector.array() + (src.mpNext - src.mVector.array());
40  }
41 };
42 
44 {}
45 
47 {
48  size_t Index = 0;
49  size_t * pIt = mVector.array();
50  size_t * pEnd = pIt + mVector.size();
51 
52  for (; pIt != pEnd; ++pIt, ++Index)
53  {
54  *pIt = Index;
55  }
56 
57  if (Index != 0)
58  {
59  mpNext = mVector.array();
60  mpBeyond = pEnd;
61  }
62 }
63 
64 void CPermutation::shuffle(const size_t & swaps)
65 {
66  if (mpRandom == NULL || mpNext == NULL) return;
67 
68  if (mVector.size() > 1)
69  {
70  unsigned C_INT32 max = mVector.size() - 1;
71  size_t Swaps = std::min(swaps, mVector.size());
72 
73  // We swap each element once.
74  size_t tmp;
75  size_t * pBegin = mVector.array();
76  size_t * pIt = pBegin;
77  size_t * pEnd = pIt + Swaps;
78  size_t * pTo;
79 
80  for (; pIt != pEnd; ++pIt)
81  {
82  pTo = pBegin + mpRandom->getRandomU(max);
83 
84  if (pTo != pIt)
85  {
86  tmp = *pTo;
87  *pTo = *pIt;
88  *pIt = tmp;
89  }
90  }
91  }
92 }
93 
94 const size_t & CPermutation::pick()
95 {
96  if (mpRandom == NULL || mpNext == NULL) return InvalidIndex;
97 
98  if (mVector.size() > 1)
99  {
101  }
102 
103  return *mpNext;
104 }
105 
106 const size_t & CPermutation::next()
107 {
108  if (mpRandom == NULL || mpNext == NULL) return InvalidIndex;
109 
110  mpNext++;
111 
112  if (mpNext >= mpBeyond)
113  {
114  mpNext -= mVector.size();
115  }
116 
117  return *mpNext;
118 }
size_t * mpBeyond
Definition: CPermutation.h:83
CVector< size_t > mVector
Definition: CPermutation.h:73
const size_t & next()
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_INT32
Definition: copasi.h:90
const size_t & pick()
virtual unsigned C_INT32 getRandomU()
Definition: CRandom.cpp:173
void shuffle(const size_t &swaps=C_INVALID_INDEX)
size_t size() const
Definition: CVector.h:100
static size_t InvalidIndex
Definition: CPermutation.h:16
CType * array()
Definition: CVector.h:139
size_t * mpNext
Definition: CPermutation.h:78
#define min(a, b)
Definition: f2c.h:175
CRandom * mpRandom
Definition: CPermutation.h:68
#define max(a, b)
Definition: f2c.h:176