ATLAS Offline Software
CombinationsGen.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef TRIGHLTHETHYPO_COMBINATIONSGEN_H
6 #define TRIGHLTHETHYPO_COMBINATIONSGEN_H
7 
8 #include <algorithm>
9 #include <string>
10 #include <vector>
11 #include <sstream>
12 
13 /*
14 Combinations generator. Given n, k > 0, n>= k, next() returns the
15 next combination in a pair of std::array<int, k>, bool, where bool
16 = false means the array is not a valid combination (ie the iteration
17 has terminated)
18 */
19 
21  public:
22  CombinationsGen(unsigned int n, unsigned int k):
23  m_more{true}, m_invalid{false}{
24 
25  if (k<=n){
26  m_bitmask = std::string(k, 1);
27  m_bitmask.resize(n, 0);
28  } else {
29  m_invalid = true;
30  }
31  }
32 
33  std::pair<std::vector<unsigned int>, bool> next() {
34 
35  std::vector<unsigned int> comb;
36  if (m_invalid){return {comb, false};}
37 
38  if(not m_more){return {comb, false};}
39 
40  for(std::size_t i = 0; i < m_bitmask.size(); ++i){
41  if(m_bitmask[i]){comb.push_back(i);}
42  }
43  m_more = std::prev_permutation(m_bitmask.begin(), m_bitmask.end());
44  return {comb, true};
45  }
46 
47 private:
48  bool m_more;
49  bool m_invalid;
50  std::string m_bitmask;
51 };
52 #endif
CombinationsGen::CombinationsGen
CombinationsGen(unsigned int n, unsigned int k)
Definition: CombinationsGen.h:22
CombinationsGen
Definition: CombinationsGen.h:20
CombinationsGen::m_more
bool m_more
Definition: CombinationsGen.h:48
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
CombinationsGen::m_invalid
bool m_invalid
Definition: CombinationsGen.h:49
CombinationsGen::next
std::pair< std::vector< unsigned int >, bool > next()
Definition: CombinationsGen.h:33
CombinationsGen::m_bitmask
std::string m_bitmask
Definition: CombinationsGen.h:50
fitman.k
k
Definition: fitman.py:528