00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLTABLE_H
00015 #define DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLTABLE_H
00016
00017 #include "config.h"
00018 #include "EqStr.h"
00019
00029 class SymbolTable {
00030
00031 public:
00032
00033
00034 SymbolTable();
00035 SymbolTable(const SymbolTable& other);
00036 const SymbolTable& operator=(const SymbolTable& other);
00037 bool operator==(const SymbolTable& other) const;
00038 bool operator<(const SymbolTable& other) const;
00039 size_t hash() const;
00040 friend std::ostream& operator<<(std::ostream& s, const SymbolTable& me);
00041 std::ostream& print_to(std::ostream& stream) const;
00042 ~SymbolTable();
00043
00044
00045
00050 size_t getTableNumber() const { return tableNumber; }
00051
00054 typedef unsigned int Type;
00057 typedef unsigned int Index;
00058
00062 const Type type(Index index) const
00063 { return records[index].type; }
00067 const char* text(Index index) const
00068 { return records[index].text; }
00073 const void keep(Index index)
00074 { ++records[index].refCount; }
00079 const bool release(Index index)
00080 {
00081 bool reclaimed = (0 == --records[index].refCount);
00082 if (reclaimed) reclaim(index);
00083 return reclaimed;
00084 }
00085
00090 Index lookupOrAdd(Type type, const char* text, bool* added_ptr=0);
00091
00095 size_t size() const { return count; }
00096
00097 private:
00098
00099 friend class Symbol;
00100
00101 void reclaim(Index index);
00102
00103 Type numTypes;
00104
00105 struct Record {
00106 Type type;
00107 char* text;
00108 size_t refCount;
00109 size_t nextHole;
00111 Symbol boundTo;
00112
00113 };
00114
00115 std::vector<Record> records;
00116 size_t first_hole;
00117 size_t count;
00118
00119 typedef std::hash_map<const char*, size_t, std::hash<const char*>, EqStr>
00120 Map;
00121 typedef Map::iterator Iter;
00122
00123 std::vector<Map> map;
00124
00125 const Record& record(Index i) { return records[i]; }
00126
00127 size_t tableNumber;
00128 static size_t nextTableNumber;
00129
00130 };
00131
00132 STANDARD_EXTERNAL(SymbolTable)
00133
00134 #endif