Jpp
Functions
JPermutation.cc File Reference
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <algorithm>
#include "JTools/JPermutation.hh"
#include "JMath/JMathToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test permutations (JTOOLS::next_permutation).

Author
mdejong

Definition in file JPermutation.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 22 of file JPermutation.cc.

23 {
24  using namespace std;
25 
26  vector<int> buffer;
27  unsigned int N;
28  int debug;
29 
30  try {
31 
32  JParser<> zap("Example program to test permutations.");
33 
34  zap['i'] = make_field(buffer);
35  zap['N'] = make_field(N);
36  zap['d'] = make_field(debug) = 1;
37 
38  zap(argc, argv);
39  }
40  catch(const exception &error) {
41  FATAL(error.what() << endl);
42  }
43 
44 
45  using namespace JPP;
46 
47  cout.tie(&cerr);
48 
49 
50  if (N > buffer.size()) {
51  FATAL("Number of elements " << N << " > " << buffer.size() << endl);
52  }
53 
54  sort(buffer.begin(), buffer.end(), less<int>());
55 
56  vector<int>::iterator p = buffer.begin();
57 
58  advance(p, N);
59 
60  vector< set<int> > data;
61 
62  int n = 0;
63 
64  do {
65 
66  ++n;
67 
68  DEBUG('[' << setw(3) << n << ']');
69 
70  for (vector<int>::const_iterator i = buffer.begin(); i != p; ++i) {
71  DEBUG(' ' << setw(3) << *i);
72  }
73 
74  data.push_back(set<int>());
75 
76  for (vector<int>::const_iterator i = buffer.begin(); i != p; ++i) {
77  data.rbegin()->insert(*i);
78  }
79 
80  DEBUG(" |");
81 
82  for (vector<int>::const_iterator i = p; i != buffer.end(); ++i) {
83  DEBUG(' ' << setw(3) << *i);
84  }
85 
86  DEBUG(endl);
87 
88  } while (next_permutation(buffer.begin(), p, buffer.end(), less<int>()));
89 
90 
91  for (vector< set<int> >::const_iterator p = data.begin(); p != data.end(); ++p) {
92  for (vector< set<int> >::const_iterator q = p; ++q != data.end(); ) {
93  ASSERT(*p != *q);
94  }
95  }
96 
97 
98  NOTICE("number of permutations " << n << ' ' << factorial(buffer.size(), N) << endl);
99 
100  ASSERT(n == factorial(buffer.size(), N));
101 }
JMATH::factorial
long long int factorial(const long long int n)
Determine factorial.
Definition: JMathToolkit.hh:41
JTOOLS::next_permutation
bool next_permutation(T __begin, T __last, T __end, JComparator_t compare, std::bidirectional_iterator_tag)
Implementation of method next_permutation for bidirectional iterators.
Definition: JPermutation.hh:20
ASSERT
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
JROOT::advance
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
JTOOLS::n
const int n
Definition: JPolint.hh:628
std::vector< int >
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
NOTICE
#define NOTICE(A)
Definition: JMessage.hh:64
std::set< int >
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
debug
int debug
debug level
Definition: JSirene.cc:59
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
FATAL
#define FATAL(A)
Definition: JMessage.hh:67