Skip to content

Commit

Permalink
feat: Add accessors for node index and const values (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q authored Oct 16, 2023
1 parent d411313 commit 1fac65b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use ident::{IdentList, InvalidIdentifier};
pub use rewrite::{Rewrite, SimpleReplacement, SimpleReplacementError};

use portgraph::multiportgraph::MultiPortGraph;
use portgraph::{Hierarchy, NodeIndex, PortMut, UnmanagedDenseMap};
use portgraph::{Hierarchy, PortMut, UnmanagedDenseMap};
use thiserror::Error;

#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -214,6 +214,12 @@ pub trait PortIndex {
fn index(self) -> usize;
}

/// A trait for getting the index of a node.
pub trait NodeIndex {
/// Returns the index of the node.
fn index(self) -> usize;
}

/// A port in the incoming direction.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default, Debug)]
pub struct IncomingPort {
Expand Down Expand Up @@ -355,15 +361,15 @@ impl Hugr {
source = ordered[source.index.index()];
}

let target: Node = NodeIndex::new(position).into();
let target: Node = portgraph::NodeIndex::new(position).into();
if target != source {
self.graph.swap_nodes(target.index, source.index);
self.op_types.swap(target.index, source.index);
self.hierarchy.swap_nodes(target.index, source.index);
rekey(source, target);
}
}
self.root = NodeIndex::new(0);
self.root = portgraph::NodeIndex::new(0);

// Finish by compacting the copy nodes.
// The operation nodes will be left in place.
Expand Down Expand Up @@ -494,6 +500,12 @@ impl TryFrom<Port> for OutgoingPort {
}
}

impl NodeIndex for Node {
fn index(self) -> usize {
self.index.into()
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A DataFlow wire, defined by a Value-kind output port of a node
// Stores node and offset to output port
Expand Down
5 changes: 5 additions & 0 deletions src/std_extensions/arithmetic/float_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl ConstF64 {
pub fn new(value: f64) -> Self {
Self { value }
}

/// Returns the value of the constant
pub fn value(&self) -> f64 {
self.value
}
}

impl KnownTypeConst for ConstF64 {
Expand Down
20 changes: 20 additions & 0 deletions src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ impl ConstIntU {
}
Ok(Self { log_width, value })
}

/// Returns the value of the constant
pub fn value(&self) -> u64 {
self.value
}

/// Returns the number of bits of the constant
pub fn log_width(&self) -> u8 {
self.log_width
}
}

impl ConstIntS {
Expand All @@ -123,6 +133,16 @@ impl ConstIntS {
}
Ok(Self { log_width, value })
}

/// Returns the value of the constant
pub fn value(&self) -> i64 {
self.value
}

/// Returns the number of bits of the constant
pub fn log_width(&self) -> u8 {
self.log_width
}
}

#[typetag::serde]
Expand Down

0 comments on commit 1fac65b

Please sign in to comment.