ATLAS Offline Software
Loading...
Searching...
No Matches
CombinationsGenerator Class Reference

generate all possible combinations of objects More...

#include <CombinationsGenerator.h>

Collaboration diagram for CombinationsGenerator:

Public Member Functions

 CombinationsGenerator (std::size_t n, std::size_t k)
std::vector< std::size_t > get () const
bool bump ()

Private Attributes

bool m_invalid
std::string m_bitmask
std::size_t m_N
std::size_t m_K
std::vector< std::size_t > m_NequalsKvec

Friends

std::ostream & operator<< (std::ostream &os, const CombinationsGenerator &cg)

Detailed Description

generate all possible combinations of objects

Warning
The class is not making any assumption as if the this are combinations with objects repeated, i.e. it works on set of indices.

Constructor & Destructor Documentation

◆ CombinationsGenerator()

CombinationsGenerator::CombinationsGenerator ( std::size_t n,
std::size_t k )
inline

Definition at line 29 of file CombinationsGenerator.h.

29 :
30 m_invalid{false}, m_N{n}, m_K(k){
31
32 // if n==k, std::prev_permutations never returns false,
33 // so treat as a special case
34 if (m_N == 0 or m_K > m_N) {
35 m_invalid = true;
36 } else if (m_N==m_K) {
37 m_NequalsKvec.reserve(m_K);
38 for(std::size_t i = 0u; i != m_K; ++i){
39 m_NequalsKvec.push_back(i);
40 }
41 } else if (m_K < m_N){
42 m_bitmask = std::string(m_K, 1);
43 m_bitmask.resize(m_N, 0);
44 } else {
45 m_invalid = true;
46 }
47 }
std::vector< std::size_t > m_NequalsKvec

Member Function Documentation

◆ bump()

bool CombinationsGenerator::bump ( )
inline

Definition at line 65 of file CombinationsGenerator.h.

65 {
66 // returns true if have cycled
67
68 if (m_K < m_N and m_K > 0) {
69 return ! std::prev_permutation(m_bitmask.begin(), m_bitmask.end());
70 }
71 return true;
72 }

◆ get()

std::vector< std::size_t > CombinationsGenerator::get ( ) const
inline

Definition at line 50 of file CombinationsGenerator.h.

50 {
51
52 if (m_K < m_N and m_K > 0) {
53 std::vector<std::size_t> comb;
54 for(std::size_t i = 0; i < m_bitmask.size(); ++i){
55 if(m_bitmask[i]){comb.push_back(i);}
56 }
57 return comb;
58 }
59
60 if(m_K == m_N) {return m_NequalsKvec;}
61
62 return std::vector<std::size_t>();
63 }

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const CombinationsGenerator & cg )
friend

Definition at line 7 of file CombinationsGenerator.cxx.

7 {
8 os << "CombinationsGenerator m_invalid " <<std::boolalpha << cg.m_invalid
9 << " bitmask len " << cg.m_bitmask.size()
10 << " m_bitmask: ";
11 for (const auto& c : cg.m_bitmask) {
12 if (c == 0) {
13 os << 0 << " ";
14 } else if (c == 1) {
15 os << 1 << " ";
16 } else {
17 os << '?' << " ";
18 }
19 }
20 os << " m_N " << cg.m_N << " m_K " << cg.m_K;
21
22 os << '\n';
23 return os;
24}

Member Data Documentation

◆ m_bitmask

std::string CombinationsGenerator::m_bitmask
private

Definition at line 77 of file CombinationsGenerator.h.

◆ m_invalid

bool CombinationsGenerator::m_invalid
private

Definition at line 76 of file CombinationsGenerator.h.

◆ m_K

std::size_t CombinationsGenerator::m_K
private

Definition at line 80 of file CombinationsGenerator.h.

◆ m_N

std::size_t CombinationsGenerator::m_N
private

Definition at line 79 of file CombinationsGenerator.h.

◆ m_NequalsKvec

std::vector<std::size_t> CombinationsGenerator::m_NequalsKvec
private

Definition at line 81 of file CombinationsGenerator.h.


The documentation for this class was generated from the following files: