coremem_types: list: switch to 'head'/'tail' terminology

This commit is contained in:
2022-07-20 13:10:14 -07:00
parent dfe27c9b56
commit 415ffb9c4d

View File

@@ -4,28 +4,28 @@ use crate::compound::peano::{P0, Peano, PNext};
struct Null;
#[derive(Copy, Clone, Default, PartialEq)]
struct LLNode<E, N> {
e: E,
struct LLNode<H, T> {
head: H,
// Null or LLNode
next: N,
tail: T,
}
impl<E, N> LLNode<E, N> {
fn new(e: E, next: N) -> Self {
Self { e, next }
impl<H, T> LLNode<H, T> {
fn new(head: H, tail: T) -> Self {
Self { head, tail }
}
fn prepend<P>(self, e: P) -> LLNode<P, Self> {
fn prepend<P>(self, head: P) -> LLNode<P, Self> {
LLNode {
e,
next: self
head,
tail: self,
}
}
}
impl<E> LLNode<E, Null> {
fn new_terminal(e: E) -> Self {
Self::new(e, Null)
impl<H> LLNode<H, Null> {
fn new_terminal(head: H) -> Self {
Self::new(head, Null)
}
}
@@ -105,22 +105,22 @@ impl<T, R, F: Fn(&T) -> R> ElementOp<T> for F {
}
}
impl<Output, F, E, N> ListOp<F, LLNode<E, N>, Output> for LLNode<E, N>
impl<Output, F, H, T> ListOp<F, LLNode<H, T>, Output> for LLNode<H, T>
where
F: ElementOp<E, Output=Output>,
N: ListOp<F, N, Output>
F: ElementOp<H, Output=Output>,
T: ListOp<F, T, Output>
{
fn apply_all(&self, f: F) {
// apply the operation to our element
f.call(&self.e);
f.call(&self.head);
// apply the operation to all tail elements
self.next.apply_all(f);
self.tail.apply_all(f);
}
fn apply_at_rel(&self, i: u32, cur: u32, f: F) -> Output {
if i == cur {
f.call(&self.e)
f.call(&self.head)
} else {
self.next.apply_at_rel(i, cur + 1, f)
self.tail.apply_at_rel(i, cur + 1, f)
}
}
}
@@ -147,27 +147,27 @@ trait IndexOp<List, Index, Elem> {
}
/// zero-index into Self
impl<E, N> IndexOp<LLNode<E, N>, P0, E> for LLNode<E, N>
impl<H, T> IndexOp<LLNode<H, T>, P0, H> for LLNode<H, T>
{
fn get(&self, _idx: P0) -> &E {
&self.e
fn get(&self, _idx: P0) -> &H {
&self.head
}
fn get_mut(&mut self, _idx: P0) -> &mut E {
&mut self.e
fn get_mut(&mut self, _idx: P0) -> &mut H {
&mut self.head
}
}
/// index N+1 into Self given that we know how to index N into Self::next
impl<E, N, P, ElemAtP> IndexOp<LLNode<E, N>, PNext<P>, ElemAtP> for LLNode<E, N>
impl<H, T, P, ElemAtP> IndexOp<LLNode<H, T>, PNext<P>, ElemAtP> for LLNode<H, T>
where
P: Peano,
N: IndexOp<N, P, ElemAtP>
T: IndexOp<T, P, ElemAtP>
{
fn get(&self, _idx: PNext<P>) -> &ElemAtP {
self.next.get(P::default())
self.tail.get(P::default())
}
fn get_mut(&mut self, _idx: PNext<P>) -> &mut ElemAtP {
self.next.get_mut(P::default())
self.tail.get_mut(P::default())
}
}