rustr/vectorx/
exprvec.rs

1use ::rdll::*;
2use ::storage::*;
3use ::traits::*;
4use ::rtype::*;
5// use ::protect::stackp::*;
6use 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
73// impl<T: SEXPbucket, E: UIntoR + Clone> From<Vec<E>> for ExprVecM<T> {
74//    fn from(x: Vec<E>) -> ExprVecM<T> {
75//        let size_x = x.len();
76//        unsafe {
77//            let rvec = Shield::new(Rf_allocVector(VECSXP, size_x as R_xlen_t));
78//            let mut xs =0;
79//            for ii in x {
80// 				SET_VECTOR_ELT(rvec.s(),  xs, Shield::new(ii.uintor()).s());
81//            	xs+=1;
82//            }
83//            ExprVecM { data: T::new(rvec.s()) }
84//        }
85//    }
86// }
87
88// impl<T: SEXPbucket,D:URNew> From<ExprVecM<T>> for Vec<D> {
89//    fn from(x: ExprVecM<T>) -> Vec<D> {
90//        unsafe {
91//            let lens = Rf_xlength(x.s());
92//            let mut vecs = Vec::with_capacity(lens as usize);
93//            for ii in 0..lens {
94//                vecs.push(D::urnew(VECTOR_ELT(x.s(), ii)));
95//            }
96//            vecs
97//        }
98//    }
99// }
100
101impl<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    // we will be counting with usize
135    type Item = SEXP;
136
137    // next() is the only required method
138    fn next(&mut self) -> Option<SEXP> {
139        // increment our count. This is why we started at zero.
140        // check to see if we've finished counting or not.
141        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    // We already have the number of iterations, so we can use it directly.
166    fn len(&self) -> usize {
167        self.size as usize
168    }
169}