From 0afd14903127ba73bca9b5a1461c2fec7f4440ef Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 20 Dec 2019 21:39:38 +0000 Subject: Use smallvec instead of custom struct --- dhall/src/syntax/ast/map.rs | 99 ++++++++++----------------------------------- 1 file changed, 22 insertions(+), 77 deletions(-) (limited to 'dhall/src/syntax') diff --git a/dhall/src/syntax/ast/map.rs b/dhall/src/syntax/ast/map.rs index c4c6126..1077dca 100644 --- a/dhall/src/syntax/ast/map.rs +++ b/dhall/src/syntax/ast/map.rs @@ -2,83 +2,26 @@ pub use dup_tree_map::DupTreeMap; pub use dup_tree_set::DupTreeSet; -mod one_or_more { - use either::Either; - use std::{iter, slice, vec}; - - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub enum OneOrMore { - One(T), - More(Vec), - } - - pub type Iter<'a, T> = Either, iter::Once<&'a T>>; - pub type IterMut<'a, T> = - Either, iter::Once<&'a mut T>>; - pub type IntoIter = Either, iter::Once>; - - impl OneOrMore { - pub fn new(x: T) -> Self { - OneOrMore::One(x) - } - - pub fn push(&mut self, x: T) { - take_mut::take(self, |sef| match sef { - OneOrMore::More(mut vec) => { - vec.push(x); - OneOrMore::More(vec) - } - OneOrMore::One(one) => OneOrMore::More(vec![one, x]), - }) - } - - pub fn iter(&self) -> Iter<'_, T> { - match self { - OneOrMore::More(vec) => Either::Left(vec.iter()), - OneOrMore::One(x) => Either::Right(iter::once(x)), - } - } - - pub fn iter_mut(&mut self) -> IterMut<'_, T> { - match self { - OneOrMore::More(vec) => Either::Left(vec.iter_mut()), - OneOrMore::One(x) => Either::Right(iter::once(x)), - } - } - } - - impl IntoIterator for OneOrMore { - type Item = T; - type IntoIter = IntoIter; - - fn into_iter(self) -> Self::IntoIter { - match self { - OneOrMore::More(vec) => Either::Left(vec.into_iter()), - OneOrMore::One(x) => Either::Right(iter::once(x)), - } - } - } -} - mod dup_tree_map { - use super::one_or_more; - use super::one_or_more::OneOrMore; + use smallvec as svec; + use smallvec::smallvec; use std::collections::{btree_map, BTreeMap}; use std::iter; + use std::{slice, vec}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DupTreeMap { - map: BTreeMap>, + map: BTreeMap>, size: usize, } pub type IterInternalIntermediate<'a, K, V> = - iter::Zip, one_or_more::Iter<'a, V>>; + iter::Zip, slice::Iter<'a, V>>; pub type IterInternal<'a, K, V> = iter::FlatMap< - btree_map::Iter<'a, K, OneOrMore>, + btree_map::Iter<'a, K, smallvec::SmallVec<[V; 1]>>, IterInternalIntermediate<'a, K, V>, for<'b> fn( - (&'b K, &'b OneOrMore), + (&'b K, &'b smallvec::SmallVec<[V; 1]>), ) -> IterInternalIntermediate<'b, K, V>, >; pub struct Iter<'a, K, V> { @@ -86,12 +29,12 @@ mod dup_tree_map { size: usize, } pub type IterMutInternalIntermediate<'a, K, V> = - iter::Zip, one_or_more::IterMut<'a, V>>; + iter::Zip, slice::IterMut<'a, V>>; pub type IterMutInternal<'a, K, V> = iter::FlatMap< - btree_map::IterMut<'a, K, OneOrMore>, + btree_map::IterMut<'a, K, smallvec::SmallVec<[V; 1]>>, IterMutInternalIntermediate<'a, K, V>, for<'b> fn( - (&'b K, &'b mut OneOrMore), + (&'b K, &'b mut smallvec::SmallVec<[V; 1]>), ) -> IterMutInternalIntermediate<'b, K, V>, >; pub struct IterMut<'a, K, V> { @@ -99,11 +42,13 @@ mod dup_tree_map { size: usize, } pub type IntoIterInternalIntermediate = - iter::Zip, one_or_more::IntoIter>; + iter::Zip, svec::IntoIter<[V; 1]>>; pub type IntoIterInternal = iter::FlatMap< - btree_map::IntoIter>, + btree_map::IntoIter>, IntoIterInternalIntermediate, - fn((K, OneOrMore)) -> IntoIterInternalIntermediate, + fn( + (K, smallvec::SmallVec<[V; 1]>), + ) -> IntoIterInternalIntermediate, >; pub struct IntoIter { iter: IntoIterInternal, @@ -128,7 +73,7 @@ mod dup_tree_map { use std::collections::btree_map::Entry; match self.map.entry(key) { Entry::Vacant(e) => { - e.insert(OneOrMore::new(value)); + e.insert(smallvec![value]); } Entry::Occupied(mut e) => e.get_mut().push(value), } @@ -147,9 +92,9 @@ mod dup_tree_map { K: Ord, { fn foo<'a, K, V>( - (k, oom): (&'a K, &'a OneOrMore), + (k, vec): (&'a K, &'a smallvec::SmallVec<[V; 1]>), ) -> IterInternalIntermediate<'a, K, V> { - iter::repeat(k).zip(oom.iter()) + iter::repeat(k).zip(vec.iter()) } Iter { iter: self.map.iter().flat_map(foo), @@ -162,9 +107,9 @@ mod dup_tree_map { K: Ord, { fn foo<'a, K, V>( - (k, oom): (&'a K, &'a mut OneOrMore), + (k, vec): (&'a K, &'a mut smallvec::SmallVec<[V; 1]>), ) -> IterMutInternalIntermediate<'a, K, V> { - iter::repeat(k).zip(oom.iter_mut()) + iter::repeat(k).zip(vec.iter_mut()) } IterMut { iter: self.map.iter_mut().flat_map(foo), @@ -191,12 +136,12 @@ mod dup_tree_map { fn into_iter(self) -> Self::IntoIter { fn foo( - (k, oom): (K, OneOrMore), + (k, vec): (K, smallvec::SmallVec<[V; 1]>), ) -> IntoIterInternalIntermediate where K: Clone, { - iter::repeat(k).zip(oom.into_iter()) + iter::repeat(k).zip(vec.into_iter()) } IntoIter { iter: self.map.into_iter().flat_map(foo), -- cgit v1.2.3