ROSE  0.9.9.139
GraphUtility.h
1 #ifndef ROSE_GraphUtility_H
2 #define ROSE_GraphUtility_H
3 
4 #include <boost/cstdint.hpp>
5 #include <Sawyer/Graph.h>
6 
7 namespace Rose {
8 
10 namespace GraphUtility {
11 
15 template<class Graph>
16 void
17 serialize(std::ostream &output, Graph &graph)
18 {
19  boost::uint32_t n = 0;
20  output.write((const char*)&n, 4);
21 
22  n = graph.nVertices();
23  output.write((const char*)&n, 4);
24 
25  n = graph.nEdges();
26  output.write((const char*)&n, 4);
27 
28  for (size_t i=0; i<graph.nVertices(); ++i) {
29  typename Graph::ConstVertexIterator vertex = graph.findVertex(i);
30  vertex->value().serialize(output);
31  }
32 
33  for (size_t i=0; i<graph.nEdges(); ++i) {
34  typename Graph::ConstEdgeIterator edge = graph.findEdge(i);
35  n = edge->source()->id();
36  output.write((const char*)&n, 4);
37  n = edge->target()->id();
38  output.write((const char*)&n, 4);
39  edge->value().serialize(output);
40  }
41 }
42 
43 template<class Graph>
44 void
45 deserialize(std::istream &input, Graph &graph)
46 {
47  graph.clear();
48  boost::uint32_t n;
49 
50  input.read((char*)&n, 4);
51  ASSERT_require2(n==0, "incorrect version number");
52 
53  input.read((char*)&n, 4);
54  size_t nverts = n;
55 
56  input.read((char*)&n, 4);
57  size_t nedges = n;
58 
59  for (size_t i=0; i<nverts; ++i) {
60  typename Graph::VertexIterator vertex = graph.insertVertex();
61  ASSERT_require2(vertex->id()==i, "unexpected vertex numbering");
62  vertex->value().deserialize(input);
63  }
64 
65  for (size_t i=0; i<nedges; ++i) {
66  input.read((char*)&n, 4);
67  typename Graph::VertexIterator source = graph.findVertex(n);
68  input.read((char*)&n, 4);
69  typename Graph::VertexIterator target = graph.findVertex(n);
70  typename Graph::EdgeIterator edge = graph.insertEdge(source, target);
71  ASSERT_require2(edge->id()==i, "unexpected edge numbering");
72  edge->value().deserialize(input);
73  }
74 }
75 
76 } // namespace
77 } // namespace
78 #endif
void serialize(std::ostream &output, Graph &graph)
Serialize a graph into a stream of bytes.
Definition: GraphUtility.h:17
Main namespace for the ROSE library.