forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicate.cc
194 lines (151 loc) · 4.8 KB
/
duplicate.cc
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
exact duplicate point filter utility.
Copyright (C) 2002-2014 Robert Lipe, [email protected]
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 "duplicate.h"
#include <algorithm> // for stable_sort
#include <cstdio> // for snprintf
#include <cstring> // for memset, strncpy
#include <QDateTime> // for QDateTime
#include "defs.h"
#include "geocache.h" // for Geocache
#include "src/core/datetime.h" // for DateTime
#if FILTERS_ENABLED
DuplicateFilter::btree_node* DuplicateFilter::addnode(btree_node* tree, btree_node* newnode, btree_node** oldnode)
{
btree_node* last = nullptr;
if (*oldnode) {
*oldnode = nullptr;
}
if (!tree) {
return (newnode);
}
btree_node* tmp = tree;
while (tmp) {
last = tmp;
if (newnode->data < tmp->data) {
tmp = tmp->right;
} else if (newnode->data > tmp->data) {
tmp = tmp->left;
} else {
if (oldnode) {
*oldnode = tmp;
}
return (nullptr);
}
}
if (newnode->data < last->data) {
last->right = newnode;
} else {
last->left = newnode;
}
return (tree);
}
void DuplicateFilter::free_tree(btree_node* tree)
{
if (tree->left) {
free_tree(tree->left);
}
if (tree->right) {
free_tree(tree->right);
}
xfree(tree);
}
/*
It looks odd that we have different comparisons for date and index.
If exported if a < b return 1
if index if a < b return -1
The reason is that we want to sort in reverse order by date, but forward
order by index. So if we have four records:
date index
June 24 0
June 25 1
June 25 2
June 24 3
we want to sort them like this:
date index
June 25 1
June 25 2
June 24 0
June 24 3
Thus, the first point we come across is the latest point, but if we
have two points with the same export date/time, we will first see the
one with the smaller index (i.e. the first of those two points that we
came across while importing waypoints.)
In the (common) case that we have no exported dates, the dates will all
be zero so the sort will end up being an expensive no-op (expensive
because, sadly, quicksort can be O(n^2) on presorted elements.)
*/
void DuplicateFilter::process()
{
btree_node* btmp = nullptr;
btree_node* sup_tree = nullptr;
btree_node* oldnode = nullptr;
struct {
char shortname[32];
char lat[13];
char lon[13];
} dupe;
Waypoint* delwpt = nullptr;
auto htable = *global_waypoint_list;
auto compare_lambda = [](const Waypoint* wa, const Waypoint* wb)->bool {
return wa->gc_data->exported > wb->gc_data->exported;
};
std::stable_sort(htable.begin(), htable.end(), compare_lambda);
for (Waypoint* waypointp : htable) {
memset(&dupe, '\0', sizeof(dupe));
if (snopt) {
strncpy(dupe.shortname, CSTRc(waypointp->shortname), sizeof(dupe.shortname) - 1);
}
if (lcopt) {
/* The degrees2ddmm stuff is a feeble attempt to
* get everything rounded the same way in a precision
* that's "close enough" for determining duplicates.
*/
snprintf(dupe.lat, sizeof(dupe.lat), "%11.3f",
degrees2ddmm(waypointp->latitude));
snprintf(dupe.lon, sizeof(dupe.lon), "%11.3f",
degrees2ddmm(waypointp->longitude));
}
unsigned long crc = get_crc32(&dupe, sizeof(dupe));
auto* newnode = (btree_node*)xcalloc(sizeof(btree_node), 1);
newnode->data = crc;
newnode->wpt = waypointp;
btmp = addnode(sup_tree, newnode, &oldnode);
if (btmp == nullptr) {
delete delwpt;
if (correct_coords && oldnode && oldnode->wpt) {
oldnode->wpt->latitude = waypointp->latitude;
oldnode->wpt->longitude = waypointp->longitude;
}
delwpt = waypointp;
waypt_del(waypointp); /* collision */
xfree(newnode);
if (purge_duplicates && oldnode) {
if (oldnode->wpt) {
waypt_del(oldnode->wpt);
delete oldnode->wpt;
oldnode->wpt = nullptr;
}
}
} else {
sup_tree = btmp;
}
}
delete delwpt;
if (sup_tree) {
free_tree(sup_tree);
}
}
#endif