00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ASTL_CLOSURE_H
00023 #define ASTL_CLOSURE_H
00024
00025
00026
00027
00028
00029
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 }
00116
00117 #endif // ASTL_CLOSURE_CURSOR