Update TODO
[dueringa_WikiWalker.git] / src / ArticleCollection.cpp
blob084912852cf8bd3521aa8eea528850a40f8126a5
1 //! \file ArticleCollection.cpp
3 #include "ArticleCollection.h"
5 #include <algorithm>
6 #include <utility>
8 #include "Article.h"
9 #include "WalkerException.h"
11 namespace WikiWalker
13 namespace CollectionUtils
15 static bool articleIsAnalyzed(ArticleCollection::const_reference x)
17 auto art = x.second;
18 if(art == nullptr) {
19 return false;
21 return art->analyzed();
24 size_t countAnalyzedArticles(const ArticleCollection& collection)
26 return std::count_if(
27 collection.begin(), collection.end(), articleIsAnalyzed);
30 bool add(ArticleCollection& collection, std::shared_ptr<Article> article)
32 auto ret = collection.insert(std::make_pair(article->title(), article));
33 return ret.second;
36 void merge(ArticleCollection& collection,
37 const ArticleCollection& other,
38 MergeStrategy strategy)
40 switch(strategy) {
41 case MergeStrategy::IgnoreDuplicates:
42 collection.insert(other.begin(), other.end());
43 break;
44 case MergeStrategy::AlwaysOverwrite:
45 for(const auto& art : other) {
46 collection[art.first] = art.second;
48 break;
49 case MergeStrategy::UseArticleWithMoreLinks:
50 for(const auto& art : other) {
51 auto articleInThis = collection[art.first];
52 auto& otherArt = art.second;
54 if(articleInThis == nullptr ||
55 (!articleInThis->analyzed() && otherArt->analyzed())) {
56 collection[art.first] = art.second;
57 continue;
60 if(!otherArt->analyzed()) {
61 continue;
64 auto linksInThis = articleInThis->countLinks();
65 auto linksInOther = otherArt->countLinks();
66 if(linksInOther > linksInThis) {
67 collection[art.first] = art.second;
70 break;
71 default:
72 throw WalkerException("Not supported");
76 /*! \todo I'm not exactly happy with returning a shared_ptr. Return
77 * reference instead? - but how to say "not found" then? */
78 std::shared_ptr<Article> get(const ArticleCollection& collection,
79 const std::string& title)
81 auto it = collection.find(title);
83 if(collection.end() == it) {
84 return nullptr;
87 return it->second;
89 } // namespace CollectionUtils
91 } // namespace WikiWalker