VectorScheduler.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file common/scheduler/VectorScheduler.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_VECTOR_SCHEDULER_HPP
  26. #define COMMON_SCHEDULER_VECTOR_SCHEDULER_HPP
  27. #include <artis-star/common/InternalEvent.hpp>
  28. #include <artis-star/common/scheduler/SchedulerHandle.hpp>
  29. #include <algorithm>
  30. #include <sstream>
  31. namespace artis {
  32. namespace common {
  33. namespace scheduler {
  34. template<class Time>
  35. class VectorScheduler :
  36. protected std::vector<InternalEvent<Time, NoSchedulerHandle> >
  37. {
  38. public:
  39. VectorScheduler()
  40. {}
  41. virtual ~VectorScheduler()
  42. {}
  43. Model<Time, NoSchedulerHandle> *get_current_model()
  44. {
  45. return VectorScheduler<Time>::front().get_model();
  46. }
  47. Models<Time, NoSchedulerHandle> get_current_models(
  48. typename Time::type time) const
  49. {
  50. Models<Time, NoSchedulerHandle> models;
  51. for (typename VectorScheduler<Time>::const_iterator it =
  52. VectorScheduler<Time>::begin();
  53. it != VectorScheduler<Time>::end() and it->get_time() == time;
  54. ++it) {
  55. models.push_back(it->get_model());
  56. }
  57. return models;
  58. }
  59. typename Time::type get_current_time() const
  60. { return VectorScheduler<Time>::front().get_time(); }
  61. void init(typename Time::type time,
  62. Model<Time, NoSchedulerHandle> *model)
  63. {
  64. VectorScheduler<Time>::push_back(
  65. InternalEvent<Time, NoSchedulerHandle>(time, model));
  66. std::sort(VectorScheduler<Time>::begin(),
  67. VectorScheduler<Time>::end());
  68. }
  69. void put(typename Time::type time, Model<Time, NoSchedulerHandle> *model)
  70. {
  71. remove(model);
  72. VectorScheduler<Time>::push_back(
  73. InternalEvent<Time, NoSchedulerHandle>(time, model));
  74. std::sort(VectorScheduler<Time>::begin(),
  75. VectorScheduler<Time>::end());
  76. }
  77. std::string to_string() const
  78. {
  79. std::stringstream ss;
  80. ss << "Scheduler = { ";
  81. for (typename VectorScheduler<Time>::const_iterator it =
  82. VectorScheduler<Time>::begin();
  83. it != VectorScheduler<Time>::end(); ++it) {
  84. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  85. << ") ";
  86. }
  87. ss << "}";
  88. return ss.str();
  89. }
  90. private:
  91. void remove(Model<Time, NoSchedulerHandle> *model)
  92. {
  93. typename VectorScheduler<Time>::iterator jt =
  94. VectorScheduler<Time>::begin();
  95. while (jt != VectorScheduler<Time>::end()) {
  96. if (jt->get_model() == model) {
  97. jt = VectorScheduler<Time>::erase(jt);
  98. } else {
  99. ++jt;
  100. }
  101. }
  102. }
  103. };
  104. }
  105. }
  106. } // namespace artis common scheduler
  107. #endif