Ron Parker wrote:
> Here's a fix for the problem with duplicate.c having undefined behavior
> in the presence of duplicate export dates. It seems to pass testo, but I
> sure wouldn't mind another pair of eyes checking it over:
I stitched this in by hand since the whitespace in this message was all
whacked out so it wouldn't apply. I do like the approach.
Did I misread the patch, or does your version now look something
like:
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;
If so, it's odd that we have different comparisons for date and index.
If exported if a < b return 1
if index if a < b return -1
is that right?
RJL
>
>
>
> Index: duplicate.c
> ===================================================================
> RCS file: /cvsroot/gpsbabel/gpsbabel/duplicate.c,v
> retrieving revision 1.6
> diff -u -r1.6 duplicate.c
> --- duplicate.c 27 Feb 2003 19:06:18 -0000 1.6
> +++ duplicate.c 13 Jun 2003 13:53:06 -0000
> @@ -142,18 +142,33 @@
> xfree(tree);
> }
>
> +typedef struct {
> + waypoint *wpt;
> + int index;
> +} wpt_ptr;
> +
> static
> int
> compare(const void *a, const void *b)
> {
> - const waypoint *wa = *(waypoint **)a;
> - const waypoint *wb = *(waypoint **)b;
> -
> - if ( wa->gc_data.exported < wb->gc_data.exported ) {
> + 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->gc_data.exported > wb->gc_data.exported ) {
> + } 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;
> }
>
> @@ -167,21 +182,24 @@
> waypoint * delwpt = NULL;
>
> int i, ct = waypt_count();
> - waypoint **htable, **bh;
> + wpt_ptr *htable, *bh;
> queue *elem, *tmp;
> extern queue waypt_head;
>
> htable = xmalloc(ct * sizeof(*htable));
> bh = htable;
>
> + i = 0;
> QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
> - *bh = (waypoint *) elem;
> + bh->wpt = (waypoint *) elem;
> + bh->index = i;
> + i ++;
> bh ++;
> }
> - qsort(htable, ct, sizeof(*bh), compare);
> + qsort(htable, ct, sizeof(*htable), compare);
>
> for (i=0;i<ct;i++) {
> - waypointp = htable[i];
> + waypointp = htable[i].wpt;
>
> memset(&dupe, '\0', sizeof(dupe));
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: eBay
> Great deals on office technology -- on eBay now! Click here:
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Gpsbabel-code mailing list
> Gpsbabel-code@...
> https://lists.sourceforge.net/lists/listinfo/gpsbabel-code
|