CMS 3D CMS Logo

SequentialCombinationGenerator.h
Go to the documentation of this file.
1 #ifndef SequentialCombinationGenerator_H
2 #define SequentialCombinationGenerator_H
3 
5 #include <vector>
6 #include <list>
7 #include <algorithm>
8 #include <numeric>
9 #include <functional>
10 #include <iostream>
11 
21 template<class T> class SequentialCombinationGenerator {
22 public:
24  typedef std::vector<T> Collection;
25  typedef std::vector<Collection> Combination;
26  typedef std::vector<int> Vecint;
27 
29  the_part ( part ), the_k ( part.size() ),
30  the_n ( accumulate ( part.begin(), part.end(), 0 ) )
31  {
32  sort(the_part.begin(),the_part.end());
33  the_comb.reserve(the_n);
34  };
35 
39  Combination next_combination ( Collection& coll )
40  {
41  dbg=false;
42  Combination comb;
43  Vecint newcomb=next_combi(the_comb,the_n,the_part);
44  the_comb=newcomb;
45  if (newcomb.empty()) {
46  Combination empty;
47  return empty;
48  }
49  int i=0;
50  for (int j=0;j<the_k;j++)
51  {
52  Collection temp;
53  for (int l=0;l<the_part[j];l++)
54  {
55  temp.push_back(coll[the_comb[i]-1]);
56  i++;
57  }
58  comb.push_back(temp);
59  }
60  return comb;
61  };
62 
63 private:
64  Vecint next_combi( Vecint & cold, int n, const Partition & p )
65  {
66  Vecint empty;
67  if (cold.empty()) { // first entry, initialize
68  cold.reserve(n);
69  for (int i=0;i<n;cold.push_back(++i));
70  return cold;
71  }
72  int k=p.size();
73  if (k==1) return empty;
74  Vecint cnew(cold);
75  int n1=n-p[0];
76  Vecint cold1(cold.begin()+p[0],cold.end());
77  Vecint cnew1(n1);
78  Partition p1(p.begin()+1,p.end());
79  cnew1=next_combi(cold1,n1,p1);
80  if (!cnew1.empty()) {
81  copy(cnew1.begin(),cnew1.end(),cnew.begin()+p[0]);
82  return cnew;
83  }
84  Vecint cold2(cold.begin(),cold.begin()+p[0]);
85  Vecint cnew2(p[0]);
86  sort(cold.begin(),cold.end());
87  sort(cold2.begin(),cold2.end());
88  cnew2=next_subset(cold,cold2);
89  if (cnew2.empty()) return empty;
90  copy(cnew2.begin(),cnew2.begin()+p[0],cnew.begin());
91  Vecint ss(n1);
92  set_difference(cold.begin(),cold.end(),
93  cnew2.begin(),cnew2.end(),ss.begin());
94  int ip=p[0];
95  for (int i=1;i<k;i++) {
96  if (p[i]!=p[i-1]) {
97  copy(ss.begin(),ss.end(),&cnew[ip]);
98  return cnew;
99  }
100  int mincnew2=cnew2[0];
101  if (ss[n1-1]<mincnew2) return empty;
102  Vecint::iterator j1=find_if(ss.begin(),ss.end(), [&](auto c) { return c > mincnew2;});
103  if (ss.end()-j1 < p[i]) return empty;
104  Vecint sss(j1,ss.end());
105  for (int j=0;j<p[i];j++){cnew[ip+j]=cnew2[j]=sss[j];}
106  int n2=ss.size()-cnew2.size();
107  if (n2==0) return cnew;
108  Vecint s(n2);
109  set_difference(ss.begin(),ss.end(),cnew2.begin(),cnew2.end(),s.begin());
110  ss=s;
111  ip+=p[i];
112  }
113 
114  return empty;
115 
116  };
117 
118  Vecint next_subset(const Vecint& _g, const Vecint& _c)
119  {
120  Vecint g = _g;
121  Vecint c = _c;
122  Vecint empty;
123  int n=g.size();
124  int k=c.size();
125  typename Vecint::iterator ind;
126  for (int i=k-1;i>=0;i--) {
127  if (c[i]<g[n-k+i]) {
128 
129 
130 // ind=find(&g[0],&g[n-k+i],c[i])+1;
131 
132  Vecint::iterator g2 = g.begin();
133  advance(g2,n-k+i);
134 
135  ind=find(g.begin(),g2,c[i]);
136 
137  ind++;
138 
139 
140 
141  copy(ind,ind+k-i,&c[i]);
142  return c;
143  }
144  }
145  return empty;
146  };
147 
148  void vecprint(const Vecint & v) const
149  {
150  int n=v.size();
151  for (int i=0;i<n;std::cout << v[i++]);
152  std::cout << std::endl;
153  };
154 
155 private:
156  int the_n;
157  int the_k;
158  Partition the_part;
159  mutable Vecint the_comb;
160  mutable bool dbg;
161 };
162 
163 #endif
size
Write out results.
Vecint next_subset(const Vecint &_g, const Vecint &_c)
def copy(args, dbName)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
SequentialPartitionGenerator::Partition Partition
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
Combination next_combination(Collection &coll)
#define end
Definition: vmac.h:39
int k[5][pyjets_maxn]
JetCorrectorParametersCollection coll
Definition: classes.h:10
part
Definition: HCALResponse.h:20
#define begin
Definition: vmac.h:32
double p1[4]
Definition: TauolaWrapper.h:89
Vecint next_combi(Vecint &cold, int n, const Partition &p)