[Lemon-commits] Balazs Dezso: Redesigned lgf related tools

Lemon HG hg at lemon.cs.elte.hu
Thu Apr 17 18:22:46 CEST 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/1c9a9e2f7d4d
changeset: 127:1c9a9e2f7d4d
user:      Balazs Dezso <deba [at] inf.elte.hu>
date:      Thu Apr 17 15:18:45 2008 +0100
description:
	Redesigned lgf related tools

diffstat:

5 files changed, 1698 insertions(+), 1 deletion(-)
demo/Makefile.am   |    4 
demo/lgf_demo.cc   |  153 ++++++++
lemon/Makefile.am  |    1 
lemon/lgf_reader.h |  914 ++++++++++++++++++++++++++++++++++++++++++++++++++++
lemon/lgf_writer.h |  627 +++++++++++++++++++++++++++++++++++

diffs (truncated from 1733 to 300 lines):

diff -r e1dd2a70737c -r 1c9a9e2f7d4d demo/Makefile.am
--- a/demo/Makefile.am	Mon Apr 14 10:46:41 2008 +0200
+++ b/demo/Makefile.am	Thu Apr 17 15:18:45 2008 +0100
@@ -4,9 +4,11 @@
 if WANT_DEMO
 
 noinst_PROGRAMS += \
-        demo/arg_parser_demo
+        demo/arg_parser_demo \
+	demo/lgf_demo
 
 endif WANT_DEMO
 
 demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
+demo_lgf_demo_SOURCES = demo/lgf_demo.cc
 
diff -r e1dd2a70737c -r 1c9a9e2f7d4d demo/lgf_demo.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/lgf_demo.cc	Thu Apr 17 15:18:45 2008 +0100
@@ -0,0 +1,153 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2008
+ * 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.
+ *
+ */
+
+///\ingroup demos
+///\file
+///\brief Demonstrating graph input and output
+///
+/// This simple demo program gives an example of how to read and write
+/// a graph and additional maps (on the nodes or the edges) from/to a
+/// stream. 
+///
+/// \include reader_writer_demo.cc
+
+#include <iostream>
+#include <lemon/smart_graph.h>
+#include <lemon/lgf_reader.h>
+#include <lemon/lgf_writer.h>
+#include <lemon/random.h>
+
+
+using namespace lemon;
+
+int main(int argc, const char *argv[]) {
+  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
+  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * log(n));
+  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
+
+  SmartDigraph digraph;
+
+  std::stringstream ss;
+
+  try {
+
+    typedef SmartDigraph Digraph;
+    typedef Digraph::Node Node;
+    typedef Digraph::Arc Arc;
+    typedef Digraph::ArcIt ArcIt;
+
+    typedef Digraph::NodeMap<int> PotentialMap;
+    typedef Digraph::ArcMap<int> CapacityMap;
+    typedef Digraph::ArcMap<std::string> NameMap;
+
+    Digraph digraph;
+    PotentialMap potential(digraph);
+    CapacityMap capacity(digraph);
+    NameMap name(digraph);
+
+    std::vector<Node> nodes;
+    for (int i = 0; i < n; ++i) {
+      Node node = digraph.addNode();
+      potential[node] = rnd[m];
+      nodes.push_back(node);
+    }
+
+    std::vector<Arc> arcs;
+    for (int i = 0; i < e; ++i) {
+      int s = rnd[n];
+      int t = rnd[n];
+      int c = rnd[m];
+      Arc arc = digraph.addArc(nodes[s], nodes[t]);
+      capacity[arc] = c;
+      std::ostringstream os;
+      os << "arc \t" << i << std::endl;
+      name[arc] = os.str();
+      arcs.push_back(arc);
+    }
+
+
+    DigraphWriter<Digraph>(ss, digraph).
+      nodeMap("potential", potential).
+      arcMap("capacity", capacity).
+      arcMap("name", name).
+      node("source", nodes[0]).
+      node("target", nodes[1]).
+      arc("bottleneck", arcs[e / 2]).
+      attribute("creator", "lemon library").
+      run();
+
+  } catch (DataFormatError& error) {
+    std::cerr << error.what() << std::endl;
+  }
+
+  try {
+
+    typedef SmartDigraph Digraph;
+    typedef Digraph::Node Node;
+    typedef Digraph::Arc Arc;
+    typedef Digraph::ArcIt ArcIt;
+
+    typedef Digraph::NodeMap<int> LabelMap;
+    typedef Digraph::NodeMap<int> PotentialMap;
+    typedef Digraph::ArcMap<int> CapacityMap;
+    typedef Digraph::ArcMap<std::string> NameMap;
+
+    Digraph digraph;
+    LabelMap label(digraph);
+    PotentialMap potential(digraph);
+    CapacityMap capacity(digraph);
+    NameMap name(digraph);
+
+    Node s, t;
+    Arc a;
+    
+    std::string creator;
+
+    for (int i = 0; i < n; ++i) {
+      Node node = digraph.addNode();
+      label[node] = i;
+    }
+    
+    DigraphReader<Digraph>(ss, digraph).
+      useNodes(label).
+      nodeMap("potential", potential).
+      arcMap("capacity", capacity).
+      arcMap("name", name).
+      node("source", s).
+      node("target", t).
+      arc("bottleneck", a).
+      attribute("creator", creator).
+      run();
+
+    DigraphWriter<Digraph>(std::cout, digraph).
+      nodeMap("potential", potential).
+      arcMap("capacity", capacity).
+      arcMap("name", name).
+      node("source", s).
+      node("target", t).
+      arc("bottleneck", a).
+      attribute("creator", creator).
+      run();
+
+  } catch (DataFormatError& error) {
+    std::cerr << error.what() << std::endl;
+  }
+
+
+  return 0;
+}
diff -r e1dd2a70737c -r 1c9a9e2f7d4d lemon/Makefile.am
--- a/lemon/Makefile.am	Mon Apr 14 10:46:41 2008 +0200
+++ b/lemon/Makefile.am	Thu Apr 17 15:18:45 2008 +0100
@@ -27,6 +27,7 @@
 	lemon/error.h \
 	lemon/graph_utils.h \
 	lemon/kruskal.h \
+	lemon/lgf_reader.h \
 	lemon/list_graph.h \
 	lemon/maps.h \
 	lemon/math.h \
diff -r e1dd2a70737c -r 1c9a9e2f7d4d lemon/lgf_reader.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/lgf_reader.h	Thu Apr 17 15:18:45 2008 +0100
@@ -0,0 +1,914 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2008
+ * 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.
+ *
+ */
+
+///\ingroup lemon_io
+///\file
+///\brief Lemon Graph Format reader.
+
+
+#ifndef LEMON_LGF_READER_H
+#define LEMON_LGF_READER_H
+
+#include <iostream>
+#include <fstream>
+#include <sstream>
+
+#include <set>
+#include <map>
+
+#include <lemon/assert.h>
+#include <lemon/graph_utils.h>
+
+#include <lemon/lgf_writer.h>
+
+#include <lemon/concept_check.h>
+#include <lemon/concepts/maps.h>
+
+namespace lemon {
+
+  namespace _reader_bits {
+
+    template <typename Value>
+    struct DefaultConverter {
+      Value operator()(const std::string& str) {
+	std::istringstream is(str);
+	Value value;
+	is >> value;
+
+	char c;
+	if (is >> std::ws >> c) {
+	  throw DataFormatError("Remaining characters in token");
+	}
+	return value;
+      }
+    };
+
+    template <>
+    struct DefaultConverter<std::string> {
+      std::string operator()(const std::string& str) {
+	return str;
+      }
+    };
+
+    template <typename _Item>    
+    class MapStorageBase {
+    public:
+      typedef _Item Item;
+
+    public:
+      MapStorageBase() {}
+      virtual ~MapStorageBase() {}
+
+      virtual void set(const Item& item, const std::string& value) = 0;
+
+    };
+
+    template <typename _Item, typename _Map, 
+	      typename _Converter = DefaultConverter<typename _Map::Value> >
+    class MapStorage : public MapStorageBase<_Item> {
+    public:
+      typedef _Map Map;
+      typedef _Converter Converter;
+      typedef _Item Item;
+      
+    private:
+      Map& _map;
+      Converter _converter;
+
+    public:
+      MapStorage(Map& map, const Converter& converter = Converter()) 
+	: _map(map), _converter(converter) {}
+      virtual ~MapStorage() {}
+
+      virtual void set(const Item& item ,const std::string& value) {
+	_map.set(item, _converter(value));
+      }
+    };
+
+    class ValueStorageBase {
+    public:
+      ValueStorageBase() {}
+      virtual ~ValueStorageBase() {}
+
+      virtual void set(const std::string&) = 0;
+    };
+
+    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
+    class ValueStorage : public ValueStorageBase {



More information about the Lemon-commits mailing list