Skip to content

Commit 59babc6

Browse files
committed
c/pareto.c (pareto_rank_naive): Simplify.
1 parent d1d6a2a commit 59babc6

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

c/pareto.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pareto_rank_3d(const double * restrict points, size_t size)
1515
{
1616
ASSUME(size >= 2);
1717
const size_t orig_size = size;
18-
1918
const bool keep_weakly = true;
19+
2020
int * rank = calloc(size, sizeof(*rank));
2121
int front = 0;
2222

@@ -285,38 +285,36 @@ pareto_rank_2d(const double * restrict points, size_t size)
285285
Evolutionary Computation, 7(5):503–515, 2003.
286286
*/
287287
static int *
288-
pareto_rank_naive (const double * points, size_t size, dimension_t dim)
288+
pareto_rank_naive(const double * restrict points, size_t size, dimension_t dim)
289289
{
290+
ASSUME(dim >= 2);
290291
ASSUME(size >= 2);
291292
const size_t orig_size = size;
292293
const bool keep_weakly = true;
293294
int * rank = calloc(size, sizeof(*rank));
294-
int front = 0;
295+
int front = 1;
295296

296-
bool * nondom = malloc(size * sizeof(*nondom));
297+
bool * dominated = calloc(size, sizeof(*dominated));
297298
const double ** p = malloc(size * sizeof(*p));
298299
for (size_t k = 0; k < size; k++)
299300
p[k] = points + dim * k;
300301

301302
while (true) {
302303
ASSUME(size >= 2);
303-
for (size_t k = 0; k < size; k++)
304-
nondom[k] = true;
305-
306304
size_t new_size = size;
307305
size_t min_k = 0, last_dom_pos = orig_size;
308306
for (size_t j = 1; j < size; j++) {
309307
const double * restrict pj = p[j];
310308
size_t k = min_k;
311-
assert(nondom[j]);
312-
while (!nondom[k])
309+
assert(!dominated[j]);
310+
while (dominated[k])
313311
k++;
314312
min_k = k;
315313
ASSUME(k < j);
316-
ASSUME(nondom[k]);
314+
ASSUME(!dominated[k]);
317315
for (; k < j; k++) {
318-
assert(nondom[j]);
319-
if (!nondom[k]) continue;
316+
assert(!dominated[j]);
317+
if (dominated[k]) continue;
320318

321319
const double * restrict pk = p[k];
322320
// Use unsigned instead of bool to allow auto-vectorization.
@@ -337,11 +335,11 @@ pareto_rank_naive (const double * points, size_t size, dimension_t dim)
337335
if (!(dom_k | dom_j)) continue;
338336

339337
assert(dom_k ^ dom_j); // At least one but not both can be removed.
340-
new_size--; // Something dominated
338+
new_size--; // Something dominated.
341339

342340
last_dom_pos = (dom_j) ? j : k;
343-
assert(nondom[last_dom_pos]);
344-
nondom[last_dom_pos] = false;
341+
assert(!dominated[last_dom_pos]);
342+
dominated[last_dom_pos] = true;
345343
if (dom_j) break;
346344
}
347345
}
@@ -352,24 +350,26 @@ pareto_rank_naive (const double * points, size_t size, dimension_t dim)
352350
if (size == 1) {
353351
assert(last_dom_pos < orig_size);
354352
// Update the only dominated point.
355-
rank[(p[last_dom_pos] - points) / dim] = front + 1;
353+
rank[(p[last_dom_pos] - points) / dim] = front;
356354
}
357-
free(nondom);
355+
free(dominated);
358356
free(p);
359357
return rank;
360358
}
361359
size_t k = 0;
362-
while (!nondom[k]) k++; // Find first nondominated
360+
while (dominated[k]) k++; // Find first nondominated
363361
for (size_t n = k; k < size; k++) {
364362
do { // Find next dominated
365363
n++;
366-
} while (nondom[n]);
364+
} while (!dominated[n]);
367365
p[k] = p[n];
368366
}
369367
// Update ranks. Everything still in list p goes to the next front.
370-
front++;
371368
for (k = 0; k < size; k++)
372369
rank[(p[k] - points) / dim] = front;
370+
for (k = 0; k < size; k++)
371+
dominated[k] = false;
372+
front++;
373373
}
374374
}
375375

0 commit comments

Comments
 (0)