From 19e0ecfdb832985dd833dade252637a760c71e85 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 7 May 2019 22:27:37 +0200 Subject: Replace hashmap-based context with a vec-based one --- dhall/src/core/context.rs | 51 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'dhall') diff --git a/dhall/src/core/context.rs b/dhall/src/core/context.rs index 56dec03..73ad516 100644 --- a/dhall/src/core/context.rs +++ b/dhall/src/core/context.rs @@ -1,6 +1,5 @@ use std::rc::Rc; -use dhall_syntax::context::Context as SimpleContext; use dhall_syntax::{Label, V}; use crate::core::thunk::Thunk; @@ -16,7 +15,7 @@ pub(crate) enum CtxItem { } #[derive(Debug, Clone)] -pub(crate) struct Context(Rc>>); +pub(crate) struct Context(Rc)>>); #[derive(Debug, Clone)] pub(crate) struct NormalizationContext(Context<()>); @@ -26,26 +25,40 @@ pub(crate) struct TypecheckContext(Context); impl Context { pub(crate) fn new() -> Self { - Context(Rc::new(SimpleContext::new())) + Context(Rc::new(Vec::new())) } pub(crate) fn insert_kept(&self, x: &Label, t: T) -> Self where T: Shift + Clone, { - Context(Rc::new(self.0.map(|_, e| e.shift(1, &x.into())).insert( - x.clone(), - CtxItem::Kept(x.into(), t.shift(1, &x.into())), - ))) - } - pub(crate) fn insert_replaced(&self, x: &Label, e: Thunk, t: T) -> Self + let mut vec: Vec<_> = self + .0 + .iter() + .map(|(l, i)| (l.clone(), i.shift(1, &x.into()))) + .collect(); + vec.push((x.clone(), CtxItem::Kept(x.into(), t.shift(1, &x.into())))); + Context(Rc::new(vec)) + } + pub(crate) fn insert_replaced(&self, x: &Label, th: Thunk, t: T) -> Self where T: Clone, { - Context(Rc::new(self.0.insert(x.clone(), CtxItem::Replaced(e, t)))) + let mut vec = self.0.as_ref().clone(); + vec.push((x.clone(), CtxItem::Replaced(th, t))); + Context(Rc::new(vec)) } pub(crate) fn lookup(&self, var: &V