Logo AND Algorithmique Numérique Distribuée

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