VectorScheduler.hpp 4.4 KB

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