From 8d0b036ef37c1f7a9c7c6f34fa7308e1e2a84959 Mon Sep 17 00:00:00 2001 From: helly Date: Tue, 25 Oct 2005 22:02:43 +0000 Subject: [PATCH] - Implement SymbolTable using map instead of linked list as inspired by #1335305 symbol table reimplementation --- CHANGELOG | 1 + actions.cc | 20 ++++++++++++-------- parser.h | 32 +++++++++++++++++++++++++------- substr.h | 5 +++++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 730bee04..51f95223 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ Version 0.9.11 (????-??-??) --------------------------- +- Implemented #1335305 symbol table reimplementation, just slightly modifed. Version 0.9.10 (2005-09-04) --------------------------- diff --git a/actions.cc b/actions.cc index 3f32d279..afc394b1 100644 --- a/actions.cc +++ b/actions.cc @@ -12,20 +12,24 @@ namespace re2c { -Symbol *Symbol::first = NULL; - -Symbol::Symbol(const SubStr &str) : next(first), name(str), re(NULL) +void Symbol::ClearTable() { - first = this; + symbol_table.clear(); } +Symbol::SymbolTable Symbol::symbol_table; + Symbol *Symbol::find(const SubStr &str) { - for (Symbol *sym = first; sym; sym = sym->next) - if (sym->name == str) - return sym; + const std::string ss(str.to_string()); + SymbolTable::const_iterator it = symbol_table.find(ss); - return new Symbol(str); + if (it == symbol_table.end()) + { + return (*symbol_table.insert(SymbolTable::value_type(ss, new Symbol(str))).first).second; + } + + return (*it).second; } void showIns(std::ostream &o, const Ins &i, const Ins &base) diff --git a/parser.h b/parser.h index ceb94656..58435f26 100644 --- a/parser.h +++ b/parser.h @@ -5,22 +5,40 @@ #include "scanner.h" #include "re.h" #include +#include namespace re2c { class Symbol { - public: - static Symbol *first; - Symbol *next; - Str name; - RegExp *re; -public: - Symbol(const SubStr&); + RegExp* re; + static Symbol *find(const SubStr&); + static void ClearTable(); + + ~Symbol() + { + /** \todo should we delete 're'? */ + } + +protected: + + Symbol(const SubStr& str) + : name(str) + , re(NULL) + { + } + +private: + + typedef std::map SymbolTable; + + static SymbolTable symbol_table; + + Str name; }; void line_source(unsigned int, std::ostream&); diff --git a/substr.h b/substr.h index cb046f48..255f6ec6 100644 --- a/substr.h +++ b/substr.h @@ -3,6 +3,7 @@ #define _substr_h #include +#include #include "basics.h" namespace re2c @@ -22,6 +23,10 @@ public: SubStr(char*); SubStr(const SubStr&); void out(std::ostream&) const; + std::string to_string() const + { + return std::string(str, len); + } }; class Str: public SubStr -- 2.50.1