1 #if !defined  HAVE_COMBINATION_REC_H__
2 #define       HAVE_COMBINATION_REC_H__
3 // This file is part of the FXT library.
4 // Copyright (C) 2010, 2012, 2013, 2014, 2018, 2019 Joerg Arndt
5 // License: GNU General Public License version 3 or later,
6 // see the file COPYING.txt in the main directory.
7 
8 
9 #include "fxttypes.h"
10 #include "comb/comb-print.h"
11 
12 
13 class comb_rec
14 // Combinations in lexicographic, Gray code,
15 // complemented enup, and complemented Eades-McKay order.
16 // Recursive algorithm.
17 {
18 public:
19     ulong n_, k_;  // (n choose k)
20     ulong *rv_;   //  combination: k elements 0<=x[j]<k in increasing order
21     // == Record of Visits in graph
22     ulong rq_;   // condition that determines the order:
23     // 0 ==> lexicographic order
24     // 1 ==> Gray code
25     // 2 ==> complemented enup order
26     // 3 ==> complemented Eades-McKay sequence
27     ulong nq_;   // whether to reverse order
28     ulong ct_;   // count combinations
29     ulong rct_;  // count recursions (==work)
30     void (*visit_)(const comb_rec &);  // function to call with each combination
31 
32     comb_rec(const comb_rec&) = delete;
33     comb_rec & operator = (const comb_rec&) = delete;
34 
35 public:
comb_rec(ulong n,ulong k)36     explicit comb_rec(ulong n, ulong k)
37     {
38         n_ = n;
39         k_ = k;
40         rv_ = new ulong[k_+1];
41         ++rv_;
42         rv_[-1] = -1UL;
43     }
44 
~comb_rec()45     ~comb_rec()
46     {
47         --rv_;
48         delete [] rv_;
49     }
50 
51     void generate(void (*visit)(const comb_rec &), ulong rq, ulong nq=0)
52     {
53         visit_ = visit;
54         rq_ = rq;
55         nq_ = nq;
56         ct_ = 0;
57         rct_ = 0;
58         next_rec(0);
59     }
60 
61     void print_set(const char *bla=nullptr)  const
62     { ::print_set(bla, rv_, k_); }
63 
64     void print_deltaset(const char *bla=nullptr)  const
65     { print_set_as_deltaset(bla, rv_, k_, n_); }
66 
67 private:
68     void next_rec(ulong d);
69 };
70 // -------------------------
71 
72 
73 #endif  // !defined HAVE_COMBINATION_REC_H__
74