#ifndef BHASH_HEADER #define BHASH_HEADER 1 ////////////////////////////////////////////// //! file="amma/Contain/Branch/BHash.hh" //! lib=Branch //! author="Charles Galambos" //! docentry="Containers.Hash Tables" //! rcsid="$Id: BHash.hh,v 1.12 2000/02/27 19:15:24 ees1cg Exp $" //! date="26/02/97" #include "amma/SArray1d.hh" #include "amma/KPair.hh" #include "amma/BList.hh" #include "amma/HashAR.hh" // Needed for the hash functions.. #include "amma/Empty.hh" //! userlevel=Develop //: Branching Hash table. // Not finised yet. // !!!! Big object !!!! template class BHashC { public: inline BHashC(); // Default constructor. inline BHashC(const BHashC &Oth); //: Copy constructor. // This creates a new handle. inline BHashC(const BHashC &Oth,BooleanT doResize); // Copy constructor. // Creates a copy of the list table. if 'doResize' is // true it will also check if a table resize is needed. inline void Insert(const K &Key,const T &Data); // Insert element into table. // This will override value if key already exists. inline const T *Lookup(const K &Key) const; // Lookup by key. void Add(const BHashC &oth); //: Add contents of another table into this one. private: typedef HashElemT HashElem; SArray1dC > > table; IntT elements; }; //: Branching Hash table. // !!!! Small object !!!! template class BHashIterC { public: inline BHashIterC() {} //: Default constructor. BHashIterC(const BHashC &Oth); // Constructor. inline BooleanT First(); //: Goto first element in table. BooleanT IsElm() const { it.IsElm(); } //: At valid element ? inline BooleanT Next(); //: Goto next element. T &Data() { return it.Data().Data(); } //: Access data. const T &Data() const { return it.Data().Data(); } //: Access data. const K &Key() const { return it.Data().Key(); } //: Access key. private: BooleanT NextValid(); //: Skip forward to the next valid element. SArray1dC > > table; int tp; // Position in table. BListIterC > it; HashARC done; }; //////////////////////////////////////////////////////////////////// // Default constructor. template inline BHashC::BHashC() : table(10), elements(0) {} ///////////////////// // Copy constructor. template inline BHashC::BHashC(const BHashC &oth) : table(oth.table), elements(oth.elements) {} // Copy constructor. // Creates a copy of the list table. if 'doResize' is // true it will also check if a table resize is needed. template inline BHashC::BHashC(const BHashC &oth,BooleanT doResize) : table(table.Copy()), elements(oth.elements) { } ///////////////////// // Insert element into table. template inline void BHashC::Insert(const K &key,const T &data) { table[StdHash(key) % table.Size()].InsFirst(HashElem(key,data)); elements++; } ///////////////////// // Lookup by key. template inline const T *BHashC::Lookup(const K &key) const { for(BListIterC it(table[StdHash(key) % table.Size()]);it.IsElm();it.Next()) { if(it.Data().GetKey() == key) return it.Data().Data(); } return 0; } ///////////////////////////////////////////////////// //: Skip forward to the next valid element. template BooleanT BHashIterC::NextValid() { while(!it.IsElm()) { do { tp++; if(tp >= tableSize()) return false; it = table[tp]; } while(!it.IsElm()) ; if(!done.IsMember(it.Data().Key())) break; it.Next(); } return true; } //: Goto first element in table. template BooleanT BHashIterC::First() { done.Empty(); tp = 0; it = table[0]; return NextValid(); } //: Constructor. template BHashIterC::BHashIterC(const BHashC &Oth) { First(); } //: Goto next element. template BooleanT BHashIterC::Next() { it.Next(); return NextValid(); } //////////////////////////////////////////////// //: Add contents of another table into this one. template void BHashC::Add(const BHashC &oth) { for(BHashIterC it(*this);it.IsElm();it.Next()) Insert(it.Data()); } #endif