test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
combination.h
Go to the documentation of this file.
1 //=======================================================
2 // combination.h
3 // Description : Template class to find combinations
4 //=======================================================
5 // Copyright 2003 - 2006 Wong Shao Voon
6 // No warranty, implied or expressed, is included.
7 // Author is not liable for any type of loss through
8 // the use of this source code. Use it at your own risk!
9 //=======================================================
10 
11 
12 #ifndef __COMBINATION_H__
13 #define __COMBINATION_H__
14 
15 
16 namespace stdcomb
17 {
18 
19 // Non recursive template function
20 template <class BidIt>
21 
22 inline bool next_combination(BidIt n_begin, BidIt n_end,
23 BidIt r_begin, BidIt r_end)
24 {
25 
26  bool boolmarked=false;
27  BidIt r_marked;
28 
29  BidIt n_it1=n_end;
30  --n_it1;
31 
32 
33  BidIt tmp_r_end=r_end;
34  --tmp_r_end;
35 
36  for(BidIt r_it1=tmp_r_end; r_it1!=r_begin || r_it1==r_begin; --r_it1,--n_it1)
37  {
38  if(*r_it1==*n_it1 )
39  {
40  if(r_it1!=r_begin) //to ensure not at the start of r sequence
41  {
42  boolmarked=true;
43  r_marked=(--r_it1);
44  ++r_it1;//add it back again
45  continue;
46  }
47  else // it means it is at the start the sequence, so return false
48  return false;
49  }
50  else //if(*r_it1!=*n_it1 )
51  {
52  //marked code
53  if(boolmarked==true)
54  {
55  //for loop to find which marked is in the first sequence
56  BidIt n_marked;//mark in first sequence
57  for (BidIt n_it2=n_begin;n_it2!=n_end;++n_it2)
58  if(*r_marked==*n_it2) {n_marked=n_it2;break;}
59 
60 
61  BidIt n_it3=++n_marked;
62  for (BidIt r_it2=r_marked;r_it2!=r_end;++r_it2,++n_it3)
63  {
64  *r_it2=*n_it3;
65  }
66  return true;
67  }
68  for(BidIt n_it4=n_begin; n_it4!=n_end; ++n_it4)
69  if(*r_it1==*n_it4)
70  {
71  *r_it1=*(++n_it4);
72  return true;
73  }
74  }
75  }
76 
77  return true;//will never reach here
78 }
79 
80 // Non recursive template function with Pred
81 template <class BidIt, class Prediate>
82 
83 inline bool next_combination(
84  BidIt n_begin,
85  BidIt n_end,
86  BidIt r_begin,
87  BidIt r_end,
88  Prediate Equal)
89 {
90 
91  bool boolmarked=false;
92  BidIt r_marked;
93 
94  BidIt n_it1=n_end;
95  --n_it1;
96 
97 
98  BidIt tmp_r_end=r_end;
99  --tmp_r_end;
100 
101  for(BidIt r_it1=tmp_r_end; r_it1!=r_begin || r_it1==r_begin; --r_it1,--n_it1)
102  {
103  if( Equal( *r_it1, *n_it1) )
104  {
105  if(r_it1!=r_begin) //to ensure not at the start of r sequence
106  {
107  boolmarked=true;
108  r_marked=(--r_it1);
109  ++r_it1;//add it back again
110  continue;
111  }
112  else // it means it is at the start the sequence, so return false
113  return false;
114  }
115  else //if(*r_it1!=*n_it1 )
116  {
117  //marked code
118  if(boolmarked==true)
119  {
120  //for loop to find which marked is in the first sequence
121  BidIt n_marked;//mark in first sequence
122  for (BidIt n_it2=n_begin;n_it2!=n_end;++n_it2)
123  if( Equal( *r_marked, *n_it2) ) {n_marked=n_it2;break;}
124 
125 
126  BidIt n_it3=++n_marked;
127  for (BidIt r_it2=r_marked;r_it2!=r_end;++r_it2,++n_it3)
128  {
129  *r_it2=*n_it3;
130  }
131  return true;
132  }
133  for(BidIt n_it4=n_begin; n_it4!=n_end; ++n_it4)
134  if( Equal(*r_it1, *n_it4) )
135  {
136  *r_it1=*(++n_it4);
137  return true;
138  }
139  }
140  }
141 
142  return true;//will never reach here
143 }
144 
145 
146 // Non recursive template function
147 template <class BidIt>
148 
149 inline bool prev_combination(BidIt n_begin, BidIt n_end,
150 BidIt r_begin, BidIt r_end)
151 {
152 
153  BidIt marked;//for r
154  BidIt r_marked;
155  BidIt n_marked;
156 
157 
158  BidIt tmp_n_end=n_end;
159  --tmp_n_end;
160 
161  BidIt r_it1=r_end;
162  --r_it1;
163 
164  for(BidIt n_it1=tmp_n_end; n_it1!=n_begin || n_it1==n_begin ; --n_it1)
165  {
166  if(*r_it1==*n_it1)
167  {
168  r_marked=r_it1;
169  n_marked=n_it1;
170  break;
171  }
172  }
173 
174  BidIt n_it2=n_marked;
175 
176 
177  BidIt tmp_r_end=r_end;
178  --tmp_r_end;
179 
180  for(BidIt r_it2=r_marked; r_it2!=r_begin || r_it2==r_begin; --r_it2,--n_it2)
181  {
182  if(*r_it2==*n_it2 )
183  {
184  if(r_it2==r_begin&& !(*r_it2==*n_begin) )
185  {
186  for(BidIt n_it3=n_begin;n_it3!=n_end;++n_it3)
187  {
188  if(*r_it2==*n_it3)
189  {
190  marked=r_it2;
191  *r_it2=*(--n_it3);
192 
193  BidIt n_it4=n_end;
194  --n_it4;
195  for(BidIt r_it3=tmp_r_end; (r_it3!=r_begin || r_it3==r_begin) &&r_it3!=marked; --r_it3,--n_it4)
196  {
197  *r_it3=*n_it4;
198  }
199  return true;
200  }
201  }
202  }
203  else if(r_it2==r_begin&&*r_it2==*n_begin)
204  {
205  return false;//no more previous combination;
206  }
207  }
208  else //if(*r_it2!=*n_it2 )
209  {
210  ++r_it2;
211  marked=r_it2;
212  for(BidIt n_it5=n_begin;n_it5!=n_end;++n_it5)
213  {
214  if(*r_it2==*n_it5)
215  {
216  *r_it2=*(--n_it5);
217 
218  BidIt n_it6=n_end;
219  --n_it6;
220  for(BidIt r_it4=tmp_r_end; (r_it4!=r_begin || r_it4==r_begin) &&r_it4!=marked; --r_it4,--n_it6)
221  {
222  *r_it4=*n_it6;
223  }
224  return true;
225  }
226  }
227  }
228  }
229  return false;//Will never reach here, unless error
230 }
231 
232 
233 // Non recursive template function with Pred
234 template <class BidIt, class Prediate>
235 
236 inline bool prev_combination(
237  BidIt n_begin,
238  BidIt n_end,
239  BidIt r_begin,
240  BidIt r_end,
241  Prediate Equal)
242 {
243 
244  BidIt marked;//for r
245  BidIt r_marked;
246  BidIt n_marked;
247 
248 
249  BidIt tmp_n_end=n_end;
250  --tmp_n_end;
251 
252  BidIt r_it1=r_end;
253  --r_it1;
254 
255  for(BidIt n_it1=tmp_n_end; n_it1!=n_begin || n_it1==n_begin ; --n_it1)
256  {
257  if( Equal(*r_it1, *n_it1) )
258  {
259  r_marked=r_it1;
260  n_marked=n_it1;
261  break;
262  }
263  }
264 
265  BidIt n_it2=n_marked;
266 
267 
268  BidIt tmp_r_end=r_end;
269  --tmp_r_end;
270 
271  for(BidIt r_it2=r_marked; r_it2!=r_begin || r_it2==r_begin; --r_it2,--n_it2)
272  {
273  if( Equal(*r_it2, *n_it2) )
274  {
275  if(r_it2==r_begin&& !Equal(*r_it2, *n_begin) )
276  {
277  for(BidIt n_it3=n_begin;n_it3!=n_end;++n_it3)
278  {
279  if(Equal(*r_it2, *n_it3))
280  {
281  marked=r_it2;
282  *r_it2=*(--n_it3);
283 
284  BidIt n_it4=n_end;
285  --n_it4;
286  for(BidIt r_it3=tmp_r_end; (r_it3!=r_begin || r_it3==r_begin) &&r_it3!=marked; --r_it3,--n_it4)
287  {
288  *r_it3=*n_it4;
289  }
290  return true;
291  }
292  }
293  }
294  else if(r_it2==r_begin&&Equal(*r_it2, *n_begin))
295  {
296  return false;//no more previous combination;
297  }
298  }
299  else //if(*r_it2!=*n_it2 )
300  {
301  ++r_it2;
302  marked=r_it2;
303  for(BidIt n_it5=n_begin;n_it5!=n_end;++n_it5)
304  {
305  if(Equal(*r_it2, *n_it5))
306  {
307  *r_it2=*(--n_it5);
308 
309  BidIt n_it6=n_end;
310  --n_it6;
311  for(BidIt r_it4=tmp_r_end; (r_it4!=r_begin || r_it4==r_begin) &&r_it4!=marked; --r_it4,--n_it6)
312  {
313  *r_it4=*n_it6;
314  }
315  return true;
316  }
317  }
318  }
319  }
320  return false;//Will never reach here, unless error
321 }
322 
323 
324 // Recursive template function
325 template <class RanIt, class Func>
326 
327 void recursive_combination(RanIt nbegin, RanIt nend, int n_column,
328  RanIt rbegin, RanIt rend, int r_column,int loop, Func func)
329 {
330 
331  int r_size=rend-rbegin;
332 
333 
334  int localloop=loop;
335  int local_n_column=n_column;
336 
337  //A different combination is out
338  if(r_column>(r_size-1))
339  {
340  func(rbegin,rend);
341  return;
342  }
344 
345  for(int i=0;i<=loop;++i)
346  {
347 
348  RanIt it1=rbegin;
349  for(int cnt=0;cnt<r_column;++cnt)
350  {
351  ++it1;
352  }
353 
354  RanIt it2=nbegin;
355  for(int cnt2=0;cnt2<n_column+i;++cnt2)
356  {
357  ++it2;
358  }
359 
360  *it1=*it2;
361 
362  ++local_n_column;
363 
364  recursive_combination(nbegin,nend,local_n_column,
365  rbegin,rend,r_column+1,localloop,func);
366  --localloop;
367  }
368 
369 }
370 
371 }
372 
373 #endif
int i
Definition: DBlmapReader.cc:9
void recursive_combination(RanIt nbegin, RanIt nend, int n_column, RanIt rbegin, RanIt rend, int r_column, int loop, Func func)
Definition: combination.h:327
int loop
CMSSW
bool prev_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end)
Definition: combination.h:149
bool next_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end)
Definition: combination.h:22