2023-10-19 23:05:11 +02:00
|
|
|
#include "../includes/Projet.hpp"
|
|
|
|
|
2023-10-27 20:29:48 +02:00
|
|
|
Projet::Projet() : debut(Tache("Début", 0)), fin(Tache("Fin", 0)) {
|
|
|
|
fin.depends_from(debut);
|
|
|
|
}
|
2023-10-27 18:01:17 +02:00
|
|
|
|
2023-10-19 23:05:11 +02:00
|
|
|
Projet::~Projet() {}
|
|
|
|
|
2023-10-27 20:29:48 +02:00
|
|
|
Projet::Projet(const Projet &src) : debut(src.debut), fin(src.fin) {}
|
2023-10-19 23:05:11 +02:00
|
|
|
|
|
|
|
const Projet &Projet::operator=(const Projet &src) {
|
|
|
|
if (this == &src) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2023-10-24 16:47:59 +02:00
|
|
|
|
2023-10-27 20:29:48 +02:00
|
|
|
std::ostream &operator<<(std::ostream &out, const Projet &data) {
|
|
|
|
for (Tache *t : data.taches) {
|
|
|
|
out << *t << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
// - tache2 ne doit pas dépendre transitivement de tache1
|
|
|
|
// - permet aussi de vérifier si tache1 == tache2
|
|
|
|
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 20:29:48 +02:00
|
|
|
const Tache *Projet::contains(const int id) const {
|
|
|
|
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 20:29:48 +02:00
|
|
|
const Tache *Projet::contains(const std::string name) const {
|
|
|
|
for (Tache *t : this->taches) {
|
|
|
|
if (name == t->name) {
|
|
|
|
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-27 20:29:48 +02:00
|
|
|
const std::vector<Tache *> Projet::consult_tasks() const { return taches; }
|
|
|
|
|
2023-10-27 21:40:27 +02:00
|
|
|
std::vector<Tache *> Projet::topologicalSort() {
|
|
|
|
// Construction de la pile
|
|
|
|
std::vector<Tache *> pile;
|
|
|
|
for (Tache *it : taches) {
|
|
|
|
if (!it->visite) {
|
|
|
|
it->PP_postfixe(pile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse de la pile
|
|
|
|
std::reverse(pile.begin(), pile.end());
|
2023-10-27 18:01:17 +02:00
|
|
|
|
2023-10-27 21:40:27 +02:00
|
|
|
// Renvoie la liste de tâches topologiquement triée
|
|
|
|
return pile;
|
2023-10-27 18:01:17 +02:00
|
|
|
}
|