mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
Add a SmallVec data structure that saves a list on the stack
This commit is contained in:
parent
7a77702e78
commit
a003451c1f
5 changed files with 66 additions and 0 deletions
|
@ -15,3 +15,4 @@ hashbrown.workspace = true
|
|||
im-rc.workspace = true
|
||||
im.workspace = true
|
||||
wyhash.workspace = true
|
||||
smallvec.workspace = true
|
||||
|
|
|
@ -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;
|
||||
|
|
61
crates/compiler/collections/src/small_vec.rs
Normal file
61
crates/compiler/collections/src/small_vec.rs
Normal 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))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue