HeapScheduler.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <sstream>
  30. namespace paradevs { namespace common { namespace scheduler {
  31. template < class Time, class T >
  32. class HeapScheduler :
  33. public boost::heap::fibonacci_heap <
  34. InternalEvent < Time, T >,
  35. boost::heap::compare <
  36. EventCompare < InternalEvent < Time, T > > > >
  37. {
  38. public:
  39. typedef HeapScheduler < Time, T > type;
  40. typedef Model < Time, T > model_type;
  41. typedef Models < Time, T > models_type;
  42. typedef InternalEvent < Time, T > internal_event_type;
  43. HeapScheduler()
  44. { }
  45. virtual ~HeapScheduler()
  46. { }
  47. model_type* get_current_model()
  48. {
  49. return type::top().get_model();
  50. }
  51. models_type get_current_models(typename Time::type time) const
  52. {
  53. models_type models;
  54. for (typename type::ordered_iterator it = type::ordered_begin();
  55. it != type::ordered_end() and it->get_time() == time; ++it) {
  56. models.push_back(it->get_model());
  57. }
  58. return models;
  59. }
  60. typename Time::type get_current_time() const
  61. {
  62. return type::top().get_time();
  63. }
  64. void init(typename Time::type time, model_type* model)
  65. {
  66. model->handle(T(type::push(internal_event_type(time, model))));
  67. }
  68. void put(typename Time::type time, model_type* model)
  69. {
  70. typename Time::type previous_time =
  71. (*model->handle()._handle).get_time();
  72. (*model->handle()._handle).set_time(time);
  73. if (previous_time < time) {
  74. type::decrease(model->handle()._handle);
  75. } else if (previous_time > time) {
  76. type::increase(model->handle()._handle);
  77. }
  78. }
  79. std::string to_string() const
  80. {
  81. std::stringstream ss;
  82. ss << "Scheduler = { ";
  83. for (typename type::ordered_iterator it = type::ordered_begin();
  84. it != type::ordered_end(); ++it) {
  85. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  86. << ") ";
  87. }
  88. ss << "}";
  89. return ss.str();
  90. }
  91. };
  92. } } } // namespace paradevs common scheduler
  93. #endif