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
00068 float getMaxLoadFactor() const { return myMaxLoadFactor; }
00069 void setMaxLoadFactor(float max_load_factor);
00070 float getMinLoadFactor() const { return myMinLoadFactor; }
00071 void setMinLoadFactor(float min_load_factor);
00072
00073
00074 void mergeTable(const UT_SymbolTable &table);
00075
00076
00077
00078
00079 const char *getStringReference(const char *symbol);
00080
00081
00082
00083
00084
00085
00086 char *newName(const char *symbol) const;
00087
00088
00089
00090
00091 bool getUniqueNumberedPrefix(const char *symbol,
00092 UT_String &unique_prefix) const;
00093
00094 void outputStats(FILE *output) const;
00095 void dumpTable(FILE *output) const;
00096
00097 int64 getMemUsage(bool only_this=false) const;
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 int traverseConst(
00110 int (*function)(UT_Thing &, const char *, void *),
00111 void *data) const;
00112 int traverse(int (*function)(UT_Thing &, const char *, void *),
00113 void *data);
00114
00115 class UT_API traverser
00116 {
00117 public:
00118 traverser()
00119 : myCurrList(0), myEndList(0) { }
00120 traverser(const traverser &src)
00121 { *this = src; }
00122 ~traverser() { }
00123
00124 UT_Thing &thing() { return myCurrSym.thing(); }
00125 const char *name() const { return myCurrSym.name(); }
00126
00127 int operator==(const traverser &cmp) const
00128 {
00129 return ((!myCurrList && !cmp.myCurrList) ||
00130 ((myCurrList == cmp.myCurrList) &&
00131 (myCurrSym == cmp.myCurrSym)));
00132 }
00133 int operator!=(const traverser &cmp) const
00134 { return !(*this == cmp); }
00135
00136 bool atEnd() const
00137 {
00138 return !myCurrList;
00139 }
00140
00141 traverser &operator++()
00142 {
00143 if( ++myCurrSym == myCurrList->end() )
00144 {
00145 ++myCurrList;
00146 advanceToNextList();
00147 }
00148 return *this;
00149 }
00150
00151 const traverser &operator=(const traverser &src)
00152 {
00153 myCurrList = src.myCurrList;
00154 myEndList = src.myEndList;
00155 myCurrSym = src.myCurrSym;
00156 return *this;
00157 }
00158
00159 private:
00160 traverser(const UT_SymList *list, const UT_SymList *endlist)
00161 : myCurrList(list), myEndList(endlist)
00162 { advanceToNextList(); }
00163
00164 void advanceToNextList()
00165 {
00166 while( myCurrList != myEndList &&
00167 myCurrList->begin() == myCurrList->end() )
00168 ++myCurrList;
00169 if( myCurrList == myEndList )
00170 myCurrList = 0;
00171 else
00172 myCurrSym = myCurrList->begin();
00173 }
00174
00175 const UT_SymList *myCurrList;
00176 const UT_SymList *myEndList;
00177 UT_SymList::traverser myCurrSym;
00178
00179 friend class UT_SymbolTable;
00180 };
00181
00182 traverser begin() const
00183 {
00184 if (myTable)
00185 return traverser(myTable, myTable + mySize);
00186 return traverser();
00187 }
00188 traverser end() const { return traverser(); }
00189
00190 private:
00191 #ifndef INTEL_COMPILER
00192
00193
00194
00195 UT_SymbolTable(const UT_SymbolTable&) { }
00196 #endif
00197 UT_SymbolTable& operator=(const UT_SymbolTable&);
00198
00199 private:
00200 enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
00201 void adjustTableSize(UT_AdjustType adjust_type);
00202
00203
00204 UT_SymList *myTable;
00205 int mySize;
00206 unsigned int myNbrEntries;
00207 fpreal myMaxLoadFactor;
00208 fpreal myMinLoadFactor;
00209 bool myIsTraversing;
00210 };
00211
00212
00213 class UT_API UT_SnapshotTable : public UT_SymbolTable
00214 {
00215 public:
00216 UT_SnapshotTable() : UT_SymbolTable() {}
00217 virtual ~UT_SnapshotTable();
00218
00219 virtual void setSnapshotWatch(UT_Thing watch) = 0;
00220 virtual void snapshot(const char *symbol) = 0;
00221 virtual void updateWatch(const char *symbol) = 0;
00222 void getSnapshot(const char *symbol, UT_Thing *data) const
00223 {
00224 if (!findSymbol(symbol, data))
00225 data->value.longv = 0L;
00226 }
00227
00228 protected:
00229 virtual void getWatch(UT_Thing *watch) const = 0;
00230 private:
00231 };
00232
00233 #endif