2022-12-10 21:27:33 -05:00
|
|
|
use std::fmt::Debug;
|
2022-12-03 20:38:56 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-12-04 00:02:15 -05:00
|
|
|
#[derive(Debug)]
|
2022-12-03 20:38:56 -05:00
|
|
|
pub struct List<T> {
|
|
|
|
head: Option<Rc<Node<T>>>,
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:02:15 -05:00
|
|
|
impl<T> Clone for List<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
List {
|
|
|
|
head: self.head.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 21:35:30 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-12-03 20:38:56 -05:00
|
|
|
pub struct Node<T> {
|
|
|
|
data: T,
|
|
|
|
parent: Option<Rc<Node<T>>>,
|
|
|
|
}
|
|
|
|
|
2022-12-11 02:07:12 -05:00
|
|
|
impl<T> Node<T> {
|
|
|
|
pub fn get_data(&self) -> &T {
|
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:33 -05:00
|
|
|
impl<T> List<T> {
|
2022-12-03 20:38:56 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
List { head: None }
|
|
|
|
}
|
|
|
|
|
2023-04-10 10:49:28 -04:00
|
|
|
pub fn branch_from(trunk: &Rc<Node<T>>) -> Self {
|
|
|
|
List {
|
|
|
|
head: Some(trunk.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:38:56 -05:00
|
|
|
pub fn push_front(&self, data: T) -> List<T> {
|
|
|
|
List {
|
|
|
|
head: Some(Rc::new(Node {
|
|
|
|
data: data,
|
|
|
|
parent: self.head.clone(),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 21:36:22 -05:00
|
|
|
pub fn pop_front(&mut self) -> (Option<T>, List<T>) {
|
|
|
|
match self.head.take() {
|
2022-12-10 21:27:33 -05:00
|
|
|
None => (None, List::new()),
|
|
|
|
Some(popped_node) => {
|
2022-12-16 00:47:33 -05:00
|
|
|
let extracted_node = match Rc::try_unwrap(popped_node) {
|
|
|
|
Ok(node) => node,
|
2022-12-18 03:04:18 -05:00
|
|
|
Err(_) => panic!("try_unwrap failed on Rc in pop_front on List."),
|
2022-12-16 00:47:33 -05:00
|
|
|
};
|
2022-12-10 21:27:33 -05:00
|
|
|
(
|
|
|
|
Some(extracted_node.data),
|
|
|
|
List {
|
|
|
|
head: extracted_node.parent,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 22:12:13 -05:00
|
|
|
pub fn without_front(&self) -> List<T> {
|
|
|
|
List {
|
2022-12-04 00:02:15 -05:00
|
|
|
head: self.head.as_ref().map(|node| node.parent.clone()).flatten(),
|
2022-12-03 22:12:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 21:06:04 -05:00
|
|
|
pub fn get_data(&self) -> Option<&T> {
|
2022-12-03 21:50:06 -05:00
|
|
|
self.head.as_ref().map(|rc_node| &rc_node.data)
|
2022-12-03 20:38:56 -05:00
|
|
|
}
|
2022-12-03 22:12:13 -05:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.head.is_none()
|
|
|
|
}
|
2022-12-10 22:04:39 -05:00
|
|
|
|
|
|
|
pub fn ptr_eq(&self, other: &List<T>) -> bool {
|
|
|
|
match (self.head.as_ref(), other.head.as_ref()) {
|
|
|
|
(None, None) => true,
|
|
|
|
(None, Some(_)) | (Some(_), None) => false,
|
|
|
|
(Some(me), Some(them)) => Rc::ptr_eq(me, them),
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 02:07:12 -05:00
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &Rc<Node<T>>> {
|
|
|
|
NodeIter {
|
|
|
|
position: &self.head,
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 23:52:52 -05:00
|
|
|
|
|
|
|
pub fn iter_until<'a>(&'a self, other: &'a List<T>) -> impl Iterator<Item = &Rc<Node<T>>> {
|
|
|
|
NodeIterUntil {
|
|
|
|
position: &self.head,
|
|
|
|
stop: &other.head,
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 00:47:33 -05:00
|
|
|
|
|
|
|
pub fn into_iter_until<'a>(self, other: &'a List<T>) -> impl Iterator<Item = T> + 'a {
|
|
|
|
NodeIntoIterUntil {
|
|
|
|
position: self,
|
|
|
|
stop: &other,
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 20:38:56 -05:00
|
|
|
}
|
2022-12-11 01:04:51 -05:00
|
|
|
|
2022-12-11 01:07:16 -05:00
|
|
|
pub struct NodeIter<'a, T> {
|
|
|
|
position: &'a Option<Rc<Node<T>>>,
|
2022-12-11 01:04:51 -05:00
|
|
|
}
|
|
|
|
|
2022-12-11 01:07:16 -05:00
|
|
|
impl<'a, T> Iterator for NodeIter<'a, T> {
|
|
|
|
type Item = &'a Rc<Node<T>>;
|
2022-12-11 01:04:51 -05:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let (return_value, next_position) = match &self.position {
|
|
|
|
None => return None,
|
|
|
|
Some(rc_node) => {
|
2022-12-11 01:07:16 -05:00
|
|
|
let next_position = &rc_node.parent;
|
|
|
|
let return_value = rc_node;
|
2022-12-11 01:04:51 -05:00
|
|
|
(return_value, next_position)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.position = next_position;
|
|
|
|
Some(return_value)
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 23:52:52 -05:00
|
|
|
|
|
|
|
pub struct NodeIterUntil<'a, T> {
|
|
|
|
position: &'a Option<Rc<Node<T>>>,
|
|
|
|
stop: &'a Option<Rc<Node<T>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Iterator for NodeIterUntil<'a, T> {
|
|
|
|
type Item = &'a Rc<Node<T>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match (self.position, self.stop) {
|
|
|
|
(_, None) => {}
|
|
|
|
(None, _) => {}
|
|
|
|
(Some(this_rc), Some(stop_rc)) => {
|
|
|
|
if Rc::ptr_eq(this_rc, stop_rc) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let (return_value, next_position) = match &self.position {
|
|
|
|
None => return None,
|
|
|
|
Some(rc_node) => {
|
|
|
|
let next_position = &rc_node.parent;
|
|
|
|
let return_value = rc_node;
|
|
|
|
(return_value, next_position)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.position = next_position;
|
|
|
|
Some(return_value)
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 00:47:33 -05:00
|
|
|
|
|
|
|
pub struct NodeIntoIterUntil<'a, T> {
|
|
|
|
position: List<T>,
|
|
|
|
stop: &'a List<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Iterator for NodeIntoIterUntil<'a, T> {
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.position.ptr_eq(self.stop) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let (popped_element, new_position) = self.position.pop_front();
|
|
|
|
self.position = new_position;
|
|
|
|
popped_element
|
|
|
|
}
|
|
|
|
}
|