1use ::rdll::*;
2use ::storage::*;
3use ::traits::*;
4use ::rtype::*;
5use error::*;
7use error::REKind::NotCompatible;
8use std::ops::Range;
9
10
11pub type ExprVec = ExprVecM<Preserve>;
12
13
14impl<T: SEXPbucket> ExprVecM<T> {
15 pub fn new(x: SEXP) -> RResult<ExprVecM<T>> {
16
17 if RTYPEOF(x) != EXPRSXP {
18 return rerror(NotCompatible("expecting a expression vector".into()));
19 }
20 Ok(ExprVecM { data: T::new(x) })
21 }
22 pub fn alloc(x: usize) -> ExprVecM<T> {
23 ExprVecM { data: T::new(unsafe { Rf_allocVector(EXPRSXP, x as R_xlen_t) }) }
24 }
25 pub fn alloc_matrix(x: ::std::os::raw::c_int, y: ::std::os::raw::c_int) -> ExprVecM<T> {
26 ExprVecM { data: T::new(unsafe { Rf_allocMatrix(EXPRSXP, x, y) }) }
27 }
28 pub fn at(&self, ind: usize) -> Option<SEXP> {
29 unsafe {
30 if Rf_xlength(self.s()) <= ind as R_xlen_t {
31 return None;
32 }
33 Some(VECTOR_ELT(self.s(), ind as R_xlen_t))
34 }
35 }
36 pub unsafe fn uat(&self, ind: usize) -> SEXP {
37 VECTOR_ELT(self.s(), ind as R_xlen_t)
38 }
39 pub unsafe fn uset<TT: ToExpr>(&mut self, ind: usize, value: TT) {
40 SET_VECTOR_ELT(self.s(), ind as R_xlen_t, value.expr());
41 }
42 pub fn set<TT: ToExpr>(&mut self, ind: usize, value: TT) -> RResult<()> {
43 unsafe {
44 if Rf_xlength(self.s()) <= ind as R_xlen_t {
45 return rraise("index out of bound");
46 }
47 SET_VECTOR_ELT(self.s(), (ind - 1) as R_xlen_t, value.expr());
48 Ok(())
49 }
50 }
51 pub fn range(&self, ind: Range<usize>) -> Option<Vec<SEXP>> {
52 unsafe {
53 if Rf_xlength(self.s()) <= ind.end as R_xlen_t {
54 return None;
55 }
56 let mut vecs = Vec::with_capacity((ind.end - ind.start) as usize);
57 for ii in ind {
58 vecs.push(VECTOR_ELT(self.s(), ii as R_xlen_t));
59 }
60 Some(vecs)
61 }
62 }
63 pub fn is_duplicated(&self, from_last: bool) -> R_xlen_t {
64 let last = if from_last {
65 Rboolean::TRUE
66 } else {
67 Rboolean::FALSE
68 };
69 unsafe { Rf_any_duplicated(self.s(), last) }
70 }
71}
72
73impl<T: SEXPbucket> URNew for ExprVecM<T> {
102 unsafe fn urnew(x: SEXP) -> Self {
103 ExprVecM { data: T::new(x) }
104 }
105}
106impl<T: SEXPbucket> RNew for ExprVecM<T> {
107 fn rnew(x: SEXP) -> RResult<Self> {
108 Self::new(x)
109 }
110}
111
112impl<T: SEXPbucket> RSize for ExprVecM<T> {
113 fn rsize(&self) -> R_xlen_t {
114 unsafe { Rf_xlength(self.s()) }
115 }
116}
117
118gen_traits_sexp!(ExprVecM);
119
120
121impl<T: SEXPbucket> RName for ExprVecM<T> {}
122impl<T: SEXPbucket> RDim for ExprVecM<T> {
123 type Output = SEXP;
124}
125
126#[derive(Debug)]
127pub struct ExprIter<T: SEXPbucket> {
128 size: R_xlen_t,
129 next: R_xlen_t,
130 ty: ExprVecM<T>,
131}
132
133impl<T: SEXPbucket> Iterator for ExprIter<T> {
134 type Item = SEXP;
136
137 fn next(&mut self) -> Option<SEXP> {
139 if self.next < self.size {
142 self.next += 1;
143 unsafe { Some(VECTOR_ELT(self.ty.s(), self.next - 1)) }
144 } else {
145 None
146 }
147
148 }
149}
150
151impl<T: SEXPbucket> IntoIterator for ExprVecM<T> {
152 type Item = SEXP;
153 type IntoIter = ExprIter<T>;
154
155 fn into_iter(self) -> Self::IntoIter {
156 ExprIter {
157 size: self.rsize(),
158 next: 0,
159 ty: self,
160 }
161 }
162}
163
164impl<T: SEXPbucket> ExactSizeIterator for ExprIter<T> {
165 fn len(&self) -> usize {
167 self.size as usize
168 }
169}