From c2b4a2d9b40efbe4f6cb6fd04f6cb90639f4985f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 12 May 2019 18:44:28 +0200 Subject: Implement binary encoding Closes #39 --- dhall_syntax/src/core/map.rs | 79 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 12 deletions(-) (limited to 'dhall_syntax/src/core/map.rs') diff --git a/dhall_syntax/src/core/map.rs b/dhall_syntax/src/core/map.rs index e5b399e..63f19cd 100644 --- a/dhall_syntax/src/core/map.rs +++ b/dhall_syntax/src/core/map.rs @@ -62,20 +62,30 @@ mod dup_tree_map { size: usize, } - pub type IterInternal<'a, K, V> = + pub type IterInternalIntermediate<'a, K, V> = iter::Zip, one_or_more::Iter<'a, V>>; - pub type Iter<'a, K, V> = iter::FlatMap< + pub type IterInternal<'a, K, V> = iter::FlatMap< btree_map::Iter<'a, K, OneOrMore>, - IterInternal<'a, K, V>, - for<'b> fn((&'b K, &'b OneOrMore)) -> IterInternal<'b, K, V>, + IterInternalIntermediate<'a, K, V>, + for<'b> fn( + (&'b K, &'b OneOrMore), + ) -> IterInternalIntermediate<'b, K, V>, >; - pub type IntoIterInternal = + pub struct Iter<'a, K, V> { + iter: IterInternal<'a, K, V>, + size: usize, + } + pub type IntoIterInternalIntermediate = iter::Zip, one_or_more::IntoIter>; - pub type IntoIter = iter::FlatMap< + pub type IntoIterInternal = iter::FlatMap< btree_map::IntoIter>, - IntoIterInternal, - fn((K, OneOrMore)) -> IntoIterInternal, + IntoIterInternalIntermediate, + fn((K, OneOrMore)) -> IntoIterInternalIntermediate, >; + pub struct IntoIter { + iter: IntoIterInternal, + size: usize, + } impl DupTreeMap { pub fn new() -> Self @@ -115,10 +125,13 @@ mod dup_tree_map { { fn foo<'a, K, V>( (k, oom): (&'a K, &'a OneOrMore), - ) -> IterInternal<'a, K, V> { + ) -> IterInternalIntermediate<'a, K, V> { iter::repeat(k).zip(oom.iter()) } - self.map.iter().flat_map(foo) + Iter { + iter: self.map.iter().flat_map(foo), + size: self.size, + } } } @@ -139,13 +152,18 @@ mod dup_tree_map { type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { - fn foo((k, oom): (K, OneOrMore)) -> IntoIterInternal + fn foo( + (k, oom): (K, OneOrMore), + ) -> IntoIterInternalIntermediate where K: Clone, { iter::repeat(k).zip(oom.into_iter()) } - self.map.into_iter().flat_map(foo) + IntoIter { + iter: self.map.into_iter().flat_map(foo), + size: self.size, + } } } @@ -176,4 +194,41 @@ mod dup_tree_map { map } } + + impl<'a, K, V> Iterator for Iter<'a, K, V> { + type Item = (&'a K, &'a V); + + fn next(&mut self) -> Option { + let next = self.iter.next(); + if next.is_some() { + self.size -= 1; + } + next + } + + fn size_hint(&self) -> (usize, Option) { + (self.size, Some(self.size)) + } + } + + impl Iterator for IntoIter + where + K: Clone, + { + type Item = (K, V); + + fn next(&mut self) -> Option { + let next = self.iter.next(); + if next.is_some() { + self.size -= 1; + } + next + } + + fn size_hint(&self) -> (usize, Option) { + (self.size, Some(self.size)) + } + } + + // unsafe impl iter::TrustedLen for IntoIter {} } -- cgit v1.2.3