190 lines
8.0 KiB
C++
190 lines
8.0 KiB
C++
|
|
#ifndef CRENEAUHORAIRE
|
||
|
|
#define CRENEAUHORAIRE
|
||
|
|
#include <vector>
|
||
|
|
#include <iostream>
|
||
|
|
#include "Date.h"
|
||
|
|
namespace modellib {
|
||
|
|
/*! @file CreneauHoraire.h*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @class CreneauHoraire
|
||
|
|
* @brief Class describing a time slot
|
||
|
|
*/
|
||
|
|
class CreneauHoraire {
|
||
|
|
private:
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Begin date
|
||
|
|
*/
|
||
|
|
SolverDate m_debut;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief End date
|
||
|
|
*/
|
||
|
|
SolverDate m_fin;
|
||
|
|
|
||
|
|
|
||
|
|
public:
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Default constructor
|
||
|
|
*/
|
||
|
|
CreneauHoraire() = default;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Copy constructor
|
||
|
|
* @param creneau the time slot to copy
|
||
|
|
*/
|
||
|
|
CreneauHoraire(const CreneauHoraire &creneau);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Constructor
|
||
|
|
* @param debut begin of the time slot
|
||
|
|
* @param fin end of the time slot
|
||
|
|
*/
|
||
|
|
CreneauHoraire(const SolverDate &debut, const SolverDate &fin);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Destructor
|
||
|
|
*/
|
||
|
|
virtual ~CreneauHoraire() = default;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Getter to the beginning of the time slot, allow modification
|
||
|
|
* @return beginning of the time slot
|
||
|
|
*/
|
||
|
|
inline SolverDate &getDebut() { return m_debut; }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Getter to the beginning of the time slot, doesnt allow modification
|
||
|
|
* @return beginning of the time slot
|
||
|
|
*/
|
||
|
|
[[nodiscard]] inline SolverDate const &getDebutC() const { return m_debut; }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Getter to the end of the time slot, allow modification
|
||
|
|
* @return end of the time slot
|
||
|
|
*/
|
||
|
|
inline SolverDate &getFin() { return m_fin; }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief setter to the beginning of the time slot
|
||
|
|
* @param deb
|
||
|
|
*/
|
||
|
|
void setDebut(const SolverDate& deb) { m_debut = deb; };
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief setter to the end of the time slot
|
||
|
|
* @param fin
|
||
|
|
*/
|
||
|
|
void setFin(const SolverDate& fin) { m_fin = fin; };
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Getter to the end of the time slot, doesnt allow modification
|
||
|
|
* @return end of the time slot
|
||
|
|
*/
|
||
|
|
[[nodiscard]] inline SolverDate const &getFinC() const { return m_fin; }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Function to export the object in json format
|
||
|
|
* @param format if true the format respect the following "aaaa-mm-jjThh:mm:ssZ" else "annee": aaaa, "mois": mm, etc...
|
||
|
|
* @return the corresponding json
|
||
|
|
*/
|
||
|
|
[[nodiscard]] nlohmann::json to_json(bool format = false) const;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Function to create a CreneauHoraire object from a json description
|
||
|
|
* @param json the input json
|
||
|
|
* @param format if true the format respect the following "aaaa-mm-jjThh:mm:ssZ" else "annee": aaaa, "mois": mm, etc...
|
||
|
|
* @return the newly created CreneauHoraire object
|
||
|
|
*/
|
||
|
|
static CreneauHoraire from_json(nlohmann::json &json, bool format = false);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Function to check if two time slots are overlapping and return the overlap period
|
||
|
|
* @param trainSlot The first time slot
|
||
|
|
* @param trackSlot The second time slot
|
||
|
|
* @return a pair indicating if there is a match in the first value, and the overlapping period in the second value. The second value contains the beginning date of the overlap (relatively to date zero) and the duration of the overlap
|
||
|
|
*/
|
||
|
|
inline static std::pair<bool, std::pair<unsigned short, unsigned short>>
|
||
|
|
checkSlotsCompatibility(const CreneauHoraire &trainSlot, const CreneauHoraire &trackSlot) {
|
||
|
|
std::pair<bool, std::pair<unsigned short, unsigned short>> match;
|
||
|
|
match.first = false;
|
||
|
|
/*if (trackSlot.getDebutC() <= trainSlot.getDebutC() && trainSlot.getFinC() <= trackSlot.getFinC()
|
||
|
|
|| trainSlot.getDebutC() <= trackSlot.getDebutC() && trackSlot.getFinC() <= trainSlot.getFinC()
|
||
|
|
|| trackSlot.getDebutC() <= trainSlot.getFinC() && trainSlot.getDebutC() <= trackSlot.getDebutC()
|
||
|
|
|| trainSlot.getDebutC() <= trackSlot.getFinC() && trackSlot.getDebutC() <= trainSlot.getDebutC()
|
||
|
|
|| trackSlot.getFinC() <= trainSlot.getFinC() && trainSlot.getDebutC() <= trackSlot.getFinC()
|
||
|
|
|| trainSlot.getFinC() <= trackSlot.getFinC() && trackSlot.getDebutC() <= trainSlot.getFinC()
|
||
|
|
)*/
|
||
|
|
unsigned short begTrain = trainSlot.getDebutC().getRelativeDate();
|
||
|
|
unsigned short begTrack = trackSlot.getDebutC().getRelativeDate();
|
||
|
|
unsigned short endTrain = trainSlot.getFinC().getRelativeDate();
|
||
|
|
unsigned short endTrack = trackSlot.getFinC().getRelativeDate();
|
||
|
|
if ((begTrack <= begTrain && endTrain <= endTrack)
|
||
|
|
|| (begTrain <= begTrack && endTrack <= endTrain)
|
||
|
|
|| (begTrack <= endTrain && begTrain <= begTrack)
|
||
|
|
|| (begTrain <= endTrack && begTrack <= begTrain)
|
||
|
|
|| (endTrack <= endTrain && begTrain <= endTrack)
|
||
|
|
|| (endTrain <= endTrack && begTrack <= endTrain)
|
||
|
|
) {
|
||
|
|
unsigned short overlapBeginTime;
|
||
|
|
unsigned short overlapEndTime;
|
||
|
|
|
||
|
|
if ((begTrain) <= begTrack) {
|
||
|
|
overlapBeginTime = begTrack;// - *SolverDate::getDateDebut();
|
||
|
|
if ((trackSlot.getDebutC().t_timestampMinutes -
|
||
|
|
SolverDate::getDateDebut().t_timestampMinutes) % (60U / configlib::Configuration::Global.TIME_UNIT) !=
|
||
|
|
0U) {
|
||
|
|
overlapBeginTime++;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
overlapBeginTime = begTrain; //- *SolverDate::getDateDebut();
|
||
|
|
if ((trainSlot.getDebutC().t_timestampMinutes -
|
||
|
|
SolverDate::getDateDebut().t_timestampMinutes) % (60U / configlib::Configuration::Global.TIME_UNIT) !=
|
||
|
|
0U) {
|
||
|
|
overlapBeginTime++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ((endTrain) <= endTrack) { overlapEndTime = (endTrain); }// - *SolverDate::getDateDebut(); }
|
||
|
|
else { overlapEndTime = endTrack; } //- *SolverDate::getDateDebut(); }
|
||
|
|
match.first = true;
|
||
|
|
match.second.first = overlapBeginTime;
|
||
|
|
match.second.second = overlapEndTime - overlapBeginTime;
|
||
|
|
}
|
||
|
|
return match;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Function to check if two time slots have at least a certain duration of time (in minutes) in common.
|
||
|
|
* Similar to checkSlotsCompatibility but is needed for more precision (less than an hour of time unit) in swap algorithms.
|
||
|
|
* @param cr1 the first time slot
|
||
|
|
* @param cr2 the second time slot
|
||
|
|
* @param duration the duration in minutes we want to check
|
||
|
|
* @return true if the overlap timing if bigger of equal to duration. False otherwise or if there is no overlap
|
||
|
|
*/
|
||
|
|
inline static bool checkCommonDuration(const CreneauHoraire &cr1, const CreneauHoraire &cr2,
|
||
|
|
unsigned int duration)//duration in minutes under an hour
|
||
|
|
{
|
||
|
|
unsigned int beg = std::max(cr1.getDebutC().t_timestampMinutes,
|
||
|
|
cr2.getDebutC().t_timestampMinutes);
|
||
|
|
unsigned int end = std::min(cr1.getFinC().t_timestampMinutes,
|
||
|
|
cr2.getFinC().t_timestampMinutes);
|
||
|
|
return (end >= beg) && (end - beg) >= duration;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline bool operator==(const CreneauHoraire& comp) const
|
||
|
|
{
|
||
|
|
return comp.getDebutC().getRelativeDate() == getDebutC().getRelativeDate() && comp.getFinC().getRelativeDate() == getFinC().getRelativeDate();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
struct hash_creneau{
|
||
|
|
size_t operator()(const modellib::CreneauHoraire& val) const {
|
||
|
|
size_t h = std::hash<unsigned int>()(val.getDebutC().getRelativeDate());
|
||
|
|
size_t h2 = std::hash<unsigned int>()(val.getFinC().getRelativeDate());
|
||
|
|
return h^h2;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
#endif
|