prjunnamed_netlist/cell/
instance.rs1use std::{collections::BTreeMap, ops::Range};
2
3use crate::{IoValue, Net, ParamValue, Value};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct Instance {
7 pub kind: String,
8 pub params: BTreeMap<String, ParamValue>,
9 pub inputs: BTreeMap<String, Value>,
10 pub outputs: BTreeMap<String, Range<usize>>,
11 pub ios: BTreeMap<String, IoValue>,
12}
13
14impl Instance {
15 pub fn new(kind: impl Into<String>) -> Self {
16 Instance {
17 kind: kind.into(),
18 params: Default::default(),
19 inputs: Default::default(),
20 outputs: Default::default(),
21 ios: Default::default(),
22 }
23 }
24
25 pub fn output_len(&self) -> usize {
26 self.outputs.values().map(|range| range.len()).sum()
27 }
28
29 pub fn get_param_string(&self, name: &str) -> Option<&str> {
30 let val = self.params.get(name)?;
31 if let ParamValue::String(val) = val { Some(val) } else { None }
32 }
33
34 pub fn add_param(&mut self, name: impl Into<String>, value: impl Into<ParamValue>) {
35 self.params.insert(name.into(), value.into());
36 }
37
38 pub fn rename_param(&mut self, name_from: impl AsRef<str>, name_to: impl Into<String>) {
39 if let Some(param) = self.params.remove(name_from.as_ref()) {
40 assert!(self.params.insert(name_to.into(), param).is_none());
41 }
42 }
43
44 pub fn rename_input(&mut self, name_from: impl AsRef<str>, name_to: impl Into<String>) {
45 if let Some(input) = self.inputs.remove(name_from.as_ref()) {
46 assert!(self.inputs.insert(name_to.into(), input).is_none());
47 }
48 }
49
50 pub fn rename_output(&mut self, name_from: impl AsRef<str>, name_to: impl Into<String>) {
51 if let Some(output) = self.outputs.remove(name_from.as_ref()) {
52 assert!(self.outputs.insert(name_to.into(), output).is_none());
53 }
54 }
55
56 pub fn rename_io(&mut self, name_from: impl AsRef<str>, name_to: impl Into<String>) {
57 if let Some(io) = self.ios.remove(name_from.as_ref()) {
58 assert!(self.ios.insert(name_to.into(), io).is_none());
59 }
60 }
61
62 pub fn visit(&self, mut f: impl FnMut(Net)) {
63 for val in self.inputs.values() {
64 val.visit(&mut f);
65 }
66 }
67
68 pub fn visit_mut(&mut self, mut f: impl FnMut(&mut Net)) {
69 for val in self.inputs.values_mut() {
70 val.visit_mut(&mut f);
71 }
72 }
73}