📄 debug.h
字号:
/* * ASTL - the Automaton Standard Template Library. * C++ generic components for Finite State Automata handling. * Copyright (C) 2000-2003 Vincent Le Maout (vincent.lemaout@chello.fr). * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#ifndef ASTL_DEBUG_H#define ASTL_DEBUG_H#include <cursor.h>#include <concept.h>#include <tools.h>#include <iostream>#include <utility> // pair<>using namespace std;ASTL_BEGIN_NAMESPACE// A plain cursor with default behavior checking its state before any processingtemplate <class DFA>class cursor_debug : public cursor_concept{public: typedef cursor_debug self; typedef typename DFA::state_type state_type; typedef typename DFA::tag_type tag_type; typedef typename DFA::char_type char_type; typedef typename DFA::char_traits char_traits; // Backward compatibility typedefs: typedef state_type State; typedef tag_type Tag; typedef char_type Alphabet; typedef char_traits Sigma;protected: const DFA *dfa; state_type q; bool singular;public: cursor_debug() : dfa(NULL), singular(true) { cerr << "\t create " << endl; } cursor_debug(const DFA &a) : dfa(&a), singular(true) { cerr << "\t create " << endl; } cursor_debug(const DFA &a, state_type p) : dfa(&a), q(p) { cerr << "\t create " << endl; if (q == dfa->null_state) { cerr << "cursor::cursor(DFA, state_type) : warning" << endl; cerr << "\t cursor set on the sink state" << endl; } } state_type sink_state() const { cerr << "\t sink_state " << endl; if (singular) { cerr << "cursor::sink_state() : warning" << endl; cerr << "\t accessing sink state through a singular cursor" << endl; } return dfa->null_state; } state_type src() const { cerr << "\t src " << endl; if (singular) { cerr << "cursor::src() : warning" << endl; cerr << "\t trying to access state through a singular cursor" << endl; } return q; } self& operator= (state_type p) { cerr << "\t = state_type " << endl; q = p; if (q == dfa->null_state) { cerr << "cursor::operator= (state_type) : warning" << endl; cerr << "\t assigning sink state to the cursor" << endl; } singular = false; return *this; } self& operator= (const self &x) { cerr << "\t = self " << endl; if (this == &x) { cerr << "cursor::operator= (cursor &x) : warning" << endl; cerr << "\t assigning cursor to itself (this == &x)" << endl; } q = x.q; dfa = x.dfa; singular = x.singular; if (singular) { cerr << "cursor::operator= (cursor &x) : warning" << endl; cerr << "\t x has a singular value" << endl; } return *this; } bool operator== (const self &x) const { cerr << "\t == " << endl; if (this == &x) { cerr << "cursor::operator== (cursor &x) : warning" << endl; cerr << "\t comparing cursor to itself (this == &x)" << endl; } if (singular) { cerr << "cursor::operator==() : warning" << endl; cerr << "\t comparing a singular cursor" << endl; } return q == x.q; } bool sink() const { cerr << "\t sink " << endl; if (singular) { cerr << "cursor::sink() : warning" << endl; cerr << "\t testing a singular cursor" << endl; } return q == dfa->null_state; } bool forward(const char_type &letter) { cerr << "\tforward " << letter << endl; if (singular) { cerr << "cursor::forward(letter) : warning" << endl; cerr << "\t trying to move along a transition through a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "cursor::forward(letter) : warning" << endl; cerr << "\t source state is the sink state" << endl; } q = dfa->delta1(q, letter); return q != dfa->null_state; } bool src_final() const { cerr << "\t =src_final" << endl; if (singular) { cerr << "cursor::src_final() : warning" << endl; cerr << "\t trying to access source state of a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "cursor::src_final() : warning" << endl; cerr << "\t trying to access sink state" << endl; } return dfa->final(q); } const tag_type& src_tag() const { cerr << "\t =src_tag " << endl; if (singular) { cerr << "cursor::src_tag() : warning" << endl; cerr << "\t testing a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "cursor::src_tag() : warning" << endl; cerr << "\t trying to access tag of sink state" << endl; } return dfa->tag(q); } bool exists(const char_type &letter) const { // transition exists ? cerr << "\t exist " << endl; if (singular) { cerr << "cursor::exist() : warning" << endl; cerr << "\t testing for a transition through a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "cursor::exist() : warning" << endl; cerr << "\t testing for an outgoing transition of sink state" << endl; } return dfa->delta1(q, letter) != dfa->null_state; }};// A forward cursor with default behavior checking its state before any processingtemplate <class DFA>class forward_cursor_debug : public forward_cursor_concept{public: typedef forward_cursor_debug self; typedef typename DFA::state_type state_type; typedef typename DFA::tag_type tag_type; typedef typename DFA::char_type char_type; typedef typename DFA::char_traits char_traits; // Backward compatibility typedefs: typedef state_type State; typedef tag_type Tag; typedef char_type Alphabet; typedef char_traits Sigma;protected: typedef typename DFA::edges_type::const_iterator edges_iterator; const DFA *dfa; state_type q; edges_iterator transition; bool dereferenceable, singular; // singular: the only allowed operation is assignment // dereferenceable: letter and aim of current transition // are accessiblepublic: forward_cursor_debug(const DFA &a, state_type p, const char_type& Letter) : dfa(&a), q(p), dereferenceable(false), singular(false) { if (q == dfa->null_state) cerr << "forward_cursor::forward_cursor(DFA, state_type, letter): warning" << endl << "\t cursor initialized with null state" << endl; else if ((transition = dfa->delta2(q).find(Letter)) == dfa->delta2(q).end()) { cerr << "forward_cursor::forward_cursor(DFA, state_type, letter): warning" << endl; cerr << "\t cursor set on a sink transition" << endl; } else dereferenceable = true; } forward_cursor_debug(const DFA &a, state_type p, edges_iterator t) : dfa(&a), q(p), transition(t), dereferenceable(false), singular(false) { if (q == dfa->null_state) { cerr << "forward_cursor::forward_cursor(DFA, state_type, edges_iterator):"; cerr << " warning" << endl << "\t cursor initialized with null state" << endl; } else dereferenceable = t != dfa->delta2(q).end(); } forward_cursor_debug(const DFA &a, state_type p) : dfa(&a), q(p), dereferenceable(false), singular(false) { cerr << "\t create " << endl; if (q == dfa->null_state) { cerr << "forward_cursor::forward_cursor(DFA, state_type): warning" << endl; cerr << "\t cursor initialized with null state" << endl; } } forward_cursor_debug(const DFA &a) : dfa(&a), q(a.initial()), dereferenceable(false), singular(true) { if (q == dfa->null_state) { cerr << "forward_cursor::forward_cursor(DFA): warning" << endl; cerr << "\t cursor initialized with initial state which is null" << endl; } } forward_cursor_debug() : dfa(NULL), dereferenceable(false), singular(true) { } self& operator= (state_type p) { cerr << "\t assigning" << endl; q = p; dereferenceable = false; if (q == dfa->null_state) { cerr << "forward_cursor::operator= (state_type): warning" << endl; cerr << "\t assigning null state" << endl; } singular = false; return *this; } self& operator= (const self &x) { cerr << "\t assigning self " << endl; if (this == &x) { cerr << "forward_cursor::operator= (forward_cursor x): warning" << endl; cerr << "\t assigning cursor to itself (this == &x)" << endl; } q = x.q; transition = x.transition; dereferenceable = x.dereferenceable; singular = x.singular; dfa = x.dfa; if (singular) { cerr << "forward_cursor::operator= (forward_cursor x): warning" << endl; cerr << "\t x has a singular value" << endl; } if (dfa == NULL) { cerr << "forward_cursor::operator= (forward_cursor x): WARNING" << endl; cerr << "\t assigning a non-initialized cursor x (dfa == NULL)" << endl; } return *this; } bool operator== (const self &x) const { cerr << "\t == " << endl; if (this == &x) { cerr << "forward_cursor::operator== : warning" << endl; cerr << "\t comparing cursor with itself" << endl; } return (q == x.q && transition == x.transition); } char_type letter() const { cerr << "\t letter " << endl; if (singular) { cerr << "forward_cursor::letter() : CRITICAL" << endl; cerr << "\t trying to access transition through a singular cursor" << endl; } else if (!dereferenceable) { cerr << "forward_cursor::letter() : CRITICAL" << endl; cerr << "\t trying to access transition through a non dereferenceable cursor" << endl; cerr << "\t state = " << q << endl; } return (*transition).first; } bool sink() const {cerr << "\t sink " << endl; if (singular) { cerr << "forward_cursor::sink() : WARNING" << endl; cerr << "\t testing for sink state on a singular cursor" << endl; } return q == dfa->null_state; } state_type sink_state() const { cerr << "\t sink_state( " << endl; if (singular) { cerr << "forward_cursor::sink_state() : WARNING" << endl; cerr << "\t requesting the sink state value from a singular cursor" << endl; } return dfa->null_state; } // Obsolet: void to_sink() { if (singular) { cerr << "forward_cursor::to_sink() : WARNING" << endl; cerr << "\t moving a singular cursor to the sink state" << endl; } if (dfa == NULL) { cerr << "forward_cursor::to_sink() : CRITICAL" << endl; cerr << "\t trying to move to the sink state with a non-initialized cursor (dfa == NULL)" << endl; } q = dfa->null_state; } bool first_transition() { cerr << "\t first_transition() " << endl; if (singular) { cerr << "forward_cursor::first_transition() : CRITICAL" << endl; cerr << "\t seeking first outgoing transition of a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "forward_cursor::first_transition() : CRITICAL" << endl; cerr << "\t trying to access first transition of sink source state" << endl; } return dereferenceable = (transition = dfa->delta2(q).begin()) != dfa->delta2(q).end(); } bool next_transition() { cerr << "\tnext_transition() " << endl; if (singular) { cerr << "forward_cursor::next_transition() : CRITICAL" << endl; cerr << "\t seeking next transition of a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "forward_cursor::next_transition() : CRITICAL" << endl; cerr << "\t seeking next transition of sink source state" << endl; } else if (!dereferenceable) { cerr << "forward_cursor::next_transition() : WARNING" << endl; cerr << "\t seeking next transition of a non dereferenceable cursor" << endl; } return dereferenceable = (++transition != dfa->delta2(q).end()); } void forward() { cerr << "\t forward() " << endl; if (singular) { cerr << "forward_cursor::forward() : CRITICAL" << endl; cerr << "\t trying to move along transition of a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "forward_cursor::forward() : CRITICAL" << endl; cerr << "\t trying to move along transition of sink source state" << endl; } else if (!dereferenceable) { cerr << "forward_cursor::forward() : CRITICAL" << endl; cerr << "\t trying to move along transition of a non dereferenceable cursor" << endl; } q = (*transition).second; dereferenceable = false; } bool find(const char_type &letter) { cerr << "\t find(" << q << " " << letter << ") " << endl; if (singular) { cerr << "forward_cursor::find() : CRITICAL" << endl; cerr << "\t trying to find an outgoing transition through a singular cursor" << endl; } else if (q == dfa->null_state) { cerr << "forward_cursor::find() : CRITICAL" << endl; cerr << "\t trying to find an outgoing transition of sink source state" << endl; } edges_iterator tmp = dfa->delta2(q).find(letter); if (tmp != dfa->delta2(q).end()) { transition = tmp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -