Skip to content

Commit

Permalink
[Cider2] Use map for pins (#2244)
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Aug 2, 2024
1 parent 367370a commit 67cc51f
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions interp/src/flatten/structures/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use crate::{
serialization::{DataDump, MemoryDeclaration, PrintCode},
values::Value,
};
use ahash::HashMap;
use ahash::HashSet;
use ahash::HashSetExt;
use ahash::{HashMap, HashMapExt};
use itertools::Itertools;
use owo_colors::OwoColorize;
use slog::warn;
Expand Down Expand Up @@ -226,27 +226,26 @@ impl Debug for CellLedger {

#[derive(Debug, Clone)]
struct PinnedPorts {
port_vals_list: Vec<(GlobalPortIdx, Value)>,
map: HashMap<GlobalPortIdx, Value>,
}

impl PinnedPorts {
pub fn iter(&self) -> impl Iterator<Item = &(GlobalPortIdx, Value)> + '_ {
self.port_vals_list.iter()
pub fn iter(&self) -> impl Iterator<Item = (&GlobalPortIdx, &Value)> + '_ {
self.map.iter()
}

pub fn new() -> Self {
Self {
port_vals_list: vec![],
map: HashMap::new(),
}
}

pub fn push(&mut self, port: GlobalPortIdx, val: Value) {
// linear scan is probably fine here since the list should be relatively small
assert!(
!self.port_vals_list.iter().any(|x| x.0 == port),
"attempting to pin the same port twice"
);
self.port_vals_list.push((port, val));
pub fn insert(&mut self, port: GlobalPortIdx, val: Value) {
self.map.insert(port, val);
}

pub fn remove(&mut self, port: GlobalPortIdx) {
self.map.remove(&port);
}
}

Expand Down Expand Up @@ -1088,11 +1087,9 @@ impl<C: AsRef<Context> + Clone> Environment<C> {
self.ports[*idx].as_option().map(|x| x.val().clone())
}

/// Pins the port with the given name to the given value. This may only be
/// used for input ports on the entrypoint component (excluding the go port)
/// and will panic if used otherwise. Intended for external use. Unrelated
/// to the rust pin.
pub fn pin_value<S: AsRef<str>>(&mut self, port: S, val: Value) {
/// Returns an input port for the entrypoint component. Will error if the
/// port is not found.
fn get_root_input_port<S: AsRef<str>>(&self, port: S) -> GlobalPortIdx {
let string = port.as_ref();

let root = Self::get_root();
Expand All @@ -1102,16 +1099,30 @@ impl<C: AsRef<Context> + Clone> Environment<C> {
let found = def_list.find(|offset| {
let def_idx = self.ctx.as_ref().secondary[ledger.comp_id].port_offset_map[*offset];
self.ctx.as_ref().lookup_name(self.ctx.as_ref().secondary[def_idx].name) == string
}).expect("Could not find port with given name in the entrypoint component's input ports");
}).unwrap_or_else(|| panic!("Could not find port '{string}' in the entrypoint component's input ports"));

assert!(
found != self.ctx.as_ref().primary[ledger.comp_id].go,
"Cannot pin the go port"
);
&ledger.index_bases + found
}

/// Pins the port with the given name to the given value. This may only be
/// used for input ports on the entrypoint component (excluding the go port)
/// and will panic if used otherwise. Intended for external use. Unrelated
/// to the rust pin.
pub fn pin_value<S: AsRef<str>>(&mut self, port: S, val: Value) {
let port = self.get_root_input_port(port);

let go = self.get_comp_go(Self::get_root());
assert!(port != go, "Cannot pin the go port");

let found = &ledger.index_bases + found;
self.pinned_ports.insert(port, val);
}

self.pinned_ports.push(found, val);
/// Unpins the port with the given name. This may only be
/// used for input ports on the entrypoint component (excluding the go port)
/// and will panic if used otherwise. Intended for external use.
pub fn unpin_value<S: AsRef<str>>(&mut self, port: S) {
let port = self.get_root_input_port(port);
self.pinned_ports.remove(port);
}
}

Expand Down Expand Up @@ -1203,6 +1214,13 @@ impl<C: AsRef<Context> + Clone> Simulator<C> {
self.env.pin_value(port, val)
}

/// Unpins the port with the given name. This may only be
/// used for input ports on the entrypoint component (excluding the go port)
/// and will panic if used otherwise. Intended for external use.
pub fn unpin_value<S: AsRef<str>>(&mut self, port: S) {
self.env.unpin_value(port)
}

/// Lookup the value of a port on the entrypoint component by name. Will
/// error if the port is not found.
pub fn lookup_port_from_string(&self, port: &String) -> Option<Value> {
Expand Down

0 comments on commit 67cc51f

Please sign in to comment.