1#ifndef LINKED_LIST_VISUALIZATION_H
2#define LINKED_LIST_VISUALIZATION_H
15namespace linked_list_visualization {
17#define OPEN_COMMAND "open"
19inline void visualize(std::string& _generate, std::string newFileName =
"unnamed.dot") {
20 auto start_time = std::chrono::high_resolution_clock::now();
22 if (newFileName.size() < 5 || newFileName.substr(newFileName.length() - 4) !=
".dot")
23 newFileName +=
".dot";
26 std::ofstream outFile(newFileName);
29 if (outFile.is_open()) {
31 outFile <<
"digraph list {" << std::endl;
36 outFile <<
"}" << std::endl;
41 std::cout <<
"Visualization file '" << newFileName <<
"' created successfully."
43 auto end_time = std::chrono::high_resolution_clock::now();
45 std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
46 double runtime_sec =
static_cast<double>(duration.count()) / 1e6;
48 std::cout <<
"Visualization runtime: " << runtime_sec <<
" sec" << std::endl;
49 std::string openCommand = OPEN_COMMAND + std::string(
" ") + newFileName;
50 system(openCommand.c_str());
52 std::cerr <<
"Error: Unable to open file '" << newFileName <<
"' for writing."
55 }
catch (
const std::exception& e) {
56 std::cerr <<
"Error: " << e.what() << std::endl;