moved reserve

This commit is contained in:
Clément
2025-06-04 18:49:59 +02:00
parent d2324d1020
commit 191048d2a7
3 changed files with 147 additions and 146 deletions

View File

@@ -17,16 +17,13 @@ std::ostream& operator<<(std::ostream& os, const Graphe& g) {
}
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";
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;
@@ -66,8 +63,10 @@ Graphe::Graphe(const string& fileName) {
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") {
@@ -78,12 +77,9 @@ Graphe::Graphe(const string& fileName) {
}
}
m_Vertecies.reserve(nb_vertices);
m_Edges.reserve(nb_edges);
if(verticesSection) {
if (verticesSection) {
// cout << "Reading vertices : " << line << endl;
if(line.starts_with("VER")) {
if (line.size() >= 3 && line.substr(0, 3) == "VER") {
continue;
}
regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)");
@@ -108,7 +104,7 @@ Graphe::Graphe(const string& fileName) {
}
if (edgesSection) {
// cout << "Reading edges : " << line << endl;
if(line.starts_with("EDG")) {
if (line.size() >= 3 && line.substr(0, 3) == "EDG") {
continue;
}
regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)");

View File

@@ -1,15 +1,15 @@
#pragma once
#include <iostream>
void printYellow(const std::string& text) {
inline void printYellow(const std::string& text) {
std::cout << "\033[33m" << text << "\033[0m";
}
void printRed(const std::string& text) {
inline void printRed(const std::string& text) {
std::cout << "\033[31m" << text << "\033[0m";
}
void printGreen(const std::string& text) {
inline void printGreen(const std::string& text) {
std::cout << "\033[32m" << text << "\033[0m";
}
void printBlue(const std::string& text) {
inline void printBlue(const std::string& text) {
std::cout << "\033[34m" << text << "\033[0m";
}

View File

@@ -1,33 +1,38 @@
#include <iostream>
#include "Algo.h"
#include "Graphe.h"
#include <chrono>
#include <ctime>
#include <iostream>
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<std::chrono::milliseconds>(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";
// }
}