CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes
SequentialPartitionGenerator Class Reference

#include <SequentialPartitionGenerator.h>

Public Types

typedef std::vector< int > Partition
 

Public Member Functions

Partition next_partition ()
 
 SequentialPartitionGenerator (int n, int k, int pmin=1)
 
 SequentialPartitionGenerator (int n, int k, int pmin, int pmax)
 

Private Member Functions

bool first_part (Partition &p, int k, int n, int pmin, int pmax) const
 
bool next_part (Partition &p) const
 

Private Attributes

int n_first
 
int n_next
 
int the_k
 
int the_n
 
Partition the_part
 
int the_pmax
 
int the_pmin
 

Detailed Description

Class to compute partitions of size k of an integer n.

Definition at line 10 of file SequentialPartitionGenerator.h.

Member Typedef Documentation

typedef std::vector<int> SequentialPartitionGenerator::Partition

Definition at line 12 of file SequentialPartitionGenerator.h.

Constructor & Destructor Documentation

SequentialPartitionGenerator::SequentialPartitionGenerator ( int  n,
int  k,
int  pmin = 1 
)
SequentialPartitionGenerator::SequentialPartitionGenerator ( int  n,
int  k,
int  pmin,
int  pmax 
)

Member Function Documentation

bool SequentialPartitionGenerator::first_part ( SequentialPartitionGenerator::Partition p,
int  k,
int  n,
int  pmin,
int  pmax 
) const
private

Definition at line 20 of file SequentialPartitionGenerator.cc.

References popcon2dropbox::copy(), first_part(), mps_fire::i, min(), n_first, and createTree::pp.

Referenced by first_part(), and next_part().

22 {
23  n_first++;
24  bool done=false;
25  switch (k) {
26  case 1:
27  p[0]=std::min(pmax,n);
28  return (n<=pmax && p[0]>=pmin);
29  case 2:
30  for (int i=std::min(pmax,n-1);i>=pmin;i--) {
31  if ((done=(i>=n-i && n-i>=pmin))) {p[0]=i;p[1]=n-i;}
32  return done;
33  }
34  default:
35  Partition pp(p.begin()+1,p.end());
36  for (int i=std::min(pmax,n-k+1);i>=pmin;i--) {
37  p[0]=i;
38  done=this->first_part(pp,k-1,n-p[0],pmin,p[0]);
39  if (done) {copy(pp.begin(),pp.end(),p.begin()+1);}
40  return done;
41  }
42  }
43  return done;
44 }
Partition
Definition: HLTHPDFilter.cc:32
T min(T a, T b)
Definition: MathUtil.h:58
int k[5][pyjets_maxn]
bool first_part(Partition &p, int k, int n, int pmin, int pmax) const
bool SequentialPartitionGenerator::next_part ( SequentialPartitionGenerator::Partition p) const
private

Definition at line 46 of file SequentialPartitionGenerator.cc.

References popcon2dropbox::copy(), first_part(), mps_fire::i, gen::k, gen::n, n_next, createTree::pp, the_k, the_n, the_pmax, and the_pmin.

Referenced by next_partition().

48 {
49  n_next++;
50  bool done=false;
51  int k=p.size();
52  switch (k) {
53  case 0: // empty partition: first call to next_part, initialize
54  p.insert(p.begin(),the_k,0);
55  return this->first_part(p,the_k,the_n,the_pmin,the_pmax);
56  case 1:
57  return false;
58  default:
59  int n=0;
60  for (int i=0;i<k;i++) n=n+p[i];
62  done = (pp.size()>1 ? this->next_part(pp) : false);
63  if (done)
64  {
65  copy(pp.begin(),pp.end(),p.begin()+1);
66  } else {
67  done = (p[0]==1 ? false :
68  this->first_part(pp,k-1,n-p[0]+1,the_pmin,p[0]-1));
69  if (done) { --p[0];copy(pp.begin(),pp.end(),p.begin()+1); }
70  };
71  return done;
72  };
73  return done;
74 }
int k[5][pyjets_maxn]
bool first_part(Partition &p, int k, int n, int pmin, int pmax) const
SequentialPartitionGenerator::Partition SequentialPartitionGenerator::next_partition ( )

Get the next partition, in a well-defined series of partition

Definition at line 9 of file SequentialPartitionGenerator.cc.

References relativeConstraints::empty, next_part(), and the_part.

10 {
11  bool done=next_part(the_part);
12  if (done)
13  {
14  return the_part;
15  };
17  return empty;
18 }

Member Data Documentation

int SequentialPartitionGenerator::n_first
mutableprivate

Definition at line 34 of file SequentialPartitionGenerator.h.

Referenced by first_part().

int SequentialPartitionGenerator::n_next
mutableprivate

Definition at line 35 of file SequentialPartitionGenerator.h.

Referenced by next_part().

int SequentialPartitionGenerator::the_k
private

Definition at line 30 of file SequentialPartitionGenerator.h.

Referenced by next_part().

int SequentialPartitionGenerator::the_n
private

Definition at line 29 of file SequentialPartitionGenerator.h.

Referenced by next_part().

Partition SequentialPartitionGenerator::the_part
private

Definition at line 33 of file SequentialPartitionGenerator.h.

Referenced by next_partition().

int SequentialPartitionGenerator::the_pmax
private

Definition at line 32 of file SequentialPartitionGenerator.h.

Referenced by next_part().

int SequentialPartitionGenerator::the_pmin
private

Definition at line 31 of file SequentialPartitionGenerator.h.

Referenced by next_part().