| 1 |
#include <Rinternals.h> |
| 2 |
|
| 3 |
SEXP reflist_init(void){ |
| 4 |
return R_NilValue; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
SEXP reflist_add(SEXP x, SEXP target){ |
| 9 |
if(!Rf_isPairList(x)) |
| 10 |
Rf_error("Not a LISTSXP"); |
| 11 |
return(Rf_cons(target, x)); |
| 12 |
} |
| 13 |
|
| 14 |
SEXP reflist_has(SEXP x, SEXP target){ |
| 15 |
if(!Rf_isPairList(x)) |
| 16 |
Rf_error("Not a LISTSXP"); |
| 17 |
while(x != R_NilValue){ |
| 18 |
if(CAR(x) == target) |
| 19 |
return(Rf_ScalarLogical(1)); |
| 20 |
x = CDR(x); |
| 21 |
} |
| 22 |
return(Rf_ScalarLogical(0)); |
| 23 |
} |
| 24 |
|
| 25 |
SEXP reflist_remove(SEXP x, SEXP target){ |
| 26 |
if(!Rf_isPairList(x)) |
| 27 |
Rf_error("Not a LISTSXP"); |
| 28 |
|
| 29 |
|
| 30 |
if(x != R_NilValue && CAR(x) == target) |
| 31 |
return(CDR(x)); |
| 32 |
SEXP prev = x; |
| 33 |
SEXP current = CDR(x); |
| 34 |
|
| 35 |
|
| 36 |
while(current != R_NilValue){ |
| 37 |
if(CAR(current) == target){ |
| 38 |
SETCDR(prev, CDR(current)); |
| 39 |
return(x); |
| 40 |
} |
| 41 |
prev = current; |
| 42 |
current = CDR(current); |
| 43 |
} |
| 44 |
Rf_error("Object not found in reflist!"); |
| 45 |
} |
| 46 |
|
| 47 |
SEXP reflist_length(SEXP x) { |
| 48 |
if(!Rf_isPairList(x)) |
| 49 |
Rf_error("Not a LISTSXP"); |
| 50 |
int i = 0; |
| 51 |
while(x != R_NilValue){ |
| 52 |
i++; |
| 53 |
x = CDR(x); |
| 54 |
} |
| 55 |
return Rf_ScalarInteger(i); |
| 56 |
} |