The ChainCon Software (Release 0.03)
testshuffles.cpp
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A simple program for showing the shuffles.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: August 21, 2013.
26 
27 
28 // include some standard C++ header files
29 #include <vector>
30 
31 // include selected header files from the CHomP library
32 #include "chomp/system/config.h"
33 #include "chomp/system/textfile.h"
34 #include "chomp/system/timeused.h"
35 #include "chomp/system/arg.h"
36 
37 // include relevant local header files
38 #include "chaincon/shuffles.h"
39 
40 
41 // --------------------------------------------------
42 // -------------------- OVERTURE --------------------
43 // --------------------------------------------------
44 
45 /// The title of the program and licensing information.
46 const char *title = "\
47 Test Shuffles. August 12, 2013. (C) 1997-2014 by Pawel Pilarczyk.\n\
48 This is free software. No warranty. Consult 'license.txt' for details.";
49 
50 /// Brief help information on the program's usage.
51 const char *helpinfo = "\
52 This program shows all the shuffles of required size.\n\
53 Call with:\n\
54 p q - the two positive integer numbers that define the shuffles.\n\
55 Switches and additional arguments:\n\
56 --log filename - save the output to a file (without progress indicators),\n\
57 --quiet - suppress data output to the screen (whcih can be still logged),\n\
58 --help - display this brief help information only and exit.\n\
59 For more information please consult the accompanying documentation\n\
60 or ask the program's author at http://www.PawelPilarczyk.com/.";
61 
62 
63 // --------------------------------------------------
64 // ---------------------- main ----------------------
65 // --------------------------------------------------
66 
67 /// The main procedure of the program.
68 /// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
69 int main (int argc, char *argv [])
70 {
71  using namespace chomp::homology;
72 
73  // turn on a message that will appear if the program does not finish
74  program_time = "Aborted after";
75 
76  // prepare user-configurable data
77  int p = -1;
78  int q = -1;
79 
80  // analyze the command line
81  arguments a;
82  arg (a, NULL, p);
83  arg (a, NULL, q);
84  arghelp (a);
85 
86  argstreamprepare (a);
87  int argresult = a. analyze (argc, argv);
88  argstreamset ();
89 
90  // show the program's title
91  if (argresult >= 0)
92  sout << title << '\n';
93 
94  // if something was incorrect, show an additional message and exit
95  if (argresult < 0)
96  {
97  sout << "Call with '--help' for help.\n";
98  return 2;
99  }
100 
101  // if help requested or no filename present, show help information
102  if ((argresult > 0) || (p < 0) || (q < 0))
103  {
104  sout << helpinfo << '\n';
105  return 1;
106  }
107 
108  // try running the main function and catch an error message if thrown
109  try
110  {
111  Shuffles s (p, q);
112  std::vector<int> alpha (p);
113  std::vector<int> beta (q);
114  int sig (0);
115  int count = 0;
116  while (1)
117  {
118  bool moreAvailable = s. get (alpha, beta, sig);
119  sout << "Shuffle " << count << ": (";
120  for (int i = 0; i < p; ++ i)
121  sout << (i ? " " : "") << alpha [i];
122  sout << "), (";
123  for (int i = 0; i < q; ++ i)
124  sout << (i ? " " : "") << beta [i];
125  sout << "); sig = " << sig << ".\n";
126  ++ count;
127  if (!moreAvailable)
128  break;
129  }
130 
131  // check if the number of shuffles is correct
132  int all = 1;
133  for (int i = p + 1; i <= p + q; ++ i)
134  all *= i;
135  for (int i = 2; i <= q; ++ i)
136  all /= i;
137  if (count != all)
138  {
139  sout << "Wrong number of shuffles: " << count <<
140  " instead of " << all << ".\n";
141  }
142 
143  program_time = "Total time used:";
144  program_time = 1;
145  return 0;
146  }
147  catch (const char *msg)
148  {
149  sout << "ERROR: " << msg << '\n';
150  return -1;
151  }
152  catch (const std::exception &e)
153  {
154  sout << "ERROR: " << e. what () << '\n';
155  return -1;
156  }
157  catch (...)
158  {
159  sout << "ABORT: An unknown error occurred.\n";
160  return -1;
161  }
162 } /* main */
163 
An iterator of shuffles.
const char * helpinfo
Brief help information on the program&#39;s usage.
An iterator of all the (p,q)-shuffles.
Definition: shuffles.h:51
int main(int argc, char *argv [])
The main procedure of the program.
const char * title
The title of the program and licensing information.