todo: arbitrary ir

This commit is contained in:
2023-12-03 17:32:37 -08:00
parent 93cac44a99
commit 2c2268925a
16 changed files with 298 additions and 163 deletions

View File

@@ -1,6 +1,7 @@
use std::{borrow::Borrow, collections::HashMap, hash::Hash};
/// A version of [`std::collections::HashMap`] with a built-in notion of scope.
#[derive(Clone)]
pub struct ScopedMap<K: Eq + Hash + PartialEq, V> {
scopes: Vec<HashMap<K, V>>,
}
@@ -84,4 +85,24 @@ impl<K: Eq + Hash + PartialEq, V> ScopedMap<K, V> {
pub fn release_scope(&mut self) -> Option<HashMap<K, V>> {
self.scopes.pop()
}
/// Create a new scoped set by mapping over the values of this one.
pub fn map_values<F, W>(self, f: F) -> ScopedMap<K, W>
where
F: Fn(V) -> W,
{
let mut scopes = Vec::with_capacity(self.scopes.len());
for scope in self.scopes {
let mut map = HashMap::with_capacity(scope.len());
for (k, v) in scope {
map.insert(k, f(v));
}
scopes.push(map);
}
ScopedMap { scopes }
}
}