VectorScheduler.hpp 3.6 KB

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