00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __UT_HashTable_h__
00019 #define __UT_HashTable_h__
00020
00021 #include "UT_API.h"
00022 #include <stdio.h>
00023 #include <iostream.h>
00024 #include <SYS/SYS_Types.h>
00025 #include "UT_ThingList.h"
00026 #include "UT_Hash.h"
00027
00028 class UT_API UT_HashTable {
00029 public:
00030 explicit UT_HashTable();
00031 virtual ~UT_HashTable();
00032
00033
00034
00035
00036
00037 void useOrderedLists(bool ordered);
00038
00039 void addSymbol(const UT_Hash& symbol, const UT_Thing &data);
00040 int findSymbol(const UT_Hash& symbol, UT_Thing *datap,
00041 const UT_Hash **hash_symbol = 0) const;
00042
00043 int contains(const UT_Hash &symbol) const
00044 { UT_Thing value; return findSymbol(symbol, &value); }
00045
00046 template <typename T>
00047 T *findPointer(const UT_Hash &symbol) const
00048 {
00049 UT_Thing thing;
00050 if (!findSymbol(symbol, &thing))
00051 return NULL;
00052 return thing.template asPointer<T>();
00053 }
00054
00055 int deleteSymbol(const UT_Hash& symbol);
00056 int moveSymbol(const UT_Hash &oldsymbol,
00057 const UT_Hash &newsymbol);
00058 void clear();
00059
00060 unsigned entries() const { return myNbrEntries; }
00061 bool empty() const { return !myNbrEntries; }
00062
00063
00064 UT_Thing &operator[](const UT_Hash& symbol);
00065
00066
00067
00068
00069 float getMaxLoadFactor() const { return myMaxLoadFactor; }
00070 void setMaxLoadFactor(float max_load_factor);
00071 float getMinLoadFactor() const { return myMinLoadFactor; }
00072 void setMinLoadFactor(float min_load_factor);
00073
00074
00075 void mergeTable(const UT_HashTable &table);
00076
00077
00078
00079
00080 const UT_Hash *getHashReference(const UT_Hash &hash);
00081
00082 void outputStats(FILE *output) const;
00083 void outputStats(ostream &os) const;
00084 void dumpTable(FILE *output) const;
00085
00086 int64 getMemUsage(bool only_this) const;
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 int traverseConst(
00099 int (*function)(UT_Thing &, const UT_Hash&, void *),
00100 void *data) const;
00101 int traverse(int (*function)(UT_Thing &, const UT_Hash&, void *),
00102 void *data);
00103
00104 class UT_API traverser
00105 {
00106 public:
00107 traverser()
00108 : myCurrList(0), myEndList(0)
00109 { }
00110 traverser(const traverser &src)
00111 { *this = src; }
00112 ~traverser()
00113 { }
00114
00115 UT_Thing &thing()
00116 { return myCurrEntry.thing(); }
00117 UT_Hash *hash()
00118 { return myCurrEntry.hash(); }
00119
00120 int operator==(const traverser &cmp)
00121 {
00122 return ((!myCurrList && !cmp.myCurrList) ||
00123 ((myCurrList == cmp.myCurrList) &&
00124 (myCurrEntry == cmp.myCurrEntry)));
00125 }
00126 int operator!=(const traverser &cmp)
00127 { return !(*this == cmp); }
00128
00129 bool atEnd() const
00130 {
00131 return !myCurrList;
00132 }
00133
00134 traverser &operator++()
00135 {
00136 if( ++myCurrEntry == myCurrList->end() )
00137 {
00138 ++myCurrList;
00139 advanceToNextList();
00140 }
00141 return *this;
00142 }
00143
00144 const traverser &operator=(const traverser &src)
00145 {
00146 myCurrList = src.myCurrList;
00147 myEndList = src.myEndList;
00148 myCurrEntry = src.myCurrEntry;
00149 return *this;
00150 }
00151
00152 private:
00153 traverser(const UT_ThingList *list, const UT_ThingList *endlist)
00154 : myCurrList(list), myEndList(endlist)
00155 { advanceToNextList(); }
00156
00157 void advanceToNextList()
00158 {
00159 while( myCurrList != myEndList &&
00160 myCurrList->begin() == myCurrList->end() )
00161 ++myCurrList;
00162 if( myCurrList == myEndList )
00163 myCurrList = 0;
00164 else
00165 myCurrEntry = myCurrList->begin();
00166 }
00167
00168 const UT_ThingList *myCurrList;
00169 const UT_ThingList *myEndList;
00170 UT_ThingList::traverser myCurrEntry;
00171
00172 friend class UT_HashTable;
00173 };
00174
00175 traverser begin() const
00176 {
00177 if( myTable )
00178 return traverser(myTable, myTable + mySize);
00179 return traverser();
00180 }
00181 traverser end() const
00182 { return traverser(); }
00183
00184 private:
00185 enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
00186 void adjustTableSize(UT_AdjustType adjust_type);
00187
00188
00189 UT_ThingList *myTable;
00190 int mySize;
00191 unsigned int myNbrEntries;
00192 fpreal myMaxLoadFactor;
00193 fpreal myMinLoadFactor;
00194 bool myUseOrdered;
00195 bool myIsTraversing;
00196 };
00197
00198 #endif