123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /**
- * @file HeapScheduler.hpp
- * @author The ARTIS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * ARTIS - the multimodeling and simulation environment
- * This file is a part of the ARTIS environment
- *
- * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef COMMON_SCHEDULER_HEAP_SCHEDULER_HPP
- #define COMMON_SCHEDULER_HEAP_SCHEDULER_HPP
- #include <artis-star/common/InternalEvent.hpp>
- #include <boost/heap/fibonacci_heap.hpp>
- #include <sstream>
- namespace artis {
- namespace common {
- template<class Time>
- class Model;
- template<class Time>
- class Models;
- namespace scheduler {
- template<class Time, class T>
- class HeapScheduler :
- public boost::heap::fibonacci_heap<
- InternalEvent<Time>,
- boost::heap::compare<
- EventCompare<InternalEvent<Time> > > >
- {
- public:
- typedef HeapScheduler<Time, T> type;
- typedef Model<Time> model_type;
- typedef Models<Time> models_type;
- typedef InternalEvent<Time> internal_event_type;
- HeapScheduler()
- {}
- virtual ~HeapScheduler()
- {}
- model_type *get_current_model()
- {
- return type::top().get_model();
- }
- models_type get_current_models(const typename Time::type &time) const
- {
- models_type models;
- typename models_type::iterator it;
- for (typename type::ordered_iterator it = type::ordered_begin();
- it != type::ordered_end() and it->get_time() == time; ++it) {
- std::string str = it->get_model()->get_name();
- auto it2 = find_if(models.begin(), models.end(),
- [&str](const model_type *obj) {
- return obj->get_name() == str;
- });
- if (it2 == models.end()) {
- models.push_back(it->get_model());
- }
- }
- return models;
- }
- models_type get_current_models(const typename Time::type &begin,
- const typename Time::type &end) const
- {
- models_type models;
- typename models_type::iterator it;
- for (typename type::ordered_iterator it = type::ordered_begin();
- it != type::ordered_end() and (std::abs(it->get_time() - begin) < 1e-6
- or (it->get_time() > begin
- and it->get_time() < end)); ++it) {
- std::string str = it->get_model()->get_name();
- auto it2 = find_if(models.begin(), models.end(),
- [&str](const model_type *obj) {
- return obj->get_name() == str;
- });
- if (it2 == models.end()) {
- models.push_back(it->get_model());
- }
- }
- return models;
- }
- typename Time::type get_current_time() const
- {
- return type::top().get_time();
- }
- void init(typename Time::type time, model_type *model)
- {
- model->handle(T(type::push(internal_event_type(time, model))));
- }
- void put(typename Time::type time, const model_type *model)
- {
- typename Time::type previous_time =
- (*model->handle()._handle).get_time();
- if (previous_time != time) {
- (*model->handle()._handle).set_time(time);
- if (previous_time < time) {
- type::decrease(model->handle()._handle);
- } else if (previous_time > time) {
- type::increase(model->handle()._handle);
- }
- }
- }
- void remove_model(model_type *model)
- {
- type::erase(model->handle()._handle);
- }
- std::string to_string() const
- {
- std::stringstream ss;
- ss << "Scheduler = { ";
- for (typename type::ordered_iterator it = type::ordered_begin();
- it != type::ordered_end(); ++it) {
- ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
- << ") ";
- }
- ss << "} [" << type::size() << "]";
- return ss.str();
- }
- };
- }
- }
- } // namespace artis common scheduler
- #endif
|