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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "NetworkComparators.h"
bool NetworkBookItemComparator::operator () (const shared_ptr<NetworkItem> &bookPtr0, const shared_ptr<NetworkItem> &bookPtr1) const {
const bool book0isABook =
bookPtr0->isInstanceOf(NetworkBookItem::TYPE_ID);
const bool book1isABook =
bookPtr1->isInstanceOf(NetworkBookItem::TYPE_ID);
if (!book0isABook && !book1isABook) {
return bookPtr0->Title < bookPtr1->Title;
}
if (!book0isABook || !book1isABook) {
return !book0isABook;
}
const NetworkBookItem &book0 = (NetworkBookItem &) *bookPtr0;
const NetworkBookItem &book1 = (NetworkBookItem &) *bookPtr1;
const std::vector<NetworkBookItem::AuthorData> &authors0 = book0.Authors;
const std::vector<NetworkBookItem::AuthorData> &authors1 = book1.Authors;
if (authors0.empty() && !authors1.empty()) {
return true;
}
if (authors1.empty() && !authors0.empty()) {
return false;
}
if (!authors0.empty() && !authors1.empty()) {
if (authors0.front().SortKey != authors1.front().SortKey) {
return authors0.front().SortKey < authors1.front().SortKey;
}
}
/*if (book0.Index != book1.Index) {
return book0.Index < book1.Index;
}*/
const bool book0HasSeriesTitle = !book0.SeriesTitle.empty();
const bool book1HasSeriesTitle = !book1.SeriesTitle.empty();
if (book0HasSeriesTitle && book1HasSeriesTitle) {
const int comp = book0.SeriesTitle.compare(book1.SeriesTitle);
if (comp != 0) {
return comp < 0;
} else {
int diff = book0.IndexInSeries - book1.IndexInSeries;
if (diff != 0) {
return diff < 0;
}
}
return book0.Title < book1.Title;
}
const std::string &book0Key = book0HasSeriesTitle ? book0.SeriesTitle : book0.Title;
const std::string &book1Key = book1HasSeriesTitle ? book1.SeriesTitle : book1.Title;
const int comp = book0Key.compare(book1Key);
if (comp != 0) {
return comp < 0;
}
return book1HasSeriesTitle;
}
NetworkAuthorComparator::NetworkAuthorComparator(const std::map<NetworkBookItem::AuthorData, unsigned int> &rates) : myRates(rates) {
}
bool NetworkAuthorComparator::operator () (const NetworkBookItem::AuthorData &author0, const NetworkBookItem::AuthorData &author1) const {
std::map<NetworkBookItem::AuthorData, unsigned int>::const_iterator it1 = myRates.find(author0);
std::map<NetworkBookItem::AuthorData, unsigned int>::const_iterator it2 = myRates.find(author1);
if (it1 == myRates.end() && it2 == myRates.end()) {
return author0 < author1;
}
if (it1 == myRates.end()) {
return false;
}
if (it2 == myRates.end()) {
return true;
}
if (it1->second != it2->second) {
return it1->second < it2->second;
}
return author0 < author1;
}
bool NetworkBookItemByTitleComparator::operator ()(const shared_ptr<NetworkItem> &bookPtr0, const shared_ptr<NetworkItem> &bookPtr1) const {
return bookPtr0->Title < bookPtr1->Title;
}
bool NetworkBookItemBySeriesComparator::operator ()(const shared_ptr<NetworkItem> &bookPtr0, const shared_ptr<NetworkItem> &bookPtr1) const {
const NetworkBookItem &book0 = static_cast<const NetworkBookItem&>(*bookPtr0);
const NetworkBookItem &book1 = static_cast<const NetworkBookItem&>(*bookPtr1);
if (book0.SeriesTitle != book1.SeriesTitle) {
return book0.SeriesTitle < book1.SeriesTitle;
}
const int diff = book0.IndexInSeries - book1.IndexInSeries;
if (diff != 0) {
return diff < 0 ? true : false;
}
return book0.Title < book1.Title;
}
|