HeapScheduler.hpp 3.5 KB

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