From 2755cb01092363062016bc51349870b1330d1d6f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 7 Aug 2019 15:58:57 +0200 Subject: Sort labels in projection expressions --- dhall_syntax/src/core/map.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) (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 63f19cd..6a0ebda 100644 --- a/dhall_syntax/src/core/map.rs +++ b/dhall_syntax/src/core/map.rs @@ -1,5 +1,6 @@ /// A sorted map that allows multiple values for each key. pub use dup_tree_map::DupTreeMap; +pub use dup_tree_set::DupTreeSet; mod one_or_more { use either::Either; @@ -232,3 +233,97 @@ mod dup_tree_map { // unsafe impl iter::TrustedLen for IntoIter {} } + +mod dup_tree_set { + use super::DupTreeMap; + use std::iter; + + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct DupTreeSet { + map: DupTreeMap, + } + + pub type Iter<'a, K> = iter::Map< + super::dup_tree_map::Iter<'a, K, ()>, + for<'b> fn((&'b K, &'b ())) -> &'b K, + >; + pub type IntoIter = + iter::Map, fn((K, ())) -> K>; + + impl DupTreeSet { + pub fn new() -> Self + where + K: Ord, + { + DupTreeSet { + map: DupTreeMap::new(), + } + } + + pub fn len(&self) -> usize { + self.map.len() + } + pub fn is_empty(&self) -> bool { + self.map.is_empty() + } + + pub fn iter(&self) -> Iter<'_, K> + where + K: Ord, + { + fn foo<'a, K>((k, ()): (&'a K, &'a ())) -> &'a K { + k + } + self.map.iter().map(foo) + } + } + + impl Default for DupTreeSet + where + K: Ord, + { + fn default() -> Self { + Self::new() + } + } + + impl IntoIterator for DupTreeSet + where + K: Ord + Clone, + { + type Item = K; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + fn foo((k, ()): (K, ())) -> K { + k + } + self.map.into_iter().map(foo) + } + } + + impl<'a, K> IntoIterator for &'a DupTreeSet + where + K: Ord, + { + type Item = &'a K; + type IntoIter = Iter<'a, K>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } + } + + impl iter::FromIterator for DupTreeSet + where + K: Ord, + { + fn from_iter(iter: T) -> Self + where + T: IntoIterator, + { + let map = iter.into_iter().map(|k| (k, ())).collect(); + DupTreeSet { map } + } + } +} -- cgit v1.2.3