todo: arbitrary ir
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user