Add a SmallVec data structure that saves a list on the stack

This commit is contained in:
Ayaz Hafiz 2023-03-30 18:13:52 -05:00
parent 7a77702e78
commit a003451c1f
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
5 changed files with 66 additions and 0 deletions

1
Cargo.lock generated
View file

@ -3193,6 +3193,7 @@ dependencies = [
"hashbrown 0.13.2",
"im",
"im-rc",
"smallvec",
"wyhash",
]

View file

@ -146,6 +146,7 @@ serde-xml-rs = "0.6.0"
serde_json = "1.0.94" # update roc_std/Cargo.toml on change
serial_test = "1.0.0"
signal-hook = "0.3.15"
smallvec = { version = "1.10.0", features = ["const_generics", "const_new"] }
snafu = { version = "0.7.4", features = ["backtraces"] }
static_assertions = "1.1.0" # update roc_std/Cargo.toml on change
strip-ansi-escapes = "0.1.1"

View file

@ -15,3 +15,4 @@ hashbrown.workspace = true
im-rc.workspace = true
im.workspace = true
wyhash.workspace = true
smallvec.workspace = true

View file

@ -6,6 +6,7 @@
pub mod all;
mod reference_matrix;
mod small_string_interner;
mod small_vec;
pub mod soa;
mod vec_map;
mod vec_set;
@ -13,5 +14,6 @@ mod vec_set;
pub use all::{default_hasher, BumpMap, ImEntry, ImMap, ImSet, MutMap, MutSet, SendMap};
pub use reference_matrix::{ReferenceMatrix, Sccs, TopologicalSort};
pub use small_string_interner::SmallStringInterner;
pub use small_vec::SmallVec;
pub use vec_map::VecMap;
pub use vec_set::VecSet;

View file

@ -0,0 +1,61 @@
use std::fmt::Debug;
use smallvec::SmallVec as Vec;
#[derive(Default)]
pub struct SmallVec<T, const N: usize>(Vec<[T; N]>);
impl<T, const N: usize> SmallVec<T, N> {
pub const fn new() -> Self {
Self(Vec::new_const())
}
pub fn push(&mut self, value: T) {
self.0.push(value)
}
pub fn pop(&mut self) -> Option<T> {
self.0.pop()
}
}
impl<T, const N: usize> std::ops::Deref for SmallVec<T, N> {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<T, const N: usize> Debug for SmallVec<T, N>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<T, const N: usize> Clone for SmallVec<T, N>
where
T: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T, const N: usize> IntoIterator for SmallVec<T, N> {
type Item = T;
type IntoIter = <Vec<[T; N]> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<T, const N: usize> FromIterator<T> for SmallVec<T, N> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self(Vec::from_iter(iter))
}
}