diff --git a/ch17/ex17.3/main.cpp b/ch17/ex17.3/main.cpp deleted file mode 100644 index bfccd8d0..00000000 --- a/ch17/ex17.3/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 3 Mar 2014 - * @remark This code is for the exercises from C++ Primer 5th Edition - * @note - ***************************************************************************/ -//! -//! Exercise 17.3: -//! Rewrite the TextQuery programs from ยง 12.3 (p. 484) to use a tuple instead -//! of the QueryResult class. Explain which design you think is better and why. -//! -// The orignal way is more formal.The second way is quick to implement ,but hard to -// refactor.So the second way is better for testing. -//! - -#include -#include -#include -#include - - -int main() -{ - - -} diff --git a/ch17/ex17.3/textquery.cpp b/ch17/ex17.3/textquery.cpp deleted file mode 100644 index e255ea2b..00000000 --- a/ch17/ex17.3/textquery.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/*************************************************************************** - * @file textqueryr.cpp - * @author Alan.W - * @date 29 DEC 2013 - * @remark - ***************************************************************************/ - -#include "textquery.h" -#include "queryresult.h" -#include -#include -#include - -//! Constructor -TextQuery::TextQuery(std::ifstream & is) : file(new std::vector) -{ - //! each line - std::string line; - while(std::getline(is,line)) - { - file->push_back(line); - //! current line index - int index = file->size() - 1; - - //! for each word - std::stringstream lineSteam(line); - std::string word; - while(lineSteam >> word) - { - //! fetch the smart pointer which is null when the word first time seen - std::shared_ptr< - std::set>& sp_lineIndex = wm[word]; - - //! if null, allcate a new set to contain line indices - if(!sp_lineIndex) - sp_lineIndex.reset(new std::set); - - //! insert - sp_lineIndex->insert(index); - } - } - - //! =debugging= - std::cout << "@alan : the size of wm is " << wm.size() <<"\n"; - - for(const auto &line : *file) - std::cout<<"@alan : \t" << line << "\n"; - - /* - for(const auto &e : wm) - { - std::cout << e.first << " :\n"; - for (const auto &index : *(e.second)) - { - std::cout << index << " "; - } - std::cout << "\n"; - } - */ - //! =end= -} - -/** - * @brief do a query opertion and return QueryResult object. - */ -QueryResult -TextQuery::query(const std::string &sought) const -{ - //! dynamicaly allocated set used for the word does not appear. - static std::shared_ptr> noData(new std::set); - - //! fetch the iterator to the matching element in the map. - //std::map>>::const_iterator - auto iter = wm.find(sought); - if(iter == wm.end()) - return QueryResult(sought, noData, file); - else - return QueryResult(sought, iter->second, file); -} - -/** - * @brief do a query opertion and return tuple. - */ -result_tuple TextQuery::query_return_tuple(const std::string &sought) -{ - //! dynamicaly allocated set used for the word does not appear. - static std::shared_ptr> noData(new std::set); - - //! fetch the iterator to the matching element in the map. - //std::map>>::const_iterator - auto iter = wm.find(sought); - if(iter == wm.end()) - return result_tuple(sought, noData, file); - else - return result_tuple(sought, iter->second, file); -} - diff --git a/ch17/ex17.3/textquery.h b/ch17/ex17.3/textquery.h deleted file mode 100644 index 38f660b4..00000000 --- a/ch17/ex17.3/textquery.h +++ /dev/null @@ -1,44 +0,0 @@ -/*************************************************************************** - * @file textqueryr.h - * @author Alan.W - * @date 29 DEC 2013 - * @remark - ***************************************************************************/ - -#ifndef TEXTQUERY_H -#define TEXTQUERY_H - -#include -#include -#include -#include -#include -#include - -class QueryResult; - -class TextQuery -{ - -public: - typedef std::vector::size_type index_Tp; - typedef std::tuple >, - std::shared_ptr>> result_tuple; - - //! constructor - TextQuery(std::ifstream&); - - //! query operation returns QueryResult - QueryResult - query(const std::string&) const; - - //! query operation returns tuple - result_tuple query_return_tuple(const std::string& sought); -private: - std::shared_ptr> file; - std::map>> wm; -}; - -#endif // TEXTQUERY_H