Skip to content

Commit

Permalink
opt:remove custom smallset and use qset instead (#2089)
Browse files Browse the repository at this point in the history
* opt:remove smallset and use qset instead
  • Loading branch information
xiaoyifang authored Jan 20, 2025
1 parent b8de6f0 commit 359c8c3
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/dict/mediawiki.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QNetworkReply>
#include <QUrl>
#include <QtXml>
#include <QSet>
#include <algorithm>
#include <list>
#include "audiolink.hh"
Expand Down Expand Up @@ -408,28 +409,9 @@ class MediaWikiArticleRequest: public MediaWikiDataRequestSlots

void requestFinished( QNetworkReply * ) override;

/// This simple set implementation should be much more efficient than tree-
/// and hash-based standard/Qt containers when there are very few elements.
template< typename T >
class SmallSet
{
public:
bool insert( T x )
{
if ( std::find( elements.begin(), elements.end(), x ) != elements.end() ) {
return false;
}
elements.push_back( x );
return true;
}

private:
std::vector< T > elements;
};

/// The page id set allows to filter out duplicate articles in case MediaWiki
/// redirects the main word and words in the alts collection to the same page.
SmallSet< long long > addedPageIds;
QSet< long long > addedPageIds;
Class * dictPtr;
};

Expand Down Expand Up @@ -528,10 +510,14 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
else {
QDomNode parseNode = dd.namedItem( "api" ).namedItem( "parse" );

if ( !parseNode.isNull()
&& parseNode.toElement().attribute( "revid" ) != "0"
// Don't show the same article more than once:
&& addedPageIds.insert( parseNode.toElement().attribute( "pageid" ).toLongLong() ) ) {
long long pageId = 0;
if ( !parseNode.isNull() && parseNode.toElement().attribute( "revid" ) != "0" ) {
pageId = parseNode.toElement().attribute( "pageid" ).toLongLong();
}

if ( pageId != 0 && !addedPageIds.contains( pageId ) ) {
addedPageIds.insert( pageId );

QDomNode textNode = parseNode.namedItem( "text" );

if ( !textNode.isNull() ) {
Expand Down

0 comments on commit 359c8c3

Please sign in to comment.