HeapScheduler.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 1
  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(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. typename Time::type get_current_time() const
  72. {
  73. return type::top().get_time();
  74. }
  75. void init(typename Time::type time, model_type* model)
  76. {
  77. model->handle(T(type::push(internal_event_type(time, model))));
  78. }
  79. void put(typename Time::type time, const model_type* model)
  80. {
  81. typename Time::type previous_time =
  82. (*model->handle()._handle).get_time();
  83. if (previous_time != time) {
  84. (*model->handle()._handle).set_time(time);
  85. if (previous_time < time) {
  86. type::decrease(model->handle()._handle);
  87. } else if (previous_time > time) {
  88. type::increase(model->handle()._handle);
  89. }
  90. }
  91. }
  92. std::string to_string() const
  93. {
  94. std::stringstream ss;
  95. ss << "Scheduler = { ";
  96. for (typename type::ordered_iterator it = type::ordered_begin();
  97. it != type::ordered_end(); ++it) {
  98. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  99. << ") ";
  100. }
  101. ss << "} [" << type::size() << "]";
  102. return ss.str();
  103. }
  104. };
  105. }
  106. }
  107. } // namespace artis common scheduler
  108. #endif