Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / kernel / resource / profile / FutureEvtSet.cpp
1 /* Copyright (c) 2004-2022. 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 {
11 namespace kernel {
12 namespace profile {
13
14 simgrid::kernel::profile::FutureEvtSet future_evt_set; // FIXME: singleton antipattern
15
16 FutureEvtSet::FutureEvtSet() = default;
17 FutureEvtSet::~FutureEvtSet()
18 {
19   while (not heap_.empty()) {
20     delete heap_.top().second;
21     heap_.pop();
22   }
23 }
24
25 /** @brief Schedules an event to a future date */
26 void FutureEvtSet::add_event(double date, Event* evt)
27 {
28   heap_.emplace(date, evt);
29 }
30
31 /** @brief returns the date of the next occurring event (or -1 if empty) */
32 double FutureEvtSet::next_date() const
33 {
34   return heap_.empty() ? -1.0 : heap_.top().first;
35 }
36
37 /** @brief Retrieves the next occurring event, or nullptr if none happens before date */
38 Event* FutureEvtSet::pop_leq(double date, double* value, resource::Resource** resource)
39 {
40   double event_date = next_date();
41   if (event_date > date || heap_.empty())
42     return nullptr;
43
44   Event* event       = heap_.top().second;
45   Profile* profile   = event->profile;
46   DatedValue dateVal = profile->next(event);
47
48   *resource = event->resource;
49   *value    = dateVal.value_;
50
51   heap_.pop();
52
53   return event;
54 }
55 } // namespace profile
56 } // namespace kernel
57 } // namespace simgrid