use {
crate::CircularList,
alloc::boxed::Box,
core::{cell::UnsafeCell, ptr},
};
pub mod cursor;
struct Pointers<T> {
next: *mut ListHead<T>,
prev: *mut ListHead<T>,
}
impl<T> Default for Pointers<T> {
fn default() -> Self {
Self {
next: ptr::null_mut(),
prev: ptr::null_mut(),
}
}
}
impl<T> Pointers<T> {
fn set(&mut self, next: *mut ListHead<T>, prev: *mut ListHead<T>) {
self.next = next;
self.prev = prev;
}
fn set_both(&mut self, link: *mut ListHead<T>) {
self.next = link;
self.prev = link;
}
fn set_next(&mut self, link: *mut ListHead<T>) {
self.next = link;
}
fn set_prev(&mut self, link: *mut ListHead<T>) {
self.prev = link;
}
}
pub struct ListHead<T> {
pointers: UnsafeCell<Pointers<T>>,
value: T,
}
impl<T> ListHead<T> {
pub fn new(val: T) -> *mut Self {
let mut new = Box::new(Self {
pointers: Default::default(),
value: val,
});
unsafe {
let ptr = &raw mut *new;
new.pointers_mut().set_both(ptr);
}
Box::into_raw(new)
}
unsafe fn pointers(&self) -> &Pointers<T> {
unsafe { &*self.pointers.get() }
}
unsafe fn pointers_mut<'a>(&self) -> &'a mut Pointers<T> {
unsafe { &mut *self.pointers.get() }
}
pub fn next(&self) -> &Self {
unsafe { &*self.pointers().next }
}
pub fn next_mut_ptr(&self) -> *mut Self {
unsafe { self.pointers_mut().next }
}
pub fn next_ptr(&self) -> *const Self {
unsafe { self.pointers().next }
}
pub fn prev_mut_ptr(&self) -> *mut Self {
unsafe { self.pointers_mut().prev }
}
pub fn prev_ptr(&self) -> *const Self {
unsafe { self.pointers().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) {
if prev == next {
unsafe {
(*next).pointers_mut().set_both(new);
}
} else {
unsafe {
(*next).pointers_mut().set_prev(new);
(*prev).pointers_mut().set_next(new);
}
}
unsafe {
(*new).pointers_mut().set(next, prev);
}
}
unsafe fn __del(prev: *mut Self, next: *mut Self) {
unsafe {
(*next).pointers_mut().set_prev(prev);
(*prev).pointers_mut().set_next(next);
}
}
pub unsafe fn del_entry(to_del: *mut Self) -> (*mut Self, T) {
unsafe {
let prev = (*to_del).prev_mut_ptr();
let next = (*to_del).next_mut_ptr();
Self::__del(prev, next);
let to_del = Box::from_raw(to_del);
(next, to_del.value)
}
}
pub unsafe fn add(this: *mut Self, new: *mut Self) {
unsafe {
Self::__add(new, (*this).prev_mut_ptr(), this);
}
}
pub unsafe fn add_after(this: *mut Self, new: *mut Self) {
unsafe {
Self::__add(new, this, (*this).next_mut_ptr());
}
}
unsafe fn __replace(old: *mut Self, new: *mut Self) {
if old == new {
return;
}
unsafe {
(*(*old).next_mut_ptr()).pointers_mut().set_prev(new);
(*(*old).prev_mut_ptr()).pointers_mut().set_next(new);
(*new)
.pointers_mut()
.set((*old).next_mut_ptr(), (*old).prev_mut_ptr());
(*old).pointers_mut().set_both(ptr::null_mut());
}
}
pub unsafe fn swap(entry1: *mut Self, entry2: *mut Self) {
unsafe {
let mut pos = (*entry2).prev_mut_ptr();
Self::__del(pos, (*entry2).next_mut_ptr());
Self::__replace(entry1, entry2);
if pos == entry1 {
pos = entry2;
}
Self::__add(entry1, pos, (*pos).next_mut_ptr());
}
}
pub unsafe fn move_entry(entry: *mut Self, prev: *mut Self, next: *mut Self) {
unsafe {
Self::__del((*entry).prev_mut_ptr(), (*entry).next_mut_ptr());
Self::__add(entry, prev, next);
}
}
pub unsafe fn add_list(list: *mut Self, next: *mut Self) {
unsafe {
let last_of_list = (*list).prev_mut_ptr();
let prev = (*next).prev_mut_ptr();
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_mut_ptr();
Self::__del((*new_head).prev_mut_ptr(), head);
Self::__del(new_tail, new_head);
}
}
pub unsafe fn into_value(this: *mut Self) -> T {
let this = unsafe { Box::from_raw(this) };
this.value
}
}
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;
let next = r.next_mut_ptr();
(r.value_mut(), next)
};
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)
}
}