00001 // 00002 // Dictionary.h 00003 // 00004 // Dictionary: This class provides an object lookup table. 00005 // Each object in the dictionary is indexed with a string. 00006 // The objects can be returned by mentioning their 00007 // string index. 00008 // 00009 // Part of the ht://Dig package <http://www.htdig.org/> 00010 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00011 // For copyright details, see the file COPYING in your distribution 00012 // or the GNU General Public License version 2 or later 00013 // <http://www.gnu.org/copyleft/gpl.html> 00014 // 00015 // $Id: Dictionary_8h-source.html,v 1.1 2008/06/08 10:12:53 sebdiaz Exp $ 00016 // 00017 00018 #ifndef _Dictionary_h_ 00019 #define _Dictionary_h_ 00020 00021 #include "Object.h" 00022 #include "htString.h" 00023 #include "List.h" 00024 00025 class Dictionary; 00026 class DictionaryEntry; 00027 00028 class DictionaryCursor { 00029 public: 00030 // 00031 // Support for the Start_Get and Get_Next routines 00032 // 00033 int currentTableIndex; 00034 DictionaryEntry *currentDictionaryEntry; 00035 }; 00036 00037 class Dictionary : public Object 00038 { 00039 public: 00040 // 00041 // Construction/Destruction 00042 // 00043 Dictionary(); 00044 Dictionary(const Dictionary& other); 00045 Dictionary(int initialCapacity); 00046 Dictionary(int initialCapacity, float loadFactor); 00047 ~Dictionary(); 00048 00049 // 00050 // Adding and deleting items to and from the dictionary 00051 // 00052 void Add(const String& name, Object *obj); 00053 int Remove(const String& name); 00054 00055 // 00056 // Searching can be done with the Find() member of the array indexing 00057 // operator 00058 // 00059 Object *Find(const String& name) const; 00060 Object *operator[](const String& name) const; 00061 int Exists(const String& name) const; 00062 00063 // 00064 // We want to be able to go through all the entries in the 00065 // dictionary in sequence. To do this, we have the same 00066 // traversal interface as the List class 00067 // 00068 void Start_Get() { Start_Get(cursor); } 00069 void Start_Get(DictionaryCursor& cursor) const; 00070 // 00071 // Get the next key 00072 // 00073 char *Get_Next() { return Get_Next(cursor); } 00074 char *Get_Next(DictionaryCursor& cursor) const; 00075 // 00076 // Get the next entry 00077 // 00078 Object *Get_NextElement() { return Get_NextElement(cursor); } 00079 Object *Get_NextElement(DictionaryCursor& cursor) const; 00080 void Release(); 00081 void Destroy(); 00082 int Count() const { return count; } 00083 00084 private: 00085 DictionaryEntry **table; 00086 int tableLength; 00087 int initialCapacity; 00088 int count; 00089 int threshold; 00090 float loadFactor; 00091 00092 DictionaryCursor cursor; 00093 00094 void rehash(); 00095 void init(int, float); 00096 unsigned int hashCode(const char *key) const; 00097 }; 00098 00099 #endif