forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ex12_33.cpp | ||
// Exercise 12.33 | ||
// | ||
// Created by pezy on 1/1/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Rewrite the TextQuery and QueryResult classes to use a StrBlob | ||
// instead of a vector<string> to hold the input file. | ||
|
||
#include "ex12_33.h" | ||
#include <sstream> | ||
#include <algorithm> | ||
|
||
TextQuery::TextQuery(std::ifstream &ifs) : input(new StrBlob) | ||
{ | ||
StrBlob::size_type lineNo{0}; | ||
for (string line; std::getline(ifs, line); ++lineNo) { | ||
input->push_back(line); | ||
std::istringstream line_stream(line); | ||
for (string text, word; line_stream >> text; word.clear()) { | ||
// avoid read a word followed by punctuation(such as: word, ) | ||
std::remove_copy_if(text.begin(), text.end(), std::back_inserter(word), ispunct); | ||
// use reference avoid count of shared_ptr add. | ||
auto &nos = result[word]; | ||
if (!nos) nos.reset(new std::set<StrBlob::size_type>); | ||
nos->insert(lineNo); | ||
} | ||
} | ||
} | ||
|
||
QueryResult TextQuery::query(const string& str) const | ||
{ | ||
// use static just allocate once. | ||
static shared_ptr<std::set<StrBlob::size_type>> nodate(new std::set<StrBlob::size_type>); | ||
auto found = result.find(str); | ||
if (found == result.end()) return QueryResult(str, nodate, input); | ||
else return QueryResult(str, found->second, input); | ||
} | ||
|
||
std::ostream& print(std::ostream &out, const QueryResult& qr) | ||
{ | ||
out << qr.word << " occurs " << qr.nos->size() << (qr.nos->size() > 1 ? " times" : " time") << std::endl; | ||
for (auto it = qr.begin(); it != qr.end(); ++it) { | ||
ConstStrBlobPtr p(*qr.get_file(), *it); | ||
out << "\t(line " << *it + 1 << ") " << p.deref() << std::endl; | ||
} | ||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// ex12_33.h | ||
// Exercise 12.33 | ||
// | ||
// Created by pezy on 1/1/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// In Chapter 15 we’ll extend our query system and will need some additional members | ||
// in the QueryResult class. | ||
// Add members named [begin] and [end] that return iterators into the set of line numbers | ||
// returned by a given query, and a member named [get_file] that | ||
// returns a shared_ptr to the file in the QueryResult object. | ||
|
||
#ifndef CP5_ex12_33_h | ||
#define CP5_ex12_33_h | ||
|
||
#include "ex12_22.h" | ||
using std::shared_ptr; | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <map> | ||
#include <set> | ||
|
||
class QueryResult; | ||
class TextQuery { | ||
public: | ||
TextQuery(std::ifstream &); | ||
QueryResult query(const string&) const; | ||
private: | ||
shared_ptr<StrBlob> input; | ||
std::map<string, shared_ptr<std::set<StrBlob::size_type>>> result; | ||
}; | ||
|
||
class QueryResult { | ||
public: | ||
using ResultIter = std::set<StrBlob::size_type>::iterator; | ||
friend std::ostream& print(std::ostream &, const QueryResult&); | ||
public: | ||
QueryResult(const string &s, shared_ptr<std::set<StrBlob::size_type>> set, shared_ptr<StrBlob> v) : word(s), nos(set), input(v) {} | ||
ResultIter begin() const { return nos->begin(); } | ||
ResultIter end() const { return nos->end(); } | ||
shared_ptr<StrBlob> get_file() const { return input; } | ||
private: | ||
string word; | ||
shared_ptr<std::set<StrBlob::size_type>> nos; | ||
shared_ptr<StrBlob> input; | ||
}; | ||
|
||
std::ostream& print(std::ostream &, const QueryResult&); | ||
|
||
#endif |