VectorScheduler.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file VectorScheduler.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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 <common/InternalEvent.hpp>
  28. #include <algorithm>
  29. #include <sstream>
  30. namespace paradevs { namespace common { namespace scheduler {
  31. template < class Time >
  32. class VectorScheduler : protected std::vector < InternalEvent < Time > >
  33. {
  34. public:
  35. VectorScheduler()
  36. { }
  37. virtual ~VectorScheduler()
  38. { }
  39. Model < Time >* get_current_model()
  40. {
  41. return VectorScheduler < Time >::front().get_model();
  42. }
  43. Models < Time > get_current_models(typename Time::type time) const
  44. {
  45. Models < Time > 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
  55. { return VectorScheduler < Time >::front().get_time(); }
  56. void init(typename Time::type time, Model < Time >* model)
  57. {
  58. VectorScheduler < Time >::push_back(
  59. InternalEvent < Time >(time, model));
  60. std::sort(VectorScheduler < Time >::begin(),
  61. VectorScheduler < Time >::end());
  62. }
  63. void put_increase(typename Time::type time, Model < Time >* model)
  64. {
  65. remove(model);
  66. VectorScheduler < Time >::push_back(
  67. InternalEvent < Time >(time, model));
  68. std::sort(VectorScheduler < Time >::begin(),
  69. VectorScheduler < Time >::end());
  70. }
  71. void put_decrease(typename Time::type time, Model < Time >* model)
  72. {
  73. remove(model);
  74. VectorScheduler < Time >::push_back(
  75. InternalEvent < Time >(time, model));
  76. std::sort(VectorScheduler < Time >::begin(),
  77. VectorScheduler < Time >::end());
  78. }
  79. std::string to_string() const
  80. {
  81. std::stringstream ss;
  82. ss << "Scheduler = { ";
  83. for (typename VectorScheduler < Time >::const_iterator it =
  84. VectorScheduler < Time >::begin();
  85. it != VectorScheduler < Time >::end(); ++it) {
  86. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  87. << ") ";
  88. }
  89. ss << "}";
  90. return ss.str();
  91. }
  92. private:
  93. void remove(Model < Time >* model)
  94. {
  95. typename VectorScheduler < Time >::iterator jt =
  96. VectorScheduler < Time >::begin();
  97. while (jt != VectorScheduler < Time >::end()) {
  98. if (jt->get_model() == model) {
  99. jt = VectorScheduler < Time >::erase(jt);
  100. } else {
  101. ++jt;
  102. }
  103. }
  104. }
  105. };
  106. } } } // namespace paradevs common scheduler
  107. #endif