-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppsByDownloadCountTree.cpp
79 lines (62 loc) · 2.46 KB
/
AppsByDownloadCountTree.cpp
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
/****************************************************************************/
/* */
/* AppsByDownloadCountTree class implementation */
/* */
/****************************************************************************/
#include <cstdlib> // NULL definition
#include "AppsByDownloadCountTree.h"
#include "AVLTree.h"
#include "common.h"
void AppsByDownloadCountTree::addApp(const AppsListIterator& appData) {
// This function is basically an alias to AVLTree::insert()
// Create a DownloadCountAndId to be used as key
DownloadCountAndId key(appData->appId, appData->downloadCount);
insert(key, appData);
}
void AppsByDownloadCountTree::removeApp(int downloadCount, int appId) {
// This function is basically an alias to AVLTree::remove()
// Create a DownloadCountAndId to be used as key
DownloadCountAndId key(appId, downloadCount);
remove(key);
}
int AppsByDownloadCountTree::predKeyData(const DownloadCountAndId& key,
const AppsListIterator& data) const {
// Compare an appId with an AppData pointer:
// Simply compare the given appId with the appId in the structure
// pointed by data.
if (key.downloadCount < data->downloadCount) {
return -1;
} else if (key.downloadCount > data->downloadCount) {
return 1;
}
// Equal downloadCount, sort using appId
// NOTE: Lower appId is considered larger value here, to match instructions
// in FAQ and PDF about finding the top app
if (data->appId < key.appId) {
return -1;
} else if (data->appId > key.appId) {
return 1;
}
// Equal downloadCount and appId
// NOTE: This should not be possible as appId is unique
return 0;
}
int AppsByDownloadCountTree::predDataData(const AppsListIterator& data,
const AppsListIterator& other) const {
if (data->downloadCount < other->downloadCount) {
return -1;
} else if (data->downloadCount > other->downloadCount) {
return 1;
}
// Equal downloadCount, sort using appId
// NOTE: Lower appId is considered larger value here, to match instructions
// in FAQ and PDF about finding the top app
if (other->appId < data->appId) {
return -1;
} else if (other->appId > data->appId) {
return 1;
}
// Equal downloadCount and appId
// NOTE: This should not be possible as appId is unique
return 0;
}