rust/vec2d/src/lib.rs
changeset 14032 2869c2ccb1b8
parent 14030 2ebd505e62c1
child 14121 69db1d2e4cec
--- a/rust/vec2d/src/lib.rs	Tue Oct 30 05:55:58 2018 +0300
+++ b/rust/vec2d/src/lib.rs	Tue Oct 30 19:05:52 2018 +0300
@@ -1,10 +1,12 @@
+extern crate integral_geometry;
+
 use std::ops::{Index, IndexMut};
 use std::slice::SliceIndex;
+use integral_geometry::Size;
 
 pub struct Vec2D<T> {
     data: Vec<T>,
-    width: usize,
-    height: usize,
+    size: Size,
 }
 
 impl<T> Index<usize> for Vec2D<T> {
@@ -12,67 +14,70 @@
 
     #[inline]
     fn index(&self, row: usize) -> &[T] {
-        debug_assert!(row < self.height);
+        debug_assert!(row < self.height());
 
-        let pos = row * self.width;
+        let pos = row * self.width();
 
-        &self.data[pos..pos + self.width]
+        &self.data[pos..pos + self.width()]
     }
 }
 
 impl<T> IndexMut<usize> for Vec2D<T> {
     #[inline]
     fn index_mut(&mut self, row: usize) -> &mut [T] {
-        debug_assert!(row < self.height);
+        debug_assert!(row < self.height());
 
-        let pos = row * self.width;
+        let pos = row * self.width();
 
-        &mut self.data[pos..pos + self.width]
+        &mut self.data[pos..pos + self.size.width]
     }
 }
 
-impl<T: Copy> Vec2D<T> {
-    pub fn new(width: usize, height: usize, value: T) -> Self {
-        Self {
-            data: vec![value; width * height],
-            width,
-            height,
-        }
-    }
-
+impl <T> Vec2D<T> {
     #[inline]
     pub fn width(&self) -> usize {
-        self.width
+        self.size.width
     }
 
     #[inline]
     pub fn height(&self) -> usize {
-        self.height
+        self.size.height
+    }
+
+    #[inline]
+    pub fn size(&self) -> Size {
+        self.size
+    }
+}
+
+impl<T: Copy> Vec2D<T> {
+    pub fn new(size: Size, value: T) -> Self {
+        Self { size, data: vec![value; size.area()] }
     }
 
     #[inline]
     pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
-        self.data.get(row * self.width + column)
+        self.data.get(row * self.width() + column)
     }
 
     #[inline]
     pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> {
-        self.data.get_mut(row * self.width + column)
+        self.data.get_mut(row * self.size.width + column)
     }
 
     #[inline]
     pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output {
-        self.data.get_unchecked(row * self.width + column)
+        self.data.get_unchecked(row * self.width() + column)
     }
 
     #[inline]
     pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
-        self.data.get_unchecked_mut(row * self.width + column)
+        self.data.get_unchecked_mut(row * self.size.width + column)
     }
 
     #[inline]
     pub fn rows(&self) -> impl Iterator<Item = &[T]> {
-        self.data.chunks(self.width)
+        self.data.chunks(self.width())
     }
 }
 
@@ -82,10 +87,10 @@
 
     #[test]
     fn basics() {
-        let mut v: Vec2D<u8> = Vec2D::new(2, 3, 0xff);
+        let mut v: Vec2D<u8> = Vec2D::new(Size::new(2, 3), 0xff);
 
-        assert_eq!(v.width, 2);
-        assert_eq!(v.height, 3);
+        assert_eq!(v.width(), 2);
+        assert_eq!(v.height(), 3);
 
         assert_eq!(v[0][0], 0xff);
         assert_eq!(v[2][1], 0xff);