#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Logic/Index/LiteralIndex.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_LITERALINDEX_HEADER #define RAVLLOGIC_LITERALINDEX_HEADER 1 /////////////////////////////////////////////////////////// //! rcsid="$Id: LiteralIndex.hh,v 1.5 2002/05/25 19:27:48 craftit Exp $" //! lib=RavlLogic //! docentry="Ravl.Logic.Index" //! file="Ravl/Logic/Index/LiteralIndex.hh" //! author="Charles Galambos" #include "Ravl/Logic/LiteralIndexBase.hh" #include "Ravl/Logic/LiteralIndexLeaf.hh" namespace RavlLogicN { template class LiteralIndexIterC; //! userlevel=Develop //: Literal index. template class LiteralIndexBodyC : public LiteralIndexBaseBodyC { public: LiteralIndexBodyC() {} //: Default constructor. protected: virtual LiteralIndexLeafC NewLeaf(const LiteralC &key) { return LiteralIndexLeafDataC(true,key); } //: Generate a new leaf. public: bool Lookup(const LiteralC &key,DataT &val) { LiteralIndexLeafC leaf = LiteralIndexBaseBodyC::Lookup(key); if(!leaf.IsValid()) return false; LiteralIndexLeafDataC t(leaf); val = t.Data(); return true; } //: Lookup value associated with the key in the index. // if cound copy the value to 'val' and true is return. bool Insert(const LiteralC &key,const DataT &data) { LiteralIndexLeafDataC t(LiteralIndexBaseBodyC::Insert(key)); RavlAssert(t.IsValid()); t.Data() = data; return true; } //: Insert data with given key into index. // Returns false if key already exists. DataT &Access(const LiteralC &key) { LiteralIndexLeafDataC t(LiteralIndexBaseBodyC::Insert(key)); RavlAssert(t.IsValid()); return t.Data(); } //: Access data associated with a literal. }; //! userlevel=Normal //! autolink="on" //: Literal index. // This is an index of 'DataT's based on a Literal key. It maybe queryied directly // with 'Lookup' or iterated through for data items matching a pattern with a // 'LiteralIndexFilterC' template class LiteralIndexC : public LiteralIndexBaseC { public: LiteralIndexC() {} //: Default constructor. // Creates an invalid handle. LiteralIndexC(bool) : LiteralIndexBaseC(*new LiteralIndexBodyC()) {} //: Constructor. protected: LiteralIndexC(LiteralIndexBodyC &bod) : LiteralIndexBaseC(bod) {} //: Body constructor. LiteralIndexBodyC &Body() { return static_cast &>(LiteralIndexBaseC::Body()); } //: Access body. const LiteralIndexBodyC &Body() const { return static_cast &>(LiteralIndexBaseC::Body()); } //: Access body. public: bool Lookup(const LiteralC &key,DataT &val) { return Body().Lookup(key,val); } //: Lookup value associated with the key in the index. // if cound copy the value to 'val' and true is return. bool Insert(const LiteralC &key,const DataT &leaf) { return Body().Insert(key,leaf); } //: Insert data with given key into index. // Returns false if key already exists. DataT &Access(const LiteralC &key) { return Body().Access(key); } //: Access data associated with a literal. DataT &operator[](const LiteralC &key) { return Body().Access(key); } //: Access data associated with a literal. friend class LiteralIndexIterC; }; template ostream &operator<<(ostream &strm,const LiteralIndexC &index) { strm << ((const LiteralIndexBaseC &) index); return strm; } //: Output to stream. template istream &operator>>(istream &strm,LiteralIndexC &index) { strm >> ((const LiteralIndexBaseC &) index); return strm; } //: Input from stream. } #endif