From a8e696f62f14296b94964adb1946d7e2b5ef5ebd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 10 May 2019 19:49:38 +0200 Subject: Write a custom map type that allows duplicates --- dhall_syntax/src/core/map.rs | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 dhall_syntax/src/core/map.rs (limited to 'dhall_syntax/src/core/map.rs') diff --git a/dhall_syntax/src/core/map.rs b/dhall_syntax/src/core/map.rs new file mode 100644 index 0000000..c76ac34 --- /dev/null +++ b/dhall_syntax/src/core/map.rs @@ -0,0 +1,178 @@ +/// A sorted map that allows multiple values for each key. +pub use dup_tree_map::DupTreeMap; + +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 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)), + } + } + } + + 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 std::collections::{btree_map, BTreeMap}; + use std::iter; + + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct DupTreeMap { + map: BTreeMap>, + size: usize, + } + + pub type IterInternal<'a, K, V> = + iter::Zip, one_or_more::Iter<'a, V>>; + pub type Iter<'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>, + >; + pub type IntoIterInternal = + iter::Zip, one_or_more::IntoIter>; + pub type IntoIter = iter::FlatMap< + btree_map::IntoIter>, + IntoIterInternal, + fn((K, OneOrMore)) -> IntoIterInternal, + >; + + impl DupTreeMap { + pub fn new() -> Self + where + K: Ord, + { + DupTreeMap { + map: BTreeMap::new(), + size: 0, + } + } + + pub fn insert(&mut self, key: K, value: V) + where + K: Ord, + { + use std::collections::btree_map::Entry; + match self.map.entry(key) { + Entry::Vacant(e) => { + e.insert(OneOrMore::new(value)); + } + Entry::Occupied(mut e) => e.get_mut().push(value), + } + } + + pub fn len(&self) -> usize { + self.size + } + pub fn is_empty(&self) -> bool { + self.size == 0 + } + + pub fn iter(&self) -> Iter<'_, K, V> + where + K: Ord, + { + fn foo<'a, K, V>( + (k, oom): (&'a K, &'a OneOrMore), + ) -> IterInternal<'a, K, V> { + iter::repeat(k).zip(oom.iter()) + } + self.map.iter().flat_map(foo) + } + } + + impl Default for DupTreeMap + where + K: Ord, + { + fn default() -> Self { + Self::new() + } + } + + impl IntoIterator for DupTreeMap + where + K: Ord + Clone, + { + type Item = (K, V); + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + fn foo((k, oom): (K, OneOrMore)) -> IntoIterInternal + where + K: Clone, + { + iter::repeat(k).zip(oom.into_iter()) + } + self.map.into_iter().flat_map(foo) + } + } + + impl<'a, K, V> IntoIterator for &'a DupTreeMap + where + K: Ord, + { + type Item = (&'a K, &'a V); + type IntoIter = Iter<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } + } + + impl iter::FromIterator<(K, V)> for DupTreeMap + where + K: Ord, + { + fn from_iter(iter: T) -> Self + where + T: IntoIterator, + { + let mut map = DupTreeMap::new(); + for (k, v) in iter { + map.insert(k, v); + } + map + } + } +} -- cgit v1.2.3