This repository has been archived on 2023-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
GestionProjet/src/Projet.cpp

118 lines
2.4 KiB
C++
Raw Normal View History

2023-10-19 23:05:11 +02:00
#include "../includes/Projet.hpp"
2023-10-28 12:09:52 +02:00
Projet::Projet() : fin(new Tache("Fin", 0)), debut(new Tache("Début", 0)) {
2023-10-28 01:34:58 +02:00
fin->ajouteDependance(*debut);
2023-10-27 23:27:41 +02:00
taches.push_back(fin);
taches.push_back(debut);
}
2023-10-28 02:09:10 +02:00
Projet::~Projet() { free_taches(); }
2023-10-27 23:27:41 +02:00
void Projet::_copy(const Projet &src) {
// Copie des tâches
taches.reserve(src.taches.size());
for (Tache *t : src.taches) {
taches.push_back(t);
2023-10-27 23:27:41 +02:00
}
2023-10-27 20:29:48 +02:00
}
2023-10-27 18:01:17 +02:00
void Projet::free_taches() { taches.clear(); }
2023-10-19 23:05:11 +02:00
2023-10-27 23:27:41 +02:00
Projet::Projet(const Projet &src) : debut(src.debut), fin(src.fin) {
_copy(src);
}
2023-10-19 23:05:11 +02:00
const Projet &Projet::operator=(const Projet &src) {
if (this == &src) {
return *this;
}
2023-10-27 23:27:41 +02:00
debut = src.debut;
fin = src.fin;
2023-10-28 02:09:10 +02:00
free_taches();
2023-10-27 23:27:41 +02:00
_copy(src);
2023-10-19 23:05:11 +02:00
return *this;
}
2023-10-24 16:47:59 +02:00
2023-10-28 02:59:00 +02:00
std::ostream &Projet::print(std::ostream &out) const {
2023-10-28 12:08:13 +02:00
// Liste vide
2023-10-28 02:59:00 +02:00
if (taches.empty()) {
out << "[]";
return out;
}
2023-10-28 12:08:13 +02:00
out << "[\n";
2023-10-28 02:59:00 +02:00
for (Tache *t : taches) {
2023-10-28 12:08:13 +02:00
out << " " << *t << ",\n";
2023-10-27 20:29:48 +02:00
}
2023-10-28 12:08:13 +02:00
out << ']';
2023-10-28 02:59:00 +02:00
2023-10-27 20:29:48 +02:00
return out;
}
2023-10-28 02:59:00 +02:00
std::ostream &operator<<(std::ostream &out, const Projet &data) {
return data.print(out);
}
2023-10-27 20:29:48 +02:00
const std::pair<const int, const int> Projet::pick_two_random_tasks() const {
// Choix aléatoire d'une tâche
2023-10-27 19:27:53 +02:00
size_t size = this->taches.size();
size_t rand1 = static_cast<size_t>(rand()) % size;
size_t rand2 = static_cast<size_t>(rand()) % size;
2023-10-27 18:01:17 +02:00
2023-10-27 20:29:48 +02:00
Tache *tache1 = this->taches[rand1];
Tache *tache2 = this->taches[rand2];
2023-10-28 01:34:58 +02:00
// tache2 ne doit pas dépendre transitivement de tache1
2023-10-27 20:29:48 +02:00
if (tache2->depends_from(*tache1)) {
return pick_two_random_tasks();
}
int id1 = tache1->unique_id;
int id2 = tache2->unique_id;
2023-10-24 16:47:59 +02:00
return std::make_pair(id1, id2);
}
2023-10-27 23:27:41 +02:00
Tache *Projet::contains(const int id) const {
2023-10-27 20:29:48 +02:00
for (Tache *t : this->taches) {
if (id == t->unique_id) {
return t;
2023-10-27 18:01:17 +02:00
}
}
2023-10-24 16:47:59 +02:00
return nullptr;
}
2023-10-27 23:27:41 +02:00
Tache *Projet::contains(const std::string name) const {
2023-10-27 20:29:48 +02:00
for (Tache *t : this->taches) {
2023-10-28 04:14:51 +02:00
if (name == t->get_name()) {
2023-10-27 20:29:48 +02:00
return t;
}
2023-10-27 18:01:17 +02:00
}
2023-10-24 16:47:59 +02:00
2023-10-27 20:29:48 +02:00
return nullptr;
2023-10-24 16:47:59 +02:00
}
2023-10-26 19:08:00 +02:00
2023-10-28 02:35:22 +02:00
const std::vector<const Tache *> Projet::consult_tasks() const {
return std::vector<const Tache *>(taches.begin(), taches.end());
}
2023-10-27 20:29:48 +02:00
2023-10-28 01:34:58 +02:00
void Projet::topologicalSort() {
2023-10-27 21:40:27 +02:00
// Construction de la pile
std::vector<Tache *> pile;
for (Tache *it : taches) {
if (!it->visite) {
it->PP_postfixe(pile);
}
}
2023-10-28 01:34:58 +02:00
// Nettoyage des marques
cleanMarks();
2023-10-27 21:40:27 +02:00
// Reverse de la pile
std::reverse(pile.begin(), pile.end());
2023-10-27 18:01:17 +02:00
2023-10-28 01:34:58 +02:00
// Modifie la liste des tâches topologiquement triée
taches = pile;
2023-10-27 18:01:17 +02:00
}