Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / kernel / resource / profile / FutureEvtSet.cpp
1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
7 #include "src/kernel/resource/profile/Event.hpp"
8 #include "src/kernel/resource/profile/Profile.hpp"
9
10 namespace simgrid::kernel::profile {
11
12 simgrid::kernel::profile::FutureEvtSet future_evt_set; // FIXME: singleton antipattern
13
14 FutureEvtSet::FutureEvtSet() = default;
15 FutureEvtSet::~FutureEvtSet()
16 {
17   while (not heap_.empty()) {
18     delete heap_.top().second;
19     heap_.pop();
20   }
21 }
22
23 /** @brief Schedules an event to a future date */
24 void FutureEvtSet::add_event(double date, Event* evt)
25 {
26   heap_.emplace(date, evt);
27 }
28
29 /** @brief returns the date of the next occurring event (or -1 if empty) */
30 double FutureEvtSet::next_date() const
31 {
32   return heap_.empty() ? -1.0 : heap_.top().first;
33 }
34
35 /** @brief Retrieves the next occurring event, or nullptr if none happens before date */
36 Event* FutureEvtSet::pop_leq(double date, double* value, resource::Resource** resource)
37 {
38   if (next_date() > date || heap_.empty())
39     return nullptr;
40
41   Event* event       = heap_.top().second;
42   Profile* profile   = event->profile;
43   DatedValue dateVal = profile->next(event);
44
45   *resource = event->resource;
46   *value    = dateVal.value_;
47
48   heap_.pop();
49
50   return event;
51 }
52 } // namespace simgrid::kernel::profile