View | Details | Raw Unified | Return to bug 16491
Collapse All | Expand All

(-)src/main/unique.c (-4 / +50 lines)
Lines 854-859 Link Here
854
    else type = TYPEOF(x) < TYPEOF(table) ? TYPEOF(table) : TYPEOF(x);
854
    else type = TYPEOF(x) < TYPEOF(table) ? TYPEOF(table) : TYPEOF(x);
855
    PROTECT(x	  = coerceVector(x,	type)); nprot++;
855
    PROTECT(x	  = coerceVector(x,	type)); nprot++;
856
    PROTECT(table = coerceVector(table, type)); nprot++;
856
    PROTECT(table = coerceVector(table, type)); nprot++;
857
858
    /* scalar x */
859
    if ( LENGTH(x) == 1 && ! incomp ) {
860
      PROTECT(ans = ScalarInteger(nmatch)); nprot++;
861
      switch (type) {
862
      case STRSXP: {
863
	  SEXP x_val = STRING_ELT(x,0);
864
	  for (int i=0; i < LENGTH(itable); i++) {
865
	    if ( STRING_ELT(table,i) == x_val ) { INTEGER(ans)[0] = i + 1; break; }
866
	  }
867
	  break; }
868
      case LGLSXP:
869
      case INTSXP: {
870
	  int x_val = INTEGER(x)[0];
871
	  int *table_p = INTEGER(table);
872
	  for (int i=0; i < LENGTH(itable); i++) {
873
	    if ( table_p[i] == x_val ) { INTEGER(ans)[0] = i + 1; break; }
874
	  }
875
	  break; }
876
      case REALSXP: {
877
	  double x_val = REAL(x)[0];
878
	  double *table_p = REAL(table);
879
	  for (int i=0; i < LENGTH(itable); i++) {
880
	    if ( table_p[i] == x_val ) { INTEGER(ans)[0] = i + 1; break; }
881
	  }
882
	  break; }
883
      case CPLXSXP: {
884
	Rcomplex x_val = COMPLEX(x)[0];
885
	for (int i=0; i < LENGTH(itable); i++) {
886
	  if ( COMPLEX(table)[i].r == x_val.r && COMPLEX(table)[i].i == x_val.i ) { INTEGER(ans)[0] = i + 1; break; }
887
	}
888
	break; }
889
      case RAWSXP:
890
	for (int i=0; i < LENGTH(itable); i++) {
891
	  if ( RAW(table)[i] == RAW(x)[0] ) { INTEGER(ans)[0] = i + 1; break; }
892
	}
893
	break;
894
      }
895
      UNPROTECT(nprot);
896
      return ans;
897
    } else {
898
      
857
    if (incomp) { PROTECT(incomp = coerceVector(incomp, type)); nprot++; }
899
    if (incomp) { PROTECT(incomp = coerceVector(incomp, type)); nprot++; }
858
    data.nomatch = nmatch;
900
    data.nomatch = nmatch;
859
    HashTableSetup(table, &data, NA_INTEGER);
901
    HashTableSetup(table, &data, NA_INTEGER);
Lines 903-909 Link Here
903
    UNPROTECT(nprot);
945
    UNPROTECT(nprot);
904
    return ans;
946
    return ans;
905
}
947
}
948
}
906
949
950
951
907
SEXP matchE(SEXP itable, SEXP ix, int nmatch, SEXP env)
952
SEXP matchE(SEXP itable, SEXP ix, int nmatch, SEXP env)
908
{
953
{
909
    return match5(itable, ix, nmatch, NULL, env);
954
    return match5(itable, ix, nmatch, NULL, env);
Lines 927-938 Link Here
927
    int nomatch = asInteger(CADDR(args));
972
    int nomatch = asInteger(CADDR(args));
928
    SEXP incomp = CADDDR(args);
973
    SEXP incomp = CADDDR(args);
929
974
930
    if(length(incomp) && /* S has FALSE to mean empty */
975
    /* S has incomp == FALSE to mean empty */
931
       !(isLogical(incomp) && length(incomp) == 1 && LOGICAL(incomp)[0] == 0))
976
    if ( isNull(incomp) || (length(incomp) == 1 && isLogical(incomp) && LOGICAL(incomp)[0] == 0) ) {
977
      return match5(CADR(args), CAR(args), nomatch, NULL, env);
978
    } else {
932
	return match5(CADR(args), CAR(args), nomatch, incomp, env);
979
	return match5(CADR(args), CAR(args), nomatch, incomp, env);
933
    else
934
	return matchE(CADR(args), CAR(args), nomatch, env);
935
}
980
}
981
}
936
982
937
/* pmatch and charmatch return integer positions, so cannot be used
983
/* pmatch and charmatch return integer positions, so cannot be used
938
   for long vector tables */
984
   for long vector tables */

Return to bug 16491