Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::ops::{Sub, Div, Mul};
- use std::fmt::{Formatter, Display, Result};
- use std::sync::{Arc, Mutex};
- use std::slice::Iter;
- use std::iter::Map;
- #[macro_export] macro_rules! dist {
- ($a:expr, $b:expr) => {(($a*$a + $b*$b) as f32).sqrt()};
- }
- #[derive(Debug, PartialEq, Clone, Copy)]
- pub struct Point<N> {
- pub x: N,
- pub y: N
- }
- impl<T: Display> Display for Point<T> {
- fn fmt(&self, f: &mut Formatter) -> Result {
- // Customize so only `x` and `y` are denoted.
- write!(f, "x: {}, y: {}", self.x, self.y)
- }
- }
- impl<N: Sub<Output = N> + Copy> Sub for Point<N> {
- type Output = Point<N>;
- fn sub(self, other: Point<N>) -> Point<N> {
- Point {
- x: self.x - other.x,
- y: self.y - other.y,
- }
- }
- }
- impl<N: Sub<Output = N> + Copy> Sub<N> for Point<N> {
- type Output = Point<N>;
- fn sub(self, d: N) -> Point<N> {
- Point {
- x: self.x - d,
- y: self.y - d,
- }
- }
- }
- impl<N: Div<Output = N> + Copy> Div<N> for Point<N> {
- type Output = Point<N>;
- fn div(self, d: N) -> Point<N> {
- Point {
- x: self.x/d,
- y: self.y/d,
- }
- }
- }
- #[derive(Debug, Clone)]
- struct OffNode<T> {
- idx: usize,
- pub error: f32,
- pub offset: Point<isize>,
- pub startp: Point<usize>,
- pub ending: Point<usize>,
- children: Option<(usize, usize)>,
- storage: Arc<Mutex<Vec<OffNode<T>>>>,
- pub data_a: Option<Rc<Vec<T>>>,
- pub data_b: Option<Rc<Vec<T>>>
- }
- impl<T> OffNode<T>
- where T: Copy
- {
- pub fn new(data_a: Rc<Vec<T>>, data_b: Rc<Vec<T>>, height: usize, width: usize, storage: Arc<Mutex<Vec<OffNode<T>>>>, idx: usize) -> OffNode<T> {
- OffNode {
- idx: idx,
- error: 0.0,
- offset: Point { x: 0, y: 0 },
- startp: Point { x: 0, y: 0 },
- ending: Point { x: width, y: height },
- children: None,
- storage: storage,
- data_a: Some(data_a.clone()),
- data_b: Some(data_b.clone())
- }
- }
- fn new_child(&self, dw: usize, dh: usize, idx: usize) -> OffNode<T> {
- let half: Point<usize> = (self.ending - self.startp) / 2;
- let offset = self.offset.clone();
- let startp = Point {
- x: self.startp.x + half.x * dw,
- y: self.startp.y + half.y * dh
- };
- let endx = if dw == 1 { self.ending.x } else { self.startp.x + half.x };
- let endy = if dh == 1 { self.ending.y } else { self.startp.y + half.y };
- OffNode {
- idx: idx,
- error: 0.0,
- offset: offset,
- startp: startp,
- ending: Point { x: endx, y: endy },
- children: None,
- storage: self.storage.clone(),
- data_a: None,
- data_b: None
- }
- }
- pub fn new_children(&mut self) {
- let mut storage = self.storage.lock().unwrap();
- let last = storage.len();
- for i in (0..=1) {
- for j in (0..=1) {
- storage.push(self.new_child(i, j, last));
- }
- }
- self.children = Some((last, 4));
- }
- pub fn get_children(&self) -> Vec<OffNode<T>> {
- let storage = self.storage.clone();
- let storage = storage.lock().unwrap();
- let (first, count) = self.children.unwrap_or((0, 0));
- let mut res = vec![];
- for n in storage.iter().skip(first).take(count) {
- res.push(n.clone());
- };
- return res;
- }
- }
- impl<T: Display> Display for OffNode<T> {
- fn fmt(&self, f: &mut Formatter) -> Result {
- write!(f, "offset {}\nstartp {}\nending {}", self.offset, self.startp, self.ending)
- }
- }
- fn main() {
- let dst = dist!(4, 3);
- let mut a = None;
- a.replace(4);
- println!("Hello, world! {} {}", dst, a.unwrap());
- let a = vec![0u8; 999];
- let b = vec![0u8; 999];
- let ar = Rc::new(a);
- let br = Rc::new(b);
- let storage = Arc::new(Mutex::new(vec![]));
- let mut root = OffNode::new(ar.clone(), br.clone(), 512, 512, storage.clone(), 0);
- root.new_children();
- let st = &storage.clone();
- let child = &st.lock().unwrap()[1];
- println!("{}", child);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement