NeoPZ
tpzpermutation.cpp
Go to the documentation of this file.
1 
6 #include "tpzpermutation.h"
7 #include "TPZStream.h"
8 #include "Hash/TPZHash.h"
9 
10 TPZPermutation::TPZPermutation(int n) : fCounter(n,0), fOrder(n,-1)
11 {
12  int i;
13  for(i=0; i<n; i++) fOrder[i] = i;
14 }
15 
17 {
18 }
19 
21 {
22  fCounter = copy.fCounter;
23  fOrder = copy.fOrder;
24  return *this;
25 }
26 
28 {
29 }
30 
32 void TPZPermutation::Permute(const TPZVec<int> &in, TPZVec<int> &out) const
33 {
34  int i,n=fCounter.NElements();
35  for(i=0; i<n; i++) out[fOrder[i]] = in[i];
36 }
38 {
39  int i,n=fCounter.NElements();
40  for(i=0; i<n; i++) out[fOrder[i]] = in[i];
41 }
42 
44 {
45  int n = fCounter.NElements();
46  int i = n-2;
47  while(i >= 0 && ((++fCounter[i]) %= n-i) == 0) i--;
48  fOrder.Fill(-1);
49  int count;
50  for(i=0; i<n; i++)
51  {
52  count = 0;
53  int index = 0;
54  while(fCounter[i] > count || fOrder[index] != -1)
55  {
56  if(fOrder[index] == -1) count++;
57  index++;
58  }
59  fOrder[index] = i;
60  }
61 }
62 
64 {
65  int nel = fCounter.NElements();
66  int in;
67  for(in=0; in<nel; in++) if(fCounter[in] != 0) return false;
68  return true;
69 }
70 
72  return Hash("TPZPermutation");
73 }
74 
75 void TPZPermutation::Read(TPZStream& buf, void* context) {
76  buf.Read(fCounter);
77  buf.Read(fOrder);
78 }
79 
80 void TPZPermutation::Write(TPZStream& buf, int withclassid) const {
81  buf.Write(fCounter);
82  buf.Write(fOrder);
83 }
void Permute(const TPZVec< int > &in, TPZVec< int > &out) const
Applies the current permutation on the vector in and produces the vector out.
void Read(TPZStream &buf, void *context) override
read objects from the stream
Contains declaration of the TPZPermutation class which generates all permutations of n values...
TPZManVector< int > fOrder
Variable which contains the current permutations.
TPZPermutation(int n)
Constructor with number of permutations.
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
TPZManVector< int > fCounter
Variable which represents a counter for the permutations.
void operator++()
Operator increment.
void Fill(const T &copy, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
Definition: pzvec.h:460
TPZPermutation & operator=(const TPZPermutation &copy)
Operator attribution.
~TPZPermutation()
Default destructor.
Contains declaration of the abstract TPZStream class. TPZStream defines the interface for saving and ...
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
int ClassId() const override
Define the class id associated with the class.
This class generates all permutations of n values. Utility.
virtual void Read(bool &val)
Definition: TPZStream.cpp:91