add Mat::from_exact_iter()
MODIFY
src/hub/manual/core/mat.rs
src/hub/manual/core/mat.rs
@@ -127,6 +127,15 @@ impl Mat {
unsafe { &mut *(r as *mut _ as *mut T) }
}
+ /// Create new `Mat` from the iterator of known size
+ pub fn from_exact_iter<T: DataType>(s: impl ExactSizeIterator<Item=T>) -> Result<Self> {
+ let mut out = unsafe { Self::new_rows_cols(s.len() as _, 1, T::typ()) }?;
+ for (i, x) in s.enumerate() {
+ *out.at_2d_mut::<T>(i as _, 0)? = x;
+ }
+ Ok(out)
+ }
+
#[inline(always)]
pub(crate) fn _at<T: DataType>(&self, i0: i32) -> Result<&T> {
self.match_format::<T>()

MODIFY
tests/mat.rs
tests/mat.rs
@@ -41,6 +41,35 @@ fn mat_create() -> Result<()> {
Ok(())
}
+#[test]
+fn mat_from_iter() -> Result<()> {
+ {
+ let mut vec = VectorOfint::new();
+ vec.push(1);
+ vec.push(2);
+ vec.push(3);
+ let mat = Mat::from_exact_iter(vec.into_iter())?;
+ assert_eq!(3, mat.rows()?);
+ assert_eq!(1, mat.cols()?);
+ assert_eq!(i32::typ(), mat.typ()?);
+ assert_eq!(1, *mat.at_2d::<i32>(0, 0)?);
+ assert_eq!(2, *mat.at_2d::<i32>(1, 0)?);
+ assert_eq!(3, *mat.at_2d::<i32>(2, 0)?);
+ }
+
+ {
+ let vec: Vec<i32> = vec![1, 2, 3];
+ let mat = Mat::from_exact_iter(vec.into_iter())?;
+ assert_eq!(3, mat.rows()?);
+ assert_eq!(1, mat.cols()?);
+ assert_eq!(i32::typ(), mat.typ()?);
+ assert_eq!(1, *mat.at_2d::<i32>(0, 0)?);
+ assert_eq!(2, *mat.at_2d::<i32>(1, 0)?);
+ assert_eq!(3, *mat.at_2d::<i32>(2, 0)?);
+ }
+ Ok(())
+}
+
#[test]
fn mat_for_rows_and_cols() -> Result<()> {
let mat = unsafe { Mat::new_rows_cols(400, 300, Vec3d::typ()) }?;

add Mat::from_exact_iter()