forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicate.cc
281 lines (231 loc) · 6.28 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
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., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include "defs.h"
#include "filterdefs.h"
#include <stdio.h>
#include <stdlib.h> // qsort
#if FILTERS_ENABLED
static char* snopt = NULL;
static char* lcopt = NULL;
static char* purge_duplicates = NULL;
static char* correct_coords = NULL;
static
arglist_t dup_args[] = {
{
"shortname", &snopt, "Suppress duplicate waypoints based on name",
NULL, ARGTYPE_BEGIN_REQ | ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"location", &lcopt, "Suppress duplicate waypoint based on coords",
NULL, ARGTYPE_END_REQ | ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"all", &purge_duplicates, "Suppress all instances of duplicates",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"correct", &correct_coords, "Use coords from duplicate points",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
ARG_TERMINATOR
};
typedef struct btree_node {
struct btree_node* left, *right;
unsigned long data;
Waypoint* wpt;
} btree_node;
static btree_node*
addnode(btree_node* tree, btree_node* newnode, btree_node** oldnode)
{
btree_node* tmp, * last = NULL;
if (*oldnode) {
*oldnode = NULL;
}
if (!tree) {
return (newnode);
}
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 (NULL);
}
}
if (newnode->data < last->data) {
last->right = newnode;
} else {
last->left = newnode;
}
return (tree);
}
void
free_tree(btree_node* tree)
{
if (tree->left) {
free_tree(tree->left);
}
if (tree->right) {
free_tree(tree->right);
}
xfree(tree);
}
typedef struct {
Waypoint* wpt;
int index;
} wpt_ptr;
/*
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.)
*/
static
int
compare(const void* a, const void* b)
{
const wpt_ptr* wa = (wpt_ptr*)a;
const wpt_ptr* wb = (wpt_ptr*)b;
if (wa->wpt->gc_data->exported < wb->wpt->gc_data->exported) {
return 1;
} else if (wa->wpt->gc_data->exported > wb->wpt->gc_data->exported) {
return -1;
}
/* If the exported dates are the same, sort by index. */
if (wa->index < wb->index) {
return -1;
} else if (wa->index > wb->index) {
return 1;
}
/* If index and date are the same, it's the same element. */
return 0;
}
static void
duplicate_process(void)
{
Waypoint* waypointp;
btree_node* newnode, * btmp, * sup_tree = NULL;
btree_node* oldnode = NULL;
unsigned long crc = 0;
struct {
char shortname[32];
char lat[13];
char lon[13];
} dupe;
Waypoint* delwpt = NULL;
int i, ct = waypt_count();
wpt_ptr* htable, *bh;
queue* elem, *tmp;
htable = (wpt_ptr*) xmalloc(ct * sizeof(*htable));
bh = htable;
i = 0;
#if NEWQ
foreach(Waypoint* waypointp, waypt_list) {
bh->wpt = waypointp;
#else
QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
bh->wpt = (Waypoint*) elem;
#endif
bh->index = i;
i ++;
bh ++;
}
qsort(htable, ct, sizeof(*htable), compare);
for (i=0; i<ct; i++) {
waypointp = htable[i].wpt;
memset(&dupe, '\0', sizeof(dupe));
if (snopt) {
strncpy(dupe.shortname, CSTRc(waypointp->shortname), sizeof(dupe.shortname) - 1);
}
if (lcopt) {
/* let sprintf take care of rounding */
sprintf(dupe.lat, "%11.4f", waypointp->latitude);
sprintf(dupe.lon, "%11.4f", waypointp->longitude);
/* The degrees2ddmm stuff is a feeble attempt to
* get everything rounded the same way in a precision
* that's "close enough" for determining duplicates.
*/
sprintf(dupe.lat, "%11.3f", degrees2ddmm(waypointp->latitude));
sprintf(dupe.lon, "%11.3f", degrees2ddmm(waypointp->longitude));
}
crc = get_crc32(&dupe, sizeof(dupe));
newnode = (btree_node*)xcalloc(sizeof(btree_node), 1);
newnode->data = crc;
newnode->wpt = waypointp;
btmp = addnode(sup_tree, newnode, &oldnode);
if (btmp == NULL) {
if (delwpt) {
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 = NULL;
}
}
} else {
sup_tree = btmp;
}
}
if (delwpt) {
delete delwpt;
}
xfree(htable);
if (sup_tree) {
free_tree(sup_tree);
}
}
filter_vecs_t duplicate_vecs = {
NULL,
duplicate_process,
NULL,
NULL,
dup_args
};
#endif