11#include "Rcommon.h"
22#include "eaf.h"
33
4- #define DECLARE_CALL (RET_TYPE , NAME , ...) \
5- extern RET_TYPE NAME(__VA_ARGS__);
4+ #define DECLARE_CALL (NAME , ...) extern SEXP NAME(__VA_ARGS__);
65#include "init.h"
76#undef DECLARE_CALL
87
@@ -52,8 +51,7 @@ compute_eaf_C(SEXP DATA, SEXP CUMSIZES, SEXP PERCENTILE)
5251 eaf_t * * eaf = compute_eaf_helper (DATA , nobj , cumsizes , nruns , percentile , nlevels );
5352 int totalpoints = eaf_totalpoints (eaf , nlevels );
5453
55- SEXP mat ;
56- PROTECT (mat = Rf_allocMatrix (REALSXP , totalpoints , nobj + 1 ));
54+ SEXP mat = PROTECT (Rf_allocMatrix (REALSXP , totalpoints , nobj + 1 ));
5755 eaf2matrix_R (REAL (mat ), eaf , nobj , totalpoints , percentile , nlevels );
5856 eaf_free (eaf , nlevels );
5957 UNPROTECT (1 );
@@ -271,8 +269,7 @@ R_read_datasets(SEXP FILENAME)
271269 const int ntotal = cumsizes [nruns - 1 ];
272270
273271 /* FIXME: Is this the fastest way to transfer a matrix from C to R ? */
274- SEXP DATA ;
275- PROTECT (DATA = Rf_allocMatrix (REALSXP , ntotal , nobj + 1 ));
272+ SEXP DATA = PROTECT (Rf_allocMatrix (REALSXP , ntotal , nobj + 1 ));
276273 double * rdata = REAL (DATA );
277274 matrix_transpose_double (rdata , data , ntotal , nobj );
278275
@@ -290,7 +287,7 @@ R_read_datasets(SEXP FILENAME)
290287
291288#include "nondominated.h"
292289
293- void
290+ SEXP
294291normalise_C (SEXP DATA , SEXP RANGE , SEXP LBOUND , SEXP UBOUND , SEXP MAXIMISE )
295292{
296293 int nprotected = 0 ;
@@ -310,6 +307,7 @@ normalise_C(SEXP DATA, SEXP RANGE, SEXP LBOUND, SEXP UBOUND, SEXP MAXIMISE)
310307 lbound , ubound );
311308 free (maximise );
312309 UNPROTECT (nprotected );
310+ return R_NilValue ;
313311}
314312
315313SEXP
@@ -356,15 +354,12 @@ pareto_ranking_C(SEXP DATA)
356354SEXP
357355hypervolume_C (SEXP DATA , SEXP REFERENCE )
358356{
359- int nprotected = 0 ;
360357 /* We transpose the matrix before calling this function. */
361358 SEXP_2_DOUBLE_MATRIX (DATA , data , nobj , npoint );
362359 SEXP_2_DOUBLE_VECTOR (REFERENCE , reference , reference_len );
363360 assert (nobj == reference_len );
364- new_real_vector (hv , 1 );
365- hv [0 ] = fpli_hv (data , nobj , npoint , reference );
366- UNPROTECT (nprotected );
367- return Rexp (hv );
361+ double hv = fpli_hv (data , nobj , npoint , reference );
362+ return Rf_ScalarReal (hv );
368363}
369364
370365SEXP
@@ -385,20 +380,16 @@ hv_contributions_C(SEXP DATA, SEXP REFERENCE)
385380SEXP
386381rect_weighted_hv2d_C (SEXP DATA , SEXP RECTANGLES )
387382{
388- int nprotected = 0 ;
389383 /* We transpose the matrix before calling this function. */
390384 SEXP_2_DOUBLE_MATRIX (DATA , data , nobj , npoint );
391385 SEXP_2_DOUBLE_MATRIX (RECTANGLES , rectangles , unused , rectangles_nrow );
392- new_real_vector (hv , 1 );
393- hv [0 ] = rect_weighted_hv2d (data , npoint , rectangles , rectangles_nrow );
394- UNPROTECT (nprotected );
395- return Rexp (hv );
386+ double hv = rect_weighted_hv2d (data , npoint , rectangles , rectangles_nrow );
387+ return Rf_ScalarReal (hv );
396388}
397389
398390SEXP
399391preprocess_rectangles_C (SEXP RECTANGLES , SEXP REFERENCE )
400392{
401- int nprotected = 0 ;
402393 /* We transpose the matrix before calling this function. */
403394 SEXP_2_DOUBLE_MATRIX (RECTANGLES , rectangles , ncol , nrow );
404395 SEXP_2_DOUBLE_VECTOR (REFERENCE , reference , reference_len );
@@ -411,7 +402,7 @@ preprocess_rectangles_C(SEXP RECTANGLES, SEXP REFERENCE)
411402 rectangles [k * ncol + 3 ] = MIN (rectangles [k * ncol + 3 ], reference [1 ]);
412403 }
413404 int skip_nrow = 0 ;
414- int * skip = (int * ) malloc (nrow * sizeof (int ));
405+ int * skip = (int * ) R_alloc (nrow , sizeof (int ));
415406 for (int k = 0 ; k < nrow ; k ++ ) {
416407 bool empty = (rectangles [k * ncol + 0 ] == rectangles [k * ncol + 2 ]
417408 || rectangles [k * ncol + 1 ] == rectangles [k * ncol + 3 ]);
@@ -420,8 +411,6 @@ preprocess_rectangles_C(SEXP RECTANGLES, SEXP REFERENCE)
420411 }
421412
422413 if (skip_nrow == 0 ) {
423- free (skip );
424- UNPROTECT (nprotected );
425414 return RECTANGLES ;
426415 }
427416 int new_nrow = nrow - skip_nrow ;
@@ -440,8 +429,7 @@ preprocess_rectangles_C(SEXP RECTANGLES, SEXP REFERENCE)
440429 k = skip [s ] + 1 ;
441430 }
442431 }
443- free (skip );
444- UNPROTECT (nprotected + 1 );
432+ UNPROTECT (1 );
445433 return R_dest ;
446434}
447435
@@ -450,7 +438,6 @@ preprocess_rectangles_C(SEXP RECTANGLES, SEXP REFERENCE)
450438SEXP
451439whv_hype_C (SEXP DATA , SEXP IDEAL , SEXP REFERENCE , SEXP NSAMPLES , SEXP DIST , SEXP SEED , SEXP MU )
452440{
453- int nprotected = 0 ;
454441 SEXP_2_DOUBLE_MATRIX (DATA , data , nobj , npoints );
455442 SEXP_2_DOUBLE_VECTOR (IDEAL , ideal , ideal_len );
456443 SEXP_2_DOUBLE_VECTOR (REFERENCE , reference , reference_len );
@@ -460,20 +447,19 @@ whv_hype_C(SEXP DATA, SEXP IDEAL, SEXP REFERENCE, SEXP NSAMPLES, SEXP DIST, SEXP
460447 SEXP_2_STRING (DIST , dist_type );
461448 SEXP_2_UINT32 (SEED , seed );
462449
463- new_real_vector ( hv , 1 ) ;
450+ double hv ;
464451 if (0 == strcmp (dist_type , "uniform" )) {
465- hv [ 0 ] = whv_hype_unif (data , npoints , ideal , reference , nsamples , seed );
452+ hv = whv_hype_unif (data , npoints , ideal , reference , nsamples , seed );
466453 } else if (0 == strcmp (dist_type , "exponential" )) {
467454 const double * mu = REAL (MU );
468- hv [ 0 ] = whv_hype_expo (data , npoints , ideal , reference , nsamples , seed , mu [0 ]);
455+ hv = whv_hype_expo (data , npoints , ideal , reference , nsamples , seed , mu [0 ]);
469456 } else if (0 == strcmp (dist_type , "point" )) {
470457 const double * mu = REAL (MU );
471- hv [ 0 ] = whv_hype_gaus (data , npoints , ideal , reference , nsamples , seed , mu );
458+ hv = whv_hype_gaus (data , npoints , ideal , reference , nsamples , seed , mu );
472459 } else {
473460 Rf_error ("unknown 'dist' value: %s" , dist_type );
474461 }
475- UNPROTECT (nprotected );
476- return Rexp (hv );
462+ return Rf_ScalarReal (hv );
477463}
478464
479465#include "epsilon.h"
@@ -492,7 +478,6 @@ static inline SEXP
492478unary_metric_ref (SEXP DATA , SEXP REFERENCE , SEXP MAXIMISE ,
493479 enum unary_metric_t metric , SEXP EXTRA )
494480{
495- int nprotected = 0 ;
496481 /* We transpose the matrix before calling this function. */
497482 SEXP_2_DOUBLE_MATRIX (DATA , data , nobj , npoint );
498483 double * ref = REAL (REFERENCE );
@@ -501,32 +486,31 @@ unary_metric_ref(SEXP DATA, SEXP REFERENCE, SEXP MAXIMISE,
501486 SEXP_2_LOGICAL_BOOL_VECTOR (MAXIMISE , maximise , maximise_len );
502487 assert (nobj == maximise_len );
503488
504- new_real_vector ( value , 1 ) ;
489+ double value ;
505490 switch (metric ) {
506491 case EPSILON_ADD :
507- value [ 0 ] = epsilon_additive (data , nobj , npoint , ref , ref_size , maximise );
492+ value = epsilon_additive (data , nobj , npoint , ref , ref_size , maximise );
508493 break ;
509494 case EPSILON_MUL :
510- value [ 0 ] = epsilon_mult (data , nobj , npoint , ref , ref_size , maximise );
495+ value = epsilon_mult (data , nobj , npoint , ref , ref_size , maximise );
511496 break ;
512497 case INV_GD :
513- value [ 0 ] = IGD (data , nobj , npoint , ref , ref_size , maximise );
498+ value = IGD (data , nobj , npoint , ref , ref_size , maximise );
514499 break ;
515500 case INV_GDPLUS :
516- value [ 0 ] = IGD_plus (data , nobj , npoint , ref , ref_size , maximise );
501+ value = IGD_plus (data , nobj , npoint , ref , ref_size , maximise );
517502 break ;
518503 case AVG_HAUSDORFF : {
519504 SEXP_2_INT (EXTRA , p );
520- value [ 0 ] = avg_Hausdorff_dist (data , nobj , npoint , ref , ref_size , maximise , p );
505+ value = avg_Hausdorff_dist (data , nobj , npoint , ref , ref_size , maximise , p );
521506 break ;
522507 }
523508 default :
524509 Rf_error ("unknown unary metric" );
525510 }
526511
527512 free (maximise );
528- UNPROTECT (nprotected );
529- return Rexp (value );
513+ return Rf_ScalarReal (value );
530514}
531515
532516SEXP
0 commit comments