closure.h

00001 /*
00002  * ASTL - the Automaton Standard Template Library.
00003  * C++ generic components for Finite State Automata handling.
00004  * Copyright (C) 2000-2005 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_CLOSURE_H
00023 #define ASTL_CLOSURE_H
00024 
00025 // A closure cursor is a forward cursor adapter
00026 // hiding all outgoing transitions not labelled with the user-specified
00027 // letter (TODO: useless class, that can be done with the
00028 // filter_cursor, moreover the asynchrone determinizing cursor
00029 // implemented with this class is, I'm afraid, far from optimal)
00030 
00031 #include <astl.h>
00032 
00033 namespace astl {
00034 
00035 template <typename ForwardCursor>
00036 class closure_cursor : public ForwardCursor
00037 {
00038 public:
00039   typedef ForwardCursor             super;
00040   typedef closure_cursor            self;
00041   typedef typename super::char_type char_type;
00042   typedef typename super::state_type state_type;
00043 
00044 protected:
00045   char_type x;
00046 
00047 public:
00048   closure_cursor()
00049     : super()
00050   { }
00051 
00052   closure_cursor(const ForwardCursor &c, const char_type &a)
00053     : super(c), x(a)
00054   { }
00055 
00056   self& operator=(const self &c) {
00057     super::operator=(c);
00058     x = c.x;
00059     return *this;
00060   }
00061 
00062   self& operator=(state_type q) {
00063     super::operator=(q);
00064     return *this;
00065   }
00066 
00067   bool first() {
00068     if (super::first()) {
00069       if (super::letter() != x)
00070         return next();
00071       return true;
00072     }
00073     return false;
00074   }
00075     
00076   bool next() {
00077     while (super::next())
00078       if (super::letter() == x) return true;
00079     return false;
00080   }
00081   
00082   bool forward(const char_type &a) {
00083     if (a == x) return super::forward(a);
00084     *this = super::sink_state();
00085     return false;
00086   }
00087 
00088   void forward() {
00089     super::forward();
00090   }
00091 };
00092 
00093 template <class ForwardCursor>
00094 inline
00095 closure_cursor<ForwardCursor> 
00096 closurec(const ForwardCursor &c, const typename ForwardCursor::char_type &a)
00097 {
00098   return closure_cursor<ForwardCursor>(c, a);
00099 }
00100   
00101 template <typename DFirstCursor, typename OutputCursor>
00102 OutputCursor 
00103 crop(OutputCursor out, DFirstCursor first, DFirstCursor last = DFirstCursor())
00104 {
00105   while (first != last) {
00106     *out++ = first.src();
00107     do 
00108       *out++ = first.aim();
00109     while (first.forward());
00110     while (!first.forward());
00111   }
00112   return out;
00113 }
00114 
00115 } // namespace astl
00116 
00117 #endif // ASTL_CLOSURE_CURSOR

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