Skip to content

[Rcpp-devel] Possible error in setequal ??

6 messages · Søren Højsgaard, Kevin Ushey, Qiang Kou +1 more

#
Dear all,

There might be an error in setequal() from sugar. Consider this

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
bool seteq1(CharacterVector x, CharacterVector y){
  return setequal(x,y);
}

//[[Rcpp::export]]
bool seteq2(CharacterVector x, CharacterVector y){
  return
	(((CharacterVector) setdiff(x,y)).length()==0) &
	(((CharacterVector) setdiff(y,x)).length()==0) ;
}

The examples below does not give what I expect when calling seteq1 whereas seteq2 gives what I expect. Am I completely wrong here?

Regards
S?ren

seteq1(c(1,2,3), c(3,2,1))
seteq1(c("a","b"), c("a","b"))
seteq1(c(1,2,3), c(3,2))
seteq1(c("a","b"), c("a","b","k"))

seteq2(c(1,2,3), c(3,2,1))
seteq2(c("a","b"), c("a","b"))
seteq2(c(1,2,3), c(3,2))
seteq2(c("a","b"), c("a","b","k"))
[1] FALSE
[1] FALSE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
#
Hi S?ren,

Thanks for the bug report -- it looks like you're right, `setequal` is
broken, and we never knew about it because we don't have any unit
tests for it. Whoops!

I'll take a look at what's going on and commit a fix + tests soon --
for now, you can use the workaround with `setdiff` which appears to
behave correctly.

Cheers,
Kevin
On Fri, Jan 2, 2015 at 3:13 PM, S?ren H?jsgaard <sorenh at math.aau.dk> wrote:
#
I think the bug is from [1].

This line doesn't make much sense.

Best,

KK


[1]
https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/sugar/functions/setdiff.h#L80
On Fri, Jan 2, 2015 at 9:22 PM, Kevin Ushey <kevinushey at gmail.com> wrote:

            

  
    
#
It seems fixed after removing that line [1].
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE

[1]
https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/sugar/functions/setdiff.h#L80
On Fri, Jan 2, 2015 at 9:23 PM, Qiang Kou <qkou at umail.iu.edu> wrote:

            

  
    
#
Hi Qiang,

I think you're right -- I just took a peek and that looks like the
offending bit. I'm going to strip it out, run tests and then commit if
everything looks good.

Thanks!
Kevin
On Fri, Jan 2, 2015 at 6:26 PM, Qiang Kou <qkou at umail.iu.edu> wrote:
#
Qiang, Kevin,
On 2 January 2015 at 18:28, Kevin Ushey wrote:
| I think you're right -- I just took a peek and that looks like the
| offending bit. I'm going to strip it out, run tests and then commit if
| everything looks good.

Big Thank you! to both of you. 

I folded the pull request into main; we should now be in better shape for
setequal.

Cheers, Dirk