Skip to content

Commit 0178774

Browse files
authored
Leverage hashable NetRef (#10)
1 parent 99e7679 commit 0178774

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/attribute.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bitvec::vec::BitVec;
88
use std::collections::{HashMap, HashSet};
99

1010
use crate::{
11-
circuit::{Identifier, Instantiable},
11+
circuit::Instantiable,
1212
netlist::{NetRef, Netlist},
1313
};
1414

@@ -92,10 +92,10 @@ pub struct AttributeFilter<'a, I: Instantiable> {
9292
_netlist: &'a Netlist<I>,
9393
// The keys to filter by
9494
keys: Vec<AttributeKey>,
95-
// The set of identifiers that have this attribute
96-
set: HashSet<Identifier>,
9795
/// The mapping of netrefs that have this attribute
98-
map: HashMap<Identifier, NetRef<I>>,
96+
map: HashMap<AttributeKey, HashSet<NetRef<I>>>,
97+
/// Contains a dedup collection of all filtered nodes
98+
full_set: HashSet<NetRef<I>>,
9999
}
100100

101101
impl<'a, I> AttributeFilter<'a, I>
@@ -104,32 +104,29 @@ where
104104
{
105105
/// Create a new filter for the netlist
106106
fn new(netlist: &'a Netlist<I>, keys: Vec<AttributeKey>) -> Self {
107-
let mut set = HashSet::new();
108107
let mut map = HashMap::new();
108+
let mut full_set = HashSet::new();
109109
for nr in netlist.objects() {
110110
for attr in nr.attributes() {
111111
if keys.contains(attr.key()) {
112-
if let Some(inst) = nr.get_instance_name() {
113-
set.insert(inst.clone());
114-
map.insert(inst.clone(), nr.clone());
115-
}
116-
for net in nr.nets() {
117-
set.insert(net.get_identifier().clone());
118-
}
112+
map.entry(attr.key().clone())
113+
.or_insert_with(HashSet::new)
114+
.insert(nr.clone());
115+
full_set.insert(nr.clone());
119116
}
120117
}
121118
}
122119
Self {
123120
_netlist: netlist,
124121
keys,
125-
set,
126122
map,
123+
full_set,
127124
}
128125
}
129126

130-
/// Check if an identifier has one of the attributes
131-
pub fn has(&self, id: &Identifier) -> bool {
132-
self.set.contains(id)
127+
/// Check if an node matches any of the filter keys
128+
pub fn has(&self, n: &NetRef<I>) -> bool {
129+
self.map.values().any(|s| s.contains(n))
133130
}
134131

135132
/// Return a slice to the keys that were used for filtering
@@ -144,10 +141,10 @@ where
144141
{
145142
type Item = NetRef<I>;
146143

147-
type IntoIter = std::collections::hash_map::IntoValues<Identifier, NetRef<I>>;
144+
type IntoIter = std::collections::hash_set::IntoIter<NetRef<I>>;
148145

149146
fn into_iter(self) -> Self::IntoIter {
150-
self.map.into_values()
147+
self.full_set.into_iter()
151148
}
152149
}
153150

src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ where
151151
}
152152
}
153153
nodes.reverse();
154+
nodes.dedup();
154155

155156
for node in nodes {
156157
if node.is_an_input() {
157158
comb_depth.insert(node.clone(), 0);
158159
} else {
159-
// TODO(matth2k): get_driver_net() relies on a weak reference. Rewrite without it.
160160
let max_depth: usize = (0..node.get_num_input_ports())
161161
.filter_map(|i| netlist.get_driver(node.clone(), i))
162162
.filter_map(|n| comb_depth.get(&n))

0 commit comments

Comments
 (0)