Skip to main content

mdnt_support/circuit/
injected.rs

1//! Types for creating IR on the fly during synthesis.
2
3use std::{
4    collections::HashMap,
5    ops::{Deref, DerefMut},
6};
7
8use crate::ir::stmt::IRStmt;
9
10/// Records additional IR that gets added after synthesis.
11pub struct InjectedIR<R, E>(HashMap<R, Vec<IRStmt<(usize, E)>>>);
12
13impl<R, E> InjectedIR<R, E> {
14    /// Adds the IR of the other into self.
15    pub fn combine_ir(&mut self, other: Self)
16    where
17        R: std::hash::Hash + Copy + Eq,
18    {
19        for (region, ir) in other {
20            self.entry(region).or_default().extend(ir);
21        }
22    }
23}
24
25impl<R, E> Deref for InjectedIR<R, E> {
26    type Target = HashMap<R, Vec<IRStmt<(usize, E)>>>;
27
28    fn deref(&self) -> &Self::Target {
29        &self.0
30    }
31}
32
33impl<R, E> DerefMut for InjectedIR<R, E> {
34    fn deref_mut(&mut self) -> &mut Self::Target {
35        &mut self.0
36    }
37}
38
39impl<R, E> Default for InjectedIR<R, E> {
40    fn default() -> Self {
41        Self(Default::default())
42    }
43}
44
45impl<R: std::fmt::Debug, E: std::fmt::Debug> std::fmt::Debug for InjectedIR<R, E> {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "{:?}", self.0)
48    }
49}
50
51impl<R, E> IntoIterator for InjectedIR<R, E> {
52    type Item = (R, Vec<IRStmt<(usize, E)>>);
53
54    type IntoIter = <HashMap<R, Vec<IRStmt<(usize, E)>>> as IntoIterator>::IntoIter;
55
56    fn into_iter(self) -> Self::IntoIter {
57        self.0.into_iter()
58    }
59}
60
61impl<'a, R, E> IntoIterator for &'a InjectedIR<R, E> {
62    type Item = (&'a R, &'a Vec<IRStmt<(usize, E)>>);
63
64    type IntoIter = <&'a HashMap<R, Vec<IRStmt<(usize, E)>>> as IntoIterator>::IntoIter;
65
66    fn into_iter(self) -> Self::IntoIter {
67        (&self.0).into_iter()
68    }
69}
70
71impl<'a, R, E> IntoIterator for &'a mut InjectedIR<R, E> {
72    type Item = (&'a R, &'a mut Vec<IRStmt<(usize, E)>>);
73
74    type IntoIter = <&'a mut HashMap<R, Vec<IRStmt<(usize, E)>>> as IntoIterator>::IntoIter;
75
76    fn into_iter(self) -> Self::IntoIter {
77        (&mut self.0).into_iter()
78    }
79}