56 bool _isIdentifier_(std::string_view key) {
57 if (key.empty() || (key.front() >=
'0' && key.front() <=
'9'))
return false;
58 for (
const char c: key) {
59 const bool ok = (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9')
61 if (!ok)
return false;
68 _buffer_ = directed ?
"digraph" :
"graph";
71 _buffer_ += quote(name);
78 result.reserve(text.size() + 2);
80 for (
const char c: text) {
82 case '"' : result +=
"\\\"";
break;
83 case '\\' : result +=
"\\\\";
break;
84 case '\n' : result +=
"\\n";
break;
85 case '\r' : result +=
"\\r";
break;
86 default : result += c;
95 const auto [end, error] = std::to_chars(buffer, buffer +
sizeof(buffer), value);
96 return std::string(buffer, error == std::errc() ? end : buffer);
102 if (!_isIdentifier_(attribute.
key)) {
109 std::string result =
"[";
110 for (std::size_t i = 0; i < attributes.size(); ++i) {
111 if (i > 0) result +=
", ";
118 if (attributes.empty())
return;
135 std::string statement =
_indent_() + id;
136 if (!attributes.empty()) statement +=
' ' +
_attributes_(attributes);
141 const std::string& to,
144 if (!attributes.empty()) statement +=
' ' +
_attributes_(attributes);
153 _node_(std::to_string(
id), attributes);
162 _edge_(std::to_string(from), std::to_string(to), attributes);
167 std::string statement =
_indent_() +
"subgraph ";
168 if (!name.empty()) statement +=
quote(name) +
' ';
170 const std::string inner(2 * (
_depth_ + 2),
' ');
171 for (
const auto& attribute: attributes)
172 statement += inner +
_attribute_(attribute) +
";\n";
Exception: at least one argument passed to a function is not what was expected.
Exception : operation not allowed.
void nodeDefaults(const DotAttributes &attributes)
node [attributes];, the default attributes of the nodes that follow (none : does nothing).
void _edge_(const std::string &from, const std::string &to, const DotAttributes &attributes)
void graphAttributes(const DotAttributes &attributes)
graph [attributes];, the attributes of the graph (does nothing if there is none).
void edgeDefaults(const DotAttributes &attributes)
edge [attributes];, the default attributes of the edges that follow (none : does nothing).
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
void endSubgraph()
Closes the last subgraph that is open.
static std::string number(double value)
The shortest text that reads back as value (0.5, 1, 1e-05).
DotWriter(bool directed=true, std::string_view name="")
Starts a graph.
static std::string _attributes_(const DotAttributes &attributes)
void _defaults_(const char *keyword, const DotAttributes &attributes)
std::string _indent_() const
void _node_(const std::string &id, const DotAttributes &attributes)
static std::string quote(std::string_view text)
text between double quotes, in which ", \, line feed and carriage return are escaped.
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
void beginSubgraph(std::string_view name, const DotAttributes &attributes={})
Opens a subgraph : subgraph "name" { key="value"; ... .
void graphAttribute(const DotAttribute &attribute)
key="value";, an attribute of the graph or of the subgraph being written.
static std::string _attribute_(const DotAttribute &attribute)
A minimal writer of graphs in the dot language of graphviz.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
std::vector< DotAttribute > DotAttributes
a list of attributes
An attribute of a graph, a node or an edge : key="value".