AlgoPlus v0.1.0
Loading...
Searching...
No Matches
graph_visualization.h
1#ifndef GRAPH_VISUALIZATION_H
2#define GRAPH_VISUALIZATION_H
3
4#ifdef __cplusplus
5#include <chrono>
6#include <fstream>
7#include <iostream>
8#include <string>
9#include <thread>
10#endif
11
12using namespace std;
13
14/*
15 *namespace for graph visualization function and .dot files creation.
16 */
17namespace graph_visualization {
18
19#define OPEN_COMMAND "open"
20
21/*
22 *visualize function.
23 *@param __generate: string that contains the needed data to generate the .dot
24 *file.
25 *@param newFileName: if needed, the filename can be changed(default
26 *"unnamed.dot")
27 */
28inline void visualize(std::string& _generate, std::string newFileName = "unnamed.dot") {
29 auto start_time = std::chrono::high_resolution_clock::now();
30 try {
31 if (newFileName.size() < 5 || newFileName.substr(newFileName.length() - 4) != ".dot")
32 newFileName += ".dot";
33 // newFileName = "examples/" + newFileName;
34 // Open the file for writing
35 std::ofstream outFile(newFileName);
36
37 // Check if the file is successfully opened
38 if (outFile.is_open()) {
39 // Write the DOT format header
40 outFile << "graph G{" << std::endl;
41
42 // Generate DOT code recursively
43 outFile << _generate;
44 // Write the DOT format footer
45 outFile << "}" << std::endl;
46
47 // Close the file
48 outFile.close();
49
50 std::cout << "Visualization file '" << newFileName << "' created successfully."
51 << std::endl;
52 auto end_time = std::chrono::high_resolution_clock::now();
53 auto duration =
54 std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
55 double runtime_sec = static_cast<double>(duration.count()) / 1e6;
56
57 std::cout << "Visualization runtime: " << runtime_sec << " sec" << std::endl;
58 std::string openCommand = OPEN_COMMAND + std::string(" ") + newFileName;
59 system(openCommand.c_str());
60 } else {
61 std::cerr << "Error: Unable to open file '" << newFileName << "' for writing."
62 << std::endl;
63 }
64 } catch (const std::exception& e) {
65 std::cerr << "Error: " << e.what() << std::endl;
66 }
67};
68
69}; // namespace graph_visualization
70
71/*
72 *namespace for di-graph visualization function and .dot files creation.
73 */
74namespace digraph_visualization {
75
76#define OPEN_COMMAND "open"
77
78/*
79 *visualize function.
80 *@param __generate: string that contains the needed data to generate the .dot
81 *file.
82 *@param newFileName: if needed, the filename can be changed(default
83 *"unnamed.dot")
84 */
85inline void visualize(std::string& _generate, std::string newFileName = "unnamed.dot") {
86 auto start_time = std::chrono::high_resolution_clock::now();
87 try {
88 if (newFileName.size() < 5 || newFileName.substr(newFileName.length() - 4) != ".dot")
89 newFileName += ".dot";
90 // newFileName = "examples/" + newFileName;
91 // Open the file for writing
92 std::ofstream outFile(newFileName);
93
94 // Check if the file is successfully opened
95 if (outFile.is_open()) {
96 // Write the DOT format header
97 outFile << "digraph G{" << std::endl;
98
99 // Generate DOT code recursively
100 outFile << _generate;
101 // Write the DOT format footer
102 outFile << "}" << std::endl;
103
104 // Close the file
105 outFile.close();
106
107 std::cout << "Visualization file '" << newFileName << "' created successfully."
108 << std::endl;
109 auto end_time = std::chrono::high_resolution_clock::now();
110 auto duration =
111 std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
112 double runtime_sec = static_cast<double>(duration.count()) / 1e6;
113
114 std::cout << "Visualization runtime: " << runtime_sec << " sec" << std::endl;
115 std::string openCommand = OPEN_COMMAND + std::string(" ") + newFileName;
116 system(openCommand.c_str());
117 } else {
118 std::cerr << "Error: Unable to open file '" << newFileName << "' for writing."
119 << std::endl;
120 }
121 } catch (const std::exception& e) {
122 std::cerr << "Error: " << e.what() << std::endl;
123 }
124};
125
126}; // namespace digraph_visualization
127
128#endif // GRAPH_VISUALIZATION_H