[Lemon-commits] alpar: r3221 - in lemon/trunk: demo lemon
Lemon SVN
svn at lemon.cs.elte.hu
Sat Mar 3 13:05:05 CET 2007
Author: alpar
Date: Sat Mar 3 13:05:05 2007
New Revision: 3221
Added:
lemon/trunk/demo/arg_parser_demo.cc
lemon/trunk/lemon/arg_parser.cc
lemon/trunk/lemon/arg_parser.h
lemon/trunk/lemon/dist_log.h
Modified:
lemon/trunk/demo/Makefile.am
lemon/trunk/lemon/Makefile.am
Log:
arg_parser.h: A command line argument parser.
dist_log.h: A tool for measuring one and two dimensional distributions.
Modified: lemon/trunk/demo/Makefile.am
==============================================================================
--- lemon/trunk/demo/Makefile.am (original)
+++ lemon/trunk/demo/Makefile.am Sat Mar 3 13:05:05 2007
@@ -17,6 +17,7 @@
if WANT_DEMO
noinst_PROGRAMS += \
+ demo/arg_parser_demo \
demo/circulation_demo \
demo/csp_demo \
demo/dim_to_dot \
@@ -50,6 +51,8 @@
endif WANT_DEMO
+demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
+
demo_csp_demo_SOURCES = demo/csp_demo.cc
demo_circulation_demo_SOURCES = demo/circulation_demo.cc
Added: lemon/trunk/demo/arg_parser_demo.cc
==============================================================================
--- (empty file)
+++ lemon/trunk/demo/arg_parser_demo.cc Sat Mar 3 13:05:05 2007
@@ -0,0 +1,53 @@
+#include <lemon/arg_parser.h>
+
+using namespace lemon;
+int main(int argc, char **argv)
+{
+ ArgParser ap(argc,argv);
+ int i;
+ std::string s;
+ double d;
+ bool b,sil;
+ bool g1,g2,g3;
+ ap.option("n", "an integer input", i, true)
+ .option("val", "a double input", d)
+ .synonym("vals","val")
+ .option("name", "a string input", s)
+ .option("f", "a switch", b)
+ .option("nohelp", "", sil)
+ .option("gra","Choise A",g1)
+ .option("grb","Choise B",g2)
+ .option("grc","Choise C",g3)
+ .optionGroup("gr","gra")
+ .optionGroup("gr","grb")
+ .optionGroup("gr","grc")
+ .mandatoryGroup("gr")
+ .onlyOneGroup("gr")
+ .other("infile","The input file")
+ .other("...");
+
+ ap.parse();
+
+ std::cout << "Parameters of '" << ap.commandName() << "':\n";
+
+ if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl;
+ if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl;
+ if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl;
+ if(ap.given("f")) std::cout << " -f is given\n";
+ if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl;
+
+ switch(ap.files().size()) {
+ case 0:
+ std::cout << " No file argument was given.\n";
+ break;
+ case 1:
+ std::cout << " 1 file argument was given. It is:\n";
+ break;
+ default:
+ std::cout << " "
+ << ap.files().size() << " file arguments were given. They are:\n";
+ }
+ for(unsigned int i=0;i<ap.files().size();++i)
+ std::cout << " '" << ap.files()[i] << "'\n";
+
+}
Modified: lemon/trunk/lemon/Makefile.am
==============================================================================
--- lemon/trunk/lemon/Makefile.am (original)
+++ lemon/trunk/lemon/Makefile.am Sat Mar 3 13:05:05 2007
@@ -7,6 +7,7 @@
lib_LTLIBRARIES += lemon/libemon.la
lemon_libemon_la_SOURCES = \
+ lemon/arg_parser.cc \
lemon/lp_base.cc \
lemon/lp_skeleton.cc \
lemon/base.cc \
@@ -33,7 +34,7 @@
endif
lemon_HEADERS += \
- lemon/circulation.h \
+ lemon/arg_parser.h \
lemon/bellman_ford.h \
lemon/bfs.h \
lemon/bin_heap.h \
@@ -41,6 +42,7 @@
lemon/bp_matching.h \
lemon/bpugraph_adaptor.h \
lemon/bucket_heap.h \
+ lemon/circulation.h \
lemon/color.h \
lemon/config.h \
lemon/concept_check.h \
@@ -49,6 +51,7 @@
lemon/dag_shortest_path.h \
lemon/dfs.h \
lemon/dijkstra.h \
+ lemon/dist_log.h \
lemon/dim2.h \
lemon/dimacs.h \
lemon/edge_set.h \
Added: lemon/trunk/lemon/arg_parser.cc
==============================================================================
--- (empty file)
+++ lemon/trunk/lemon/arg_parser.cc Sat Mar 3 13:05:05 2007
@@ -0,0 +1,379 @@
+/* -*- C++ -*-
+ * lemon/main_params.h - Part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#include <lemon/arg_parser.h>
+
+namespace lemon {
+
+ void ArgParser::_showHelp(void *p)
+ {
+ ((ArgParser*)p)->showHelp();
+ exit(1);
+ }
+
+ ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
+ _command_name(argv[0]) {
+ option("-help","Print a short help message",_showHelp,this);
+ synonym("help","-help");
+ synonym("h","-help");
+
+ }
+
+ ArgParser &ArgParser::option(const std::string &name,
+ const std::string &help,
+ int &value, bool obl)
+ {
+ ParData p;
+ p.int_p=&value;
+ p.help=help;
+ p.type=INTEGER;
+ p.mandatory=obl;
+ _opts[name]=p;
+ return *this;
+ }
+
+ ArgParser &ArgParser::option(const std::string &name,
+ const std::string &help,
+ double &value, bool obl)
+ {
+ ParData p;
+ p.double_p=&value;
+ p.help=help;
+ p.type=DOUBLE;
+ p.mandatory=obl;
+ _opts[name]=p;
+ return *this;
+ }
+
+ ArgParser &ArgParser::option(const std::string &name,
+ const std::string &help,
+ bool &value, bool obl)
+ {
+ ParData p;
+ p.bool_p=&value;
+ p.help=help;
+ p.type=BOOL;
+ p.mandatory=obl;
+ _opts[name]=p;
+
+ value = false;
+
+ return *this;
+ }
+
+ ArgParser &ArgParser::option(const std::string &name,
+ const std::string &help,
+ std::string &value, bool obl)
+ {
+ ParData p;
+ p.string_p=&value;
+ p.help=help;
+ p.type=STRING;
+ p.mandatory=obl;
+ _opts[name]=p;
+ return *this;
+ }
+
+ ArgParser &ArgParser::option(const std::string &name,
+ const std::string &help,
+ void (*func)(void *),void *data)
+ {
+ ParData p;
+ p.func_p.p=func;
+ p.func_p.data=data;
+ p.help=help;
+ p.type=FUNC;
+ p.mandatory=false;
+ _opts[name]=p;
+ return *this;
+ }
+ ArgParser &ArgParser::optionGroup(const std::string &group,
+ const std::string &opt)
+ {
+ Opts::iterator i = _opts.find(opt);
+ if(i==_opts.end()) exit(3); ///\todo throw exception instead
+ else if(i->second.ingroup) exit(3); ///\todo throw exception instead
+ else {
+ GroupData &g=_groups[group];
+ g.opts.push_back(opt);
+ i->second.ingroup=true;
+ }
+ return *this;
+ }
+
+ ArgParser &ArgParser::onlyOneGroup(const std::string &group)
+ {
+ GroupData &g=_groups[group];
+ g.only_one=true;
+ return *this;
+ }
+
+ ArgParser &ArgParser::synonym(const std::string &syn,
+ const std::string &opt)
+ {
+ Opts::iterator o = _opts.find(opt);
+ Opts::iterator s = _opts.find(syn);
+ if(o==_opts.end()||s!=_opts.end())
+ exit(3); ///\todo throw exception instead
+ else {
+ ParData p;
+ p.help=opt;
+ p.mandatory=false;
+ p.syn=true;
+ _opts[syn]=p;
+ o->second.has_syn=true;
+ }
+ return *this;
+ }
+
+ ArgParser &ArgParser::mandatoryGroup(const std::string &group)
+ {
+ GroupData &g=_groups[group];
+ g.mandatory=true;
+ return *this;
+ }
+
+ ArgParser &ArgParser::other(const std::string &name,
+ const std::string &help)
+ {
+ _others_help.push_back(OtherArg(name,help));
+ return *this;
+ }
+
+ void ArgParser::show(std::ostream &os,Opts::iterator i)
+ {
+ os << "-" << i->first;
+ if(i->second.has_syn)
+ for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
+ if(j->second.syn&&j->second.help==i->first)
+ os << "|-" << j->first;
+ switch(i->second.type) {
+ case STRING:
+ os << " str";
+ break;
+ case INTEGER:
+ os << " int";
+ break;
+ case DOUBLE:
+ os << " num";
+ break;
+ default:
+ break;
+ }
+ }
+
+ void ArgParser::show(std::ostream &os,Groups::iterator i)
+ {
+ GroupData::Opts::iterator o=i->second.opts.begin();
+ while(o!=i->second.opts.end()) {
+ show(os,_opts.find(*o));
+ ++o;
+ if(o!=i->second.opts.end()) os<<'|';
+ }
+ }
+
+ void ArgParser::showHelp(Opts::iterator i)
+ {
+ if(i->second.help.size()==0||i->second.syn) return;
+ std::cerr << " ";
+ show(std::cerr,i);
+ std::cerr << std::endl;
+ std::cerr << " " << i->second.help << std::endl;
+ }
+ void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
+ {
+ if(i->help.size()==0) return;
+ std::cerr << " " << i->name << std::endl
+ << " " << i->help << std::endl;
+ }
+
+ void ArgParser::shortHelp()
+ {
+ const unsigned int LINE_LEN=77;
+ const std::string indent(" ");
+ std::cerr << "Usage:\n " << _command_name;
+ int pos=_command_name.size()+2;
+ for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
+ std::ostringstream cstr;
+ cstr << ' ';
+ if(!g->second.mandatory) cstr << '[';
+ show(cstr,g);
+ if(!g->second.mandatory) cstr << ']';
+ if(pos+cstr.str().size()>LINE_LEN) {
+ std::cerr << std::endl << indent;
+ pos=indent.size();
+ }
+ std::cerr << cstr.str();
+ pos+=cstr.str().size();
+ }
+ for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
+ if(!i->second.ingroup&&!i->second.syn) {
+ std::ostringstream cstr;
+ cstr << ' ';
+ if(!i->second.mandatory) cstr << '[';
+ show(cstr,i);
+ if(!i->second.mandatory) cstr << ']';
+ if(pos+cstr.str().size()>LINE_LEN) {
+ std::cerr << std::endl << indent;
+ pos=indent.size();
+ }
+ std::cerr << cstr.str();
+ pos+=cstr.str().size();
+ }
+ for(std::vector<OtherArg>::iterator i=_others_help.begin();
+ i!=_others_help.end();++i)
+ {
+ std::ostringstream cstr;
+ cstr << ' ' << i->name;
+
+ if(pos+cstr.str().size()>LINE_LEN) {
+ std::cerr << std::endl << indent;
+ pos=indent.size();
+ }
+ std::cerr << cstr.str();
+ pos+=cstr.str().size();
+ }
+ std::cerr << std::endl;
+ }
+
+ void ArgParser::showHelp()
+ {
+ shortHelp();
+ std::cerr << "Where:\n";
+ for(std::vector<OtherArg>::iterator i=_others_help.begin();
+ i!=_others_help.end();++i) showHelp(i);
+ for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
+ exit(1);
+ }
+
+
+ void ArgParser::unknownOpt(std::string arg)
+ {
+ std::cerr << "\nUnknown option: " << arg << "\n";
+ std::cerr << "\nType '" << _command_name <<
+ " --help' to obtain a short summary on the usage.\n\n";
+ exit(1);
+ }
+
+ void ArgParser::requiresValue(std::string arg, OptType t)
+ {
+ std::cerr << "Argument '" << arg << "' requires a";
+ switch(t) {
+ case STRING:
+ std::cerr << " string";
+ break;
+ case INTEGER:
+ std::cerr << "n integer";
+ break;
+ case DOUBLE:
+ std::cerr << " floating point";
+ break;
+ default:
+ break;
+ }
+ std::cerr << " value\n\n";
+ showHelp();
+ }
+
+
+ void ArgParser::checkMandatories()
+ {
+ bool ok=true;
+ for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
+ if(i->second.mandatory&&!i->second.set)
+ {
+ if(ok)
+ std::cerr << _command_name
+ << ": The following mandatory arguments are missing.\n";
+ ok=false;
+ showHelp(i);
+ }
+ for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
+ if(i->second.mandatory||i->second.only_one)
+ {
+ int set=0;
+ for(GroupData::Opts::iterator o=i->second.opts.begin();
+ o!=i->second.opts.end();++o)
+ if(_opts.find(*o)->second.set) ++set;
+ if(i->second.mandatory&&!set) {
+ std::cerr << _command_name
+ << ": At least one of the following arguments is mandatory.\n";
+ ok=false;
+ for(GroupData::Opts::iterator o=i->second.opts.begin();
+ o!=i->second.opts.end();++o)
+ showHelp(_opts.find(*o));
+ }
+ if(i->second.only_one&&set>1) {
+ std::cerr << _command_name
+ << ": At most one of the following arguments can be given.\n";
+ ok=false;
+ for(GroupData::Opts::iterator o=i->second.opts.begin();
+ o!=i->second.opts.end();++o)
+ showHelp(_opts.find(*o));
+ }
+ }
+ if(!ok) {
+ std::cerr << "\nType '" << _command_name <<
+ " --help' to obtain a short summary on the usage.\n\n";
+ exit(1);
+ }
+ }
+
+ ArgParser &ArgParser::parse()
+ {
+ for(int ar=1; ar<_argc; ++ar) {
+ std::string arg(_argv[ar]);
+ if (arg[0] != '-' || arg.size() == 1) {
+ _file_args.push_back(arg);
+ }
+ else {
+ Opts::iterator i = _opts.find(arg.substr(1));
+ if(i==_opts.end()) unknownOpt(arg);
+ else {
+ if(i->second.syn) i=_opts.find(i->second.help);
+ ParData &p(i->second);
+ if (p.type==BOOL) *p.bool_p=true;
+ else if (p.type==FUNC) p.func_p.p(p.func_p.data);
+ else if(++ar==_argc) requiresValue(arg, p.type);
+ else {
+ std::string val(_argv[ar]);
+ std::istringstream vals(val);
+ switch(p.type) {
+ case STRING:
+ *p.string_p=val;
+ break;
+ case INTEGER:
+ vals >> *p.int_p;
+ break;
+ case DOUBLE:
+ vals >> *p.double_p;
+ break;
+ default:
+ break;
+ }
+ if(p.type!=STRING&&(!vals||!vals.eof()))
+ requiresValue(arg, p.type);
+ }
+ p.set = true;
+ }
+ }
+ }
+ checkMandatories();
+
+ return *this;
+ }
+
+}
Added: lemon/trunk/lemon/arg_parser.h
==============================================================================
--- (empty file)
+++ lemon/trunk/lemon/arg_parser.h Sat Mar 3 13:05:05 2007
@@ -0,0 +1,239 @@
+/* -*- C++ -*-
+ * lemon/main_params.h - Part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_ARG_PARSER
+#define LEMON_ARG_PARSER
+
+#include <vector>
+#include <map>
+#include <list>
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+
+///\ingroup misc
+///\file
+///\brief A tools to parse command line arguments.
+///
+///\author Alpar Juttner
+
+namespace lemon {
+
+ ///Command line arguments parser
+
+ ///\ingroup misc
+ ///Command line arguments parser
+ ///
+ class ArgParser {
+
+ static void _showHelp(void *p);
+ protected:
+
+ int _argc;
+ char **_argv;
+
+ enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
+
+ class ParData {
+ public:
+ union {
+ bool *bool_p;
+ int *int_p;
+ double *double_p;
+ std::string *string_p;
+ struct {
+ void (*p)(void *);
+ void *data;
+ } func_p;
+
+ };
+ std::string help;
+ bool mandatory;
+ OptType type;
+ bool set;
+ bool ingroup;
+ bool has_syn;
+ bool syn;
+
+ ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
+ has_syn(false), syn(false) {}
+ };
+
+ typedef std::map<std::string,ParData> Opts;
+ Opts _opts;
+
+ class GroupData
+ {
+ public:
+ typedef std::list<std::string> Opts;
+ Opts opts;
+ bool only_one;
+ bool mandatory;
+ GroupData() :only_one(false), mandatory(false) {}
+ };
+
+ typedef std::map<std::string,GroupData> Groups;
+ Groups _groups;
+
+ struct OtherArg
+ {
+ std::string name;
+ std::string help;
+ OtherArg(std::string n, std::string h) :name(n), help(h) {}
+
+ };
+
+ std::vector<OtherArg> _others_help;
+ std::vector<std::string> _file_args;
+ std::string _command_name;
+
+ public:
+
+ ///\e
+ ArgParser(int argc, char **argv);
+
+ ///Add a new integer type option
+
+ ///\param name The name of the option. The leading '-' must be omitted.
+ ///\param help A help string.
+ ///\retval value The value of the argument will be written to this variable.
+ ///\param obl Indicate if the option is mandatory.
+ ArgParser &option(const std::string &name,
+ const std::string &help,
+ int &value, bool obl=false);
+
+ ///Add a new floating type option
+
+ ///\param name The name of the option. The leading '-' must be omitted.
+ ///\param help A help string.
+ ///\retval value The value of the argument will be written to this variable.
+ ///\param obl Indicate if the option is mandatory.
+ ArgParser &option(const std::string &name,
+ const std::string &help,
+ double &value, bool obl=false);
+
+ ///Add a new bool type option
+
+ ///\param name The name of the option. The leading '-' must be omitted.
+ ///\param help A help string.
+ ///\retval value The value of the argument will be written to this variable.
+ ///\param obl Indicate if the option is mandatory.
+ ////\note A mandatory bool obtion is of very little use.)
+ ArgParser &option(const std::string &name,
+ const std::string &help,
+ bool &value, bool obl=false);
+
+ ///Add a new string type option
+
+ ///\param name The name of the option. The leading '-' must be omitted.
+ ///\param help A help string.
+ ///\retval value The value of the argument will be written to this variable.
+ ///\param obl Indicate if the option is mandatory.
+ ArgParser &option(const std::string &name,
+ const std::string &help,
+ std::string &value, bool obl=false);
+
+ ///Bind a function to an option.
+
+ ///\param name The name of the option. The leading '-' must be omitted.
+ ///\param help A help string.
+ ///\retval func The function to be called when the option is given. It
+ /// must be of type "void f(void *)"
+ ///\param data Data to be passed to \c func
+ ArgParser &option(const std::string &name,
+ const std::string &help,
+ void (*func)(void *),void *data);
+
+ ///Boundle some options into a group
+
+ /// You can group some option by calling this function repeatedly for each
+ /// option to be grupped with the same groupname.
+ ///\param group The group name
+ ///\param opt The option name
+ ArgParser &optionGroup(const std::string &group,
+ const std::string &opt);
+
+ ///Make the members of a group exclusive
+
+ ///If you call this function for a group, than at most one of them can be
+ ///given at the same time
+ ArgParser &onlyOneGroup(const std::string &group);
+
+ ///Create synonym to an option
+
+ ///With this function you can create a sysnonym called \c sys of the
+ ///option \c opt.
+ ArgParser &synonym(const std::string &syn,
+ const std::string &opt);
+
+ ///Make a group mandatory
+
+ ///Using this function, at least one of the members of \c group
+ ///must be given.
+ ArgParser &mandatoryGroup(const std::string &group);
+
+ ///Give help string for non-parsed arguments.
+
+ ///With this function you can give help string for non-parsed arguments.
+ ///the parameter \c name will be printed in the short usage line, while
+ ///\c help gives a more detailed description.
+ ArgParser &other(const std::string &name,
+ const std::string &help="");
+
+ ///Non option type arguments.
+
+ ///Gives back a reference to a vector consisting of the program arguments
+ ///not starting with a '-' character.
+ std::vector<std::string> &files() { return _file_args; }
+
+ ///Give back the command name (the 0th argument)
+ const std::string &commandName() { return _command_name; }
+
+ void show(std::ostream &os,Opts::iterator i);
+ void show(std::ostream &os,Groups::iterator i);
+ void showHelp(Opts::iterator i);
+ void showHelp(std::vector<OtherArg>::iterator i);
+ void shortHelp();
+ void showHelp();
+
+ void unknownOpt(std::string arg);
+
+ void requiresValue(std::string arg, OptType t);
+ void checkMandatories();
+
+ ///\e
+ ArgParser &parse();
+
+ /// Synonym for parse()
+ ArgParser &run()
+ {
+ return parse();
+ }
+
+ ///Check if an opion has been given to the command.
+ bool given(std::string op)
+ {
+ Opts::iterator i = _opts.find(op);
+ return i!=_opts.end()?i->second.set:false;
+ }
+
+ };
+}
+
+
+
+#endif // LEMON_MAIN_PARAMS
Added: lemon/trunk/lemon/dist_log.h
==============================================================================
--- (empty file)
+++ lemon/trunk/lemon/dist_log.h Sat Mar 3 13:05:05 2007
@@ -0,0 +1,137 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2006
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_RANDOM_H
+#define LEMON_RANDOM_H
+
+#include<iostream>
+#include<fstream>
+#include<string>
+
+#include <lemon/dim2.h>
+
+///\ingroup misc
+///\file
+///\brief Measure a Distribution
+///
+///\todo Needs lot more docs
+///
+
+
+namespace lemon {
+
+ ///Measure a distribution
+ class DistLog
+ {
+ std::vector<int> _dist;
+ double _lo,_up;
+ int _count;
+ bool _cut;
+ public:
+ ///\e
+ Dist(double lo,double up,int gr,bool cut=true)
+ : _dist(gr,0),_lo(lo),_up(up),_count(0),_cut(cut) {}
+ ///\e
+ void operator()(double v)
+ {
+ if(_cut) {
+ if(_lo<=v && v<_up)
+ _dist[int((v-_lo)/(_up-_lo)*_dist.size())]++;
+ }
+ else {
+ _dist[std::max(0,std::min(int(_dist.size())-1,
+ int((v-_lo)/(_up-_lo)*_dist.size())
+ ))]++;
+ }
+ _count++;
+ }
+ ///\e
+ void dump(std::ostream& os=std::cout)
+ {
+ for(int i=0;i<_dist.size();i++)
+ os << _lo+(i+0.5)*(_up-_lo)/_dist.size() << ' '
+ << double(_dist[i])/_count << std::endl;
+ }
+ ///\e
+ void dump(const std::string& file_name)
+ {
+ dump(std::ofstream(file_name.c_str()));
+ }
+ };
+
+ ///Measure a two dimensional distribution
+ class DistLog2
+ {
+ public:
+ typedef dim2::Point<double> Point;
+ private:
+ std::vector<int> _dist;
+ int _gr;
+ Point _lo,_up;
+ int _count;
+ bool _cut;
+ public:
+ ///\e
+ Dist2(Point a,Point b,int gr,bool cut=true) :
+ _dist(gr*gr,0),_gr(gr),
+ _lo(a),_up(b),_count(0),_cut(cut) {}
+ ///\e
+ Dist2(double lox,double upx,double loy,double upy,int gr,bool cut=true) :
+ _dist(gr*gr,0),_gr(gr),
+ _lo(Point(lox,loy)),_up(Point(upx,upy)),_count(0),_cut(cut) {}
+ ///\e
+ void operator()(Point v)
+ {
+ if(_cut)
+ {
+ if(v.x>=_lo.x && v.x<_up.x && v.y>=_lo.y && v.y<_up.y)
+ _dist[int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)*_gr+
+ int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)]++;
+ }
+ else {
+ _dist[std::max(0,std::min(_gr-1,
+ int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)
+ ))*_gr+
+ std::max(0,std::min(_gr-1,
+ int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)
+ ))
+ ]++;
+ }
+ _count++;
+ }
+ ///\e
+ void dump(std::ostream& os=std::cout)
+ {
+ for(int i=0;i<_gr;i++)
+ {
+ for(int j=0;j<_gr;j++)
+ os << _lo.x+(i+0.5)*(_up.x-_lo.x)/_gr << ' '
+ << _lo.y+(j+0.5)*(_up.y-_lo.y)/_gr << ' '
+ << double(_dist[i*_gr+j])/_count << std::endl;
+ os << std::endl;
+ }
+ }
+ ///\e
+ void dump(const std::string& file_name)
+ {
+ dump(std::ofstream(file_name.c_str()));
+ }
+ };
+}
+
+#endif
More information about the Lemon-commits
mailing list