VectorScheduler.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public:
  38. VectorScheduler() {}
  39. virtual ~VectorScheduler() {}
  40. Model<Time, NoSchedulerHandle> *get_current_model() {
  41. return VectorScheduler<Time>::front().get_model();
  42. }
  43. Models<Time, NoSchedulerHandle> get_current_models(
  44. typename Time::type time) const {
  45. Models<Time, NoSchedulerHandle> models;
  46. for (typename VectorScheduler<Time>::const_iterator it =
  47. VectorScheduler<Time>::begin();
  48. it != VectorScheduler<Time>::end() and it->get_time() == time;
  49. ++it) {
  50. models.push_back(it->get_model());
  51. }
  52. return models;
  53. }
  54. typename Time::type get_current_time() const { return VectorScheduler<Time>::front().get_time(); }
  55. void init(typename Time::type time,
  56. Model<Time, NoSchedulerHandle> *model) {
  57. VectorScheduler<Time>::push_back(
  58. InternalEvent<Time, NoSchedulerHandle>(time, model));
  59. std::sort(VectorScheduler<Time>::begin(),
  60. VectorScheduler<Time>::end());
  61. }
  62. void put(typename Time::type time, Model<Time, NoSchedulerHandle> *model) {
  63. remove(model);
  64. VectorScheduler<Time>::push_back(
  65. InternalEvent<Time, NoSchedulerHandle>(time, model));
  66. std::sort(VectorScheduler<Time>::begin(),
  67. VectorScheduler<Time>::end());
  68. }
  69. std::string to_string() const {
  70. std::stringstream ss;
  71. ss << "Scheduler = { ";
  72. for (typename VectorScheduler<Time>::const_iterator it =
  73. VectorScheduler<Time>::begin();
  74. it != VectorScheduler<Time>::end(); ++it) {
  75. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  76. << ") ";
  77. }
  78. ss << "}";
  79. return ss.str();
  80. }
  81. private:
  82. void remove(Model<Time, NoSchedulerHandle> *model) {
  83. typename VectorScheduler<Time>::iterator jt =
  84. VectorScheduler<Time>::begin();
  85. while (jt != VectorScheduler<Time>::end()) {
  86. if (jt->get_model() == model) {
  87. jt = VectorScheduler<Time>::erase(jt);
  88. } else {
  89. ++jt;
  90. }
  91. }
  92. }
  93. };
  94. }
  95. }
  96. } // namespace artis common scheduler
  97. #endif