COIN-OR::LEMON - Graph Library

Changeset 2402:da8eb8f4ea41 in lemon-0.x


Ignore:
Timestamp:
03/12/07 14:26:56 (17 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3233
Message:

An improved version of ArgParser?: You don't need to give an explicit storage
for each option.
TODO: Documentation must be updated

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • demo/arg_parser_demo.cc

    r2391 r2402  
    2828  bool b,sil;
    2929  bool g1,g2,g3;
    30   ap.option("n", "an integer input", i, true)
    31     .option("val", "a double input", d)
     30  ap.refOption("n", "an integer input", i, true)
     31    .refOption("val", "a double input", d)
    3232    .synonym("vals","val")
    33     .option("name", "a string input", s)
    34     .option("f", "a switch", b)
    35     .option("nohelp", "", sil)
    36     .option("gra","Choise A",g1)
    37     .option("grb","Choise B",g2)
    38     .option("grc","Choise C",g3)
     33    .refOption("name", "a string input", s)
     34    .refOption("f", "a switch", b)
     35    .refOption("nohelp", "", sil)
     36    .refOption("gra","Choise A",g1)
     37    .refOption("grb","Choise B",g2)
     38    .refOption("grc","Choise C",g3)
    3939    .optionGroup("gr","gra")
    4040    .optionGroup("gr","grb")
  • lemon/arg_parser.cc

    r2391 r2402  
    2929  ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
    3030                                               _command_name(argv[0]) {
    31     option("-help","Print a short help message",_showHelp,this);
     31    refOption("-help","Print a short help message",_showHelp,this);
    3232    synonym("help","-help");
    3333    synonym("h","-help");
     
    3535  }
    3636
    37   ArgParser &ArgParser::option(const std::string &name,
     37  ArgParser::~ArgParser()
     38  {
     39    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
     40      if(i->second.self_delete)
     41        switch(i->second.type) {
     42        case BOOL:
     43          delete i->second.bool_p;
     44          break;
     45        case STRING:
     46          delete i->second.string_p;
     47          break;
     48        case DOUBLE:
     49          delete i->second.double_p;
     50          break;
     51        case INTEGER:
     52          delete i->second.int_p;
     53          break;
     54        case UNKNOWN:
     55          break;
     56        case FUNC:
     57          break;
     58        }
     59  }
     60 
     61
     62  ArgParser &ArgParser::intOption(const std::string &name,
     63                               const std::string &help,
     64                               int value, bool obl)
     65  {
     66    ParData p;
     67    p.int_p=new int(value);
     68    p.self_delete=true;
     69    p.help=help;
     70    p.type=INTEGER;
     71    p.mandatory=obl;
     72    p.self_delete=true;
     73    _opts[name]=p;
     74    return *this;
     75  }
     76
     77  ArgParser &ArgParser::doubleOption(const std::string &name,
     78                               const std::string &help,
     79                               double value, bool obl)
     80  {
     81    ParData p;
     82    p.double_p=new double(value);
     83    p.self_delete=true;
     84    p.help=help;
     85    p.type=DOUBLE;
     86    p.mandatory=obl;
     87    _opts[name]=p;
     88    return *this;
     89  }
     90
     91  ArgParser &ArgParser::boolOption(const std::string &name,
     92                               const std::string &help,
     93                               bool value, bool obl)
     94  {
     95    ParData p;
     96    p.bool_p=new bool(value);
     97    p.self_delete=true;
     98    p.help=help;
     99    p.type=BOOL;
     100    p.mandatory=obl;
     101    _opts[name]=p;
     102
     103    value = false;
     104
     105    return *this;
     106  }
     107
     108  ArgParser &ArgParser::stringOption(const std::string &name,
     109                               const std::string &help,
     110                               std::string value, bool obl)
     111  {
     112    ParData p;
     113    p.string_p=new std::string(value);
     114    p.self_delete=true;
     115    p.help=help;
     116    p.type=STRING;
     117    p.mandatory=obl;
     118    _opts[name]=p;
     119    return *this;
     120  }
     121
     122  ArgParser &ArgParser::refOption(const std::string &name,
    38123                               const std::string &help,
    39124                               int &value, bool obl)
     
    41126    ParData p;
    42127    p.int_p=&value;
     128    p.self_delete=false;
    43129    p.help=help;
    44130    p.type=INTEGER;
     
    48134  }
    49135
    50   ArgParser &ArgParser::option(const std::string &name,
     136  ArgParser &ArgParser::refOption(const std::string &name,
    51137                               const std::string &help,
    52138                               double &value, bool obl)
     
    54140    ParData p;
    55141    p.double_p=&value;
     142    p.self_delete=false;
    56143    p.help=help;
    57144    p.type=DOUBLE;
     
    61148  }
    62149
    63   ArgParser &ArgParser::option(const std::string &name,
     150  ArgParser &ArgParser::refOption(const std::string &name,
    64151                               const std::string &help,
    65152                               bool &value, bool obl)
     
    67154    ParData p;
    68155    p.bool_p=&value;
     156    p.self_delete=false;
    69157    p.help=help;
    70158    p.type=BOOL;
     
    77165  }
    78166
    79   ArgParser &ArgParser::option(const std::string &name,
     167  ArgParser &ArgParser::refOption(const std::string &name,
    80168                               const std::string &help,
    81169                               std::string &value, bool obl)
     
    83171    ParData p;
    84172    p.string_p=&value;
     173    p.self_delete=false;
    85174    p.help=help;
    86175    p.type=STRING;
     
    90179  }
    91180
    92   ArgParser &ArgParser::option(const std::string &name,
     181  ArgParser &ArgParser::refOption(const std::string &name,
    93182                               const std::string &help,
    94183                               void (*func)(void *),void *data)
     
    97186    p.func_p.p=func;
    98187    p.func_p.data=data;
     188    p.self_delete=false;
    99189    p.help=help;
    100190    p.type=FUNC;
     
    103193    return *this;
    104194  }
     195
    105196  ArgParser &ArgParser::optionGroup(const std::string &group,
    106197                                    const std::string &opt)
     
    378469    return *this;
    379470  } 
    380    
     471
    381472}
  • lemon/arg_parser.h

    r2391 r2402  
    7171      bool has_syn;
    7272      bool syn;
    73              
     73      bool self_delete;
    7474      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    75                   has_syn(false), syn(false) {}
     75                  has_syn(false), syn(false), self_delete(false) {}
    7676    };
    7777
     
    109109    ArgParser(int argc, char **argv);
    110110
     111    ~ArgParser();
     112
    111113    ///Add a new integer type option
    112114
     
    115117    ///\retval value The value of the argument will be written to this variable.
    116118    ///\param obl Indicate if the option is mandatory.
    117     ArgParser &option(const std::string &name,
     119    ArgParser &intOption(const std::string &name,
     120                    const std::string &help,
     121                    int value=0, bool obl=false);
     122
     123    ///Add a new floating type option
     124
     125    ///\param name The name of the option. The leading '-' must be omitted.
     126    ///\param help A help string.
     127    ///\retval value The value of the argument will be written to this variable.
     128    ///\param obl Indicate if the option is mandatory.
     129    ArgParser &doubleOption(const std::string &name,
     130                      const std::string &help,
     131                      double value=0, bool obl=false);
     132
     133    ///Add a new bool type option
     134
     135    ///\param name The name of the option. The leading '-' must be omitted.
     136    ///\param help A help string.
     137    ///\retval value The value of the argument will be written to this variable.
     138    ///\param obl Indicate if the option is mandatory.
     139    ////\note A mandatory bool obtion is of very little use.)
     140    ArgParser &boolOption(const std::string &name,
     141                      const std::string &help,
     142                      bool value=false, bool obl=false);
     143
     144    ///Add a new string type option
     145
     146    ///\param name The name of the option. The leading '-' must be omitted.
     147    ///\param help A help string.
     148    ///\retval value The value of the argument will be written to this variable.
     149    ///\param obl Indicate if the option is mandatory.
     150    ArgParser &stringOption(const std::string &name,
     151                      const std::string &help,
     152                      std::string value="", bool obl=false);
     153   
     154
     155
     156
     157    ///Add a new integer type option
     158
     159    ///\param name The name of the option. The leading '-' must be omitted.
     160    ///\param help A help string.
     161    ///\retval value The value of the argument will be written to this variable.
     162    ///\param obl Indicate if the option is mandatory.
     163    ArgParser &refOption(const std::string &name,
    118164                    const std::string &help,
    119165                    int &value, bool obl=false);
     
    125171    ///\retval value The value of the argument will be written to this variable.
    126172    ///\param obl Indicate if the option is mandatory.
    127     ArgParser &option(const std::string &name,
     173    ArgParser &refOption(const std::string &name,
    128174                      const std::string &help,
    129175                      double &value, bool obl=false);
     
    136182    ///\param obl Indicate if the option is mandatory.
    137183    ////\note A mandatory bool obtion is of very little use.)
    138     ArgParser &option(const std::string &name,
     184    ArgParser &refOption(const std::string &name,
    139185                      const std::string &help,
    140186                      bool &value, bool obl=false);
     
    146192    ///\retval value The value of the argument will be written to this variable.
    147193    ///\param obl Indicate if the option is mandatory.
    148     ArgParser &option(const std::string &name,
     194    ArgParser &refOption(const std::string &name,
    149195                      const std::string &help,
    150196                      std::string &value, bool obl=false);
     
    157203    ///  must be of type "void f(void *)"
    158204    ///\param data Data to be passed to \c func
    159     ArgParser &option(const std::string &name,
     205    ArgParser &refOption(const std::string &name,
    160206                    const std::string &help,
    161207                    void (*func)(void *),void *data);
     
    233279      return i!=_opts.end()?i->second.set:false;
    234280    }
    235    
     281
     282
     283    class RefType
     284    {
     285      ArgParser &_parser;
     286      std::string _name;
     287    public:
     288      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
     289      operator bool()
     290      {
     291        Opts::iterator i = _parser._opts.find(_name);
     292        if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
     293        else if(i->second.type!=ArgParser::BOOL) exit(3);
     294        else return *(i->second.bool_p);
     295      }
     296      operator std::string()
     297      {
     298        Opts::iterator i = _parser._opts.find(_name);
     299        if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
     300        else if(i->second.type!=ArgParser::STRING) exit(3);
     301        else return *(i->second.string_p);
     302      }
     303      operator double()
     304      {
     305        Opts::iterator i = _parser._opts.find(_name);
     306        if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
     307        else if(i->second.type!=ArgParser::DOUBLE) exit(3);
     308        else return *(i->second.double_p);
     309      }
     310      operator int()
     311      {
     312        Opts::iterator i = _parser._opts.find(_name);
     313        if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
     314        else if(i->second.type!=ArgParser::INTEGER) exit(3);
     315        else return *(i->second.int_p);
     316      }
     317
     318    };
     319
     320    RefType operator[](const std::string &n)
     321    {
     322      return RefType(*this, n);
     323    }
     324   
     325     
    236326  };
    237327}
  • tools/lgf-gen.cc

    r2391 r2402  
    3030#include <algorithm>
    3131#include <lemon/unionfind.h>
     32#include <lemon/time_measure.h>
    3233
    3334using namespace lemon;
     
    3738UGRAPH_TYPEDEFS(ListUGraph);
    3839
     40bool progress=true;
     41
    3942int N;
    40 int girth;
     43// int girth;
    4144
    4245ListUGraph g;
     
    235238
    236239void minTree() {
     240  int en=0;
     241  int pr=0;
    237242  std::vector<Pedge> pedges;
     243  Timer T;
     244  std::cout << T.realTime() << "s: Setting up the edges...\n";
    238245  for(NodeIt n(g);n!=INVALID;++n)
    239     for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
    240       {
    241         Pedge p;
    242         p.a=n;
    243         p.b=m;
    244         p.len=(coords[m]-coords[n]).normSquare();
    245         pedges.push_back(p);
    246       }
     246    {
     247      for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
     248        {
     249          Pedge p;
     250          p.a=n;
     251          p.b=m;
     252          p.len=(coords[m]-coords[n]).normSquare();
     253          pedges.push_back(p);
     254        }
     255      en++;
     256      if(progress && en>=pr*double(N)/100)
     257        {
     258          std::cout << pr << "%  \r" << std::flush;
     259          pr++;
     260        }
     261    }
     262  std::cout << T.realTime() << "s: Sorting the edges...\n";
    247263  std::sort(pedges.begin(),pedges.end(),pedgeLess);
     264  std::cout << T.realTime() << "s: Creating the tree...\n";
    248265  ListUGraph::NodeMap<int> comp(g);
    249266  UnionFind<ListUGraph::NodeMap<int> > uf(comp);
    250267  for (NodeIt it(g); it != INVALID; ++it)
    251     uf.insert(it);
    252 
    253   int en=0;
     268    uf.insert(it); 
    254269  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
    255270    {
    256271      if ( uf.join(pi->a,pi->b) ) {
    257272        g.addEdge(pi->a,pi->b);
    258         en++;
    259         if(en>=N-1) return;
    260       }
    261     }
     273        if(en>=N-1)
     274          break;
     275      }
     276    }
     277  std::cout << T.realTime() << "s: Done\n";
    262278}
    263279
     
    268284  ArgParser ap(argc,argv);
    269285
    270   bool eps;
     286//   bool eps;
    271287  bool disc_d, square_d, gauss_d;
    272   bool tsp_a,two_a,tree_a;
     288//   bool tsp_a,two_a,tree_a;
    273289  int num_of_cities=1;
    274290  double area=1;
    275291  N=100;
    276   girth=10;
     292//   girth=10;
    277293  std::string ndist("disc");
    278   ap.option("n", "Number of nodes (default is 100)", N)
    279     .option("g", "Girth parameter (default is 10)", girth)
    280     .option("cities", "Number of cities (default is 1)", num_of_cities)
    281     .option("area", "Full relative area of the cities (default is 1)", area)
    282     .option("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
     294  ap.refOption("n", "Number of nodes (default is 100)", N)
     295    .intOption("g", "Girth parameter (default is 10)", 10)
     296    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
     297    .refOption("area", "Full relative area of the cities (default is 1)", area)
     298    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
    283299    .optionGroup("dist", "disc")
    284     .option("square", "Nodes are evenly distributed on a unit square", square_d)
     300    .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
    285301    .optionGroup("dist", "square")
    286     .option("gauss",
     302    .refOption("gauss",
    287303            "Nodes are located according to a two-dim gauss distribution",
    288304            gauss_d)
     
    290306//     .mandatoryGroup("dist")
    291307    .onlyOneGroup("dist")
    292     .option("eps", "Also generate .eps output (prefix.eps)",eps)
    293     .option("2con", "Create a two connected planar graph",two_a)
     308    .boolOption("eps", "Also generate .eps output (prefix.eps)")
     309    .boolOption("2con", "Create a two connected planar graph")
    294310    .optionGroup("alg","2con")
    295     .option("tree", "Create a min. cost spanning tree",tree_a)
     311    .boolOption("tree", "Create a min. cost spanning tree")
    296312    .optionGroup("alg","tree")
    297     .option("tsp", "Create a TSP tour",tsp_a)
     313    .boolOption("tsp", "Create a TSP tour")
    298314    .optionGroup("alg","tsp")
    299315    .onlyOneGroup("alg")
     
    353369    }
    354370 
    355   if(tsp_a) {
     371  if(ap["tsp"]) {
    356372    tsp();
    357373    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
    358374  }
    359   else if(two_a) {
     375  else if(ap["2con"]) {
    360376    std::cout << "Make triangles\n";
    361377    //   triangle();
    362     sparseTriangle(girth);
     378    sparseTriangle(ap["g"]);
    363379    std::cout << "Make it sparser\n";
    364     sparse2(girth);
     380    sparse2(ap["g"]);
    365381  }
    366   else if(tree_a) {
     382  else if(ap["tree"]) {
    367383    minTree();
    368384  }
     
    375391    tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
    376392  std::cout << "Total edge length  : " << tlen << std::endl;
    377   if(eps)
     393  if(ap["eps"])
    378394    graphToEps(g,prefix+".eps").
    379395      scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
Note: See TracChangeset for help on using the changeset viewer.