1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/work/deba/graph_io_test.cc Wed Dec 08 20:54:26 2004 +0000
1.3 @@ -0,0 +1,23 @@
1.4 +#include <lemon/smart_graph.h>
1.5 +#include <lemon/graph_reader.h>
1.6 +
1.7 +#include <iostream>
1.8 +#include <fstream>
1.9 +
1.10 +using namespace std;
1.11 +using namespace lemon;
1.12 +
1.13 +int main() {
1.14 + ifstream input("test.lgf");
1.15 + SmartGraph graph;
1.16 + GraphReader<SmartGraph> reader(input, graph);
1.17 + SmartGraph::NodeMap<int> cost(graph);
1.18 + reader.readNodeMap("cost", cost);
1.19 + SmartGraph::NodeMap<string> color(graph);
1.20 + reader.readNodeMap("color", color);
1.21 + reader.read();
1.22 + for (SmartGraph::NodeIt it(graph); it != INVALID; ++it) {
1.23 + cout << cost[it] << color[it] << endl;
1.24 + }
1.25 + return 0;
1.26 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/src/work/deba/graph_reader.h Wed Dec 08 20:54:26 2004 +0000
2.3 @@ -0,0 +1,212 @@
2.4 +/* -*- C++ -*-
2.5 + * src/lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library
2.6 + *
2.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
2.9 + *
2.10 + * Permission to use, modify and distribute this software is granted
2.11 + * provided that this copyright notice appears in all copies. For
2.12 + * precise terms see the accompanying LICENSE file.
2.13 + *
2.14 + * This software is provided "AS IS" with no warranty of any kind,
2.15 + * express or implied, and with no claim as to its suitability for any
2.16 + * purpose.
2.17 + *
2.18 + */
2.19 +
2.20 +///\ingroup gio
2.21 +///\file
2.22 +///\brief Map utilities.
2.23 +
2.24 +#include <iostream>
2.25 +#include <sstream>
2.26 +
2.27 +#include <map>
2.28 +#include <vector>
2.29 +
2.30 +#include <lemon/error.h>
2.31 +
2.32 +/// \todo fix exceptions
2.33 +
2.34 +
2.35 +namespace lemon {
2.36 +
2.37 + struct DefaultReaderTraits {
2.38 +
2.39 + template <typename _Value>
2.40 + struct Reader {
2.41 + typedef _Value Value;
2.42 + void read(std::istream& is, Value& value) {
2.43 + is >> value;
2.44 + }
2.45 + };
2.46 +
2.47 + template <typename _Value>
2.48 + struct Skipper {
2.49 + typedef _Value Value;
2.50 + void read(std::istream& is) {
2.51 + Value tmp;
2.52 + is >> tmp;
2.53 + }
2.54 + };
2.55 +
2.56 + struct DefaultSkipper {
2.57 + void read(std::istream& is) {
2.58 + std::string tmp;
2.59 + is >> tmp;
2.60 + }
2.61 + };
2.62 + };
2.63 +
2.64 + class IOException {
2.65 + virtual string message() = 0;
2.66 + };
2.67 +
2.68 + class FileReaderException {
2.69 + virtual int getLine() = 0;
2.70 + };
2.71 +
2.72 +
2.73 +
2.74 + template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits>
2.75 + class GraphReader {
2.76 +
2.77 + public:
2.78 +
2.79 + typedef _Graph Graph;
2.80 + typedef typename Graph::Node Node;
2.81 + typedef typename Graph::Edge Edge;
2.82 +
2.83 + typedef _ReaderTraits ReaderTraits;
2.84 +
2.85 +
2.86 + GraphReader(std::istream& _is, Graph& _graph) : is(_is), graph(_graph) {}
2.87 +
2.88 + template <typename Map>
2.89 + GraphReader& readNodeMap(std::string name, Map& map,
2.90 + const typename ReaderTraits::template Reader<typename Map::Value>& reader =
2.91 + typename ReaderTraits::template Reader<typename Map::Value>()) {
2.92 + return readNodeMap<Map, typename ReaderTraits::template Reader<typename Map::Value> >(name, map, reader);
2.93 + }
2.94 +
2.95 + template <typename Map, typename Reader>
2.96 + GraphReader& readNodeMap(std::string name, Map& map, const Reader& reader = Reader()) {
2.97 + node_readers.insert(make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
2.98 + return *this;
2.99 + }
2.100 +
2.101 + template <typename Map>
2.102 + GraphReader& readEdgeMap(std::string name, Map& map,
2.103 + const typename ReaderTraits::template Reader<typename Map::Value>& reader =
2.104 + typename ReaderTraits::template Reader<typename Map::Value>()) {
2.105 + return readEdgeMap<Map, typename ReaderTraits::template Reader<typename Map::Value> >(name, map, reader);
2.106 + }
2.107 +
2.108 + template <typename Map, typename Reader>
2.109 + GraphReader& readEdgeMap(std::string name, Map& map, const Reader& reader = Reader()) {
2.110 + edge_readers.insert(make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
2.111 + return *this;
2.112 + }
2.113 +
2.114 + void read() {
2.115 + int line_num = 0;
2.116 + readNodeSet(line_num);
2.117 + readEdgeSet(line_num);
2.118 + {
2.119 + std::string line = readNotEmptyLine(is, line_num);
2.120 + }
2.121 + }
2.122 +
2.123 + private:
2.124 +
2.125 + void readNodeSet(int& line_num) {
2.126 + int n = 0;
2.127 + {
2.128 + std::string line = readNotEmptyLine(is, line_num);
2.129 + }
2.130 + std::vector<MapReaderBase<Node>*> index;
2.131 + {
2.132 + std::string line = readNotEmptyLine(is, line_num);
2.133 + std::string id;
2.134 + std::istringstream ls(line);
2.135 + while (ls >> id) {
2.136 + typename map<std::string, MapReaderBase<Node>* >::iterator it = node_readers.find(id);
2.137 + if (it != node_readers.end()) {
2.138 + index.push_back(it->second);
2.139 + } else {
2.140 + index.push_back(0);
2.141 + }
2.142 + ++n;
2.143 + }
2.144 + }
2.145 + std::string line;
2.146 + while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
2.147 + Node node = graph.addNode();
2.148 + std::istringstream ls(line);
2.149 + for (int i = 0; i < n; ++i) {
2.150 + if (index[i] != 0) {
2.151 + index[i]->read(ls, node);
2.152 + } else {
2.153 + default_skipper.read(ls);
2.154 + }
2.155 + }
2.156 + }
2.157 + }
2.158 +
2.159 + void readEdgeSet(int& line_num) {
2.160 +
2.161 + }
2.162 +
2.163 + std::string readNotEmptyLine(std::istream& is, int& line_num) {
2.164 + std::string line;
2.165 + while (++line_num, getline(is, line)) {
2.166 + int vi = line.find_first_not_of(" \t");
2.167 + if (vi != string::npos && line[vi] != '#') {
2.168 + return line.substr(vi);
2.169 + }
2.170 + }
2.171 + throw Exception();
2.172 + }
2.173 +
2.174 + istream& is;
2.175 + Graph& graph;
2.176 +
2.177 + typename ReaderTraits::DefaultSkipper default_skipper;
2.178 +
2.179 + template <typename _Item>
2.180 + class MapReaderBase {
2.181 + public:
2.182 + typedef _Item Item;
2.183 + virtual void read(istream& is, const Item& item) = 0;
2.184 + };
2.185 +
2.186 + template <typename _Item, typename _Map, typename _Reader>
2.187 + class MapReader : public MapReaderBase<_Item> {
2.188 + public:
2.189 + typedef _Map Map;
2.190 + typedef _Reader Reader;
2.191 + typedef _Item Item;
2.192 +
2.193 + Map& map;
2.194 + Reader reader;
2.195 +
2.196 + MapReader(Map& _map, const Reader& _reader)
2.197 + : map(_map), reader(_reader) {}
2.198 +
2.199 +
2.200 + virtual void read(istream& is, const Item& item) {
2.201 + typename Reader::Value value;
2.202 + reader.read(is, value);
2.203 + map.set(item, value);
2.204 + }
2.205 + };
2.206 +
2.207 + typedef std::map<std::string, MapReaderBase<Node>* > NodeReaders;
2.208 + NodeReaders node_readers;
2.209 +
2.210 + typedef std::map<std::string, MapReaderBase<Edge>* > EdgeReaders;
2.211 + EdgeReaders edge_readers;
2.212 +
2.213 + };
2.214 +
2.215 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/src/work/deba/map_utils.h Wed Dec 08 20:54:26 2004 +0000
3.3 @@ -0,0 +1,98 @@
3.4 +/* -*- C++ -*-
3.5 + * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
3.6 + *
3.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
3.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
3.9 + *
3.10 + * Permission to use, modify and distribute this software is granted
3.11 + * provided that this copyright notice appears in all copies. For
3.12 + * precise terms see the accompanying LICENSE file.
3.13 + *
3.14 + * This software is provided "AS IS" with no warranty of any kind,
3.15 + * express or implied, and with no claim as to its suitability for any
3.16 + * purpose.
3.17 + *
3.18 + */
3.19 +
3.20 +///\ingroup gutils
3.21 +///\file
3.22 +///\brief Map utilities.
3.23 +
3.24 +#include <map>
3.25 +
3.26 +
3.27 +namespace lemon {
3.28 +
3.29 + /// \addtogroup gutils
3.30 + /// @{
3.31 +
3.32 +
3.33 + /// \brief General inversable map type.
3.34 +
3.35 + /// This type provides simple inversable map functions.
3.36 + /// The InversableMap wraps an arbitrary ReadWriteMap
3.37 + /// and if a key is setted to a new value then store it
3.38 + /// in the inverse map.
3.39 + template <
3.40 + typename _Graph,
3.41 + typename _Map,
3.42 + template <typename, typename> class _InvMap = std::Map
3.43 + >
3.44 + class InversableMap : protected _Map {
3.45 +
3.46 + public:
3.47 +
3.48 + typename _Map Map;
3.49 + typename _InvMap<Map::Value, Map::Key> InverseMap;
3.50 +
3.51 + typename _Map::Key Key;
3.52 + typename _Map::Value Value;
3.53 + typename _Map::ConstReference ConstReference;
3.54 +
3.55 + /// Constructor.
3.56 +
3.57 + /// Construct a new InversableMap for the graph.
3.58 + ///
3.59 + InversableMap(const Graph& graph) : Map(graph) {}
3.60 +
3.61 + /// The setter function of the map.
3.62 +
3.63 + /// It sets the map and the inverse map
3.64 + void set(const Key& key, const Value& val) {
3.65 + Value oldval = Map::operator[](key);
3.66 + InverseMap::iterator it = invMap.find(oldval);
3.67 + if (it != invMap.end() && it->second == key) {
3.68 + invMap.erase(it);
3.69 + }
3.70 + invMap.insert(make_pair(val, key));
3.71 + Map::set(key, val);
3.72 + }
3.73 +
3.74 + ConstReference operator[](const Key&) const {
3.75 + return Map::operator[](key);
3.76 + }
3.77 +
3.78 + virtual void add(const Key&) {
3.79 + Map::add(key);
3.80 + }
3.81 +
3.82 + virtual void erase(const Key&) {
3.83 + Value val = Map::operator[](key);
3.84 + InverseMap::iterator it = invMap.find(val);
3.85 + if (it != invMap.end() && it->second == key) {
3.86 + invMap.erase(it);
3.87 + }
3.88 + Map::erase(key);
3.89 + }
3.90 +
3.91 + const InverseMap& inverse() const {
3.92 + return invMap;
3.93 + }
3.94 +
3.95 +
3.96 + private:
3.97 + InverseMap invMap;
3.98 + };
3.99 +
3.100 +}
3.101 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/src/work/deba/test.lgf Wed Dec 08 20:54:26 2004 +0000
4.3 @@ -0,0 +1,30 @@
4.4 +
4.5 +@nodes
4.6 +
4.7 +id cost color
4.8 +
4.9 +1 1 green
4.10 +2 2 blue
4.11 +# hatalom dicsoseg
4.12 +3 1 red
4.13 +4 2 red
4.14 +5 1 green
4.15 +10 1 green # ez nem egy igazan jo sor
4.16 + # hello - bello csucsok
4.17 +6 2 blue
4.18 +7 1 blue
4.19 +8 2 red
4.20 +9 1 green
4.21 +@edges
4.22 +source target weight
4.23 +1 4 10
4.24 +3 5 29
4.25 +3 4 92
4.26 +2 3 92
4.27 +9 5 49
4.28 +10 4 40
4.29 +1 3 84
4.30 +6 7 83
4.31 +8 9 37
4.32 +7 8 89
4.33 +@end