Created
March 20, 2018 17:48
-
-
Save cgdangelo/06015dc663fdff3b012358a6d683a324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Event.h" | |
namespace simfantasy { | |
namespace events { | |
Event::Event(Simulation *simulation) : simulation(simulation) {} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SIMFANTASY_EVENT_H | |
#define SIMFANTASY_EVENT_H | |
#include <chrono> | |
#include "Simulation.h" | |
namespace simfantasy { | |
namespace events { | |
using std::chrono::high_resolution_clock; | |
using simfantasy::simulator::Simulation; | |
class Event { | |
public: | |
explicit Event(Simulation *simulation); | |
inline high_resolution_clock::time_point timestamp() const { | |
return timestamp_; | |
} | |
inline void Unschedule() { | |
unscheduled_ = true; | |
} | |
inline bool unscheduled() const { | |
return unscheduled_; | |
} | |
private: | |
Simulation *simulation; | |
high_resolution_clock::time_point timestamp_; | |
bool unscheduled_; | |
}; | |
} | |
} | |
#endif //SIMFANTASY_EVENT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Simulation.h" | |
namespace simfantasy { | |
namespace simulator { | |
inline bool event_cmp(const Event *lhs, const Event *rhs) { | |
return lhs->timestamp() > rhs->timestamp(); | |
} | |
Simulation::Simulation() : events(event_queue(event_cmp)) {} | |
void Simulation::Run() { | |
start_time_ = current_time_ = high_resolution_clock::now(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SIMFANTASY_SIMULATION_H | |
#define SIMFANTASY_SIMULATION_H | |
#include <chrono> | |
#include <queue> | |
#include <functional> | |
#include "Event.h" | |
namespace simfantasy { | |
namespace simulator { | |
using std::chrono::high_resolution_clock; | |
using std::priority_queue; | |
using std::deque; | |
using std::function; | |
using simfantasy::events::Event; | |
using event_queue = priority_queue<Event *, deque<Event *>, function<bool(Event *, Event *)>>; | |
class Simulation { | |
public: | |
Simulation(); | |
void Run(); | |
private: | |
high_resolution_clock::time_point current_time_; | |
event_queue events; | |
high_resolution_clock::time_point start_time_; | |
}; | |
} | |
} | |
#endif //SIMFANTASY_SIMULATION_H |
Author
cgdangelo
commented
Mar 20, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment