rustr/vector/
mod.rs

1//! R Vector with boolean and number
2//!
3//!
4//!
5
6
7/// For Vectors
8/// 
9#[macro_export]
10macro_rules! gen_named_vec{
11($NumVecM:ident,$REALSXP:ident,$NumVecIter:ident,$RAW:ident,$Rbyte:ident)=> (
12
13impl<T: SEXPbucket> $NumVecM<T> {
14    pub fn new(x: SEXP) -> RResult<$NumVecM<T>> {
15            if RTYPEOF(x.clone()) != $REALSXP {
16                return rerror(NotCompatible(concat!("expecting a ",stringify!($REALSXP)).into()));
17            }
18        Ok($NumVecM { data: T::new(x.clone()) })
19    }
20	pub fn alloc(x: usize)->$NumVecM<T>{
21    	$NumVecM { data: T::new(unsafe{Rf_allocVector($REALSXP,x as R_xlen_t)}) }
22    }
23    pub fn alloc_matrix(x: usize,y: usize)->$NumVecM<T>{
24    	$NumVecM { data: T::new(unsafe{Rf_allocMatrix($REALSXP,x as ::std::os::raw::c_int,y as ::std::os::raw::c_int)}) }
25    }
26	pub fn at(&self,  ind: usize) -> Option<$Rbyte> {
27		unsafe{
28        if Rf_xlength(self.s())<=ind as R_xlen_t {
29        	return None
30        }
31        let ptr = $RAW(self.s());
32
33        Some(* ptr.offset(ind as isize) as $Rbyte)}
34    }
35	pub fn set(&mut self,  ind: usize, value: $Rbyte) -> RResult<()> {
36		unsafe{
37        if Rf_xlength(self.s())<=ind  as R_xlen_t {
38        	return rraise("index out of bound");
39        }
40        let ptr = $RAW(self.s());
41        * ptr.offset((ind) as isize)  = value as $Rbyte;
42        Ok(())}
43    }
44	pub unsafe fn uat(&self, ind: usize) -> $Rbyte {
45            let ptr = $RAW(self.s());
46            *ptr.offset(ind as isize) as $Rbyte
47    }
48    pub unsafe fn uset(&mut self, ind: usize, value: $Rbyte) {
49            let ptr = $RAW(self.s());
50            *ptr.offset(ind as isize) = value as $Rbyte;
51    }
52	pub fn range(&self,  ind: Range<usize>) -> Option<Vec<$Rbyte>> {
53		unsafe{
54        if Rf_xlength(self.s())<= ind.end as R_xlen_t {
55        	return None
56        }
57		let mut vecs = Vec::with_capacity(ind.len() as usize);
58		let ptr = $RAW(self.s());
59		for ii in ind {
60                vecs.push(*ptr.offset(ii as isize) as $Rbyte);
61		}
62        Some(vecs)}
63    }
64	pub fn is_duplicated(&self,from_last:bool)->R_xlen_t{
65    	let last = if from_last {  Rboolean::TRUE} else{Rboolean::FALSE};
66    	unsafe{
67    		Rf_any_duplicated(self.s(),last)
68    	}
69    }
70}
71
72
73impl<T: SEXPbucket, E: Into<$Rbyte> + Clone> From<Vec<E>> for $NumVecM<T> {
74    fn from(x: Vec<E>) -> $NumVecM<T> {
75        let size_x = x.len();
76        unsafe {
77            let rvec = Shield::new(Rf_allocVector($REALSXP, size_x as R_xlen_t));
78            let rptr = $RAW(rvec.s());
79            for ii in 0..size_x {
80                *rptr.offset(ii as isize) = (*x.get_unchecked(ii as usize)).clone().into() as $Rbyte;
81            }
82            $NumVecM { data: T::new(rvec.s()) }
83        }
84    }
85}
86
87impl<T: SEXPbucket> From<$NumVecM<T>> for Vec<$Rbyte> {
88    fn from(x: $NumVecM<T>) -> Vec<$Rbyte> {
89        unsafe {
90            let lens = Rf_xlength(x.s());
91            let mut vecs = Vec::with_capacity(lens as usize);
92            let rptr = $RAW(x.s());
93            for ii in 0..lens {
94                vecs.push(*rptr.offset(ii as isize) as $Rbyte);
95            }
96            vecs
97        }
98    }
99}
100
101
102#[derive(Debug)]
103pub struct $NumVecIter< T: SEXPbucket> {
104    data: *mut $Rbyte,
105    size: R_xlen_t,
106    next: R_xlen_t,
107    ty: $NumVecM<T>
108}
109
110impl<T: SEXPbucket> Iterator for $NumVecIter<T> {
111// we will be counting with usize
112    type Item = $Rbyte;
113
114// next() is the only required method
115    fn next(&mut self) -> Option<$Rbyte> {
116// increment our count. This is why we started at zero.
117// check to see if we've finished counting or not.
118        if self.next < self.size {
119        	self.next += 1;
120        	unsafe{
121            Some(*self.data.offset((self.next-1) as isize))}
122        } else {
123            None
124        }
125
126    }
127}
128
129
130
131impl<T: SEXPbucket> URNew for $NumVecM<T> {
132    unsafe fn urnew(x: SEXP) -> Self {
133		$NumVecM{ data: T::new(x) }
134    }
135}
136
137
138impl<T: SEXPbucket> RNew for $NumVecM<T> {
139	fn rnew(x:SEXP)->RResult<Self>{
140		Self::new(x)
141	}
142}
143
144impl<T: SEXPbucket> RSize for $NumVecM<T>{
145	fn rsize(&self)->R_xlen_t{
146		unsafe{Rf_xlength(self.s())}
147	}
148}
149
150
151impl<T: SEXPbucket> RName for $NumVecM<T>{}
152impl<T: SEXPbucket> RDim for $NumVecM<T>{
153	type Output = $Rbyte;
154}
155
156
157impl<T: SEXPbucket> ExactSizeIterator  for $NumVecIter<T> {
158// We already have the number of iterations, so we can use it directly.
159    fn len(&self) -> usize {
160        self.size as usize
161    }
162}
163
164impl<T: SEXPbucket> IntoIterator for $NumVecM<T>{
165    type Item = $Rbyte;
166    type IntoIter = $NumVecIter<T>;
167
168    fn into_iter(self) -> Self::IntoIter {
169         unsafe{
170         	$NumVecIter{data: $RAW(self.s()),size:self.rsize(),next:0,ty:self}
171    	}
172    }
173
174}
175
176
177
178)
179}
180
181
182pub mod numvec;
183pub mod intvec;
184pub mod boolvec;
185pub mod rawvec;
186pub mod cplvec;
187
188pub use self::numvec::*;
189pub use self::intvec::*;
190pub use self::boolvec::*;
191pub use self::rawvec::*;
192pub use self::cplvec::*;