HeapScheduler.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file HeapScheduler.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_SCHEDULER_HEAP_SCHEDULER_HPP
  26. #define COMMON_SCHEDULER_HEAP_SCHEDULER_HPP 1
  27. #include <common/InternalEvent.hpp>
  28. #include <boost/heap/fibonacci_heap.hpp>
  29. #include <boost/heap/binomial_heap.hpp>
  30. #include <cmath>
  31. #include <sstream>
  32. namespace paradevs { namespace common { namespace scheduler {
  33. template < typename Time >
  34. struct Heap
  35. {
  36. typedef boost::heap::fibonacci_heap <
  37. InternalEvent < Time >, boost::heap::compare <
  38. EventCompare < InternalEvent < Time > > > > Type;
  39. typedef typename boost::heap::fibonacci_heap <
  40. InternalEvent < Time >, boost::heap::compare <
  41. EventCompare < InternalEvent <
  42. Time > > > >::handle_type Handle;
  43. };
  44. template < class Time >
  45. class HeapScheduler
  46. {
  47. typedef boost::heap::fibonacci_heap < InternalEvent < Time >,
  48. boost::heap::compare <
  49. EventCompare <
  50. InternalEvent < Time > > >
  51. > Heap;
  52. public:
  53. HeapScheduler()
  54. { }
  55. virtual ~HeapScheduler()
  56. { }
  57. Model < Time >* get_current_model()
  58. {
  59. return _heap.top().get_model();
  60. }
  61. Models < Time > get_current_models(typename Time::type time) const
  62. {
  63. Models < Time > models;
  64. for (typename Heap::ordered_iterator it = _heap.ordered_begin();
  65. it != _heap.ordered_end() and it->get_time() == time; ++it) {
  66. models.push_back(it->get_model());
  67. }
  68. return models;
  69. }
  70. typename Time::type get_current_time() const
  71. {
  72. return _heap.top().get_time();
  73. }
  74. void init(typename Time::type time, Model < Time >* model)
  75. {
  76. model->heap_id(_heap.push(InternalEvent < Time >(time, model)));
  77. }
  78. void put(typename Time::type time, Model < Time >* model)
  79. {
  80. typename Time::type previous_time = (*model->heap_id()).get_time();
  81. (*model->heap_id()).set_time(time);
  82. if (previous_time < time) {
  83. _heap.decrease(model->heap_id());
  84. } else if (previous_time > time) {
  85. _heap.increase(model->heap_id());
  86. }
  87. }
  88. std::string to_string() const
  89. {
  90. std::stringstream ss;
  91. ss << "Scheduler = { ";
  92. for (typename Heap::ordered_iterator it = _heap.ordered_begin();
  93. it != _heap.ordered_end(); ++it) {
  94. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  95. << ") ";
  96. }
  97. ss << "}";
  98. return ss.str();
  99. }
  100. private:
  101. Heap _heap;
  102. };
  103. } } } // namespace paradevs common scheduler
  104. #endif