Skip to content

Commit 4d8946d

Browse files
committed
c/nondominated.h (find_nondominated_2d_impl): Simplify assuming stable sort.
1 parent 95a6667 commit 4d8946d

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

c/nondominated.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ generate_row_pointers_asc_rev_3d(const double * restrict points, size_t size)
257257
On Finding the Maxima of a Set of Vectors. Journal of the ACM,
258258
22(4):469–476, 1975.
259259
260-
Duplicated points may be removed in any order due to qsort not being stable.
261260
*/
262261
static __force_inline__ size_t
263262
find_nondominated_2d_impl(const double * restrict points, size_t size,
@@ -269,26 +268,23 @@ find_nondominated_2d_impl(const double * restrict points, size_t size,
269268
size_t n_nondom = size, j = 1;
270269
// When compiling with -O3, GCC is able to create two versions of this loop
271270
// and move keep_weakly out.
272-
const double * restrict pk = p[0];
271+
double f1_prev = p[0][0], f2_prev = p[0][1]; // Smallest f2, then smallest f1.
273272
do {
274273
const double * restrict pj = p[j];
275-
if (pk[0] > pj[0]) {
276-
pk = pj;
277-
} else {
278-
const bool k_eq_j = (pk[0] == pj[0]) & (pk[1] == pj[1]);
279-
if (!keep_weakly || likely(!k_eq_j)) {
280-
if (k_eq_j && pj < pk) // Only the first duplicated point is kept.
281-
SWAP(pj, pk);
282-
283-
size_t pos_first_dom = row_index_from_ptr(points, pj, 2);
284-
if (nondom == NULL) {
285-
// In this context, it means "position of the first dominated solution found".
286-
n_nondom = pos_first_dom;
287-
goto early_end;
288-
}
289-
nondom[pos_first_dom] = false;
290-
n_nondom--;
274+
if (f1_prev > pj[0]) {
275+
f1_prev = pj[0];
276+
f2_prev = pj[1];
277+
} else if (!keep_weakly || likely(f1_prev != pj[0] || f2_prev != pj[1])) {
278+
// The sorting function must be stable so that we keep only the
279+
// first duplicated point.
280+
size_t pos_first_dom = row_index_from_ptr(points, pj, 2);
281+
if (nondom == NULL) {
282+
// In this context, it means "position of the first dominated solution found".
283+
n_nondom = pos_first_dom;
284+
goto early_end;
291285
}
286+
nondom[pos_first_dom] = false;
287+
n_nondom--;
292288
}
293289
j++;
294290
} while (j < size);

0 commit comments

Comments
 (0)