Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::ops::{Sub, Div, Mul};
  3. use std::fmt::{Formatter, Display, Result};
  4. use std::sync::{Arc, Mutex};
  5. use std::slice::Iter;
  6. use std::iter::Map;
  7.  
  8. #[macro_export] macro_rules! dist {
  9. ($a:expr, $b:expr) => {(($a*$a + $b*$b) as f32).sqrt()};
  10. }
  11.  
  12. #[derive(Debug, PartialEq, Clone, Copy)]
  13. pub struct Point<N> {
  14. pub x: N,
  15. pub y: N
  16. }
  17.  
  18. impl<T: Display> Display for Point<T> {
  19. fn fmt(&self, f: &mut Formatter) -> Result {
  20. // Customize so only `x` and `y` are denoted.
  21. write!(f, "x: {}, y: {}", self.x, self.y)
  22. }
  23. }
  24.  
  25.  
  26. impl<N: Sub<Output = N> + Copy> Sub for Point<N> {
  27. type Output = Point<N>;
  28. fn sub(self, other: Point<N>) -> Point<N> {
  29. Point {
  30. x: self.x - other.x,
  31. y: self.y - other.y,
  32. }
  33. }
  34. }
  35.  
  36. impl<N: Sub<Output = N> + Copy> Sub<N> for Point<N> {
  37. type Output = Point<N>;
  38. fn sub(self, d: N) -> Point<N> {
  39. Point {
  40. x: self.x - d,
  41. y: self.y - d,
  42. }
  43. }
  44. }
  45.  
  46. impl<N: Div<Output = N> + Copy> Div<N> for Point<N> {
  47. type Output = Point<N>;
  48. fn div(self, d: N) -> Point<N> {
  49. Point {
  50. x: self.x/d,
  51. y: self.y/d,
  52. }
  53. }
  54. }
  55.  
  56. #[derive(Debug, Clone)]
  57. struct OffNode<T> {
  58. idx: usize,
  59. pub error: f32,
  60. pub offset: Point<isize>,
  61. pub startp: Point<usize>,
  62. pub ending: Point<usize>,
  63. children: Option<(usize, usize)>,
  64. storage: Arc<Mutex<Vec<OffNode<T>>>>,
  65. pub data_a: Option<Rc<Vec<T>>>,
  66. pub data_b: Option<Rc<Vec<T>>>
  67. }
  68.  
  69. impl<T> OffNode<T>
  70. where T: Copy
  71. {
  72. 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> {
  73. OffNode {
  74. idx: idx,
  75. error: 0.0,
  76. offset: Point { x: 0, y: 0 },
  77. startp: Point { x: 0, y: 0 },
  78. ending: Point { x: width, y: height },
  79. children: None,
  80. storage: storage,
  81. data_a: Some(data_a.clone()),
  82. data_b: Some(data_b.clone())
  83. }
  84. }
  85. fn new_child(&self, dw: usize, dh: usize, idx: usize) -> OffNode<T> {
  86. let half: Point<usize> = (self.ending - self.startp) / 2;
  87. let offset = self.offset.clone();
  88. let startp = Point {
  89. x: self.startp.x + half.x * dw,
  90. y: self.startp.y + half.y * dh
  91. };
  92. let endx = if dw == 1 { self.ending.x } else { self.startp.x + half.x };
  93. let endy = if dh == 1 { self.ending.y } else { self.startp.y + half.y };
  94. OffNode {
  95. idx: idx,
  96. error: 0.0,
  97. offset: offset,
  98. startp: startp,
  99. ending: Point { x: endx, y: endy },
  100. children: None,
  101. storage: self.storage.clone(),
  102. data_a: None,
  103. data_b: None
  104. }
  105. }
  106. pub fn new_children(&mut self) {
  107. let mut storage = self.storage.lock().unwrap();
  108. let last = storage.len();
  109. for i in (0..=1) {
  110. for j in (0..=1) {
  111. storage.push(self.new_child(i, j, last));
  112. }
  113. }
  114. self.children = Some((last, 4));
  115. }
  116.  
  117. pub fn get_children(&self) -> Vec<OffNode<T>> {
  118. let storage = self.storage.clone();
  119. let storage = storage.lock().unwrap();
  120. let (first, count) = self.children.unwrap_or((0, 0));
  121. let mut res = vec![];
  122. for n in storage.iter().skip(first).take(count) {
  123. res.push(n.clone());
  124. };
  125. return res;
  126. }
  127.  
  128. }
  129.  
  130. impl<T: Display> Display for OffNode<T> {
  131. fn fmt(&self, f: &mut Formatter) -> Result {
  132. write!(f, "offset {}\nstartp {}\nending {}", self.offset, self.startp, self.ending)
  133. }
  134. }
  135.  
  136.  
  137. fn main() {
  138. let dst = dist!(4, 3);
  139. let mut a = None;
  140. a.replace(4);
  141. println!("Hello, world! {} {}", dst, a.unwrap());
  142. let a = vec![0u8; 999];
  143. let b = vec![0u8; 999];
  144. let ar = Rc::new(a);
  145. let br = Rc::new(b);
  146.  
  147. let storage = Arc::new(Mutex::new(vec![]));
  148. let mut root = OffNode::new(ar.clone(), br.clone(), 512, 512, storage.clone(), 0);
  149. root.new_children();
  150. let st = &storage.clone();
  151. let child = &st.lock().unwrap()[1];
  152. println!("{}", child);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement