2222#include <config.h>
2323#endif
2424
25- #include <string.h> // for memset, strcat, strlen, strncmp
25+ #include <string.h> // for memset, strcat, strlen, strncmp
2626#include <Defn.h>
2727
2828#include "statsR.h"
@@ -994,10 +994,10 @@ static int n_xVars; // nesting level: use for indentation
994994// Global "State" Variables for terms() computation :
995995// -------------------------------------------------
996996
997- static int // 0/1 (Boolean) :
998- intercept , // 1: have intercept term in the model
999- parity , // +/- parity
1000- response ; // 1: response term in the model
997+ static bool
998+ intercept , // have intercept term in the model
999+ parity , // true if "positive parity"
1000+ response ; // response term in the model
10011001static int nwords ; /* # of words (ints) to code a term */
10021002static SEXP varlist ; /* variables in the model */
10031003static PROTECT_INDEX vpi ;
@@ -1181,12 +1181,12 @@ static void ExtractVars(SEXP formula)
11811181 error (_ ("invalid model formula" )); // more than one '~'
11821182 if (isNull (CDDR (formula ))) {
11831183 Prt_xtrVars ("isLanguage, tilde, *not* response" );
1184- response = 0 ;
1184+ response = false ;
11851185 ExtractVars (CADR (formula ));
11861186 }
11871187 else {
11881188 Prt_xtrVars ("isLanguage, tilde, *response*" );
1189- response = 1 ;
1189+ response = true ;
11901190 InstallVar (CADR (formula ));
11911191 ExtractVars (CADDR (formula ));
11921192 }
@@ -1391,7 +1391,7 @@ static SEXP StripTerm(SEXP term, SEXP list)
13911391{
13921392 SEXP root = R_NilValue , prev = R_NilValue ;
13931393 if (TermZero (term ))
1394- intercept = 0 ;
1394+ intercept = false ;
13951395 while (list != R_NilValue ) {
13961396 if (TermEqual (term , CAR (list ))) {
13971397 if (prev != R_NilValue )
@@ -1592,8 +1592,8 @@ static SEXP NestTerms(SEXP left, SEXP right)
15921592
15931593static SEXP DeleteTerms (SEXP left , SEXP right )
15941594{
1595- PROTECT (left = EncodeVars (left )); parity = 1 - parity ;
1596- PROTECT (right = EncodeVars (right )); parity = 1 - parity ;
1595+ PROTECT (left = EncodeVars (left )); parity = ! parity ;
1596+ PROTECT (right = EncodeVars (right )); parity = ! parity ;
15971597 for (SEXP t = right ; t != R_NilValue ; t = CDR (t ))
15981598 left = StripTerm (CAR (t ), left );
15991599 UNPROTECT (2 );
@@ -1608,12 +1608,12 @@ static SEXP DeleteTerms(SEXP left, SEXP right)
16081608 */
16091609static SEXP EncodeVars (SEXP formula )
16101610{
1611- if (isNull (formula )) return R_NilValue ;
1611+ if (isNull (formula )) return R_NilValue ;
16121612 else if (isOne (formula )) {
1613- intercept = ( parity ) ? 1 : 0 ; return R_NilValue ;
1613+ intercept = parity ; return R_NilValue ;
16141614 }
16151615 else if (isZero (formula )) {
1616- intercept = ( parity ) ? 0 : 1 ; return R_NilValue ;
1616+ intercept = ! parity ; return R_NilValue ;
16171617 }
16181618 // else :
16191619 SEXP term ;
@@ -1720,22 +1720,21 @@ static int TermCode(SEXP termlist, SEXP thisterm, int whichbit, SEXP term)
17201720 /* Search preceding terms for a match */
17211721 /* Zero is a possibility - it is a special case */
17221722
1723- int allzero = 1 ;
1723+ bool allzero = true ;
17241724 for (int i = 0 ; i < nwords ; i ++ ) {
17251725 if (term_ [i ]) {
1726- allzero = 0 ;
1727- break ;
1726+ allzero = false; break ;
17281727 }
17291728 }
17301729 if (allzero )
17311730 return 1 ;
17321731
17331732 for (SEXP t = termlist ; t != thisterm ; t = CDR (t )) {
1734- allzero = 1 ;
1733+ allzero = true ;
17351734 int * ct = INTEGER (CAR (t ));
17361735 for (int i = 0 ; i < nwords ; i ++ )
17371736 if (term_ [i ] & ~ct [i ]) {
1738- allzero = 0 ; break ;
1737+ allzero = false ; break ;
17391738 }
17401739 if (allzero )
17411740 return 1 ;
@@ -1804,13 +1803,12 @@ SEXP termsform(SEXP args)
18041803 }
18051804
18061805 /* Preserve term order? */
1807- int keepOrder = asLogical (CAR (a ));
1808- if (keepOrder == NA_LOGICAL )
1809- keepOrder = 0 ;
1806+ int aLog = asLogical (CAR (a ));
1807+ bool keepOrder = (aLog == NA_LOGICAL ) ? 0 : (bool ) aLog ;
18101808
18111809 a = CDR (a );
1812- int allowDot = asLogical (CAR (a ));
1813- if ( allowDot == NA_LOGICAL ) allowDot = 0 ;
1810+ aLog = asLogical (CAR (a ));
1811+ bool allowDot = ( aLog == NA_LOGICAL ) ? 0 : ( bool ) aLog ;
18141812
18151813 // a := attributes(<answer>)
18161814 a = allocList ((specials == R_NilValue ) ? 8 : 9 );
@@ -1821,9 +1819,9 @@ SEXP termsform(SEXP args)
18211819 * You can evaluate it to get the model variables or use substitute
18221820 * and then pull the result apart to get the variable names. */
18231821
1824- intercept = 1 ;
1825- parity = 1 ;
1826- response = 0 ;
1822+ intercept = true ;
1823+ parity = true ;
1824+ response = false ;
18271825 PROTECT (varlist = LCONS (install ("list" ), R_NilValue ));
18281826#ifdef DEBUG_terms
18291827 n_xVars = 0 ; // the nesting level of ExtractVars()
@@ -1892,7 +1890,7 @@ SEXP termsform(SEXP args)
18921890
18931891 /* first see if any of the variables are offsets */
18941892 R_xlen_t k = 0 ;
1895- for (R_xlen_t l = response ; l < nvar ; l ++ )
1893+ for (R_xlen_t l = ( R_xlen_t ) response ; l < nvar ; l ++ )
18961894 if (!strncmp (CHAR (STRING_ELT (varnames , l )), "offset(" , 7 )) k ++ ;
18971895 if (k > 0 ) {
18981896#ifdef DEBUG_terms
@@ -1901,6 +1899,7 @@ SEXP termsform(SEXP args)
19011899 bool foundOne = false; /* has there been a non-offset term? */
19021900 /* allocate the "offsets" attribute */
19031901 SETCAR (a , v = allocVector (INTSXP , k ));
1902+ // FIXME: using R_xlen_t above but int here, as we assign to INTEGER(v)
19041903 for (int l = response , k = 0 ; l < nvar ; l ++ )
19051904 if (!strncmp (CHAR (STRING_ELT (varnames , l )), "offset(" , 7 ))
19061905 INTEGER (v )[k ++ ] = l + 1 ;
@@ -2164,11 +2163,11 @@ SEXP termsform(SEXP args)
21642163 SET_TAG (a , install ("order" ));
21652164 a = CDR (a );
21662165
2167- SETCAR (a , ScalarInteger (intercept != 0 ));
2166+ SETCAR (a , ScalarInteger (( int ) intercept ));
21682167 SET_TAG (a , install ("intercept" ));
21692168 a = CDR (a );
21702169
2171- SETCAR (a , ScalarInteger (response != 0 ));
2170+ SETCAR (a , ScalarInteger (( int ) response ));
21722171 SET_TAG (a , install ("response" ));
21732172 a = CDR (a );
21742173
0 commit comments