HeapScheduler.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file HeapScheduler.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2018 ULCO http://www.univ-littoral.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 <artis-star/common/InternalEvent.hpp>
  28. #include <boost/heap/fibonacci_heap.hpp>
  29. #include <sstream>
  30. namespace artis { namespace common {
  31. template < class Time >
  32. class Model;
  33. template < class Time >
  34. class Models;
  35. namespace scheduler {
  36. template < class Time, class T >
  37. class HeapScheduler :
  38. public boost::heap::fibonacci_heap <
  39. InternalEvent < Time >,
  40. boost::heap::compare <
  41. EventCompare < InternalEvent < Time > > > >
  42. {
  43. public:
  44. typedef HeapScheduler < Time, T > type;
  45. typedef Model < Time > model_type;
  46. typedef Models < Time > models_type;
  47. typedef InternalEvent < Time > internal_event_type;
  48. HeapScheduler()
  49. { }
  50. virtual ~HeapScheduler()
  51. { }
  52. model_type* get_current_model()
  53. {
  54. return type::top().get_model();
  55. }
  56. models_type get_current_models(typename Time::type time) const
  57. {
  58. models_type models;
  59. typename models_type::iterator it;
  60. for (typename type::ordered_iterator it = type::ordered_begin();
  61. it != type::ordered_end() and it->get_time() == time; ++it) {
  62. std::string str = it->get_model()->get_name();
  63. auto it2 = find_if(models.begin(), models.end(),
  64. [&str](const model_type* obj) {
  65. return obj->get_name() == str;
  66. });
  67. if (it2 == models.end()) {
  68. models.push_back(it->get_model());
  69. }
  70. }
  71. return models;
  72. }
  73. typename Time::type get_current_time() const
  74. {
  75. return type::top().get_time();
  76. }
  77. void init(typename Time::type time, model_type* model)
  78. {
  79. model->handle(T(type::push(internal_event_type(time, model))));
  80. }
  81. void put(typename Time::type time, const model_type* model)
  82. {
  83. typename Time::type previous_time =
  84. (*model->handle()._handle).get_time();
  85. if (previous_time != time) {
  86. (*model->handle()._handle).set_time(time);
  87. if (previous_time < time) {
  88. type::decrease(model->handle()._handle);
  89. } else if (previous_time > time) {
  90. type::increase(model->handle()._handle);
  91. }
  92. }
  93. }
  94. std::string to_string() const
  95. {
  96. std::stringstream ss;
  97. ss << "Scheduler = { ";
  98. for (typename type::ordered_iterator it = type::ordered_begin();
  99. it != type::ordered_end(); ++it) {
  100. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  101. << ") ";
  102. }
  103. ss << "} [" << type::size() << "]";
  104. return ss.str();
  105. }
  106. };
  107. } } } // namespace artis common scheduler
  108. #endif