-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-index.cc
30 lines (27 loc) · 998 Bytes
/
rss-index.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* File: rss-index.cc
* ------------------
* Presents the implementation of the RSSIndex class, which is
* little more than a glorified map.
*/
#include <algorithm>
#include "rss-index.h"
using namespace std;
void RSSIndex::add(const Article& article, const vector<string>& words) {
for (const string& word : words) { // iteration via for keyword
index[word][article]++;
}
}
static const vector<pair<Article, int> > emptyResult;
vector<pair<Article, int> > RSSIndex::getMatchingArticles(const string& word) const {
auto indexFound = index.find(word);
if (indexFound == index.end()) return emptyResult;
const map<Article, int>& matches = indexFound->second;
vector<pair<Article, int> > v;
for (const pair<Article, int>& match: matches) v.push_back(match);
sort(v.begin(), v.end(), [](const pair<Article, int>& one,
const pair<Article, int>& two) {
return one.second > two.second || (one.second == two.second && one.first < two.first);
});
return v;
}