prjunnamed_pattern/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::{
    cell::{Ref, RefCell},
    fmt::{Debug, Display},
};

use prjunnamed_netlist::{CellRef, Const, Design, Net, Trit, Value};

pub trait NetOrValue: Sized + Clone + Debug + Display {
    fn len(&self) -> usize;
    fn iter(&self) -> impl Iterator<Item = Net>;
    fn as_value(&self) -> Value {
        Value::from_iter(self.iter())
    }
    fn as_const(&self) -> Option<Const>;
    fn try_from(other: impl NetOrValue) -> Option<Self>;

    #[must_use]
    fn accumulate(capture: &mut Option<Self>, net: Net) -> bool;
}

impl NetOrValue for Net {
    fn len(&self) -> usize {
        1
    }

    fn as_const(&self) -> Option<Const> {
        Net::as_const(*self).map(|trit| Const::from(trit))
    }

    fn iter(&self) -> impl Iterator<Item = Net> {
        std::iter::once(*self)
    }

    fn try_from(other: impl NetOrValue) -> Option<Self> {
        if other.len() == 1 {
            other.iter().next()
        } else {
            None
        }
    }

    fn accumulate(capture: &mut Option<Self>, net: Net) -> bool {
        match capture {
            Some(captured_net) if *captured_net == net => return true,
            Some(_) => return false,
            None => *capture = Some(net),
        }
        true
    }
}

impl NetOrValue for Value {
    fn len(&self) -> usize {
        Value::len(self)
    }

    fn iter(&self) -> impl Iterator<Item = Net> {
        self.iter()
    }

    fn as_const(&self) -> Option<Const> {
        Value::as_const(self)
    }

    fn try_from(other: impl NetOrValue) -> Option<Self> {
        Some(Value::from_iter(other.iter()))
    }

    fn accumulate(capture: &mut Option<Self>, net: Net) -> bool {
        match capture {
            Some(value) => *value = value.concat(net),
            None => *capture = Some(Value::from(net)),
        }
        true
    }
}

// Used to interpose accesses to the design in order to track which nets were examined.
pub trait DesignDyn {
    fn find_cell(&self, net: Net) -> Result<(CellRef, usize), Trit>;

    fn inner(&self) -> &Design;
}

impl DesignDyn for Design {
    #[inline]
    fn find_cell(&self, net: Net) -> Result<(CellRef, usize), Trit> {
        Design::find_cell(self, net)
    }

    fn inner(&self) -> &Design {
        self
    }
}

pub struct CellCollector<'a> {
    inner: &'a dyn DesignDyn,
    cells: RefCell<Vec<CellRef<'a>>>,
}

impl<'a> CellCollector<'a> {
    pub fn new(design: &'a dyn DesignDyn) -> Self {
        Self { inner: design, cells: RefCell::new(Vec::new()) }
    }

    pub fn cells(&self) -> Ref<[CellRef<'a>]> {
        Ref::map(self.cells.borrow(), |cells| &cells[..])
    }

    pub fn clear(&self) {
        self.cells.borrow_mut().clear();
    }
}

impl DesignDyn for CellCollector<'_> {
    #[inline]
    fn find_cell(&self, net: Net) -> Result<(CellRef, usize), Trit> {
        match self.inner.find_cell(net) {
            Ok((cell_ref, offset)) => {
                self.cells.borrow_mut().push(cell_ref);
                Ok((cell_ref, offset))
            }
            Err(trit) => Err(trit),
        }
    }

    fn inner(&self) -> &Design {
        self.inner.inner()
    }
}