diff --git a/src/Graphe.cpp b/src/Graphe.cpp index bb57a26..a139229 100644 --- a/src/Graphe.cpp +++ b/src/Graphe.cpp @@ -6,27 +6,24 @@ using namespace std; std::ostream& operator<<(std::ostream& os, const Graphe& g) { - 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; + 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) { - // load the graph from the file ifstream file(fileName); if (!file.is_open()) { // cerr << "Error opening file: " << fileName << endl; @@ -34,112 +31,111 @@ Graphe::Graphe(const string& fileName) { } string line; bool isSettingsSection = true; - bool verticesSection = false; - bool edgesSection = false; + bool verticesSection = false; + bool edgesSection = false; while (getline(file, line)) { if (line.empty()) { if (isSettingsSection) { - isSettingsSection = false; - verticesSection = true; - // cout << "Settings section ended, now reading vertices." << endl; - continue; - } - if (verticesSection) { - verticesSection = false; - edgesSection = true; - // cout << "Vertices section ended, now reading edges." << endl; - continue; - } + isSettingsSection = false; + verticesSection = true; + // cout << "Settings section ended, now reading vertices." << endl; + continue; + } + if (verticesSection) { + verticesSection = false; + edgesSection = true; + // cout << "Vertices section ended, now reading edges." << endl; + continue; + } } if (isSettingsSection) { - // cout << "Settings :" << line << endl; + // cout << "Settings :" << line << endl; regex settingRegex(R"(^([A-Z_]+):\s([\w\d\.]+)$)"); - smatch match; - if (regex_match(line, match, settingRegex)) { - string key = match[1].str(); - string value = match[2].str(); - if (key == "NAME") { - file_name = value; - } else if (key == "COMMENT") { - comment = value; - } else if (key == "ORIENTED") { - directed = (value == "true"); - } else if (key == "NB_VERTICES") { - nb_vertices = stoi(value); - } else if (key == "NB_EDGES") { - nb_edges = stoi(value); - } else if (key == "NB_VALUES_BY_VERTEX") { - nb_values_by_vertex = stoi(value); - } else if (key == "NB_VALUES_BY_EDGE") { - nb_values_by_edge = stoi(value); - } else { - cerr << "Unknown setting: " << key << endl; - } - } + smatch match; + if (regex_match(line, match, settingRegex)) { + string key = match[1].str(); + string value = match[2].str(); + if (key == "NAME") { + file_name = value; + } else if (key == "COMMENT") { + comment = value; + } else if (key == "ORIENTED") { + directed = (value == "true"); + } else if (key == "NB_VERTICES") { + nb_vertices = stoi(value); + m_Vertecies.reserve(nb_vertices); + } else if (key == "NB_EDGES") { + nb_edges = stoi(value); + m_Edges.reserve(nb_edges); + } else if (key == "NB_VALUES_BY_VERTEX") { + nb_values_by_vertex = stoi(value); + } else if (key == "NB_VALUES_BY_EDGE") { + nb_values_by_edge = stoi(value); + } else { + cerr << "Unknown setting: " << key << endl; + } + } } - m_Vertecies.reserve(nb_vertices); - m_Edges.reserve(nb_edges); + if (verticesSection) { + // cout << "Reading vertices : " << line << endl; + if (line.size() >= 3 && line.substr(0, 3) == "VER") { + continue; + } + regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)"); + smatch match; + if (regex_match(line, match, vertexRegex)) { + Vertex v; + int id = stoi(match[1].str()); + v.m_Name = match[2].str(); + string valuesStr = match[3].str(); + regex valueRegex(R"((\d+))"); + auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex); + auto valuesEnd = sregex_iterator(); + for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) { + v.m_Values.push_back(stof((*i).str())); + } - if(verticesSection) { - // cout << "Reading vertices : " << line << endl; - if(line.starts_with("VER")) { - continue; - } - regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)"); - smatch match; - if (regex_match(line, match, vertexRegex)) { - Vertex v; - int id = stoi(match[1].str()); - v.m_Name = match[2].str(); - string valuesStr = match[3].str(); - regex valueRegex(R"((\d+))"); - auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex); - 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_VertexMap[v.m_Name] = &m_Vertecies[id]; + } else { + cerr << "Invalid vertex line: " << line << endl; + } + } + if (edgesSection) { + // cout << "Reading edges : " << line << endl; + if (line.size() >= 3 && line.substr(0, 3) == "EDG") { + continue; + } + regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)"); + smatch match; + if (regex_match(line, match, edgeRegex)) { + Edge e; + e.m_Start = &m_Vertecies[stoi(match[1].str())]; + e.m_End = &m_Vertecies[stoi(match[2].str())]; + string valuesStr = match[3].str(); + regex valueRegex(R"((\d+))"); + auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex); + auto valuesEnd = sregex_iterator(); + for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) { + e.m_Values.push_back(stof((*i).str())); + } - m_Vertecies.push_back(v); - m_VertexMap[v.m_Name] = &m_Vertecies[id]; - } else { - cerr << "Invalid vertex line: " << line << endl; - } - } - if (edgesSection) { - // cout << "Reading edges : " << line << endl; - if(line.starts_with("EDG")) { - continue; - } - regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)"); - smatch match; - if (regex_match(line, match, edgeRegex)) { - Edge e; - e.m_Start = &m_Vertecies[stoi(match[1].str())]; - e.m_End = &m_Vertecies[stoi(match[2].str())]; - string valuesStr = match[3].str(); - regex valueRegex(R"((\d+))"); - auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex); - auto valuesEnd = sregex_iterator(); - for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) { - e.m_Values.push_back(stof((*i).str())); - } - - std::size_t edgeID = m_Edges.size(); - m_Edges.push_back(e); - e.m_Start->m_Edges.push_back(&m_Edges[edgeID]); - if (!directed) { - Edge reverseEdge; - reverseEdge.m_Start = e.m_End; - reverseEdge.m_End = e.m_Start; - reverseEdge.m_Values = e.m_Values; - std::size_t index = m_Edges.size(); - m_Edges.push_back(reverseEdge); - reverseEdge.m_Start->m_Edges.push_back(&m_Edges[index]); - } - } else { - cerr << "Invalid edge line: " << line << endl; - } - } + std::size_t edgeID = m_Edges.size(); + m_Edges.push_back(e); + e.m_Start->m_Edges.push_back(&m_Edges[edgeID]); + if (!directed) { + Edge reverseEdge; + reverseEdge.m_Start = e.m_End; + reverseEdge.m_End = e.m_Start; + reverseEdge.m_Values = e.m_Values; + std::size_t index = m_Edges.size(); + m_Edges.push_back(reverseEdge); + reverseEdge.m_Start->m_Edges.push_back(&m_Edges[index]); + } + } else { + cerr << "Invalid edge line: " << line << endl; + } + } } -} \ No newline at end of file +} diff --git a/src/PrettyPrint.h b/src/PrettyPrint.h index 99c5569..7855eff 100644 --- a/src/PrettyPrint.h +++ b/src/PrettyPrint.h @@ -1,15 +1,15 @@ #pragma once #include -void printYellow(const std::string& text) { - std::cout << "\033[33m" << text << "\033[0m"; +inline void printYellow(const std::string& text) { + std::cout << "\033[33m" << text << "\033[0m"; } -void printRed(const std::string& text) { - std::cout << "\033[31m" << text << "\033[0m"; +inline void printRed(const std::string& text) { + std::cout << "\033[31m" << text << "\033[0m"; } -void printGreen(const std::string& text) { - std::cout << "\033[32m" << text << "\033[0m"; +inline void printGreen(const std::string& text) { + std::cout << "\033[32m" << text << "\033[0m"; +} +inline void printBlue(const std::string& text) { + std::cout << "\033[34m" << text << "\033[0m"; } -void printBlue(const std::string& text) { - std::cout << "\033[34m" << text << "\033[0m"; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5cb2537..dbe12eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,33 +1,38 @@ -#include - #include "Algo.h" #include "Graphe.h" +#include +#include +#include int main(int argc, char** argv) { - Graphe graphe("graphe-oriente-01.gra"); - std::cout << graphe << std::endl; - auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a")); + auto start = std::chrono::high_resolution_clock::now(); + Graphe graphe("graphe-communes.gra"); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(end - start).count(); + std::cout << "Temps de chargement du graphe : " << duration << " ms.\n"; + // std::cout << graphe << std::endl; + // auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a")); - std::cout << "Résultat :\n"; - for (auto& [vertex, longueur] : result) { - std::cout << vertex->m_Name << " " << longueur << "\n"; - } + // std::cout << "Résultat :\n"; + // for (auto& [vertex, longueur] : result) { + // 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"; - for (auto& [vertex, chemin] : result2) { - std::cout << vertex->m_Name << " " << chemin << "\n"; - } + // std::cout << "Résultat :\n"; + // for (auto& [vertex, chemin] : result2) { + // 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 << result3 << std::endl; + // std::cout << "Résultat :\n"; + // std::cout << result3 << std::endl; - auto result4 = PlusCourtCheminDjikstra(graphe, graphe.getVertex("a")); - std::cout << "Résultat :\n"; - for (auto& [vertex, longueur] : result4) { - std::cout << vertex->m_Name << " " << longueur << "\n"; - } + // auto result4 = PlusCourtCheminDjikstra(graphe, graphe.getVertex("a")); + // std::cout << "Résultat :\n"; + // for (auto& [vertex, longueur] : result4) { + // std::cout << vertex->m_Name << " " << longueur << "\n"; + // } }