1 //! \file ArticleCollection.cpp
3 #include "ArticleCollection.h"
9 #include "WalkerException.h"
13 namespace CollectionUtils
15 static bool articleIsAnalyzed(ArticleCollection::const_reference x
)
21 return art
->analyzed();
24 size_t countAnalyzedArticles(const ArticleCollection
& collection
)
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
));
36 void merge(ArticleCollection
& collection
,
37 const ArticleCollection
& other
,
38 MergeStrategy strategy
)
41 case MergeStrategy::IgnoreDuplicates
:
42 collection
.insert(other
.begin(), other
.end());
44 case MergeStrategy::AlwaysOverwrite
:
45 for(const auto& art
: other
) {
46 collection
[art
.first
] = art
.second
;
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
;
60 if(!otherArt
->analyzed()) {
64 auto linksInThis
= articleInThis
->countLinks();
65 auto linksInOther
= otherArt
->countLinks();
66 if(linksInOther
> linksInThis
) {
67 collection
[art
.first
] = art
.second
;
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
) {
89 } // namespace CollectionUtils
91 } // namespace WikiWalker