HeapScheduler.hpp 4.3 KB

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