3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_ADTS_GRAPHS_GRAPHML_H
7#define ELEMENTA_ADTS_GRAPHS_GRAPHML_H
50template <
class Vertex,
class Edge>
63 const std::string & eol =
"\r\n"):
66 withvit0_{false},witheit0_{false},
69 {
if (title.find(
'"') != std::string::npos)
70 ELE_CODE_INVARG(
"Graph title cannot contain double quote characters"); }
80 { vit0_ = vit0; withvit0_ =
true; }
85 { eit0_ = eit0; witheit0_ =
true; }
97 std::string tit_,eol_;
98 bool withvit0_,witheit0_;
119template <
class Vertex,
class Edge>
128 szerstr.
ser(chout,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
130 "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\""
132 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
134 "\txsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns"
136 "\t\thttp://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">"
138 "<key attr.name=\"nodedata\" attr.type=\"string\" "
139 "for=\"node\" id=\"d0\"/>"
141 "<key attr.name=\"edgedata\" attr.type=\"string\" "
142 "for=\"edge\" id=\"d1\"/>"
144 "<key attr.name=\"label\" attr.type=\"string\" "
145 "for=\"node\" id=\"d3\"/>"
148 szerstr.ser(chout,
"<graph ");
149 if (!tit_.empty()) szerstr.ser(chout,
"id=\"" + tit_ +
"\" ");
150 szerstr.ser(chout,
"edgedefault=\"directed\" parse.nodes=\"");
152 szerstr.ser(chout,
"\" parse.edges=\"");
153 szerstr.ser(chout,std::to_string(graph.
size_edges()));
154 szerstr.ser(chout,
"\" >" + eol_);
160 if (withvit0_) vit = vit0_;
168 ELE_CODE_TRACE({},
"Direct and indirect maps of vertices created with " <<
169 vp2i.size() <<
" items");
171 for (
size_t pos = 0; pos < vp2i.size(); ++pos)
173 szerstr.ser(chout,
"\t<node id=\"n");
174 szerstr.ser(chout,std::to_string(pos));
175 szerstr.ser(chout,
"\">" + eol_ +
"\t\t<data key=\"d3\">#" +
176 std::to_string(pos) +
178 if (szerv_ !=
nullptr)
180 szerstr.ser(chout,
"\t\t<data key=\"d0\">");
181 szerv_->ser(chout,*(vp2i[pos]));
182 szerstr.ser(chout,
"</data>" + eol_);
184 szerstr.ser(chout,
"\t</node>" + eol_);
191 if (witheit0_) e = eit0_;
198 szerstr.ser(chout,
"\t<edge id=\"e");
199 szerstr.ser(chout,std::to_string(pe));
200 szerstr.ser(chout,
"\" directed=\"true\" source=\"n");
201 szerstr.ser(chout,std::to_string(vi2p.at(v0)));
202 szerstr.ser(chout,
"\" target=\"n");
203 szerstr.ser(chout,std::to_string(vi2p.at(v1)));
204 szerstr.ser(chout,
"\">" + eol_);
205 if (szere_ !=
nullptr)
207 szerstr.ser(chout,
"\t\t<data key=\"d1\">");
208 szere_->ser(chout,*e);
209 szerstr.ser(chout,
"</data>" + eol_);
211 szerstr.ser(chout,
"\t</edge>" + eol_);
219 szerstr.ser(chout,
"</graph>" + eol_);
220 szerstr.ser(chout,
"</graphml>");
eiterator begin(void) const
Must return an iterator pointing to the first element.
A map (hash) from vertex iterators in a graph to their num. positions.
viterator begin(void) const
Must return an iterator pointing to the first element.
A vector of vertex iterators (might be repeated).
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
void setInitialVIt(const typename BaseGraph::viterator vit0)
Set VIT0 as the vertex iterator to scan all vertices of the graph.
void setInitialEIt(const typename BaseGraph::eiterator eit0)
Set EIT0 as the edge iterator to scan all edges of the graph.
Szer_GraphML(const std::string &title, elementa::base::Serializer< Vertex > *szerv=nullptr, elementa::base::Serializer< Edge > *szere=nullptr, const std::string &eol="\r\n")
Constructor. SZERV and SZERE are szers for vertex/edge data.
A serializer for any implementation of graphs that generates GraphML format.
void ser(elementa::base::OutSerCh &chout, const BaseGraph &graph)
Serializes the data.
virtual void connection(const eiterator &it, viterator &vorg, viterator &vdest) const
Fill VORG and VDEST with VRange_All iterators to the edge vertices.
virtual size_edge size_edges(void) const =0
Must return the number of edges.
virtual size_vertex size_vertices(void) const =0
Must return the number of vertices.
elementa::base::MultIterator< Edge > eiterator
Edge iterator. ForwardLegacyIterator.
elementa::base::MultIterator< Vertex > viterator
Vertex iterator. ForwardLegacyIterator.
VRange_All vrangeAll(void) const
Return a range to iterate on all Vertex data, no special order.
ERange_All erangeAll(void) const
Return a range to iterate on all Edge data, no special order.
All graph classes derive from this one.
size_t size_edge
For holding numbers of edges of a graph.
bool isEnd(void) const
Check for end. If it is invalid or points not to end, return false.
A mutable iterator that can have multiple iteration behaviors. Forward case.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
void ser(OutSerCh &chout, const std::string &s)
Serialization of a string into CHOUT.
Base abstract class / interface for any serializer.
Serializer of std::string into fixed or non-fixed size.