use {crate::CircularList, alloc::boxed::Box, core::ptr};
pub mod cursor;
pub struct ListHead<T> {
next: *const ListHead<T>,
prev: *const ListHead<T>,
value: T,
}
impl<T> ListHead<T> {
pub fn new(val: T) -> Box<Self> {
let mut new = Box::new(Self {
next: ptr::null(),
prev: ptr::null(),
value: val,
});
new.next = &*new;
new.prev = &*new;
new
}
pub fn next(&self) -> *const Self {
self.next
}
pub fn prev(&self) -> *const Self {
self.prev
}
pub fn value(&self) -> &T {
&self.value
}
pub fn value_mut(&mut self) -> &mut T {
&mut self.value
}
unsafe fn __add(new: *mut Self, prev: *mut Self, next: *mut Self) {
unsafe {
(*next).prev = new;
(*new).next = next;
(*new).prev = prev;
(*prev).next = new;
}
}
unsafe fn __del(prev: *mut Self, next: *mut Self) {
unsafe {
(*next).prev = prev;
(*prev).next = next;
}
}
pub unsafe fn del_entry(to_del: *mut Self) -> (*const Self, T) {
let mut next = (*to_del).next;
if to_del as *const _ != next {
unsafe {
Self::__del((*to_del).prev as *mut _, (*to_del).next as *mut _);
}
} else {
next = ptr::null();
}
let to_del = Box::from_raw(to_del);
(next, to_del.value)
}
pub unsafe fn add(&mut self, new: *mut Self) {
unsafe {
Self::__add(new, self.prev as *mut _, self);
}
}
pub unsafe fn add_after(&mut self, new: *mut Self) {
unsafe {
Self::__add(new, self, self.next as *mut _);
}
}
unsafe fn __replace(old: *mut Self, new: *mut Self) {
unsafe {
(*new).next = (*old).next;
(*((*new).next as *mut Self)).prev = new;
(*new).prev = (*old).prev;
(*((*new).prev as *mut Self)).next = new;
}
}
pub unsafe fn swap(entry1: *mut Self, entry2: *mut Self) {
unsafe {
let mut pos = (*entry2).prev;
Self::__del((*entry2).prev as *mut _, (*entry2).next as *mut _);
Self::__replace(entry1, entry2);
if pos == entry1 {
pos = entry2;
}
Self::__add(entry1 as *mut _, pos as *mut _, (*pos).next as *mut _);
}
}
pub unsafe fn move_entry(entry: *mut Self, prev: *mut Self, next: *mut Self) {
unsafe {
Self::__del((*entry).prev as *mut _, (*entry).next as *mut _);
Self::__add(entry, prev, next);
}
}
pub unsafe fn add_list(list: *mut Self, next: *mut Self) {
unsafe {
let last_of_list = (*list).prev as *mut Self;
let prev = (*next).prev as *mut Self;
Self::__del(last_of_list, next);
Self::__del(prev, list);
}
}
pub unsafe fn split(head: *mut Self, new_head: *mut Self) {
unsafe {
let new_tail = (*head).prev as *mut _;
Self::__del((*new_head).prev as *mut Self, head);
Self::__del(new_tail, new_head);
}
}
}
pub struct Iter<'life, T> {
_list: &'life CircularList<T>,
next: *const ListHead<T>,
}
impl<'life, T> Iterator for Iter<'life, T> {
type Item = &'life T;
fn next(&mut self) -> Option<Self::Item> {
let (current, next) = unsafe {
let r = &*self.next;
(&r.value, r.next)
};
self.next = next;
Some(current)
}
}
impl<'life, T> Iter<'life, T> {
pub fn new(list: &'life CircularList<T>) -> Self {
let first = list.head;
Self {
_list: list,
next: first,
}
}
}
pub struct IterMut<'life, T> {
_list: &'life mut CircularList<T>,
next: *mut ListHead<T>,
}
impl<'life, T> Iterator for IterMut<'life, T> {
type Item = &'life mut T;
fn next(&mut self) -> Option<Self::Item> {
let (current, next) = unsafe {
let r = &mut *self.next;
(&mut r.value, r.next as *mut _)
};
self.next = next;
Some(current)
}
}
impl<'life, T> IterMut<'life, T> {
pub fn new(list: &'life mut CircularList<T>) -> Self {
let first = list.head as *mut _;
Self {
_list: list,
next: first,
}
}
}
impl<T: PartialEq> PartialEq for ListHead<T> {
fn eq(&self, other: &Self) -> bool {
self.value.eq(&other.value)
}
}
impl<T: PartialOrd> PartialOrd for ListHead<T> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.value.partial_cmp(&other.value)
}
}