VectorScheduler.hpp 3.1 KB

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