Skip to content

Commit 404b944

Browse files
committed
Move comparison to sort.h. Move qsort closer to initialization.
1 parent 531bec9 commit 404b944

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

c/hv3dplus.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ init_sentinels(dlnode_t * list, const double * ref, dimension_t dim)
7575
projections in the (x,y)-plane of points that is kept sorted according
7676
to the 1st and 2nd coordinates, and for that list we need two sentinels
7777
to represent (-inf, ref[1]) and (ref[0], -inf). */
78-
dlnode_t * s1 = list;
79-
dlnode_t * s2 = list + 1;
80-
dlnode_t * s3 = list + 2;
8178

8279
// Allocate the 3 sentinels of dimension dim.
8380
const double z3[] = {
@@ -87,6 +84,10 @@ init_sentinels(dlnode_t * list, const double * ref, dimension_t dim)
8784
};
8885
double * x = malloc(sizeof(z3));
8986
memcpy(x, z3, sizeof(z3));
87+
dlnode_t * s1 = list;
88+
dlnode_t * s2 = list + 1;
89+
dlnode_t * s3 = list + 2;
90+
9091
// Sentinel 1
9192
s1->x = x;
9293
s1->closest[0] = s2;
@@ -121,20 +122,6 @@ init_sentinels(dlnode_t * list, const double * ref, dimension_t dim)
121122

122123
/* ---------------------------------- Sort ---------------------------------------*/
123124

124-
static int
125-
compare_point3d(const void * restrict p1, const void * restrict p2)
126-
{
127-
const double *x1 = *((const double **)p1);
128-
const double *x2 = *((const double **)p2);
129-
for (int i = 2; i >= 0; i--) {
130-
if (x1[i] < x2[i])
131-
return -1;
132-
if (x1[i] > x2[i])
133-
return 1;
134-
}
135-
return 0;
136-
}
137-
138125
static void print_x(dlnode_t * p)
139126
{
140127
assert(p != NULL);
@@ -280,18 +267,18 @@ setup_cdllist(const double * restrict data, size_t n, const double * restrict re
280267
}
281268
}
282269
n = i; // Update number of points.
270+
if (n > 1)
271+
qsort(scratch, n, sizeof(*scratch), cmp_double_asc_rev_3d);
283272

284273
dlnode_t * list = (dlnode_t *) malloc((n + 3) * sizeof(*list));
285-
dlnode_t * list3 = list+3;
286274
init_sentinels(list, ref, d);
287275
if (unlikely(n == 0)) {
288276
free(scratch);
289277
return list;
290278
}
291279

292-
qsort(scratch, n, sizeof(*scratch), compare_point3d);
293-
294280
dlnode_t * q = list+1;
281+
dlnode_t * list3 = list+3;
295282
assert(list->next == list + 1);
296283
assert(q->next == list + 2);
297284
for (size_t i = 0; i < n; i++) {
@@ -309,9 +296,11 @@ setup_cdllist(const double * restrict data, size_t n, const double * restrict re
309296
q = p;
310297
}
311298
free(scratch);
312-
q = list->prev;
313-
(list3 + n - 1)->next = q;
314-
q->prev = list3 + n - 1;
299+
assert(q == (list3 + n - 1));
300+
assert(list+2 == list->prev);
301+
// q = last point, q->next = s3, s3->prev = last point
302+
q->next = list + 2;
303+
q->next->prev = q;
315304
preprocessing(list, n);
316305
return list;
317306
}
@@ -378,7 +367,6 @@ hv3dplus(dlnode_t * list)
378367
p = p->next;
379368
}
380369
return volume;
381-
382370
}
383371

384372
double

c/sort.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,25 @@ strongly_dominates(const double * restrict x, const double * restrict y, dimensi
1717
return true;
1818
}
1919

20+
static inline int
21+
cmp_double_asc_rev(const void * restrict p1, const void * restrict p2, dimension_t dim)
22+
{
23+
const double *x1 = *((const double **)p1);
24+
const double *x2 = *((const double **)p2);
25+
for (int i = dim - 1; i >= 0; i--) {
26+
if (x1[i] < x2[i])
27+
return -1;
28+
if (x1[i] > x2[i])
29+
return 1;
30+
}
31+
return 0;
32+
}
33+
34+
static inline int
35+
cmp_double_asc_rev_3d(const void * restrict p1, const void * restrict p2)
36+
{
37+
return cmp_double_asc_rev(p1, p2, 3);
38+
}
39+
2040

2141
#endif /* !SORT_H_ */

0 commit comments

Comments
 (0)