matcher.h
// Math Type Library
// $Id: matcher.h,v 1.3 2002/05/07 12:38:55 cparpart Exp $
// (This file contains the expression tree specific template members)
//
// Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef libmath_matcher_h
#define libmath_matcher_h
#include <map>
#include <list>
#include <memory>
#include <string>
namespace math {
template<class T>
class TMatchRegistry {
public:
TMatchRegistry();
TMatchRegistry(const TMatchRegistry<T>&);
void define(const std::string& AId, const TNode<T> *ANode);
bool defined(const std::string& AId) const;
const TNode<T> *get(const std::string& AId) const;
void mark(const TNode<T> *ANode);
bool contains(const TNode<T> *ANode) const;
private:
typedef std::map<std::string, const TNode<T> *> TAnyMap;
typedef std::list<const TNode<T> *> TNodeList;
TAnyMap FAnyMap;
TNodeList FNodeList;
};
// The match template tree
template <class T>
class TMatch {
public:
virtual ~TMatch() {}
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const = 0;
};
template <class T>
class TNumMatch : public TMatch<T> {
public:
TNumMatch(const T& ANum);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
private:
T FNumber;
};
template <class T>
class TAnyMatch : public TMatch<T> {
public:
TAnyMatch(const std::string& AId);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
private:
std::string FIdent;
};
template <class T>
class T2Match : public TMatch<T> {
protected:
T2Match(TMatch<T> *ALeft, TMatch<T> *ARight);
~T2Match();
// gets additional match methods for share soon
typedef std::list<TMatch<T> *> TList;
TList FPatterns;
};
template <class T>
class TPlusMatch : public T2Match<T> {
public:
TPlusMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
};
template <class T>
class TMulMatch : public T2Match<T> {
public:
TMulMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
};
template <class T>
class TNegMatch : public TMatch<T> {
public:
TNegMatch(TMatch<T> *ANode);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
private:
std::auto_ptr<TMatch<T> > FNode;
};
template <class T>
class TDivMatch : public TMatch<T> {
public:
TDivMatch(TMatch<T> *ALeft, TMatch<T> *ARight);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
private:
std::auto_ptr<TMatch<T> > FLeft;
std::auto_ptr<TMatch<T> > FRight;
};
template <class T>
class TPowMatch : public TMatch<T> {
public:
TPowMatch(TMatch<T> *ABase, TMatch<T> *AExp);
virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
private:
std::auto_ptr<TMatch<T> > FBase;
std::auto_ptr<TMatch<T> > FExp;
};
template<class T>
class TMatcher : public TNodeVisitor<T> {
public:
typedef std::map<std::string, TNode<T> > TResult;
static bool matchExact(const TMatch<T> *AMatch, const TNode<T> *AExpr,
TMatchRegistry<T> *AReg = 0);
static bool match(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);
private:
TMatcher(const TMatch<T> *AMatch, const TNode<T> *ANode,
TMatchRegistry<T> *AReg = 0);
private:
const TMatch<T> *FMatch;
const TNode<T> *FExpr;
private:
virtual void visit(TNumberNode<T> *);
virtual void visit(TSymbolNode<T> *);
virtual void visit(TParamNode<T> *);
virtual void visit(TPlusNode<T> *);
virtual void visit(TNegNode<T> *);
virtual void visit(TMulNode<T> *);
virtual void visit(TDivNode<T> *);
virtual void visit(TPowNode<T> *);
virtual void visit(TSqrtNode<T> *);
virtual void visit(TSinNode<T> *);
virtual void visit(TCosNode<T> *);
virtual void visit(TTanNode<T> *);
virtual void visit(TLnNode<T> *);
virtual void visit(TFuncNode<T> *);
virtual void visit(TIfNode<T> *);
virtual void visit(TEquNode<T> *);
virtual void visit(TUnEquNode<T> *);
virtual void visit(TGreaterNode<T> *);
virtual void visit(TLessNode<T> *);
virtual void visit(TGreaterEquNode<T> *);
virtual void visit(TLessEquNode<T> *);
};
} // namespace math
#include <math++/matcher.tcc>
#endif
math::TGreaterNode
Definition: nodes.h:625
math::TTanNode
Definition: nodes.h:538
math::TNegNode
Definition: nodes.h:448
math::TGreaterEquNode
Definition: nodes.h:643
math::TMatch
Definition: matcher.h:64
math::TMatchRegistry::defined
bool defined(const std::string &AId) const
checks whether given id is defined or not.
math::TLessEquNode
Definition: nodes.h:652
math::TPowNode
Definition: nodes.h:486
math::TDivNode
Definition: nodes.h:474
math::TMatcher::match
static bool match(const TMatch< T > *AMatch, const TNode< T > *AExpr, TMatchRegistry< T > *AReg=0)
math::TParamNode
Definition: nodes.h:374
math::TMatcher::matchExact
static bool matchExact(const TMatch< T > *AMatch, const TNode< T > *AExpr, TMatchRegistry< T > *AReg=0)
math::TPowMatch
Definition: matcher.h:147
math::TMatchRegistry::define
void define(const std::string &AId, const TNode< T > *ANode)
defines given expression node as given id.
math::TSqrtNode
Definition: nodes.h:499
math::TNode< T >
math::TNumMatch
Definition: matcher.h:71
math::TMulNode
Definition: nodes.h:461
math::TMatchRegistry
Definition: matcher.h:34
math::TEquNode
Definition: nodes.h:607
math::TMatchRegistry::contains
bool contains(const TNode< T > *ANode) const
returns true when ANode is either marked as used or is defined as any
math::TLessNode
Definition: nodes.h:634
math::TNegMatch
Definition: matcher.h:124
math::TMulMatch
Definition: matcher.h:116
math::TMatcher
Definition: matcher.h:176
math::TSymbolNode
Definition: nodes.h:354
math::TUnEquNode
Definition: nodes.h:616
math::TAnyMatch
Definition: matcher.h:82
math::TCosNode
Definition: nodes.h:525
math::TDivMatch
Definition: matcher.h:135
math::T2Match
Definition: matcher.h:95
math::TLnNode
Definition: nodes.h:551
math::TMatchRegistry::mark
void mark(const TNode< T > *ANode)
marks given node as used
math::TIfNode
Definition: nodes.h:586
math::TPlusNode
Definition: nodes.h:435
math::TSinNode
Definition: nodes.h:512
math::TNodeVisitor
Definition: nodes.h:46
math::TPlusMatch
Definition: matcher.h:108
math::TFuncNode
Definition: nodes.h:566
math::TNumberNode
Definition: nodes.h:335