00001
00002
00003 #ifndef DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLMAP_H
00004 #define DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLMAP_H
00005
00006 #include "config.h"
00007 #include "Symbol.h"
00008
00028 class SymbolMap {
00029
00030 public:
00031
00032
00036 SymbolMap(SymbolTable* table)
00037 : hungry(false), table(table), data(table->size()) { }
00038
00043 SymbolMap() : hungry(true) { }
00044
00045
00046
00047
00048
00049 size_t hash() const;
00050 friend std::ostream& operator<<(std::ostream& s, const SymbolMap& me);
00051 std::ostream& print_to(std::ostream& stream) const;
00052 ~SymbolMap();
00053
00054
00055
00056
00057
00058 Symbol get(Symbol var) const {
00059 if (hungry && var) const_cast<SymbolMap*>(this)->setTable(&var.table());
00060 assert(&var.table() == table);
00061 if (var.index() >= data.size()) return Symbol();
00062 return data[var.index()];
00063 }
00064
00065 void set(Symbol var, Symbol value) {
00066 if (hungry && var) setTable(&var.table());
00067 assert(&var.table() == table);
00068 if (var.index() >= data.size()) data.resize(var.index()+1);
00069 data[var.index()] = value;
00070 }
00071
00072 size_t count() {
00073 size_t result = 0;
00074 for (size_t i=0; i<data.size(); i++) {
00075 if (data[i]) result++;
00076 }
00077 return result;
00078 }
00079
00080 private:
00081 bool hungry;
00082 SymbolTable* table;
00083 std::vector<Symbol> data;
00084
00085 void setTable(SymbolTable* table) {
00086 hungry = false;
00087 this->table = table;
00088 }
00089 };
00090
00091 STANDARD_EXTERNAL(SymbolMap)
00092
00093 #endif