HeapScheduler.hpp 3.5 KB

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