HeapScheduler.hpp 4.3 KB

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