TextQuery::TextQuery(std::ifstream &fin) : file(StrBlob()), wordMap(std::map<std::string,std::shared_ptr<std::set<line_no>>>()) { std::string line; //! each line while(std::getline(fin, line)) { file.push_back(line); int n = file.size() - 1; //! the current line number //! each word std::stringstream lineSteam(line); std::string word; while(lineSteam >> word) { std::shared_ptr<std::set<line_no>>& sp_lines = wordMap[word]; //! if null if(!sp_lines) { sp_lines.reset(new std::set<line_no>); } sp_lines->insert(n); } } }
//! Constructor TextQuery::TextQuery(std::ifstream & is) : file(new std::vector<std::string>) { //! 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<index_Tp>>& sp_lineIndex = wm[word]; //! if null, allcate a new set to contain line indices if(!sp_lineIndex) sp_lineIndex.reset(new std::set<index_Tp>); //! 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= }