Skip to content

Commit

Permalink
Merge pull request Mooophy#373 from chihyang/master
Browse files Browse the repository at this point in the history
Simplify header files of exercise 15.39
  • Loading branch information
Mooophy committed Dec 2, 2015
2 parents 11f4ed1 + 0896bec commit 35bdd23
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 373 deletions.
23 changes: 0 additions & 23 deletions ch15/ex15.39.40/andquery.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions ch15/ex15.39.40/andquery.h

This file was deleted.

2 changes: 0 additions & 2 deletions ch15/ex15.39.40/binaryquery.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions ch15/ex15.39.40/binaryquery.h

This file was deleted.

5 changes: 0 additions & 5 deletions ch15/ex15.39.40/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@
#include <memory>
#include <fstream>

#include "queryresult.h"
#include "textquery.h"
#include "query_base.h"
#include "query.h"
#include "andquery.h"
#include "orquery.h"
#include "notquery.h"

int main()
{
Expand Down
31 changes: 0 additions & 31 deletions ch15/ex15.39.40/notquery.cpp

This file was deleted.

42 changes: 0 additions & 42 deletions ch15/ex15.39.40/notquery.h

This file was deleted.

17 changes: 0 additions & 17 deletions ch15/ex15.39.40/orquery.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions ch15/ex15.39.40/orquery.h

This file was deleted.

67 changes: 67 additions & 0 deletions ch15/ex15.39.40/query.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,70 @@
#include <algorithm>
#include "query.h"
/**
* @brief AndQuery::eval
* @return the intersection of its operands' result sets
*/
QueryResult AndQuery::eval(const TextQuery &text) const
{
// virtual calls through the Query operands to get result sets for the operands
QueryResult left = lhs.eval(text), right = rhs.eval(text);

// set to hold the intersection of the left and right
std::shared_ptr<std::set<line_no>>
ret_lines = std::make_shared<std::set<line_no>>();

// writes the intersection of two ranges to a destination iterator
std::set_intersection(left.begin(), left.end(),
right.begin(), right.end(),
std::inserter(*ret_lines, ret_lines->begin()));

return QueryResult(rep(), ret_lines, left.get_file());
}

/**
* @brief OrQuery::eval
* @return the union of its operands' result sets
*/
QueryResult OrQuery::eval(const TextQuery &text) const
{
QueryResult right = rhs.eval(text), left= lhs.eval(text);

// copy the left-hand operand into the result set
std::shared_ptr<std::set<line_no>> ret_lines =
std::make_shared<std::set<line_no>>(left.begin(), left.end());

// inert lines from the right-hand operand
ret_lines->insert(right.begin(), right.end());

return QueryResult(rep(), ret_lines, left.get_file());
}

/**
* @brief NotQuery::eval
* @return the lines not in its operand's result set
*/
QueryResult NotQuery::eval(const TextQuery &text) const
{
// virtual call to eval through the Query operand
QueryResult result = query.eval(text);

// start out with an empty result set
std::shared_ptr<std::set<line_no>>
ret_lines = std::make_shared<std::set<line_no>>();

std::set<TextQuery::line_no>::iterator
begin = result.begin(),
end = result.end();

StrBlob::size_type sz = result.get_file().size();

for(std::size_t n = 0; n != sz; ++n)
{
if(begin == end || *begin != n)
ret_lines->insert(n);
else if (begin != end)
++begin;
}

return QueryResult(rep(), ret_lines, result.get_file());
}
Loading

0 comments on commit 35bdd23

Please sign in to comment.