stats.h

00001 /*
00002  * ASTL - the Automaton Standard Template Library.
00003  * C++ generic components for Finite State Automata handling.
00004  * Copyright (C) 2000-2003 Vincent Le Maout (vincent.lemaout@chello.fr).
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef ASTL_STATS_H
00023 #define ASTL_STATS_H
00024 
00025 #include <astl.h>
00026 
00027 namespace astl {
00028 
00029 struct DFA_stats
00030 {
00031   typedef unsigned long state_type;
00032   typedef empty tag_type;
00033   unsigned long _state_count, _trans_count, _final_states;
00034   static const state_type null_state = 0;
00035   tag_type dummy;
00036 
00037   DFA_stats()
00038     : _state_count(0), _trans_count(0), _final_states(0)
00039   { }
00040 
00041   state_type new_state() { return ++_state_count; }
00042 
00043   template <typename CharType>
00044   void set_trans(state_type from, const CharType&, state_type to) {
00045     ++_trans_count;
00046   }
00047 
00048   struct bool_reference
00049   {
00050     state_type q;
00051     DFA_stats &dfa;
00052 
00053     bool_reference(state_type p, DFA_stats &a)
00054       : q(p), dfa(a)
00055     { }
00056 
00057     bool_reference& operator= (bool b) {
00058       if (b == true)
00059         ++dfa._final_states;
00060       return *this;
00061     }
00062   };
00063 
00064   tag_type& tag(state_type q) { return dummy; }
00065   const tag_type& tag(state_type q) const { return dummy; }
00066 
00067   bool_reference final(state_type q) { return bool_reference(q, *this); }
00068 
00069   unsigned long state_count() const { return _state_count; }
00070 
00071   unsigned long trans_count() const { return _trans_count; }
00072 
00073   unsigned long final_count() const { return _final_states; }
00074 };
00075 
00076 template <typename DFirstCursor>
00077 int word_count(DFirstCursor first, DFirstCursor last = DFirstCursor())
00078 {
00079   int count = 0;
00080   if (first != last && first.src_final()) ++count;
00081 
00082   while (first != last) {
00083     do if (first.aim_final()) ++count;
00084     while (first.forward());
00085     while (!first.forward());
00086   }
00087   return count;
00088 }
00089 
00090 } // namespace astl                             
00091 
00092 #endif // ASTL_STATS_H
00093 
00094 
00095 
00096 
00097 
00098 
00099 

Generated on Sun Mar 8 02:41:35 2009 for ASTL by  doxygen 1.5.7.1