Skip to content

Commit

Permalink
VarId, LinkId
Browse files Browse the repository at this point in the history
  • Loading branch information
zrho committed Dec 10, 2024
1 parent aa73a50 commit e26887e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
10 changes: 5 additions & 5 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct Context<'a> {
/// A map from `NodeId` to the imported `Node`.
nodes: FxHashMap<model::NodeId, Node>,

local_vars: FxHashMap<(model::NodeId, model::VarIndex), LocalVar>,
local_vars: FxHashMap<model::VarId, LocalVar>,

custom_name_cache: FxHashMap<&'a str, (ExtensionId, SmolStr)>,

Expand Down Expand Up @@ -853,7 +853,7 @@ impl<'a> Context<'a> {

for (index, param) in decl.params.iter().enumerate() {
self.local_vars
.insert((node, index as _), LocalVar::new(param.r#type));
.insert(model::VarId(node, index as _), LocalVar::new(param.r#type));
}

for constraint in decl.constraints {
Expand All @@ -866,7 +866,7 @@ impl<'a> Context<'a> {
};

self.local_vars
.get_mut(&(*node, *index))
.get_mut(&model::VarId(*node, *index))
.ok_or_else(|| model::ModelError::InvalidVar(*node, *index))?
.bound = TypeBound::Copyable;
}
Expand All @@ -876,7 +876,7 @@ impl<'a> Context<'a> {

for (index, param) in decl.params.iter().enumerate() {
// NOTE: `PolyFuncType` only has explicit type parameters at present.
let bound = self.local_vars[&(node, index as model::VarIndex)].bound;
let bound = self.local_vars[&model::VarId(node, index as _)].bound;
imported_params.push(self.import_type_param(param.r#type, bound)?);
}

Expand Down Expand Up @@ -943,7 +943,7 @@ impl<'a> Context<'a> {
model::Term::Var { node, index } => {
let var = self
.local_vars
.get(&(*node, *index))
.get(&model::VarId(*node, *index))
.ok_or(model::ModelError::InvalidVar(*node, *index))?;
let decl = self.import_type_param(var.r#type, var.bound)?;
Ok(TypeArg::new_var_use(*index as _, decl))
Expand Down
14 changes: 11 additions & 3 deletions hugr-model/src/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ macro_rules! define_index {
}

define_index! {
/// Index of a node in a hugr graph.
/// Id of a node in a hugr graph.
#[derive(Debug, derive_more::Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NodeId(pub u32);
}
Expand All @@ -145,17 +145,25 @@ define_index! {
}

define_index! {
/// Index of a region in a hugr graph.
/// Id of a region in a hugr graph.
#[derive(Debug, derive_more::Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct RegionId(pub u32);
}

define_index! {
/// Index of a term in a hugr graph.
/// Id of a term in a hugr graph.
#[derive(Debug, derive_more::Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct TermId(pub u32);
}

/// The id of a link consisting of its region and the link index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LinkId(pub RegionId, pub LinkIndex);

/// The id of a variable consisting of its node and the variable index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VarId(pub NodeId, pub VarIndex);

/// A module consisting of a hugr graph together with terms.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct Module<'a> {
Expand Down
10 changes: 5 additions & 5 deletions hugr-model/src/v0/scope/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use indexmap::IndexSet;
use std::hash::BuildHasherDefault;
use thiserror::Error;

use crate::v0::{NodeId, VarIndex};
use crate::v0::{NodeId, VarId, VarIndex};

type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<FxHasher>>;

Expand Down Expand Up @@ -94,9 +94,9 @@ impl<'a> VarTable<'a> {
/// # Panics
///
/// Panics if there are no open scopes.
pub fn is_visible(&self, node: NodeId, index: VarIndex) -> bool {
pub fn is_visible(&self, var: VarId) -> bool {
let scope = self.scopes.last().unwrap();
scope.node == node && index < scope.var_count
scope.node == var.0 && var.1 < scope.var_count
}

/// Insert a new variable into the current scope.
Expand All @@ -108,7 +108,7 @@ impl<'a> VarTable<'a> {
/// # Panics
///
/// Panics if there are no open scopes.
pub fn insert(&mut self, name: &'a str) -> Result<(NodeId, VarIndex), DuplicateVarError<'a>> {
pub fn insert(&mut self, name: &'a str) -> Result<VarId, DuplicateVarError<'a>> {
let scope = self.scopes.last_mut().unwrap();
let inserted = self.vars.insert((scope.node, name));

Expand All @@ -118,7 +118,7 @@ impl<'a> VarTable<'a> {

let var_index = scope.var_count;
scope.var_count += 1;
Ok((scope.node, var_index))
Ok(VarId(scope.node, var_index))
}

/// Reset the variable table to an empty state while preserving the allocations.
Expand Down

0 comments on commit e26887e

Please sign in to comment.