HeapScheduler.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2019 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
  27. #include <artis-star/common/InternalEvent.hpp>
  28. #include <boost/heap/fibonacci_heap.hpp>
  29. #include <sstream>
  30. namespace artis {
  31. namespace common {
  32. template<class Time>
  33. class Model;
  34. template<class Time>
  35. class Models;
  36. namespace scheduler {
  37. template<class Time, class T>
  38. class HeapScheduler :
  39. public boost::heap::fibonacci_heap<
  40. InternalEvent<Time>,
  41. boost::heap::compare<
  42. EventCompare<InternalEvent<Time> > > > {
  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. virtual ~HeapScheduler() { }
  50. model_type* get_current_model()
  51. {
  52. return type::top().get_model();
  53. }
  54. models_type get_current_models(const typename Time::type& time) const
  55. {
  56. models_type models;
  57. typename models_type::iterator it;
  58. for (typename type::ordered_iterator it = type::ordered_begin();
  59. it != type::ordered_end() and it->get_time() == time; ++it) {
  60. std::string str = it->get_model()->get_name();
  61. auto it2 = find_if(models.begin(), models.end(),
  62. [&str](const model_type* obj) {
  63. return obj->get_name() == str;
  64. });
  65. if (it2 == models.end()) {
  66. models.push_back(it->get_model());
  67. }
  68. }
  69. return models;
  70. }
  71. models_type get_current_models(const typename Time::type& begin,
  72. const typename Time::type& end) const
  73. {
  74. models_type models;
  75. typename models_type::iterator it;
  76. for (typename type::ordered_iterator it = type::ordered_begin();
  77. it != type::ordered_end() and (std::abs(it->get_time() - begin) < 1e-6
  78. or (it->get_time() > begin
  79. and it->get_time() < end)); ++it) {
  80. std::string str = it->get_model()->get_name();
  81. auto it2 = find_if(models.begin(), models.end(),
  82. [&str](const model_type* obj) {
  83. return obj->get_name() == str;
  84. });
  85. if (it2 == models.end()) {
  86. models.push_back(it->get_model());
  87. }
  88. }
  89. return models;
  90. }
  91. typename Time::type get_current_time() const
  92. {
  93. return type::top().get_time();
  94. }
  95. void init(typename Time::type time, model_type* model)
  96. {
  97. model->handle(T(type::push(internal_event_type(time, model))));
  98. }
  99. void put(typename Time::type time, const model_type* model)
  100. {
  101. typename Time::type previous_time =
  102. (*model->handle()._handle).get_time();
  103. if (previous_time != time) {
  104. (*model->handle()._handle).set_time(time);
  105. if (previous_time < time) {
  106. type::decrease(model->handle()._handle);
  107. } else if (previous_time > time) {
  108. type::increase(model->handle()._handle);
  109. }
  110. }
  111. }
  112. void remove_model(model_type* model)
  113. {
  114. type::erase(model->handle()._handle);
  115. }
  116. std::string to_string() const
  117. {
  118. std::stringstream ss;
  119. ss << "Scheduler = { ";
  120. for (typename type::ordered_iterator it = type::ordered_begin();
  121. it != type::ordered_end(); ++it) {
  122. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  123. << ") ";
  124. }
  125. ss << "} [" << type::size() << "]";
  126. return ss.str();
  127. }
  128. };
  129. }
  130. }
  131. } // namespace artis common scheduler
  132. #endif