Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
graphml.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_ADTS_GRAPHS_GRAPHML_H
7#define ELEMENTA_ADTS_GRAPHS_GRAPHML_H
8
10
12
13
14namespace elementa
15{
16
17namespace adts
18{
19
20namespace graphs
21{
22
43/* ***************************************************************************
44
45 Base class: Szer_GraphML
46
47*******************************************************************************/
48
50template <class Vertex, class Edge>
51class Szer_GraphML: public elementa::base::Serializer<Graph<Vertex,Edge>>
52{
53 public:
54
56
58
60 Szer_GraphML(const std::string & title,
61 elementa::base::Serializer<Vertex> * szerv = nullptr,
62 elementa::base::Serializer<Edge> * szere = nullptr,
63 const std::string & eol = "\r\n"):
64 tit_{title},
65 eol_{eol},
66 withvit0_{false},witheit0_{false},
67 szerv_{szerv},
68 szere_{szere}
69 { if (title.find('"') != std::string::npos)
70 ELE_CODE_INVARG("Graph title cannot contain double quote characters"); }
71
72 Szer_GraphML(const Szer_GraphML &) = delete;
73 Szer_GraphML(Szer_GraphML &&) = delete;
74 Szer_GraphML & operator=(const Szer_GraphML &) = delete;
75 Szer_GraphML & operator=(Szer_GraphML &&) = delete;
76
78
79 void setInitialVIt(const typename BaseGraph::viterator vit0)
80 { vit0_ = vit0; withvit0_ = true; }
81
83
84 void setInitialEIt(const typename BaseGraph::eiterator eit0)
85 { eit0_ = eit0; witheit0_ = true; }
86
88
90 void ser(elementa::base::OutSerCh & chout, const BaseGraph & graph);
91
92 // Unimplemented deserializations.
93
94
95 private:
96
97 std::string tit_,eol_;
98 bool withvit0_,witheit0_;
99 typename BaseGraph::viterator vit0_;
100 typename BaseGraph::eiterator eit0_;
103};
104
105
106
107/* ===========================================================================
108
109 ========== IMPLEMENTATION OF TEMPLATES ==========
110
111============================================================================= */
112
113/* ***************************************************************************
114
115 Template class: Szer_GraphML
116
117*******************************************************************************/
118
119template <class Vertex, class Edge>
121 const BaseGraph & graph)
123
125
126 // header
127
128 szerstr.ser(chout,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
129 + eol_ +
130 "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\""
131 + eol_ +
132 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
133 + eol_ +
134 "\txsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns"
135 + eol_ +
136 "\t\thttp://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">"
137 + eol_ +
138 "<key attr.name=\"nodedata\" attr.type=\"string\" "
139 "for=\"node\" id=\"d0\"/>"
140 + eol_ +
141 "<key attr.name=\"edgedata\" attr.type=\"string\" "
142 "for=\"edge\" id=\"d1\"/>"
143 + eol_ +
144 "<key attr.name=\"label\" attr.type=\"string\" "
145 "for=\"node\" id=\"d3\"/>"
146 + eol_);
147
148 szerstr.ser(chout,"<graph ");
149 if (!tit_.empty()) szerstr.ser(chout,"id=\"" + tit_ + "\" ");
150 szerstr.ser(chout,"edgedefault=\"directed\" parse.nodes=\"");
151 szerstr.ser(chout,std::to_string(graph.size_vertices()));
152 szerstr.ser(chout,"\" parse.edges=\"");
153 szerstr.ser(chout,std::to_string(graph.size_edges()));
154 szerstr.ser(chout,"\" >" + eol_);
155
156 // vertices
157
158 typename BaseGraph::VectorOfVIts vp2i;
159 typename BaseGraph::viterator vit;
160 if (withvit0_) vit = vit0_;
161 else vit = graph.vrangeAll().begin();
162 while (!vit.isEnd())
163 {
164 vp2i.push_back(vit);
165 ++vit;
166 }
167 typename BaseGraph::MapOfVItPos vi2p{vp2i}; // same positions as in vp2i
168 ELE_CODE_TRACE({},"Direct and indirect maps of vertices created with " <<
169 vp2i.size() << " items");
170
171 for (size_t pos = 0; pos < vp2i.size(); ++pos)
172 {
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) +
177 "</data>" + eol_);
178 if (szerv_ != nullptr)
179 {
180 szerstr.ser(chout,"\t\t<data key=\"d0\">");
181 szerv_->ser(chout,*(vp2i[pos]));
182 szerstr.ser(chout,"</data>" + eol_);
183 }
184 szerstr.ser(chout,"\t</node>" + eol_);
185 }
186
187 // edges
188
189 typename BaseGraph::viterator v0,v1;
190 typename BaseGraph::eiterator e;
191 if (witheit0_) e = eit0_;
192 else e = graph.erangeAll().begin();
193 size_edge pe{0};
194 while (!e.isEnd())
195 {
196 graph.connection(e,v0,v1);
197
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)
206 {
207 szerstr.ser(chout,"\t\t<data key=\"d1\">");
208 szere_->ser(chout,*e);
209 szerstr.ser(chout,"</data>" + eol_);
210 }
211 szerstr.ser(chout,"\t</edge>" + eol_);
212
213 ++e;
214 ++pe;
215 }
216
217 // footer
218
219 szerstr.ser(chout,"</graph>" + eol_);
220 szerstr.ser(chout,"</graphml>");
221}
222
223
224 // GraphML
226
227} // end graphs namespace
228
229} // end adts namespace
230
231} // end elementa namespace
232
233
234#endif
235
236
eiterator begin(void) const
Must return an iterator pointing to the first element.
Definition: graphs.h:353
A map (hash) from vertex iterators in a graph to their num. positions.
Definition: graphs.h:174
viterator begin(void) const
Must return an iterator pointing to the first element.
Definition: graphs.h:298
A vector of vertex iterators (might be repeated).
Definition: graphs.h:131
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
Definition: debugging.h:283
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
Definition: exceptions.h:310
void setInitialVIt(const typename BaseGraph::viterator vit0)
Set VIT0 as the vertex iterator to scan all vertices of the graph.
Definition: graphml.h:79
void setInitialEIt(const typename BaseGraph::eiterator eit0)
Set EIT0 as the edge iterator to scan all edges of the graph.
Definition: graphml.h:84
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.
Definition: graphml.h:60
A serializer for any implementation of graphs that generates GraphML format.
Definition: graphml.h:52
void ser(elementa::base::OutSerCh &chout, const BaseGraph &graph)
Serializes the data.
Definition: graphml.h:120
virtual void connection(const eiterator &it, viterator &vorg, viterator &vdest) const
Fill VORG and VDEST with VRange_All iterators to the edge vertices.
Definition: graphs.h:574
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.
Definition: graphs.h:121
elementa::base::MultIterator< Vertex > viterator
Vertex iterator. ForwardLegacyIterator.
Definition: graphs.h:118
VRange_All vrangeAll(void) const
Return a range to iterate on all Vertex data, no special order.
Definition: graphs.h:721
ERange_All erangeAll(void) const
Return a range to iterate on all Edge data, no special order.
Definition: graphs.h:730
All graph classes derive from this one.
Definition: graphs.h:107
size_t size_edge
For holding numbers of edges of a graph.
Definition: graphs.h:71
bool isEnd(void) const
Check for end. If it is invalid or points not to end, return false.
Definition: iterators.h:395
A mutable iterator that can have multiple iteration behaviors. Forward case.
Definition: iterators.h:299
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
Definition: basics.h:247
void ser(OutSerCh &chout, const std::string &s)
Serialization of a string into CHOUT.
Base abstract class / interface for any serializer.
Definition: serializers.h:189
Serializer of std::string into fixed or non-fixed size.
Definition: serializers.h:427