📄 default_simulator.h
字号:
/*! This is just passed to the event queue. */ void delete_event(const Event_key &k) { audit_event(k); //if (k== final_event()) return; //#ifdef NDEBUG if (k== null_event() || k== Event_key()) { return; } //#endif CGAL_KINETIC_LOG(LOG_SOME, "Deleting event " << k << std::endl); queue_.erase(k); //CGAL_KINETIC_LOG(LOG_LOTS , *this); } //! The type for a handle to an event. /*template <class E> struct Event_handle: public Queue::template Event_info<E>::Handle { Event_handle(typename Queue::template Event_info<E>::Pointer p): Queue::template Event_info<E>::Handle(p) { } Event_handle(){} };*/ //! Access an event /*! The type of the returned pointer is an Event_pointer<E> where E is the template argument. I recommend just fetching the event each time you need it, rather than trying to store the pointer type. This method is effectively free (it is just a cast in the default implementation). You cannot change the event using this pointer, it is simply for recovering cached data (like Solvers). If you need to store it, wrap the return value in an Event_handle to get reference counting, otherwise the event might be processed and the object for it go away. The second argument really shouldn't be there, but gcc seems to have issues sometimes if you try to specify the template value directly. */ /*template <class Ev> typename Queue::template Event_pointer<Ev>::Pointer event(const Event_key &k, const Ev& e) const { CGAL_KINETIC_LOG(LOG_LOTS, "Accessing event for key " << k << std::endl); return queue_.event(k,e); }*/ template <class Event_type> const Event_type& event(const Event_key &k) const { CGAL_precondition(k != Event_key()); CGAL_precondition(k != null_event()); //CGAL_KINETIC_LOG(LOG_LOTS, "Accessing event for key " << k << std::endl); return queue_.template get<Event_type>(k); } template <class Event_type> Event_type& event(const Event_key &k){ CGAL_precondition(k != Event_key()); CGAL_precondition(k != null_event()); //CGAL_KINETIC_LOG(LOG_LOTS, "Accessing event for key " << k << std::endl); return queue_.template get<Event_type>(k); } /*template <class Event_type> const Event_type& event(const Event_key &k, const Event_type&) const { CGAL_KINETIC_LOG(LOG_LOTS, "Accessing event for key " << k << std::endl); return queue_.template get<Event_type>(k); }*/ //! Access the time of an event const Time &event_time(const Event_key &k) const { CGAL_precondition(k != Event_key()); return queue_.priority(k); } //! Change an event to have a new object /*! */ template <class Ev> Event_key set_event(const Event_key &k, const Ev& ev) { CGAL_KINETIC_LOG(LOG_SOME, "Changed event " << k << std::flush); Event_key rk= queue_.set(k, ev); CGAL_KINETIC_LOG(LOG_SOME, " to event " << rk << std::endl); CGAL_KINETIC_LOG(LOG_LOTS, *this); return rk; } //! Set which direction time is running /*! Set it to CGAL::NEGATIVE to make time run backwards, CGAL::POSITIVE to make it run forwards. CGAL::ZERO is an error. */ void set_direction_of_time(Sign sn); //! Return the direction of time. CGAL::Sign direction_of_time() const { if (is_forward_) return CGAL::POSITIVE; else return CGAL::NEGATIVE; } //! Return the number of events which has been processed. unsigned int current_event_number() const { return number_of_events_; } bool empty() const { return queue_.empty(); } //! Process all events up to event i /*! i must be greater than current_event_number(). */ void set_current_event_number(unsigned int i) { while (!queue_.empty() && number_of_events_ < i) { /*#ifdef CGAL_KINETIC_CHECK_EXPENSIVE if (current_time() < audit_time_ && next_event_time() >= audit_time_) { audit_all_kdss(); } #endif*/ process_next_event(); } } void write(std::ostream &out) const { out << "Simulator: (" << to_double(current_time()) << "..." << to_double(end_time()) << ")\n"; out << queue_ << std::endl; } void print() const { write(std::cout); } void clear() { queue_.clear(); } void audit_events() const { queue_.audit_events(); } void audit_event(Event_key k) const { if (k!= null_event()) { CGAL_assertion(k != Event_key());#ifndef NDEBUG if (!queue_.contains(k)) { CGAL_KINETIC_ERROR("Event " << k << " is not in queue."); }#endif CGAL_assertion(queue_.contains(k)); queue_.audit_event(k); } }protected: template <class Root> bool compute_audit_time(const Root &) {#ifdef CGAL_KINETIC_ENABLE_AUDITING if (queue_.empty()) { if (cur_time_ != end_time()) { std::pair<double,double> ei= CGAL::to_interval(end_time()); CGAL_precondition(ei.first==ei.second); // I should figure out how to deal in the other case audit_time_ = ei.first; return true; } else { // bad form to just return like that audit_time_= NT(CGAL::to_interval(current_time()).first-10.0); return false; } } else { audit_time_= CGAL::to_interval(current_time()).second; if (CGAL::compare(Time(audit_time_), next_event_time()) != CGAL::SMALLER) { if(CGAL::compare(next_event_time(), current_time()) != CGAL::EQUAL) { audit_time_= mp_(current_time(), next_event_time()); return true; } else { audit_time_= CGAL::to_interval(current_time()).first -10; return false; } } else { return true; } }#else return false;#endif } bool compute_audit_time(double) {#ifdef CGAL_KINETIC_ENABLE_AUDITING //bool audit_on_doubles; if (next_event_time()-current_time() < .1) return false; else { audit_time_= (current_time()+ next_event_time())/2.0; return audit_time_ > current_time() && audit_time_ < next_event_time(); }#else return false;#endif } //! Return a time between the current and the next event to use for validating /*! This is a time between the last certificate failure time and the next. Return true if there is such a time. */ //NT compute_free_time() const; //! Process the next event void process_next_event() { CGAL_KINETIC_LOG(LOG_LOTS, *this); /*#ifndef NDEBUG if (cur_time_ < audit_time_){ audit_all_kdss(); } #endif*/ /*#ifdef CGAL_KINETIC_ENABLE_AUDITING if (CGAL::compare(next_event_time(), current_time()) != CGAL::EQUAL) { //typename Function_kernel::Rational_between_roots bet= kernel_.rational_between_roots_object(); CGAL_KINETIC_LOG(LOG_SOME, "Audit is at time " << audit_time_ << std::endl); //if (current_time() < audit_time_ && t >= audit_time_) { audit_all_kdss(); //} } else { CGAL_KINETIC_LOG(LOG_SOME, "Can't audit between events.\n"); } #endif*/ CGAL_exactness_precondition(CGAL::compare(next_event_time(),current_time()) != CGAL::SMALLER); //if (CGAL::compare(next_event_time(),cur_time_) == CGAL::LARGER) { cur_time_= next_event_time(); queue_.process_front(); ++number_of_events_; CGAL_KINETIC_LOG(LOG_LOTS, *this);#ifdef CGAL_KINETIC_ENABLE_AUDITING if (compute_audit_time(current_time())) { CGAL_KINETIC_LOG(LOG_SOME, "Audit is at time " << audit_time_ << std::endl); //if (current_time() < audit_time_ && t >= audit_time_) { audit_all_kdss(); }#endif } void audit_all_kdss() ; /*const Time &last_event_time() const { return last_event_time_; }*/private: //! add a kds to the simulator /*! Note, these use Prof. Cheritons naming convention (sort of). */ void new_listener(Listener *sk) { //std::cout << "new sim listener.\n";#ifndef NDEBUG for (unsigned int i=0; i< kdss_.size(); ++i){ CGAL_precondition(kdss_[i] != sk); }#endif kdss_.push_back(sk); } //! Remove a kds void delete_listener(Listener *kds) { //std::cout << "delete sim listener.\n"; //kdss_.erase(kds); for (unsigned int i=0; i< kdss_.size(); ++i){ if (kdss_[i] == kds) { std::swap(kdss_[i], kdss_.back()); kdss_.pop_back(); return; } } }protected: Queue queue_; std::vector<Listener*> kdss_; Time cur_time_; //, last_event_time_; unsigned int number_of_events_; typename Function_kernel::Rational_between_roots mp_; // typename Function_kernel::Is_rational ir_; // typename Function_kernel::To_rational tr_; bool is_forward_;#ifdef CGAL_KINETIC_ENABLE_AUDITING NT audit_time_;#endif};/*template <class S, bool E, class PQ> typename Default_simulator<S, E, PQ>::NT Default_simulator<S, E, PQ>::compute_free_time() const { if (!(last_time_ < next_event_time())) { std::cerr << "No time between " << current_time() << " and " << next_event_time() << std::endl; return current_time_nt()- CGAL::abs(current_time_nt()); } else { if (current_time() != last_time_){ double dt= CGAL::to_double(current_time()); NT ntdt(dt); Time tdt(ntdt); if (tdt > last_time_ && tdt < next_event_time()) return NT(dt); } return mp_(current_time(), next_event_time()); } }*/template <class S, class PQ>void Default_simulator<S, PQ>::set_direction_of_time(CGAL::Sign dir){ if (dir != direction_of_time()) { while (next_event_time() == current_time()) { std::cerr << "Processing event in order to reverse time.\n"; process_next_event(); } Time oct= cur_time_; NT tnt= NT(to_interval(cur_time_).second);// was CGAL:: Time tdt(tnt); if (tdt == cur_time_) { tnt= tnt+ NT(.000001); tdt= Time(tnt); } if (CGAL::compare(tdt, next_event_time()) != CGAL::SMALLER) { //typename Function_kernel::Rational_between_roots rbr= kernel_.rational_between_roots_object(); tnt= mp_(cur_time_, next_event_time()); tdt= Time(tnt); } cur_time_= Time(-tnt); is_forward_= !is_forward_; //end_time_=-end_time_; //last_event_time_= -std::numeric_limits<Time>::infinity(); CGAL_KINETIC_LOG(LOG_SOME, "Current time is " << cur_time_ << " and was " << oct << ", end_time() is " << end_time() << std::endl); for (typename std::vector<Listener*>::iterator it= kdss_.begin(); it != kdss_.end(); ++it) { (*it)->new_notification(Listener::DIRECTION_OF_TIME); //std::cout << "called on something.\n"; } } else { CGAL_KINETIC_LOG(LOG_SOME, dir << " " << end_time() << " " << cur_time_ << std::endl); }}template <class S, class PQ>void Default_simulator<S, PQ>::audit_all_kdss(){#ifdef CGAL_KINETIC_ENABLE_AUDITING cur_time_= Time(audit_time_); CGAL_KINETIC_LOG(LOG_SOME, "Auditing KDSs at time " << audit_time() << std::endl); for (typename std::vector<Listener*>::iterator it= kdss_.begin(); it != kdss_.end(); ++it) { //CGAL_exactness_postcondition_code((*it)->new_notification(Listener::HAS_VERIFICATION_TIME)); CGAL_postcondition_code((*it)->new_notification(Listener::HAS_AUDIT_TIME)); } queue_.audit_events();#endif}template <class S, class PQ>std::ostream &operator<<(std::ostream &out, const Default_simulator<S, PQ> &s){ s.write(out); return out;}CGAL_KINETIC_END_NAMESPACE;#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -