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; struct Null;
#[derive(Copy, Clone, Default, PartialEq)] #[derive(Copy, Clone, Default, PartialEq)]
struct LLNode<E, N> { struct LLNode<H, T> {
e: E, head: H,
// Null or LLNode // Null or LLNode
next: N, tail: T,
} }
impl<E, N> LLNode<E, N> { impl<H, T> LLNode<H, T> {
fn new(e: E, next: N) -> Self { fn new(head: H, tail: T) -> Self {
Self { e, next } Self { head, tail }
} }
fn prepend<P>(self, e: P) -> LLNode<P, Self> { fn prepend<P>(self, head: P) -> LLNode<P, Self> {
LLNode { LLNode {
e, head,
next: self tail: self,
} }
} }
} }
impl<E> LLNode<E, Null> { impl<H> LLNode<H, Null> {
fn new_terminal(e: E) -> Self { fn new_terminal(head: H) -> Self {
Self::new(e, Null) 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 where
F: ElementOp<E, Output=Output>, F: ElementOp<H, Output=Output>,
N: ListOp<F, N, Output> T: ListOp<F, T, Output>
{ {
fn apply_all(&self, f: F) { fn apply_all(&self, f: F) {
// apply the operation to our element // apply the operation to our element
f.call(&self.e); f.call(&self.head);
// apply the operation to all tail elements // 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 { fn apply_at_rel(&self, i: u32, cur: u32, f: F) -> Output {
if i == cur { if i == cur {
f.call(&self.e) f.call(&self.head)
} else { } 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 /// 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 { fn get(&self, _idx: P0) -> &H {
&self.e &self.head
} }
fn get_mut(&mut self, _idx: P0) -> &mut E { fn get_mut(&mut self, _idx: P0) -> &mut H {
&mut self.e &mut self.head
} }
} }
/// index N+1 into Self given that we know how to index N into Self::next /// 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 where
P: Peano, P: Peano,
N: IndexOp<N, P, ElemAtP> T: IndexOp<T, P, ElemAtP>
{ {
fn get(&self, _idx: PNext<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 { fn get_mut(&mut self, _idx: PNext<P>) -> &mut ElemAtP {
self.next.get_mut(P::default()) self.tail.get_mut(P::default())
} }
} }