Euphoria
dotoutput.cc
Go to the documentation of this file.
1 #include "core/dotoutput.h"
2 
3 #include <iostream>
4 
5 #include "core/stdutils.h"
6 
7 
8 
9 namespace eu::core::dot
10 {
11  // std::vector<Node> nodes;
12  // std::vector<Edge> edges;
13 
14  Node* Graph::add_new_node(const std::string& name)
15  {
16  const auto id = nodes.size();
17  nodes.emplace_back();
18 
19  Node* r = &nodes[id];
20  r->id = static_cast<NodeId>(id);
21  r->label = name;
22 
23  return r;
24  }
25 
26 
27  Edge*
29  {
30  const auto id = edges.size();
31  edges.emplace_back();
32 
33  Edge* r = &edges[id];
34  r->from = from;
35  r->to = to;
36 
37  return r;
38  }
39 
40  std::string
42  {
43  return fmt::format("node_{}", core::base_cast(id));
44  }
45 
46  void
48  {
49  constexpr const auto* const i = " ";
50  auto& out = std::cout;
51 
52  out << "digraph G\n";
53  out << "{\n";
54  for(const auto& node: nodes)
55  {
56  // todo(Gustav): escape label
57  out << i << from_id_to_string(node.id) << "[" << "label=\"" << node.label << "\"];\n";
58  }
59 
60  // todo(Gustav): sort edges?
61  for(const auto& e: edges)
62  {
63  out << i << from_id_to_string(e.from) << " -> " << from_id_to_string(e.to) << ";\n";
64  }
65  out << "}\n";
66  }
67 }
std::string from_id_to_string(NodeId id)
Definition: dotoutput.cc:41
constexpr std::underlying_type< E >::type base_cast(E e) noexcept
Definition: stdutils.h:92
std::vector< Node > nodes
Definition: dotoutput.h:27
Node * add_new_node(const std::string &name)
Definition: dotoutput.cc:14
Edge * add_new_edge(NodeId from, NodeId to)
Definition: dotoutput.cc:28
std::vector< Edge > edges
Definition: dotoutput.h:28