00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Chris AtLee 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_SortedSymbolTable.h 00015 * 00016 * COMMENTS: 00017 * This type of symbol table allows for quick iteration through it 00018 * because it also stores its data in an array. As a consequence, 00019 * deleting from the table is O(n). 00020 * 00021 * Elements in the array are stored in the order they were inserted. 00022 */ 00023 00024 #ifndef __UT_SortedSymbolTable_h__ 00025 #define __UT_SortedSymbolTable_h__ 00026 00027 #include "UT_API.h" 00028 #include "UT_String.h" 00029 #include "UT_SymbolTable.h" 00030 #include "UT_RefArray.h" 00031 #include "UT_Pair.h" 00032 00033 class UT_API UT_SortedSymbolTable 00034 { 00035 public: 00036 explicit UT_SortedSymbolTable(); 00037 virtual ~UT_SortedSymbolTable(); 00038 00039 // Empty the table 00040 void clear(); 00041 00042 // Add a symbol to the table 00043 void addSymbol(const char* symbol, UT_Thing data); 00044 00045 // Lookup a symbol 00046 // Returns 1 on success, 0 on failure 00047 int findSymbol(const char* symbol, UT_Thing* datap) const; 00048 00049 // Returns the symbol name of the given index 00050 const char* getSymbol(uint i) const; 00051 const char *getStringReference(const char *string); 00052 00053 // Delete a symbol, this is O(n) 00054 int deleteSymbol(const char* symbol); 00055 00056 // Find a symbol 00057 UT_Thing &operator[](const char* symbol); 00058 00059 // Index into the array. 00060 UT_Thing &operator[](uint i) const; 00061 UT_Thing &operator()(uint i) const 00062 { 00063 return *(myThings(i).mySecond); 00064 } 00065 00066 // How many entries there are 00067 uint entries() const; 00068 // Is the table empty? 00069 bool empty() const; 00070 00071 // Sort the thing array by the symbol name 00072 void sortThingsBySymbol() 00073 { 00074 myThings.sort(comparePair); 00075 } 00076 00077 protected: 00078 typedef UT_Pair<const char*, UT_Thing*> myPair; 00079 00080 static int comparePair(const myPair *p1, const myPair *p2) 00081 { 00082 return UT_String::compareNumberedString(p1->myFirst, p2->myFirst); 00083 } 00084 UT_SymbolTable myTable; 00085 UT_RefArray<myPair> myThings; 00086 }; 00087 00088 #endif
1.5.9