From 05715322ad643b39234e0d2f752acc762e760b35 Mon Sep 17 00:00:00 2001 From: Aria Shrimpton Date: Tue, 19 Mar 2024 22:38:45 +0000 Subject: fix adaptive containers not having all traits --- src/crates/library/src/adaptive.rs | 46 +++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/crates') diff --git a/src/crates/library/src/adaptive.rs b/src/crates/library/src/adaptive.rs index 5f21716..68bd42b 100644 --- a/src/crates/library/src/adaptive.rs +++ b/src/crates/library/src/adaptive.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use crate::traits::{Container, Mapping}; +use crate::traits::{Container, Indexable, Mapping, Stack}; use take_mut::take_or_recover; pub enum AdaptiveContainer { @@ -82,6 +82,50 @@ impl, HI: Default + Container } } +impl< + const THRESHOLD: usize, + LO: Default + Container + Stack, + HI: Default + Container + Stack, + E, + > Stack for AdaptiveContainer +{ + fn push(&mut self, elt: E) { + get_inner!(self, c, c.push(elt)); + if let Self::Low(l, _) = self { + if l.len() < THRESHOLD { + return; + } + // adapt! + take_or_recover(self, Default::default, |old| { + let Self::Low(l, _) = old else { + unreachable!(); + }; + Self::High(HI::from_iter(l)) + }); + } + } + + fn pop(&mut self) -> Option { + get_inner!(self, c, c.pop()) + } +} + +impl, HI: Default + Indexable, E> Indexable + for AdaptiveContainer +{ + fn first(&self) -> Option<&E> { + get_inner!(self, c, c.first()) + } + + fn last(&self) -> Option<&E> { + get_inner!(self, c, c.last()) + } + + fn nth(&self, n: usize) -> Option<&E> { + get_inner!(self, c, c.nth(n)) + } +} + impl Mapping for AdaptiveContainer where -- cgit v1.2.3