moved reserve
This commit is contained in:
226
src/Graphe.cpp
226
src/Graphe.cpp
@@ -6,27 +6,24 @@ using namespace std;
|
|||||||
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const Graphe& g) {
|
std::ostream& operator<<(std::ostream& os, const Graphe& g) {
|
||||||
os << "Graphe: " << g.file_name << "\n";
|
os << "Graphe: " << g.file_name << "\n";
|
||||||
os << "Comment: " << g.comment << "\n";
|
os << "Comment: " << g.comment << "\n";
|
||||||
os << "Directed: " << (g.directed ? "true" : "false") << "\n";
|
os << "Directed: " << (g.directed ? "true" : "false") << "\n";
|
||||||
os << "Number of vertices: " << g.nb_vertices << "\n";
|
os << "Number of vertices: " << g.nb_vertices << "\n";
|
||||||
os << "Number of edges: " << g.nb_edges << "\n";
|
os << "Number of edges: " << g.nb_edges << "\n";
|
||||||
os << "Vertices:\n";
|
os << "Vertices:\n";
|
||||||
for (const auto& vertex : g.m_Vertecies) {
|
for (const auto& vertex : g.m_Vertecies) {
|
||||||
os << " ID: " << vertex.m_Name << ", Values: " << vertex.m_Values << "\n";
|
os << " ID: " << vertex.m_Name << ", Values: " << vertex.m_Values << "\n";
|
||||||
}
|
}
|
||||||
os << "Edges:\n";
|
os << "Edges:\n";
|
||||||
for (const auto& edge : g.m_Edges) {
|
for (const auto& edge : g.m_Edges) {
|
||||||
os << " Start: " << edge.m_Start->m_Name
|
os << " Start: " << edge.m_Start->m_Name << ", End: " << edge.m_End->m_Name << ", Values: " << edge.m_Values << "\n";
|
||||||
<< ", End: " << edge.m_End->m_Name
|
}
|
||||||
<< ", Values: " << edge.m_Values << "\n";
|
return os;
|
||||||
}
|
|
||||||
return os;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Graphe::Graphe(const string& fileName) {
|
Graphe::Graphe(const string& fileName) {
|
||||||
// load the graph from the file
|
|
||||||
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;
|
||||||
@@ -34,112 +31,111 @@ Graphe::Graphe(const string& fileName) {
|
|||||||
}
|
}
|
||||||
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)) {
|
while (getline(file, 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;
|
// cout << "Settings section ended, now reading vertices." << endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (verticesSection) {
|
if (verticesSection) {
|
||||||
verticesSection = false;
|
verticesSection = false;
|
||||||
edgesSection = true;
|
edgesSection = true;
|
||||||
// cout << "Vertices section ended, now reading edges." << endl;
|
// cout << "Vertices section ended, now reading edges." << endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isSettingsSection) {
|
if (isSettingsSection) {
|
||||||
// cout << "Settings :" << line << endl;
|
// cout << "Settings :" << line << endl;
|
||||||
regex settingRegex(R"(^([A-Z_]+):\s([\w\d\.]+)$)");
|
regex settingRegex(R"(^([A-Z_]+):\s([\w\d\.]+)$)");
|
||||||
smatch match;
|
smatch match;
|
||||||
if (regex_match(line, match, settingRegex)) {
|
if (regex_match(line, match, settingRegex)) {
|
||||||
string key = match[1].str();
|
string key = match[1].str();
|
||||||
string value = match[2].str();
|
string value = match[2].str();
|
||||||
if (key == "NAME") {
|
if (key == "NAME") {
|
||||||
file_name = value;
|
file_name = value;
|
||||||
} else if (key == "COMMENT") {
|
} else if (key == "COMMENT") {
|
||||||
comment = value;
|
comment = value;
|
||||||
} else if (key == "ORIENTED") {
|
} else if (key == "ORIENTED") {
|
||||||
directed = (value == "true");
|
directed = (value == "true");
|
||||||
} else if (key == "NB_VERTICES") {
|
} else if (key == "NB_VERTICES") {
|
||||||
nb_vertices = stoi(value);
|
nb_vertices = stoi(value);
|
||||||
} else if (key == "NB_EDGES") {
|
m_Vertecies.reserve(nb_vertices);
|
||||||
nb_edges = stoi(value);
|
} else if (key == "NB_EDGES") {
|
||||||
} else if (key == "NB_VALUES_BY_VERTEX") {
|
nb_edges = stoi(value);
|
||||||
nb_values_by_vertex = stoi(value);
|
m_Edges.reserve(nb_edges);
|
||||||
} else if (key == "NB_VALUES_BY_EDGE") {
|
} else if (key == "NB_VALUES_BY_VERTEX") {
|
||||||
nb_values_by_edge = stoi(value);
|
nb_values_by_vertex = stoi(value);
|
||||||
} else {
|
} else if (key == "NB_VALUES_BY_EDGE") {
|
||||||
cerr << "Unknown setting: " << key << endl;
|
nb_values_by_edge = stoi(value);
|
||||||
}
|
} else {
|
||||||
}
|
cerr << "Unknown setting: " << key << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_Vertecies.reserve(nb_vertices);
|
if (verticesSection) {
|
||||||
m_Edges.reserve(nb_edges);
|
// 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) {
|
m_Vertecies.push_back(v);
|
||||||
// cout << "Reading vertices : " << line << endl;
|
m_VertexMap[v.m_Name] = &m_Vertecies[id];
|
||||||
if(line.starts_with("VER")) {
|
} else {
|
||||||
continue;
|
cerr << "Invalid vertex line: " << line << endl;
|
||||||
}
|
}
|
||||||
regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)");
|
}
|
||||||
smatch match;
|
if (edgesSection) {
|
||||||
if (regex_match(line, match, vertexRegex)) {
|
// cout << "Reading edges : " << line << endl;
|
||||||
Vertex v;
|
if (line.size() >= 3 && line.substr(0, 3) == "EDG") {
|
||||||
int id = stoi(match[1].str());
|
continue;
|
||||||
v.m_Name = match[2].str();
|
}
|
||||||
string valuesStr = match[3].str();
|
regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)");
|
||||||
regex valueRegex(R"((\d+))");
|
smatch match;
|
||||||
auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex);
|
if (regex_match(line, match, edgeRegex)) {
|
||||||
auto valuesEnd = sregex_iterator();
|
Edge e;
|
||||||
for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) {
|
e.m_Start = &m_Vertecies[stoi(match[1].str())];
|
||||||
v.m_Values.push_back(stof((*i).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);
|
std::size_t edgeID = m_Edges.size();
|
||||||
m_VertexMap[v.m_Name] = &m_Vertecies[id];
|
m_Edges.push_back(e);
|
||||||
} else {
|
e.m_Start->m_Edges.push_back(&m_Edges[edgeID]);
|
||||||
cerr << "Invalid vertex line: " << line << endl;
|
if (!directed) {
|
||||||
}
|
Edge reverseEdge;
|
||||||
}
|
reverseEdge.m_Start = e.m_End;
|
||||||
if (edgesSection) {
|
reverseEdge.m_End = e.m_Start;
|
||||||
// cout << "Reading edges : " << line << endl;
|
reverseEdge.m_Values = e.m_Values;
|
||||||
if(line.starts_with("EDG")) {
|
std::size_t index = m_Edges.size();
|
||||||
continue;
|
m_Edges.push_back(reverseEdge);
|
||||||
}
|
reverseEdge.m_Start->m_Edges.push_back(&m_Edges[index]);
|
||||||
regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)");
|
}
|
||||||
smatch match;
|
} else {
|
||||||
if (regex_match(line, match, edgeRegex)) {
|
cerr << "Invalid edge line: " << line << endl;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void printYellow(const std::string& text) {
|
inline void printYellow(const std::string& text) {
|
||||||
std::cout << "\033[33m" << text << "\033[0m";
|
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";
|
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";
|
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";
|
|
||||||
}
|
|
||||||
49
src/main.cpp
49
src/main.cpp
@@ -1,33 +1,38 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "Algo.h"
|
#include "Algo.h"
|
||||||
#include "Graphe.h"
|
#include "Graphe.h"
|
||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
Graphe graphe("graphe-oriente-01.gra");
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
std::cout << graphe << std::endl;
|
Graphe graphe("graphe-communes.gra");
|
||||||
auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a"));
|
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";
|
// 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