#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Logic/Base/BindSet.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVLLOGIC_BINDSET_HEADER #define RAVLLOGIC_BINDSET_HEADER 1 ////////////////////////////////////////////////////////// //! rcsid="$Id: BindSet.hh,v 1.8 2002/08/09 14:14:41 jonstarck Exp $" //! docentry="Ravl.Logic" //! author="Charles Galambos" //! lib=RavlLogic //! file="Ravl/Logic/Base/BindSet.hh" #include "Ravl/Logic/Literal.hh" #include "Ravl/Hash.hh" #include "Ravl/HSet.hh" namespace RavlLogicN { using namespace RavlN; #if RAVL_VISUALCPP_NAMESPACE_BUG using RavlN::HashElemC; using RavlN::RCBodyC; using RavlN::HashC; using RavlN::UIntT; using RavlN::RCHandleC; using RavlN::HashIterC; using RavlN::StringC; #endif class BindC; class BindSetC; typedef HashElemC* BindMarkT; //! userlevel=Advanced //: Information about a single binding. class BindC { public: BindC() {} BindC(const LiteralC &val,BindMarkT nnext) : value(val), next(nnext) {} LiteralC &Value() { return value; } //: Access value. const LiteralC &Value() const { return value; } //: Access value. BindMarkT &Next() { return next; } //: access next ptr. const BindMarkT &Next() const { return next; } //: access next ptr. protected: LiteralC value; BindMarkT next; }; inline ostream &operator<<(ostream &s,const BindC &bind) { s << bind.Value(); return s; } //! userlevel=Advanced //: output stream inline istream &operator<<(istream &s,BindC &bind) { RavlAssertMsg(0,"operator<<(istream &s,BindC &binds), Not implemented. "); return s; } //! userlevel=Advanced //: input stream typedef HashElemC* BindMarkT; //! userlevel=Normal //: Set of bindings. // Note: This is a small object. class BindSetBodyC : public RCBodyC, protected HashC { public: BindSetBodyC() : top(0) {} //: Default constructor. BindSetBodyC(const BindSetC &oth); //: Copy constructor. // Any bind marks in oth will NOT be valid for // the new BindSetC. BindSetBodyC(const HashC &tab); //: Construct from a table of mappings. bool IsBound(const LiteralC &var) const { return Lookup(var) != 0; } //: Is variable bound ? bool Resolve(const LiteralC &var,LiteralC &x) const { const BindC *lu = Lookup(var); if(lu == 0) return false; x = lu->Value(); return true; } //: Resolve value LiteralC Resolve(const LiteralC &var) const { const BindC *lu = Lookup(var); if(lu != 0) return lu->Value(); return var; } //: Resolve a binding. // return the value 'var' is bound to, or // if its a free variable 'var' itself. LiteralC operator[](const LiteralC &var) const { return Resolve(var); } //: Resolve a binding. bool Bind(const LiteralC &var,const LiteralC &val); //: Attempty to bind a value to var. // Will fail if var is already bound. BindMarkT Mark() const { return top; } //: Mark the current set of bindings. void Undo(BindMarkT bm); //: Undo bindings to marked place. void Undo(LiteralC var); //: Undo bindings done after and including var. UIntT Size() const { return HashC::Size(); } //: Get the number of binds in set. void Empty(); //: Remove all bindings from set. StringC Name() const; //: Bind set as string. protected: BindMarkT top; friend class BindSetC; }; //! userlevel=Normal //: Set of bindings. // Note: This is a small object. class BindSetC : public RCHandleC { public: BindSetC() {} //: Default constructor. // Creates an invalid handle. BindSetC(bool) : RCHandleC(*new BindSetBodyC()) {} //: Constructor. // Creates an empty bind set. BindSetC(const HashC &tab) : RCHandleC(*new BindSetBodyC(tab)) {} //: Construct from a table of mappings. protected: BindSetBodyC &Body() { return RCHandleC::Body(); } //: Access body. const BindSetBodyC &Body() const { return RCHandleC::Body(); } //: Access body. public: bool IsBound(const LiteralC &var) const { return Body().IsBound(var); } //: Is variable bound ? bool Resolve(const LiteralC &var,LiteralC &x) const { return Body().Resolve(var,x); } //: Resolve value // Lookup 'var' in set, if found assign its value to 'x' and // return true, otherwise return false. LiteralC Resolve(const LiteralC &var) const { return Body().Resolve(var); } //: Resolve a binding. // return the value 'var' is bound to, or // if its a free variable 'var' itself. LiteralC operator[](const LiteralC &var) const { return Resolve(var); } //: Resolve a binding. bool Bind(const LiteralC &var,const LiteralC &val) { return Body().Bind(var,val); } //: Attempty to bind a value to var. // Will fail if var is already bound. BindMarkT Mark() const { return Body().Mark(); } //: Mark the current set of bindings. void Undo(BindMarkT bm) { Body().Undo(bm); } //: Undo bindings to marked place. void Undo(LiteralC var) { Body().Undo(var); } //: Undo bindings done after and including var. UIntT Size() const { return Body().Size(); } //: Get the number of binds in set. void Empty() { Body().Empty(); } //: Remove all bindings from set. HashIterC Iter() { return HashC(Body()); } //: Iterate through binds. StringC Name() const { return Body().Name(); } //: Bind set as string. }; ostream &operator<<(ostream &s,const BindSetC &binds); //: output stream istream &operator<<(istream &s,BindSetC &binds); //: input stream } #endif