salad commit

This commit is contained in:
2022-12-01 21:37:44 +00:00
parent f6ae1e19c0
commit 3557107248
12 changed files with 411 additions and 82 deletions

View File

@@ -17,6 +17,7 @@ pub use side::Side;
pub use merge_sorted::merge_sorted;
pub use iter::BoxedIter;
pub use string_from_charset::string_from_charset;
pub use protomap::ProtoMap;
pub fn mrc_derive<T: ?Sized, P, U: ?Sized>(m: &Mrc<T>, p: P) -> Mrc<U>
where P: for<'a> FnOnce(&'a T) -> &'a U {

View File

@@ -7,12 +7,17 @@ const INLINE_ENTRIES: usize = 2;
/// Linked-array-list of key-value pairs.
/// Lookup and modification is O(n + cachemiss * n / m)
/// Can be extended by reference in O(m) < O(n)
pub struct ProtoMap<'a, K, V> {
entries: SmallVec<[(K, Option<V>); INLINE_ENTRIES]>,
prototype: Option<&'a ProtoMap<'a, K, V>>
///
/// The number of elements stored inline in a stackframe is 2 by default, which is enough for most
/// recursive algorithms. The cost of overruns is a heap allocation and subsequent heap indirections,
/// plus wasted stack space which is likely wasted L1 as well. The cost of underruns is wasted stack
/// space.
pub struct ProtoMap<'a, K, V, const STACK_COUNT: usize = 2> {
entries: SmallVec<[(K, Option<V>); STACK_COUNT]>,
prototype: Option<&'a ProtoMap<'a, K, V, STACK_COUNT>>
}
impl<'a, K, V> ProtoMap<'a, K, V> {
impl<'a, K, V, const STACK_COUNT: usize> ProtoMap<'a, K, V, STACK_COUNT> {
pub fn new() -> Self {
Self {
entries: SmallVec::new(),
@@ -104,7 +109,8 @@ impl<'a, K, V> ProtoMap<'a, K, V> {
}
/// Update the prototype, and correspondingly the lifetime of the map
pub fn set_proto<'b>(self, proto: &'b ProtoMap<'b, K, V>) -> ProtoMap<'b, K, V> {
pub fn set_proto<'b>(self, proto: &'b ProtoMap<'b, K, V, STACK_COUNT>)
-> ProtoMap<'b, K, V, STACK_COUNT> {
ProtoMap {
entries: self.entries,
prototype: Some(proto)

View File

@@ -6,14 +6,16 @@ use std::fmt::Debug;
#[derive(Clone, Copy)]
pub struct Stackframe<'a, T> {
pub item: T,
pub prev: Option<&'a Stackframe<'a, T>>
pub prev: Option<&'a Stackframe<'a, T>>,
pub len: usize
}
impl<'a, T: 'a> Stackframe<'a, T> {
pub fn new(item: T) -> Self {
Self {
item,
prev: None
prev: None,
len: 1
}
}
/// Get the item owned by this listlike, very fast O(1)
@@ -27,9 +29,22 @@ impl<'a, T: 'a> Stackframe<'a, T> {
pub fn push(&self, item: T) -> Stackframe<'_, T> {
Stackframe {
item,
prev: Some(self)
prev: Some(self),
len: self.len + 1
}
}
pub fn opush(prev: &Option<Self>, item: T) -> Option<Self> {
Some(Self {
item,
prev: prev.as_ref(),
len: prev.map_or(1, |s| s.len)
})
}
pub fn len(&self) -> usize { self.len }
pub fn pop(&self, count: usize) -> Option<&Self> {
if count == 0 {Some(self)}
else {self.prev.and_then(|prev| prev.pop(count - 1))}
}
}
impl<'a, T> Debug for Stackframe<'a, T> where T: Debug {
@@ -52,4 +67,4 @@ impl<'a, T> Iterator for StackframeIterator<'a, T> {
self.curr = prev;
Some(item)
}
}
}