KrisLibrary  1.0.0
graph/IO.h
1 #ifndef GRAPH_IO_H
2 #define GRAPH_IO_H
3 
4 #include "Graph.h"
5 #include "Operations.h"
6 #include <string>
7 #include <sstream>
8 #include <map>
9 #include <stdlib.h>
11 
12 namespace Graph {
13 
17 bool Load_TGF(std::istream& in,Graph<std::string,std::string>& G);
18 
22 void Save_TGF(std::ostream& o,const Graph<std::string,std::string>& G);
23 
27 template <class N,class E>
29 {
30  CopyStructure(G,Gs);
31  for(size_t i=0;i<G.nodes.size();i++) {
32  std::stringstream ss;
33  ss << G.nodes[i];
34  Gs.nodes[i] = ss.str();
35  }
36 }
37 
41 template <class N,class E>
43 {
44  CopyStructure(Gs,G);
45  for(size_t i=0;i<G.nodes.size();i++) {
46  std::stringstream ss(Gs.nodes[i]);
47  ss >> G.nodes[i];
48  if(ss.bad()) return false;
49  }
50  return true;
51 }
52 
56 template <class N,class E>
58 {
59  CopyStructure(G,Gs);
60  for(size_t i=0;i<G.nodes.size();i++) {
61  std::stringstream ss;
62  ss << G.nodes[i];
63  Gs.nodes[i] = ss.str();
64  }
65  for(size_t i=0;i<G.nodes.size();i++) {
68  G.Begin(i,e);
69  Gs.Begin(i,es);
70  while(!e.done()) {
71  std::stringstream ss;
72  ss<<*e;
73  *es = ss.str();
74  ++e;
75  ++es;
76  }
77  }
78 }
79 
83 template <class N,class E>
85 {
86  CopyStructure(Gs,G);
87  for(size_t i=0;i<G.nodes.size();i++) {
88  std::stringstream ss(Gs.nodes[i]);
89  ss >> G.nodes[i];
90  if(ss.bad()) return false;
91  }
92  for(size_t i=0;i<G.nodes.size();i++) {
95  G.Begin(i,e);
96  Gs.Begin(i,es);
97  while(!e.done()) {
98  std::stringstream ss(*es);
99  ss>>*e;
100  if(ss.bad()) return false;
101  ++e;
102  ++es;
103  }
104  }
105  return true;
106 }
107 
108 
109 } //namespace Graph
110 
111 #endif
Definition: Edge.h:28
void NodesToStrings(const Graph< N, E > &G, Graph< std::string, std::string > &Gs)
Serializes the graph&#39;s nodes using the ostream << operator.
Definition: graph/IO.h:28
void NodesEdgesToStrings(const Graph< N, E > &G, Graph< std::string, std::string > &Gs)
Serializes the graph&#39;s nodes and edges using the ostream << operator.
Definition: graph/IO.h:57
bool Load_TGF(std::istream &in, Graph< std::string, std::string > &G)
Loads a graph from the Trivial Graph Format.
Definition: graph/IO.cpp:6
void CopyStructure(const Graph< N1, E1 > &G1, Graph< N2, E2 > &G2)
Copies the edge structure of G1 to G2 without setting its data.
Definition: Operations.h:14
void Save_TGF(std::ostream &o, const Graph< std::string, std::string > &G)
Saves a graph to the Trivial Graph Format.
Definition: graph/IO.cpp:55
bool NodesEdgesFromStrings(const Graph< std::string, std::string > &Gs, Graph< N, E > &G)
De-serializes the graph&#39;s nodes and edges using the istream >> operator.
Definition: graph/IO.h:84
bool NodesFromStrings(const Graph< std::string, std::string > &Gs, Graph< N, E > &G)
De-serializes the graph&#39;s nodes using the istream >> operator.
Definition: graph/IO.h:42
Namespace for all classes and functions in the Graph subdirectory.
Definition: ApproximateShortestPaths.h:7
Utilities for string manipulation.
Basic template graph structure.
Definition: Graph.h:47