BasicUsageEnvironment/include/BasicHashTable.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00017 // Basic Hash Table implementation
00018 // C++ header
00019 
00020 #ifndef _BASIC_HASH_TABLE_HH
00021 #define _BASIC_HASH_TABLE_HH
00022 
00023 #ifndef _HASH_TABLE_HH
00024 #include "HashTable.hh"
00025 #endif
00026 #ifndef _NET_COMMON_H
00027 #include <NetCommon.h> // to ensure that "uintptr_t" is defined
00028 #endif
00029 
00030 // A simple hash table implementation, inspired by the hash table
00031 // implementation used in Tcl 7.6: <http://www.tcl.tk/>
00032 
00033 #define SMALL_HASH_TABLE_SIZE 4
00034 
00035 class BasicHashTable: public HashTable {
00036 private:
00037         class TableEntry; // forward
00038 
00039 public:
00040   BasicHashTable(int keyType);
00041   virtual ~BasicHashTable();
00042 
00043   // Used to iterate through the members of the table:
00044   class Iterator; friend class Iterator; // to make Sun's C++ compiler happy
00045   class Iterator: public HashTable::Iterator {
00046   public:
00047     Iterator(BasicHashTable const& table);
00048 
00049   private: // implementation of inherited pure virtual functions
00050     void* next(char const*& key); // returns 0 if none
00051 
00052   private:
00053     BasicHashTable const& fTable;
00054     unsigned fNextIndex; // index of next bucket to be enumerated after this
00055     TableEntry* fNextEntry; // next entry in the current bucket
00056   };
00057 
00058 private: // implementation of inherited pure virtual functions
00059   virtual void* Add(char const* key, void* value);
00060   // Returns the old value if different, otherwise 0
00061   virtual Boolean Remove(char const* key);
00062   virtual void* Lookup(char const* key) const;
00063   // Returns 0 if not found
00064   virtual unsigned numEntries() const;
00065 
00066 private:
00067   class TableEntry {
00068   public:
00069     TableEntry* fNext;
00070     char const* key;
00071     void* value;
00072   };
00073 
00074   TableEntry* lookupKey(char const* key, unsigned& index) const;
00075     // returns entry matching "key", or NULL if none
00076   Boolean keyMatches(char const* key1, char const* key2) const;
00077     // used to implement "lookupKey()"
00078 
00079   TableEntry* insertNewEntry(unsigned index, char const* key);
00080     // creates a new entry, and inserts it in the table
00081   void assignKey(TableEntry* entry, char const* key);
00082     // used to implement "insertNewEntry()"
00083 
00084   void deleteEntry(unsigned index, TableEntry* entry);
00085   void deleteKey(TableEntry* entry);
00086     // used to implement "deleteEntry()"
00087 
00088   void rebuild(); // rebuilds the table as its size increases
00089 
00090   unsigned hashIndexFromKey(char const* key) const;
00091     // used to implement many of the routines above
00092 
00093   unsigned randomIndex(uintptr_t i) const {
00094     return (unsigned)(((i*1103515245) >> fDownShift) & fMask);
00095   }
00096 
00097 private:
00098   TableEntry** fBuckets; // pointer to bucket array
00099   TableEntry* fStaticBuckets[SMALL_HASH_TABLE_SIZE];// used for small tables
00100   unsigned fNumBuckets, fNumEntries, fRebuildSize, fDownShift, fMask;
00101   int fKeyType;
00102 };
00103 
00104 #endif

Generated on Mon Apr 29 13:28:00 2013 for live by  doxygen 1.5.2