Skip to content

Commit

Permalink
finished ch12
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 2, 2015
1 parent c1a0426 commit 0b1c995
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions ch12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ I prefer the `do while`, cause the process according with our logic.
The `vector` can not ensure no duplicates. Hence, in terms of this programme `set` is a better option.

## Exercise 12.32 [Header](ex12_32.h)|[Implementation](ex12_32.cpp)
## Exercise 12.33 [Header](ex12_33.h)|[Implementation](ex12_33.cpp)
49 changes: 49 additions & 0 deletions ch12/ex12_33.cpp
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;
}
52 changes: 52 additions & 0 deletions ch12/ex12_33.h
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

0 comments on commit 0b1c995

Please sign in to comment.