Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.86 KB | None | 0 0
  1. use super::item::Item;
  2. use super::node::Node;
  3.  
  4. use std::fmt::Display;
  5. use std::mem;
  6. use std::rc::Rc;
  7.  
  8. pub fn insert_item<T>(start: &mut Node<T>, val: T, insert_before: fn(&T, &Item<T>) -> bool)
  9.     where T: Display
  10. {
  11.     println!("Creating item: {}", val);
  12.     let mut current = mem::take(start);
  13.     let mut previous = Node::empty();
  14.  
  15.     while let Some(x) = mem::take(&mut current.value) {
  16.         match insert_before(&val, &x) {
  17.             true => break,
  18.             false => {
  19.                 if let Ok(mut a) = Rc::try_unwrap(x) {
  20.                     previous = current;
  21.                     current = mem::take(&mut a.next);
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     let item = Node::new(Item::new(val, current));
  27.  
  28.     match previous.value {
  29.         Some(x) => {
  30.             if let Ok(mut a) = Rc::try_unwrap(x) {
  31.                 a.next = item
  32.             }
  33.         },
  34.         None => *start = item,
  35.     }
  36. }
  37.  
  38. pub fn remove_item<T>(start: &mut Node<T>, val: T, value_equals: fn(&Item<T>, &T) -> bool)
  39.     where T: Display
  40. {
  41.     let mut current = start.clone();
  42.     let mut previous = Node::empty();
  43.  
  44.     while let Some(x) = mem::take(&mut current.value) {
  45.         match value_equals(&x, &val) {
  46.             true => break,
  47.             false => {
  48.                 if let Ok(mut a) = Rc::try_unwrap(x) {
  49.                     previous = current;
  50.                     current = mem::take(&mut a.next);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.    
  56.     match current.value {
  57.         Some(x) => {
  58.             if let Ok(mut a) = Rc::try_unwrap(x) {
  59.                 let next = mem::take(&mut a.next);
  60.                 match previous.value {
  61.                     Some(y) => {
  62.                         if let Ok(mut b) = Rc::try_unwrap(y) {
  63.                             b.next = next
  64.                         }
  65.                     },
  66.                     None => *start = next,
  67.                 }
  68.             }
  69.         }
  70.         None => println!("Item {} does not exist!", val),
  71.     }
  72. }
  73.  
  74. pub fn remove_all<T>(start: &mut Node<T>)
  75.     where T: Display
  76. {
  77.     *start = Node::empty();
  78. }
  79.  
  80. pub fn print_list<T>(start: &Node<T>)
  81.     where T: Display
  82. {
  83.     let mut current = start.clone();
  84.     while let Some(x) = &current.value {
  85.         current = x.print_get_next()
  86.     }
  87. }
  88.  
  89. pub fn print_iterator<T>(start: &Node<T>)
  90.     where T: Display
  91. {
  92.     struct ItemIterator<T> where T: Display {
  93.         pub item: Node<T>
  94.     }
  95.  
  96.     impl<T> Iterator for ItemIterator<T> where T: Display {
  97.         type Item = Rc<Item<T>>;
  98.  
  99.         fn next(&mut self) -> Option<Rc<Item<T>>> {
  100.             let mut current;
  101.             current = match &self.item.value {
  102.                 Some(x) => x.next.clone(),
  103.                 None => Node::empty(),
  104.             };
  105.             mem::swap(&mut current, &mut self.item);
  106.             current.value
  107.         }
  108.     }
  109.  
  110.     for item in (ItemIterator { item: start.clone() }) {
  111.         item.print_get_next();
  112.     }
  113. }
  114.  
  115. pub fn print_array<T>(start: &Node<T>)
  116.     where T: Display
  117. {
  118.     if start.val_some() {
  119.         let mut item = start.clone();
  120.         let mut i = 0;
  121.         while let Some(_) = &item.value {
  122.             let elem = &start[i];
  123.             item = match &elem.value {
  124.                 Some(x) => x.print_get_next(),
  125.                 None => Node::empty(),
  126.             };
  127.             i += 1
  128.         }
  129.     }
  130. }
  131.  
  132. pub fn print_recursive<T>(start: &Node<T>)
  133.     where T: Display
  134. {
  135.     match &start.value {
  136.         Some(x) => print_recursive(&x.print_get_next()),
  137.         None => { },
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement