94 lines
2.8 KiB
Rust
94 lines
2.8 KiB
Rust
use std::rc::{Rc, Weak};
|
|
use std::cell::RefCell;
|
|
use r9_macro::SharedFromSelf;
|
|
use r9_macro_derive::SharedFromSelf;
|
|
|
|
#[derive(Default)]
|
|
#[derive(SharedFromSelf)]
|
|
pub struct ListHead<T> {
|
|
prev: Weak::<RefCell::<ListHead<T>>>,
|
|
next: Weak::<RefCell::<ListHead<T>>>,
|
|
data: Weak::<RefCell::<T>>,
|
|
_self_wp: Weak::<RefCell::<ListHead<T>>>,
|
|
}
|
|
|
|
impl<T> ListHead<T> {
|
|
|
|
fn new(data: Weak::<RefCell::<T>>) -> Rc::<RefCell::<Self>> {
|
|
let this = Rc::new(RefCell::new(ListHead{
|
|
prev: Default::default(),
|
|
next: Default::default(),
|
|
data: data,
|
|
_self_wp: Default::default(),
|
|
}));
|
|
this.borrow_mut()._self_wp = Rc::downgrade(&this);
|
|
this.borrow_mut().init();
|
|
return this;
|
|
}
|
|
|
|
pub fn new_head() -> Rc::<RefCell::<Self>> {
|
|
return Self::new(Default::default());
|
|
}
|
|
|
|
pub fn new_node(data: Weak::<RefCell::<T>>) -> Rc::<RefCell::<Self>> {
|
|
return Self::new(data);
|
|
}
|
|
|
|
fn init(&mut self) {
|
|
self.prev = Rc::downgrade(&self.shared_from_self());
|
|
self.next = Rc::downgrade(&self.shared_from_self());
|
|
}
|
|
|
|
pub fn data(&self) -> Weak::<RefCell::<T>> {
|
|
return self.data.clone();
|
|
}
|
|
|
|
pub fn del_init(&mut self) {
|
|
self.next.upgrade().unwrap().borrow_mut().prev = self.prev.clone();
|
|
self.prev.upgrade().unwrap().borrow_mut().next = self.next.clone();
|
|
self.init();
|
|
}
|
|
|
|
pub fn empty(&self) -> bool {
|
|
return self.next.ptr_eq(&self.prev);
|
|
}
|
|
|
|
pub fn add_tail(&self,
|
|
pnew: &Rc::<RefCell::<ListHead<T>>>) {
|
|
/*
|
|
let prev = &mut head.borrow_mut().prev;
|
|
let next = Rc::downgrade(head);
|
|
|
|
next.upgrade().unwrap().borrow_mut().prev = Rc::downgrade(pnew);
|
|
pnew.borrow_mut().next = next;
|
|
pnew.borrow_mut().prev = prev.clone();
|
|
prev.upgrade().unwrap().borrow_mut().next = Rc::downgrade(pnew);*/
|
|
}
|
|
|
|
pub fn first_entry(&self) -> Weak::<RefCell::<T>> {
|
|
if self.empty() {
|
|
}
|
|
return self.next.upgrade().unwrap().borrow().data();
|
|
}
|
|
|
|
pub fn replace_init(head: &Rc::<RefCell::<ListHead<T>>>,
|
|
pnew: &mut Rc::<RefCell::<ListHead<T>>>) {
|
|
pnew.borrow_mut().next = head.borrow_mut().next.clone();
|
|
pnew.borrow_mut().next.upgrade().unwrap().borrow_mut().prev = Rc::downgrade(pnew);
|
|
pnew.borrow_mut().prev = head.borrow_mut().prev.clone();
|
|
head.borrow_mut().init();
|
|
}
|
|
|
|
pub fn for_each(&self, cb: fn (&Weak::<RefCell::<T>>) -> bool) {
|
|
let mut pos = self.next.clone();
|
|
let self_weak = Rc::downgrade(&self.shared_from_self());
|
|
while !Weak::ptr_eq(&pos, &self_weak) {
|
|
if !cb(&pos.upgrade().unwrap().borrow().data()) {
|
|
break;
|
|
}
|
|
pos = pos.upgrade().unwrap().borrow().next.clone();
|
|
}
|
|
}
|
|
|
|
}
|