134 |
133 |
135 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} |
134 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} |
136 |
135 |
137 |
136 |
138 ~GraphWriter() { |
137 ~GraphWriter() { |
139 |
138 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); |
140 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); it != node_map_writers.end(); ++it) { |
139 it != node_map_writers.end(); ++it) { |
141 delete it->second; |
140 delete it->second; |
142 } |
141 } |
143 |
142 |
144 for (typename EdgeMapWriters::iterator it = edge_map_writers.begin(); it != edge_map_writers.end(); ++it) { |
143 for (typename EdgeMapWriters::iterator it = edge_map_writers.begin(); |
|
144 it != edge_map_writers.end(); ++it) { |
145 delete it->second; |
145 delete it->second; |
146 } |
146 } |
147 |
147 |
148 } |
148 } |
149 |
149 |
150 // Node map rules |
150 // Node map rules |
151 |
151 |
152 template <typename Map> |
152 template <typename Map> |
153 GraphWriter& writeNodeMap(std::string name, const Map& map) { |
153 GraphWriter& addNodeMap(std::string name, const Map& map) { |
154 return writeNodeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map); |
154 return addNodeMap<typename WriterTraits::template Writer< |
|
155 typename Map::Value>, Map>(name, map); |
155 } |
156 } |
156 |
157 |
157 template <typename Writer, typename Map> |
158 template <typename Writer, typename Map> |
158 GraphWriter& writeNodeMap(std::string name, const Map& map, const Writer& writer = Writer()) { |
159 GraphWriter& addNodeMap(std::string name, const Map& map, |
|
160 const Writer& writer = Writer()) { |
159 // if (node_map_writers.find(name) != node_map_writers.end()) { |
161 // if (node_map_writers.find(name) != node_map_writers.end()) { |
160 // throw Exception() << "Multiple write rule for node map: " << name; |
162 // throw Exception() << "Multiple write rule for node map: " |
|
163 // << name; |
161 // } |
164 // } |
162 node_map_writers.push_back(make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); |
165 node_map_writers.push_back( |
|
166 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); |
163 return *this; |
167 return *this; |
164 } |
168 } |
165 |
169 |
166 // Edge map rules |
170 // Edge map rules |
167 |
171 |
168 template <typename Map> |
172 template <typename Map> |
169 GraphWriter& writeEdgeMap(std::string name, const Map& map) { |
173 GraphWriter& addEdgeMap(std::string name, const Map& map) { |
170 return writeEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map); |
174 return addEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map); |
171 } |
175 } |
172 |
176 |
173 |
177 |
174 template <typename Writer, typename Map> |
178 template <typename Writer, typename Map> |
175 GraphWriter& writeEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { |
179 GraphWriter& addEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { |
176 // if (edge_map_writers.find(name) != edge_map_writers.end()) { |
180 // if (edge_map_writers.find(name) != edge_map_writers.end()) { |
177 // throw Exception() << "Multiple write rule for edge map: " << name; |
181 // throw Exception() << "Multiple write rule for edge map: " << name; |
178 // } |
182 // } |
179 edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); |
183 edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); |
180 return *this; |
184 return *this; |
181 } |
185 } |
182 |
186 |
183 // Node rules |
187 // Node rules |
184 GraphWriter& writeNode(std::string name, const Node& node) { |
188 GraphWriter& addNode(std::string name, const Node& node) { |
185 // if (node_writers.find(name) != node_writers.end()) { |
189 // if (node_writers.find(name) != node_writers.end()) { |
186 // throw Exception() << "Multiple write rule for node"; |
190 // throw Exception() << "Multiple write rule for node"; |
187 // } |
191 // } |
188 node_writers.push_back(make_pair(name, node)); |
192 node_writers.push_back(make_pair(name, node)); |
|
193 return *this; |
189 } |
194 } |
190 |
195 |
191 // Edge rules |
196 // Edge rules |
192 |
197 |
193 GraphWriter& writeEdge(std::string name, const Edge& edge) { |
198 GraphWriter& addEdge(std::string name, const Edge& edge) { |
194 // if (edge_writers.find(name) != edge_writers.end()) { |
199 // if (edge_writers.find(name) != edge_writers.end()) { |
195 // throw Exception() << "Multiple write rule for edge"; |
200 // throw Exception() << "Multiple write rule for edge"; |
196 // } |
201 // } |
197 edge_writers.push_back(make_pair(name, edge)); |
202 edge_writers.push_back(make_pair(name, edge)); |
|
203 return *this; |
198 } |
204 } |
199 |
205 |
200 void write() { |
206 void write() { |
201 writeNodeSet(); |
207 writeNodeSet(); |
202 writeEdgeSet(); |
208 writeEdgeSet(); |
228 if (node_map_writers.size() == 0) { |
234 if (node_map_writers.size() == 0) { |
229 throw Exception() << "Missing node id map"; |
235 throw Exception() << "Missing node id map"; |
230 } |
236 } |
231 os << "@edgeset" << std::endl; |
237 os << "@edgeset" << std::endl; |
232 os << "\t\t"; |
238 os << "\t\t"; |
233 for (int i = 0; i < edge_map_writers.size(); ++i) { |
239 for (int i = 0; i < (int)edge_map_writers.size(); ++i) { |
234 os << edge_map_writers[i].first << '\t'; |
240 os << edge_map_writers[i].first << '\t'; |
235 } |
241 } |
236 os << std::endl; |
242 os << std::endl; |
237 for (EdgeIt it(graph); it != INVALID; ++it) { |
243 for (EdgeIt it(graph); it != INVALID; ++it) { |
238 node_map_writers[0].second->write(os, graph.source(it)); |
244 node_map_writers[0].second->write(os, graph.source(it)); |
239 node_map_writers[0].second->write(os, graph.target(it)); |
245 node_map_writers[0].second->write(os, graph.target(it)); |
240 for (int i = 0; i < edge_map_writers.size(); ++i) { |
246 for (int i = 0; i < (int)edge_map_writers.size(); ++i) { |
241 edge_map_writers[i].second->write(os, it); |
247 edge_map_writers[i].second->write(os, it); |
242 } |
248 } |
243 os << std::endl; |
249 os << std::endl; |
244 } |
250 } |
245 } |
251 } |
293 MapWriter(const Map& _map, const Writer& _writer) |
299 MapWriter(const Map& _map, const Writer& _writer) |
294 : map(_map), writer(_writer) {} |
300 : map(_map), writer(_writer) {} |
295 |
301 |
296 |
302 |
297 virtual void write(std::ostream& os, const Item& item) { |
303 virtual void write(std::ostream& os, const Item& item) { |
298 Value value; |
|
299 writer.write(os, map[item]); |
304 writer.write(os, map[item]); |
300 } |
305 } |
301 |
306 |
302 }; |
307 }; |
303 |
308 |
304 |
309 |
305 |
310 |
306 typedef std::vector< std::pair<std::string, WriterBase<Node>*> > NodeMapWriters; |
311 typedef std::vector< std::pair<std::string, WriterBase<Node>*> > |
|
312 NodeMapWriters; |
307 NodeMapWriters node_map_writers; |
313 NodeMapWriters node_map_writers; |
308 |
314 |
309 typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > EdgeMapWriters; |
315 typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > |
|
316 EdgeMapWriters; |
310 EdgeMapWriters edge_map_writers; |
317 EdgeMapWriters edge_map_writers; |
311 |
318 |
312 typedef std::vector<std::pair<std::string, Node> > NodeWriters; |
319 typedef std::vector<std::pair<std::string, Node> > NodeWriters; |
313 NodeWriters node_writers; |
320 NodeWriters node_writers; |
314 |
321 |