Math Type Library (libmath++) 0.0.3
matcher.h
1
2// Math Type Library
3// $Id: matcher.h,v 1.3 2002/05/07 12:38:55 cparpart Exp $
4// (This file contains the expression tree specific template members)
5//
6// Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Library General Public
10// License as published by the Free Software Foundation; either
11// version 2 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Library General Public License for more details.
17//
18// You should have received a copy of the GNU Library General Public License
19// along with this library; see the file COPYING.LIB. If not, write to
20// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21// Boston, MA 02111-1307, USA.
23#ifndef libmath_matcher_h
24#define libmath_matcher_h
25
26#include <map>
27#include <list>
28#include <memory>
29#include <string>
30
31namespace math {
32
33template<class T>
35public:
38
40 void define(const std::string& AId, const TNode<T> *ANode);
42 bool defined(const std::string& AId) const;
44 const TNode<T> *get(const std::string& AId) const;
45
47 void mark(const TNode<T> *ANode);
48
50 bool contains(const TNode<T> *ANode) const;
51
52private:
53 typedef std::map<std::string, const TNode<T> *> TAnyMap;
54 typedef std::list<const TNode<T> *> TNodeList;
55
56 TAnyMap FAnyMap;
57 TNodeList FNodeList;
58};
59
61// The match template tree
62
63template <class T>
64class TMatch {
65public:
66 virtual ~TMatch() {}
67 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const = 0;
68};
69
70template <class T>
71class TNumMatch : public TMatch<T> {
72public:
73 TNumMatch(const T& ANum);
74
75 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
76
77private:
78 T FNumber;
79};
80
81template <class T>
82class TAnyMatch : public TMatch<T> {
83public:
84 TAnyMatch(const std::string& AId);
85
86 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
87
88private:
89 std::string FIdent;
90};
91
94template <class T>
95class T2Match : public TMatch<T> {
96protected:
97 T2Match(TMatch<T> *ALeft, TMatch<T> *ARight);
98 ~T2Match();
99
100 // gets additional match methods for share soon
101
102 typedef std::list<TMatch<T> *> TList;
103
104 TList FPatterns;
105};
106
107template <class T>
108class TPlusMatch : public T2Match<T> {
109public:
110 TPlusMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
111
112 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
113};
114
115template <class T>
116class TMulMatch : public T2Match<T> {
117public:
118 TMulMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
119
120 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
121};
122
123template <class T>
124class TNegMatch : public TMatch<T> {
125public:
126 TNegMatch(TMatch<T> *ANode);
127
128 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
129
130private:
131 std::auto_ptr<TMatch<T> > FNode;
132};
133
134template <class T>
135class TDivMatch : public TMatch<T> {
136public:
137 TDivMatch(TMatch<T> *ALeft, TMatch<T> *ARight);
138
139 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
140
141private:
142 std::auto_ptr<TMatch<T> > FLeft;
143 std::auto_ptr<TMatch<T> > FRight;
144};
145
146template <class T>
147class TPowMatch : public TMatch<T> {
148public:
149 TPowMatch(TMatch<T> *ABase, TMatch<T> *AExp);
150
151 virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
152
153private:
154 std::auto_ptr<TMatch<T> > FBase;
155 std::auto_ptr<TMatch<T> > FExp;
156};
157
175template<class T>
176class TMatcher : public TNodeVisitor<T> {
177public:
178 typedef std::map<std::string, TNode<T> > TResult;
179
183 static bool matchExact(const TMatch<T> *AMatch, const TNode<T> *AExpr,
184 TMatchRegistry<T> *AReg = 0);
185
190 static bool match(const TMatch<T> *AMatch, const TNode<T> *AExpr,
191 TMatchRegistry<T> *AReg = 0);
192
196 static unsigned match(const std::string& AMatch, const TNode<T> *AExpr,
197 TResult& AResult);
198
199private:
200 TMatcher(const TMatch<T> *AMatch, const TNode<T> *ANode,
201 TMatchRegistry<T> *AReg = 0);
202
203private:
204 const TMatch<T> *FMatch;
205 const TNode<T> *FExpr;
206
207private:
208 virtual void visit(TNumberNode<T> *);
209 virtual void visit(TSymbolNode<T> *);
210 virtual void visit(TParamNode<T> *);
211
212 virtual void visit(TPlusNode<T> *);
213 virtual void visit(TNegNode<T> *);
214
215 virtual void visit(TMulNode<T> *);
216 virtual void visit(TDivNode<T> *);
217
218 virtual void visit(TPowNode<T> *);
219 virtual void visit(TSqrtNode<T> *);
220
221 virtual void visit(TSinNode<T> *);
222 virtual void visit(TCosNode<T> *);
223 virtual void visit(TTanNode<T> *);
224 virtual void visit(TLnNode<T> *);
225
226 virtual void visit(TFuncNode<T> *);
227 virtual void visit(TIfNode<T> *);
228
229 virtual void visit(TEquNode<T> *);
230 virtual void visit(TUnEquNode<T> *);
231 virtual void visit(TGreaterNode<T> *);
232 virtual void visit(TLessNode<T> *);
233 virtual void visit(TGreaterEquNode<T> *);
234 virtual void visit(TLessEquNode<T> *);
235};
236
237} // namespace math
238
239#include <math++/matcher.tcc>
240
241#endif
const TNode< T > * get(const std::string &AId) const
returns expression node to given id.
bool contains(const TNode< T > *ANode) const
returns true when ANode is either marked as used or is defined as any
bool defined(const std::string &AId) const
checks whether given id is defined or not.
void define(const std::string &AId, const TNode< T > *ANode)
defines given expression node as given id.
void mark(const TNode< T > *ANode)
marks given node as used
static bool matchExact(const TMatch< T > *AMatch, const TNode< T > *AExpr, TMatchRegistry< T > *AReg=0)
static unsigned match(const std::string &AMatch, const TNode< T > *AExpr, TResult &AResult)
static bool match(const TMatch< T > *AMatch, const TNode< T > *AExpr, TMatchRegistry< T > *AReg=0)