ATLAS Offline Software
Loading...
Searching...
No Matches
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/*
14Combinations generator. Given n, k > 0, n>= k, next() returns the
15next 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
17has 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
47private:
48 bool m_more;
50 std::string m_bitmask;
51};
52#endif
CombinationsGen(unsigned int n, unsigned int k)
std::string m_bitmask
std::pair< std::vector< unsigned int >, bool > next()