00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __UT_SymbolTable_h__
00019 #define __UT_SymbolTable_h__
00020
00021 #include "UT_API.h"
00022 #include <stdio.h>
00023 #include <SYS/SYS_Types.h>
00024 #include "UT_Thing.h"
00025 #include "UT_SymList.h"
00026
00027 class UT_String;
00028
00029 class UT_API UT_SymbolTable
00030 {
00031 public:
00032 explicit UT_SymbolTable();
00033 virtual ~UT_SymbolTable();
00034
00035 void addSymbol(const char *symbol, UT_Thing data);
00036 int findSymbol(const char *symbol, UT_Thing *datap) const;
00037
00038 int contains(const char *symbol) const
00039 { UT_Thing value; return findSymbol(symbol, &value); }
00040
00041 template <typename T>
00042 T *findPointer(const char *symbol) const
00043 {
00044 UT_Thing thing;
00045 if (!findSymbol(symbol, &thing))
00046 return NULL;
00047 return thing.template asPointer<T>();
00048 }
00049
00050 int findOrCreateSymbol(const char *symbol, UT_Thing **datap);
00051 int deleteSymbol(const char *symbol);
00052 void clear();
00053
00054 const char *addSymbolAndGetReference(const char *symbol, UT_Thing data);
00055
00056
00057 unsigned entries() const { return myNbrEntries; }
00058 bool empty() const { return !myNbrEntries; }
00059 int size() const { return mySize; }
00060
00061
00062
00063 UT_Thing &operator[](const char *symbol);
00064
00065
00066
00067 void reserveTableSize(int size);
00068
00069
00070
00071
00072 float getMaxLoadFactor() const { return myMaxLoadFactor; }
00073 void setMaxLoadFactor(float max_load_factor);
00074 float getMinLoadFactor() const { return myMinLoadFactor; }
00075 void setMinLoadFactor(float min_load_factor);
00076
00077
00078 void mergeTable(const UT_SymbolTable &table);
00079
00080
00081
00082
00083 const char *getStringReference(const char *symbol);
00084
00085
00086
00087
00088
00089
00090 char *newName(const char *symbol) const;
00091
00092
00093
00094
00095 bool getUniqueNumberedPrefix(const char *symbol,
00096 UT_String &unique_prefix) const;
00097
00098 void outputStats(FILE *output) const;
00099 void dumpTable(FILE *output) const;
00100
00101 int64 getMemUsage(bool only_this=false) const;
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 int traverseConst(
00114 int (*function)(UT_Thing &, const char *, void *),
00115 void *data) const;
00116 int traverse(int (*function)(UT_Thing &, const char *, void *),
00117 void *data);
00118
00119 class UT_API traverser
00120 {
00121 public:
00122 traverser()
00123 : myCurrList(0), myEndList(0) { }
00124 traverser(const traverser &src)
00125 { *this = src; }
00126 ~traverser() { }
00127
00128 UT_Thing &thing() { return myCurrSym.thing(); }
00129 const char *name() const { return myCurrSym.name(); }
00130
00131 int operator==(const traverser &cmp) const
00132 {
00133 return ((!myCurrList && !cmp.myCurrList) ||
00134 ((myCurrList == cmp.myCurrList) &&
00135 (myCurrSym == cmp.myCurrSym)));
00136 }
00137 int operator!=(const traverser &cmp) const
00138 { return !(*this == cmp); }
00139
00140 bool atEnd() const
00141 {
00142 return !myCurrList;
00143 }
00144
00145 traverser &operator++()
00146 {
00147 if( ++myCurrSym == myCurrList->end() )
00148 {
00149 ++myCurrList;
00150 advanceToNextList();
00151 }
00152 return *this;
00153 }
00154
00155 const traverser &operator=(const traverser &src)
00156 {
00157 myCurrList = src.myCurrList;
00158 myEndList = src.myEndList;
00159 myCurrSym = src.myCurrSym;
00160 return *this;
00161 }
00162
00163 private:
00164 traverser(const UT_SymList *list, const UT_SymList *endlist)
00165 : myCurrList(list), myEndList(endlist)
00166 { advanceToNextList(); }
00167
00168 void advanceToNextList()
00169 {
00170 while( myCurrList != myEndList &&
00171 myCurrList->begin() == myCurrList->end() )
00172 ++myCurrList;
00173 if( myCurrList == myEndList )
00174 myCurrList = 0;
00175 else
00176 myCurrSym = myCurrList->begin();
00177 }
00178
00179 const UT_SymList *myCurrList;
00180 const UT_SymList *myEndList;
00181 UT_SymList::traverser myCurrSym;
00182
00183 friend class UT_SymbolTable;
00184 };
00185
00186 traverser begin() const
00187 {
00188 if (myTable)
00189 return traverser(myTable, myTable + mySize);
00190 return traverser();
00191 }
00192 traverser end() const { return traverser(); }
00193
00194 private:
00195 #ifndef INTEL_COMPILER
00196
00197
00198
00199 UT_SymbolTable(const UT_SymbolTable&) { }
00200 #endif
00201 UT_SymbolTable& operator=(const UT_SymbolTable&);
00202
00203 private:
00204 enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
00205 void adjustTableSize(UT_AdjustType adjust_type);
00206 void allocateTableOfNewSize(int size);
00207
00208
00209 UT_SymList *myTable;
00210 int mySize;
00211 unsigned int myNbrEntries;
00212 fpreal myMaxLoadFactor;
00213 fpreal myMinLoadFactor;
00214 bool myIsTraversing;
00215 };
00216
00217
00218 class UT_API UT_SnapshotTable : public UT_SymbolTable
00219 {
00220 public:
00221 UT_SnapshotTable() : UT_SymbolTable() {}
00222 virtual ~UT_SnapshotTable();
00223
00224 virtual void setSnapshotWatch(UT_Thing watch) = 0;
00225 virtual void snapshot(const char *symbol) = 0;
00226 virtual void updateWatch(const char *symbol) = 0;
00227 void getSnapshot(const char *symbol, UT_Thing *data) const
00228 {
00229 if (!findSymbol(symbol, data))
00230 data->value.longv = 0L;
00231 }
00232
00233 protected:
00234 virtual void getWatch(UT_Thing *watch) const = 0;
00235 private:
00236 };
00237
00238 #endif