Optimization + PrettyPrint
This commit is contained in:
171
src/Graphe.cpp
171
src/Graphe.cpp
@@ -1,60 +1,55 @@
|
|||||||
#include "Graphe.h"
|
#include "Graphe.h"
|
||||||
|
#include "PrettyPrint.h"
|
||||||
|
#include <chrono>
|
||||||
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <regex>
|
#include <sstream>
|
||||||
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
void debug(string s) {
|
||||||
std::ostream& operator<<(std::ostream& os, const Graphe& g) {
|
cout << s << endl;
|
||||||
os << "Graphe: " << g.file_name << "\n";
|
|
||||||
os << "Comment: " << g.comment << "\n";
|
|
||||||
os << "Directed: " << (g.directed ? "true" : "false") << "\n";
|
|
||||||
os << "Number of vertices: " << g.nb_vertices << "\n";
|
|
||||||
os << "Number of edges: " << g.nb_edges << "\n";
|
|
||||||
os << "Vertices:\n";
|
|
||||||
for (const auto& vertex : g.m_Vertecies) {
|
|
||||||
os << " ID: " << vertex.m_Name << ", Values: " << vertex.m_Values << "\n";
|
|
||||||
}
|
|
||||||
os << "Edges:\n";
|
|
||||||
for (const auto& edge : g.m_Edges) {
|
|
||||||
os << " Start: " << edge.m_Start->m_Name << ", End: " << edge.m_End->m_Name << ", Values: " << edge.m_Values << "\n";
|
|
||||||
}
|
|
||||||
return os;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Graphe::Graphe(const string& fileName) {
|
Graphe::Graphe(const string& fileName) {
|
||||||
|
auto startTime = chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
ifstream file(fileName);
|
ifstream file(fileName);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
// cerr << "Error opening file: " << fileName << endl;
|
cerr << "Error opening file: " << fileName << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string fileContents;
|
||||||
|
|
||||||
string line;
|
string line;
|
||||||
bool isSettingsSection = true;
|
bool isSettingsSection = true;
|
||||||
bool verticesSection = false;
|
bool verticesSection = false;
|
||||||
bool edgesSection = false;
|
bool edgesSection = false;
|
||||||
while (getline(file, line)) {
|
istringstream fileStream((string()));
|
||||||
|
istream& inputStream = static_cast<istream&>(file);
|
||||||
|
|
||||||
|
// Settings
|
||||||
|
while (getline(inputStream, line)) {
|
||||||
if (line.empty()) {
|
if (line.empty()) {
|
||||||
if (isSettingsSection) {
|
if (isSettingsSection) {
|
||||||
isSettingsSection = false;
|
isSettingsSection = false;
|
||||||
verticesSection = true;
|
verticesSection = true;
|
||||||
// cout << "Settings section ended, now reading vertices." << endl;
|
break;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (verticesSection) {
|
|
||||||
verticesSection = false;
|
|
||||||
edgesSection = true;
|
|
||||||
// cout << "Vertices section ended, now reading edges." << endl;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSettingsSection) {
|
if (isSettingsSection) {
|
||||||
// cout << "Settings :" << line << endl;
|
size_t colonPos = line.find(':');
|
||||||
regex settingRegex(R"(^([A-Z_]+):\s([\w\d\.]+)$)");
|
if (colonPos != string::npos) {
|
||||||
smatch match;
|
string key = line.substr(0, colonPos);
|
||||||
if (regex_match(line, match, settingRegex)) {
|
string value = line.substr(colonPos + 1);
|
||||||
string key = match[1].str();
|
|
||||||
string value = match[2].str();
|
value.erase(0, value.find_first_not_of(" \t"));
|
||||||
|
|
||||||
if (key == "NAME") {
|
if (key == "NAME") {
|
||||||
file_name = value;
|
file_name = value;
|
||||||
} else if (key == "COMMENT") {
|
} else if (key == "COMMENT") {
|
||||||
@@ -63,79 +58,113 @@ Graphe::Graphe(const string& fileName) {
|
|||||||
directed = (value == "true");
|
directed = (value == "true");
|
||||||
} else if (key == "NB_VERTICES") {
|
} else if (key == "NB_VERTICES") {
|
||||||
nb_vertices = stoi(value);
|
nb_vertices = stoi(value);
|
||||||
m_Vertecies.reserve(nb_vertices);
|
|
||||||
} else if (key == "NB_EDGES") {
|
} else if (key == "NB_EDGES") {
|
||||||
nb_edges = stoi(value);
|
nb_edges = stoi(value);
|
||||||
m_Edges.reserve(nb_edges);
|
|
||||||
} else if (key == "NB_VALUES_BY_VERTEX") {
|
} else if (key == "NB_VALUES_BY_VERTEX") {
|
||||||
nb_values_by_vertex = stoi(value);
|
nb_values_by_vertex = stoi(value);
|
||||||
} else if (key == "NB_VALUES_BY_EDGE") {
|
} else if (key == "NB_VALUES_BY_EDGE") {
|
||||||
nb_values_by_edge = stoi(value);
|
nb_values_by_edge = stoi(value);
|
||||||
} else {
|
|
||||||
cerr << "Unknown setting: " << key << endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
m_Vertecies.reserve(nb_vertices);
|
||||||
|
m_Edges.reserve(directed ? nb_edges : 2 * nb_edges);
|
||||||
|
|
||||||
|
// Vertices
|
||||||
|
int vertexCounter = 0;
|
||||||
|
while (getline(inputStream, line) && verticesSection) {
|
||||||
|
if (line.empty()) {
|
||||||
|
verticesSection = false;
|
||||||
|
edgesSection = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (verticesSection) {
|
|
||||||
// cout << "Reading vertices : " << line << endl;
|
|
||||||
if (line.size() >= 3 && line.substr(0, 3) == "VER") {
|
if (line.size() >= 3 && line.substr(0, 3) == "VER") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)");
|
|
||||||
smatch match;
|
istringstream iss(line);
|
||||||
if (regex_match(line, match, vertexRegex)) {
|
int id;
|
||||||
|
string name;
|
||||||
|
if (iss >> id >> name) {
|
||||||
Vertex v;
|
Vertex v;
|
||||||
int id = stoi(match[1].str());
|
v.m_Name = name;
|
||||||
v.m_Name = match[2].str();
|
|
||||||
string valuesStr = match[3].str();
|
float value;
|
||||||
regex valueRegex(R"((\d+))");
|
while (iss >> value) {
|
||||||
auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex);
|
v.m_Values.push_back(value);
|
||||||
auto valuesEnd = sregex_iterator();
|
|
||||||
for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) {
|
|
||||||
v.m_Values.push_back(stof((*i).str()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_Vertecies.push_back(v);
|
m_Vertecies.push_back(v);
|
||||||
m_VertexMap[v.m_Name] = &m_Vertecies[id];
|
m_VertexMap[v.m_Name] = &m_Vertecies.back();
|
||||||
} else {
|
vertexCounter++;
|
||||||
cerr << "Invalid vertex line: " << line << endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (edgesSection) {
|
|
||||||
// cout << "Reading edges : " << line << endl;
|
// Edges
|
||||||
|
while (getline(inputStream, line) && edgesSection) {
|
||||||
if (line.size() >= 3 && line.substr(0, 3) == "EDG") {
|
if (line.size() >= 3 && line.substr(0, 3) == "EDG") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)");
|
|
||||||
smatch match;
|
istringstream iss(line);
|
||||||
if (regex_match(line, match, edgeRegex)) {
|
unsigned int startId, endId;
|
||||||
|
if (iss >> startId >> endId) {
|
||||||
|
if (startId >= 0 && startId < m_Vertecies.size() && endId >= 0 && endId < m_Vertecies.size()) {
|
||||||
|
|
||||||
Edge e;
|
Edge e;
|
||||||
e.m_Start = &m_Vertecies[stoi(match[1].str())];
|
e.m_Start = &m_Vertecies[startId];
|
||||||
e.m_End = &m_Vertecies[stoi(match[2].str())];
|
e.m_End = &m_Vertecies[endId];
|
||||||
string valuesStr = match[3].str();
|
|
||||||
regex valueRegex(R"((\d+))");
|
float value;
|
||||||
auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex);
|
while (iss >> value) {
|
||||||
auto valuesEnd = sregex_iterator();
|
e.m_Values.push_back(value);
|
||||||
for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) {
|
|
||||||
e.m_Values.push_back(stof((*i).str()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t edgeID = m_Edges.size();
|
size_t edgeId = m_Edges.size();
|
||||||
m_Edges.push_back(e);
|
m_Edges.push_back(e);
|
||||||
e.m_Start->m_Edges.push_back(&m_Edges[edgeID]);
|
|
||||||
|
m_Vertecies[startId].m_Edges.push_back(&m_Edges[edgeId]);
|
||||||
|
|
||||||
if (!directed) {
|
if (!directed) {
|
||||||
Edge reverseEdge;
|
Edge reverseEdge;
|
||||||
reverseEdge.m_Start = e.m_End;
|
reverseEdge.m_Start = e.m_End;
|
||||||
reverseEdge.m_End = e.m_Start;
|
reverseEdge.m_End = e.m_Start;
|
||||||
reverseEdge.m_Values = e.m_Values;
|
reverseEdge.m_Values = e.m_Values;
|
||||||
std::size_t index = m_Edges.size();
|
|
||||||
|
size_t reverseEdgeId = m_Edges.size();
|
||||||
m_Edges.push_back(reverseEdge);
|
m_Edges.push_back(reverseEdge);
|
||||||
reverseEdge.m_Start->m_Edges.push_back(&m_Edges[index]);
|
m_Vertecies[endId].m_Edges.push_back(&m_Edges[reverseEdgeId]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cerr << "Invalid edge line: " << line << endl;
|
cerr << "Invalid vertex IDs in edge: " << startId << " -> " << endId << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto elapsed = std::round(chrono::duration<double>(chrono::high_resolution_clock::now() - startTime).count() * 1000);
|
||||||
|
debug("File: " + PrettyPrint::format(file_name, Color::Blue));
|
||||||
|
debug("Graph loaded in " +
|
||||||
|
PrettyPrint::format(to_string(static_cast<int>(elapsed)) + "ms", elapsed < 1000 ? Color::Green : Color::Yellow));
|
||||||
|
debug("Loaded " + PrettyPrint::format(to_string(m_Vertecies.size()), Color::Green) + " vertices and " +
|
||||||
|
PrettyPrint::format(to_string(m_Edges.size()), Color::Green) + " edges");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The stream operator implementation remains unchanged
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Graphe& g) {
|
||||||
|
os << "Graphe: " << PrettyPrint::format(g.file_name, Color::Blue) << "\n";
|
||||||
|
os << "Comment: " << PrettyPrint::format(g.comment, Color::Cyan) << "\n";
|
||||||
|
os << "Directed: " << (g.directed ? PrettyPrint::format("true", Color::Green) : PrettyPrint::format("false", Color::Red)) << "\n";
|
||||||
|
os << "Number of vertices: " << PrettyPrint::format(g.nb_vertices, Color::Magenta) << "\n";
|
||||||
|
os << "Number of edges: " << PrettyPrint::format(g.nb_edges, Color::Magenta) << "\n";
|
||||||
|
os << "Vertices:\n";
|
||||||
|
for (const auto& vertex : g.m_Vertecies) {
|
||||||
|
os << " Name: " << vertex.m_Name << ", Values: " << vertex.m_Values << "\n";
|
||||||
|
}
|
||||||
|
os << "Edges:\n";
|
||||||
|
for (const auto& edge : g.m_Edges) {
|
||||||
|
os << " Start: " << edge.m_Start->m_Name << ", End: " << edge.m_End->m_Name << ", Values: " << edge.m_Values << "\n";
|
||||||
|
}
|
||||||
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/PrettyPrint.cpp
Normal file
42
src/PrettyPrint.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include "PrettyPrint.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void PrettyPrint::print(const std::string& str, Color c) {
|
||||||
|
std::cout << format(str, c) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string PrettyPrint::format(const std::string& str, Color c) {
|
||||||
|
std::string colorCode;
|
||||||
|
switch (c) {
|
||||||
|
case Color::Red:
|
||||||
|
colorCode = "\033[31m";
|
||||||
|
break;
|
||||||
|
case Color::Green:
|
||||||
|
colorCode = "\033[32m";
|
||||||
|
break;
|
||||||
|
case Color::Blue:
|
||||||
|
colorCode = "\033[34m";
|
||||||
|
break;
|
||||||
|
case Color::Yellow:
|
||||||
|
colorCode = "\033[33m";
|
||||||
|
break;
|
||||||
|
case Color::Magenta:
|
||||||
|
colorCode = "\033[35m";
|
||||||
|
break;
|
||||||
|
case Color::Cyan:
|
||||||
|
colorCode = "\033[36m";
|
||||||
|
break;
|
||||||
|
case Color::White:
|
||||||
|
colorCode = "\033[37m";
|
||||||
|
break;
|
||||||
|
case Color::Black:
|
||||||
|
colorCode = "\033[30m";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return colorCode + str + "\033[0m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string PrettyPrint::format(int value, Color c) {
|
||||||
|
return format(std::to_string(value), c);
|
||||||
|
}
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
inline void printYellow(const std::string& text) {
|
enum class Color { Red, Green, Blue, Yellow, Magenta, Cyan, White, Black };
|
||||||
std::cout << "\033[33m" << text << "\033[0m";
|
|
||||||
}
|
class PrettyPrint {
|
||||||
inline void printRed(const std::string& text) {
|
public:
|
||||||
std::cout << "\033[31m" << text << "\033[0m";
|
static void print(const std::string& str, Color c);
|
||||||
}
|
static std::string format(const std::string& str, Color c);
|
||||||
inline void printGreen(const std::string& text) {
|
static std::string format(int value, Color c);
|
||||||
std::cout << "\033[32m" << text << "\033[0m";
|
};
|
||||||
}
|
|
||||||
inline void printBlue(const std::string& text) {
|
|
||||||
std::cout << "\033[34m" << text << "\033[0m";
|
|
||||||
}
|
|
||||||
|
|||||||
47
src/main.cpp
47
src/main.cpp
@@ -1,38 +1,35 @@
|
|||||||
#include "Algo.h"
|
#include "Algo.h"
|
||||||
#include "Graphe.h"
|
#include "Graphe.h"
|
||||||
#include <chrono>
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
std::string tab_graphes[4] = {
|
||||||
Graphe graphe("graphe-communes.gra");
|
"graphe-oriente-01.gra", "graphe-nonoriente-01.gra", "graphe-communes.gra", "cours-representation.gra"};
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
Graphe graphe(tab_graphes[3]);
|
||||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
std::cout << graphe << std::endl;
|
||||||
std::cout << "Temps de chargement du graphe : " << duration << " ms.\n";
|
auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a"));
|
||||||
// std::cout << graphe << std::endl;
|
|
||||||
// auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a"));
|
|
||||||
|
|
||||||
// std::cout << "Résultat :\n";
|
std::cout << "Résultat :\n";
|
||||||
// for (auto& [vertex, longueur] : result) {
|
for (auto& [vertex, longueur] : result) {
|
||||||
// std::cout << vertex->m_Name << " " << longueur << "\n";
|
std::cout << vertex->m_Name << " " << longueur << "\n";
|
||||||
// }
|
}
|
||||||
|
|
||||||
// auto result2 = PlusCourtCheminsNbArcs(graphe, graphe.getVertex("a"));
|
auto result2 = PlusCourtCheminsNbArcs(graphe, graphe.getVertex("a"));
|
||||||
|
|
||||||
// std::cout << "Résultat :\n";
|
std::cout << "Résultat :\n";
|
||||||
// for (auto& [vertex, chemin] : result2) {
|
for (auto& [vertex, chemin] : result2) {
|
||||||
// std::cout << vertex->m_Name << " " << chemin << "\n";
|
std::cout << vertex->m_Name << " " << chemin << "\n";
|
||||||
// }
|
}
|
||||||
|
|
||||||
// auto result3 = PlusCourtChemin(graphe, graphe.getVertex("a"), graphe.getVertex("e"));
|
auto result3 = PlusCourtChemin(graphe, graphe.getVertex("a"), graphe.getVertex("e"));
|
||||||
|
|
||||||
// std::cout << "Résultat :\n";
|
std::cout << "Résultat :\n";
|
||||||
// std::cout << result3 << std::endl;
|
std::cout << result3 << std::endl;
|
||||||
|
|
||||||
// auto result4 = PlusCourtCheminDjikstra(graphe, graphe.getVertex("a"));
|
auto result4 = PlusCourtCheminDjikstra(graphe, graphe.getVertex("a"));
|
||||||
// std::cout << "Résultat :\n";
|
std::cout << "Résultat :\n";
|
||||||
// for (auto& [vertex, longueur] : result4) {
|
for (auto& [vertex, longueur] : result4) {
|
||||||
// std::cout << vertex->m_Name << " " << longueur << "\n";
|
std::cout << vertex->m_Name << " " << longueur << "\n";
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user