From d0fd6f27a33f1c34be91de83d0e9174a3777b2c1 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 10 Feb 2010 13:07:42 +0100 Subject: [PATCH] ENH: use stl::set instead of hand-rolled HashTable in wmkdependParser --- wmake/src/wmkdependParser.atg | 245 ++++++++-------------------------- wmake/src/wmkdependParser.cpp | 16 +-- wmake/src/wmkdependParser.h | 145 +------------------- wmake/src/wmkdependScanner.h | 2 +- 4 files changed, 73 insertions(+), 335 deletions(-) diff --git a/wmake/src/wmkdependParser.atg b/wmake/src/wmkdependParser.atg index 2da6ebd313..5e514f5501 100644 --- a/wmake/src/wmkdependParser.atg +++ b/wmake/src/wmkdependParser.atg @@ -1,7 +1,11 @@ /*---------------------------------------------------------------------------*\ Attributed Grammar for Coco/R (-*- C++ -*- version) compile with: - coco-cpp wmkdependParser.atg + coco-cpp wmkdependParser.atg + For example, + $WM_THIRD_PARTY_DIR/coco-cpp/platforms/$WM_ARCH$WM_COMPILER/bin/coco-cpp \ + -frames $WM_THIRD_PARTY_DIR/coco-cpp/platforms/share/coco-cpp \ + wmkdependParser.atg \*---------------------------------------------------------------------------*/ [copy] /*---------------------------------*- C++ -*---------------------------------*\ @@ -42,140 +46,7 @@ SourceFiles #include #include #include - -//! @brief A simple HashTable implementation -/** - * @note This hash table is only vaguely STL-like. In accordance with - * its present purpose, this hash table only supports a constIterator - * and no deletions. For simplicity, the constIterator increment is - * simply via a next() method. Instead of comparing to an end value, - * the constIterator valid() method is used. - * For example, - * @code - * for - * ( - * HashTable::constIterator iter = myHash.begin(); - * iter.valid(); - * iter.next() - * ) - * { - * std::cerr<< "key: " << iter.key() << "\n"; - * } - * @endcode - * - */ -class StringHashSet -{ - //! An entry within the HashTable - struct hashedEntry - { - const std::string key_; //next_; - delete del; - } - } - delete[] table_; - table_ = 0; - } - - //! Return hash index for lookup name in hash table - bool hashKeyIndex(const std::string& name) const - { - int hashIdx = 0; - - // calculate hash index - for - ( - std::string::const_iterator iter = name.begin(); - iter != name.end(); - ++iter - ) - { - hashIdx = hashIdx << 1 ^ *iter; - } - - if (hashIdx < 0) - { - hashIdx = -hashIdx; - } - - return hashIdx % size_; - } - - - //! Return true if name is found in hash table - bool found(const std::string& name) const - { - const int hashIdx = hashKeyIndex(name); - - for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_) - { - if (name == ep->key_) - { - // found - return true; - } - } - - // entry not found - return false; - } - - - //! Return true if name is found in hash table, insert if not found - bool foundOrInsert(const std::string& name) - { - const int hashIdx = hashKeyIndex(name); - - for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_) - { - if (name == ep->key_) - { - // found - return true - return true; - } - } - - // not found - insert it - table_[hashIdx] = new hashedEntry(name, table_[hashIdx]); - - // entry not found (but was added) - return false - return false; - } - -}; - +#include /*---------------------------------------------------------------------------*/ @@ -188,11 +59,11 @@ COMPILER wmkdepend /*---------------------------------------------------------------------------*/ private: - //! Hash of files already visited - static StringHashSet visitedFiles_; + //! Set of files already visited + static std::set visitedFiles_; - //! Hash of (java) directories already visited - static StringHashSet visitedDirs_; + //! Set of (java) directories already visited + static std::set visitedDirs_; //! Replace all '.' with '/' static void dotToSlash(std::string& name); @@ -224,8 +95,8 @@ public: #include #include -StringHashSet Parser::visitedFiles_; -StringHashSet Parser::visitedDirs_; +std::set Parser::visitedFiles_; +std::set Parser::visitedDirs_; std::list Parser::includeDirs; std::string Parser::sourceFile; @@ -246,15 +117,15 @@ void Parser::dotToSlash(std::string& name) void Parser::ignoreDir(const std::string& name) { - visitedDirs_.foundOrInsert(name); + visitedDirs_.insert(name); } void Parser::includeFile(const std::string& name) { - if (visitedFiles_.foundOrInsert(name)) + if (!visitedFiles_.insert(name).second) { - return; + return; // already existed (did not insert) } // use stdio and buffering within Coco/R -- (faster) @@ -312,7 +183,7 @@ void Parser::importFile(const std::string& name) std::string dirGlob = name.substr(0, dotPos); dirGlob += ".*"; - if (visitedDirs_.found(dirGlob)) + if (visitedDirs_.find(dirGlob) != visitedDirs_.end()) { return; } @@ -329,9 +200,9 @@ void Parser::importFile(const std::string& name) void Parser::importDir(const std::string& name) { - if (visitedDirs_.foundOrInsert(name)) + if (!visitedDirs_.insert(name).second) { - return; + return; // already existed (did not insert) } std::string dirName = name; @@ -427,16 +298,16 @@ wmkdepend [ "include" [ - string (. - if (isUTF8()) - { - includeFile(t->toStringUTF8(1, t->length()-2)); - } - else - { - includeFile(t->toString(1, t->length()-2)); - } - .) + string (. + if (isUTF8()) + { + includeFile(t->toStringUTF8(1, t->length()-2)); + } + else + { + includeFile(t->toString(1, t->length()-2)); + } + .) ] ] [ ANY { ANY } ] '\n' // skip trailing junk @@ -444,42 +315,42 @@ wmkdepend // Fortran-style includes | "include" [ - sqstring (. - if (isUTF8()) - { - includeFile(t->toStringUTF8(1, t->length()-2)); - } - else - { - includeFile(t->toString(1, t->length()-2)); - } - .) + sqstring (. + if (isUTF8()) + { + includeFile(t->toStringUTF8(1, t->length()-2)); + } + else + { + includeFile(t->toString(1, t->length()-2)); + } + .) ] [ ANY { ANY } ] '\n' // skip trailing junk // Java imports | "import" ( - package_dir (. - if (isUTF8()) - { - importDir(t->toStringUTF8()); - } - else - { - importDir(t->toString()); - } - .) - | package_name (. - if (isUTF8()) - { - importFile(t->toStringUTF8()); - } - else - { - importFile(t->toString()); - } - .) + package_dir (. + if (isUTF8()) + { + importDir(t->toStringUTF8()); + } + else + { + importDir(t->toString()); + } + .) + | package_name (. + if (isUTF8()) + { + importFile(t->toStringUTF8()); + } + else + { + importFile(t->toString()); + } + .) ) ';' [ ANY { ANY } ] '\n' // skip trailing junk diff --git a/wmake/src/wmkdependParser.cpp b/wmake/src/wmkdependParser.cpp index b6d5e4e71a..4b4a8e4c21 100644 --- a/wmake/src/wmkdependParser.cpp +++ b/wmake/src/wmkdependParser.cpp @@ -52,8 +52,8 @@ namespace wmake { #include #include -StringHashSet Parser::visitedFiles_; -StringHashSet Parser::visitedDirs_; +std::set Parser::visitedFiles_; +std::set Parser::visitedDirs_; std::list Parser::includeDirs; std::string Parser::sourceFile; @@ -74,15 +74,15 @@ void Parser::dotToSlash(std::string& name) void Parser::ignoreDir(const std::string& name) { - visitedDirs_.foundOrInsert(name); + visitedDirs_.insert(name); } void Parser::includeFile(const std::string& name) { - if (visitedFiles_.foundOrInsert(name)) + if (!visitedFiles_.insert(name).second) { - return; + return; // already existed (did not insert) } // use stdio and buffering within Coco/R -- (faster) @@ -140,7 +140,7 @@ void Parser::importFile(const std::string& name) std::string dirGlob = name.substr(0, dotPos); dirGlob += ".*"; - if (visitedDirs_.found(dirGlob)) + if (visitedDirs_.find(dirGlob) != visitedDirs_.end()) { return; } @@ -157,9 +157,9 @@ void Parser::importFile(const std::string& name) void Parser::importDir(const std::string& name) { - if (visitedDirs_.foundOrInsert(name)) + if (!visitedDirs_.insert(name).second) { - return; + return; // already existed (did not insert) } std::string dirName = name; diff --git a/wmake/src/wmkdependParser.h b/wmake/src/wmkdependParser.h index 8c7703adbd..b91f364f27 100644 --- a/wmake/src/wmkdependParser.h +++ b/wmake/src/wmkdependParser.h @@ -44,140 +44,7 @@ SourceFiles #include #include #include - -//! @brief A simple HashTable implementation -/** - * @note This hash table is only vaguely STL-like. In accordance with - * its present purpose, this hash table only supports a constIterator - * and no deletions. For simplicity, the constIterator increment is - * simply via a next() method. Instead of comparing to an end value, - * the constIterator valid() method is used. - * For example, - * @code - * for - * ( - * HashTable::constIterator iter = myHash.begin(); - * iter.valid(); - * iter.next() - * ) - * { - * std::cerr<< "key: " << iter.key() << "\n"; - * } - * @endcode - * - */ -class StringHashSet -{ - //! An entry within the HashTable - struct hashedEntry - { - const std::string key_; //next_; - delete del; - } - } - delete[] table_; - table_ = 0; - } - - //! Return hash index for lookup name in hash table - bool hashKeyIndex(const std::string& name) const - { - int hashIdx = 0; - - // calculate hash index - for - ( - std::string::const_iterator iter = name.begin(); - iter != name.end(); - ++iter - ) - { - hashIdx = hashIdx << 1 ^ *iter; - } - - if (hashIdx < 0) - { - hashIdx = -hashIdx; - } - - return hashIdx % size_; - } - - - //! Return true if name is found in hash table - bool found(const std::string& name) const - { - const int hashIdx = hashKeyIndex(name); - - for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_) - { - if (name == ep->key_) - { - // found - return true; - } - } - - // entry not found - return false; - } - - - //! Return true if name is found in hash table, insert if not found - bool foundOrInsert(const std::string& name) - { - const int hashIdx = hashKeyIndex(name); - - for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_) - { - if (name == ep->key_) - { - // found - return true - return true; - } - } - - // not found - insert it - table_[hashIdx] = new hashedEntry(name, table_[hashIdx]); - - // entry not found (but was added) - return false - return false; - } - -}; - +#include /*---------------------------------------------------------------------------*/ @@ -191,7 +58,7 @@ namespace wmake { /*---------------------------------------------------------------------------*\ Class Errors Declaration \*---------------------------------------------------------------------------*/ -//! Parser error handing +//! Parser error handling class Errors { public: @@ -255,11 +122,11 @@ public: private: - //! Hash of files already visited - static StringHashSet visitedFiles_; + //! Set of files already visited + static std::set visitedFiles_; - //! Hash of (java) directories already visited - static StringHashSet visitedDirs_; + //! Set of (java) directories already visited + static std::set visitedDirs_; //! Replace all '.' with '/' static void dotToSlash(std::string& name); diff --git a/wmake/src/wmkdependScanner.h b/wmake/src/wmkdependScanner.h index a72bf4b968..7520b01ebd 100644 --- a/wmake/src/wmkdependScanner.h +++ b/wmake/src/wmkdependScanner.h @@ -200,7 +200,7 @@ public: Class Buffer Declaration \*---------------------------------------------------------------------------*/ /*! - * @brief Scanner Buffer Token + * @brief Scanner Buffer * * This Buffer supports the following cases: * -# seekable stream (file)