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 traverser &operator++()
00130 {
00131 if( ++myCurrEntry == myCurrList->end() )
00132 {
00133 ++myCurrList;
00134 advanceToNextList();
00135 }
00136 return *this;
00137 }
00138
00139 const traverser &operator=(const traverser &src)
00140 {
00141 myCurrList = src.myCurrList;
00142 myEndList = src.myEndList;
00143 myCurrEntry = src.myCurrEntry;
00144 return *this;
00145 }
00146
00147 private:
00148 traverser(const UT_ThingList *list, const UT_ThingList *endlist)
00149 : myCurrList(list), myEndList(endlist)
00150 { advanceToNextList(); }
00151
00152 void advanceToNextList()
00153 {
00154 while( myCurrList != myEndList &&
00155 myCurrList->begin() == myCurrList->end() )
00156 ++myCurrList;
00157 if( myCurrList == myEndList )
00158 myCurrList = 0;
00159 else
00160 myCurrEntry = myCurrList->begin();
00161 }
00162
00163 const UT_ThingList *myCurrList;
00164 const UT_ThingList *myEndList;
00165 UT_ThingList::traverser myCurrEntry;
00166
00167 friend class UT_HashTable;
00168 };
00169
00170 traverser begin() const
00171 {
00172 if( myTable )
00173 return traverser(myTable, myTable + mySize);
00174 return traverser();
00175 }
00176 traverser end() const
00177 { return traverser(); }
00178
00179 private:
00180 enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
00181 void adjustTableSize(UT_AdjustType adjust_type);
00182
00183
00184 UT_ThingList *myTable;
00185 int mySize;
00186 unsigned int myNbrEntries;
00187 fpreal myMaxLoadFactor;
00188 fpreal myMinLoadFactor;
00189 bool myUseOrdered;
00190 bool myIsTraversing;
00191 };
00192
00193 #endif