Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

matcher.h

00001 
00002 //  Math Type Library
00003 //  $Id: matcher.h,v 1.3 2002/05/07 12:38:55 cparpart Exp $
00004 //  (This file contains the expression tree specific template members)
00005 //
00006 //  Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  modify it under the terms of the GNU Library General Public
00010 //  License as published by the Free Software Foundation; either
00011 //  version 2 of the License, or (at your option) any later version.
00012 //
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Library General Public License for more details.
00017 // 
00018 //  You should have received a copy of the GNU Library General Public License
00019 //  along with this library; see the file COPYING.LIB.  If not, write to
00020 //  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021 //  Boston, MA 02111-1307, USA.
00023 #ifndef libmath_matcher_h
00024 #define libmath_matcher_h
00025 
00026 #include <map>
00027 #include <list>
00028 #include <memory>
00029 #include <string>
00030 
00031 namespace math {
00032 
00033 template<class T>
00034 class TMatchRegistry {
00035 public:
00036     TMatchRegistry();
00037     TMatchRegistry(const TMatchRegistry<T>&);
00038 
00040     void define(const std::string& AId, const TNode<T> *ANode);
00042     bool defined(const std::string& AId) const;
00044     const TNode<T> *get(const std::string& AId) const;
00045 
00047     void mark(const TNode<T> *ANode);
00048 
00050     bool contains(const TNode<T> *ANode) const;
00051 
00052 private:
00053     typedef std::map<std::string, const TNode<T> *> TAnyMap;
00054     typedef std::list<const TNode<T> *> TNodeList;
00055 
00056     TAnyMap FAnyMap;
00057     TNodeList FNodeList;
00058 };
00059 
00061 // The match template tree
00062 
00063 template <class T>
00064 class TMatch {
00065 public:
00066     virtual ~TMatch() {}
00067     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const = 0;
00068 };
00069 
00070 template <class T>
00071 class TNumMatch : public TMatch<T> {
00072 public:
00073     TNumMatch(const T& ANum);
00074 
00075     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00076 
00077 private:
00078     T FNumber;
00079 };
00080 
00081 template <class T>
00082 class TAnyMatch : public TMatch<T> {
00083 public:
00084     TAnyMatch(const std::string& AId);
00085 
00086     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00087 
00088 private:
00089     std::string FIdent;
00090 };
00091 
00094 template <class T>
00095 class T2Match : public TMatch<T> {
00096 protected:
00097     T2Match(TMatch<T> *ALeft, TMatch<T> *ARight);
00098     ~T2Match();
00099 
00100     // gets additional match methods for share soon
00101 
00102     typedef std::list<TMatch<T> *> TList;
00103 
00104     TList FPatterns;
00105 };
00106 
00107 template <class T>
00108 class TPlusMatch : public T2Match<T> {
00109 public:
00110     TPlusMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
00111 
00112     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00113 };
00114 
00115 template <class T>
00116 class TMulMatch : public T2Match<T> {
00117 public:
00118     TMulMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
00119 
00120     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00121 };
00122 
00123 template <class T>
00124 class TNegMatch : public TMatch<T> {
00125 public:
00126     TNegMatch(TMatch<T> *ANode);
00127 
00128     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00129 
00130 private:
00131     std::auto_ptr<TMatch<T> > FNode;
00132 };
00133 
00134 template <class T>
00135 class TDivMatch : public TMatch<T> {
00136 public:
00137     TDivMatch(TMatch<T> *ALeft, TMatch<T> *ARight);
00138     
00139     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00140 
00141 private:
00142     std::auto_ptr<TMatch<T> > FLeft;
00143     std::auto_ptr<TMatch<T> > FRight;
00144 };
00145 
00146 template <class T>
00147 class TPowMatch : public TMatch<T> {
00148 public:
00149     TPowMatch(TMatch<T> *ABase, TMatch<T> *AExp);
00150 
00151     virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
00152 
00153 private:
00154     std::auto_ptr<TMatch<T> > FBase;
00155     std::auto_ptr<TMatch<T> > FExp;
00156 };
00157 
00175 template<class T>
00176 class TMatcher : public TNodeVisitor<T> {
00177 public:
00178     typedef std::map<std::string, TNode<T> > TResult;
00179 
00183     static bool matchExact(const TMatch<T> *AMatch, const TNode<T> *AExpr, 
00184         TMatchRegistry<T> *AReg = 0);
00185 
00190     static bool match(const TMatch<T> *AMatch, const TNode<T> *AExpr,
00191         TMatchRegistry<T> *AReg = 0);
00192 
00196     static unsigned match(const std::string& AMatch, const TNode<T> *AExpr, 
00197         TResult& AResult);
00198 
00199 private:
00200     TMatcher(const TMatch<T> *AMatch, const TNode<T> *ANode,
00201         TMatchRegistry<T> *AReg = 0);
00202 
00203 private:
00204     const TMatch<T> *FMatch;
00205     const TNode<T> *FExpr;
00206 
00207 private:
00208     virtual void visit(TNumberNode<T> *);
00209     virtual void visit(TSymbolNode<T> *);
00210     virtual void visit(TParamNode<T> *);
00211 
00212     virtual void visit(TPlusNode<T> *);
00213     virtual void visit(TNegNode<T> *);
00214 
00215     virtual void visit(TMulNode<T> *);
00216     virtual void visit(TDivNode<T> *);
00217     
00218     virtual void visit(TPowNode<T> *);
00219     virtual void visit(TSqrtNode<T> *);
00220 
00221     virtual void visit(TSinNode<T> *);
00222     virtual void visit(TCosNode<T> *);
00223     virtual void visit(TTanNode<T> *);
00224     virtual void visit(TLnNode<T> *);
00225 
00226     virtual void visit(TFuncNode<T> *);
00227     virtual void visit(TIfNode<T> *);
00228 
00229     virtual void visit(TEquNode<T> *);
00230     virtual void visit(TUnEquNode<T> *);
00231     virtual void visit(TGreaterNode<T> *);
00232     virtual void visit(TLessNode<T> *);
00233     virtual void visit(TGreaterEquNode<T> *);
00234     virtual void visit(TLessEquNode<T> *);
00235 };
00236 
00237 } // namespace math
00238 
00239 #include <math++/matcher.tcc>
00240 
00241 #endif

Generated on Sat Apr 2 22:22:44 2005 for MathTypeLibrary(libmath++) by  doxygen 1.3.9.1