Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2020-04-09 11:34:30 -0400
committerMike Gerwitz <mike.gerwitz@ryansg.com>2020-04-28 09:06:25 -0400
commit9220de4769d7f3cfa4e155233b61ebb761a01486 (patch)
tree6600fa68d12962c4302cd71058e42d91abc5d2ef
parent0f423f3b24837cb2d6d5dc254427751ff709ab54 (diff)
downloadtame-9220de4769d7f3cfa4e155233b61ebb761a01486.tar.gz
tame-9220de4769d7f3cfa4e155233b61ebb761a01486.tar.bz2
tame-9220de4769d7f3cfa4e155233b61ebb761a01486.zip
[DEV-7084] TAMER: Finish encapsulating petgraph
This will allow us to migrate away from Petgraph in the future should we choose to do so.
-rw-r--r--tamer/src/ir/asg/base.rs60
-rw-r--r--tamer/src/ir/asg/graph.rs14
-rw-r--r--tamer/src/ir/asg/mod.rs2
-rw-r--r--tamer/src/obj/xmlo/asg_builder.rs5
4 files changed, 28 insertions, 53 deletions
diff --git a/tamer/src/ir/asg/base.rs b/tamer/src/ir/asg/base.rs
index 637c892..dd8d776 100644
--- a/tamer/src/ir/asg/base.rs
+++ b/tamer/src/ir/asg/base.rs
@@ -20,7 +20,7 @@
//! Base concrete [`Asg`] implementation.
use super::graph::{
- Asg, AsgEdge, AsgError, AsgResult, Node, ObjectRef, SortableAsg,
+ Asg, AsgEdge, AsgError, AsgResult, IndexType, Node, ObjectRef, SortableAsg,
};
use super::ident::IdentKind;
use super::object::{
@@ -28,11 +28,8 @@ use super::object::{
};
use super::Sections;
use crate::sym::Symbol;
-use fixedbitset::FixedBitSet;
-use petgraph::graph::{
- DiGraph, EdgeIndex, Graph, IndexType, Neighbors, NodeIndex,
-};
-use petgraph::visit::{DfsPostOrder, GraphBase, IntoNeighbors, Visitable};
+use petgraph::graph::{DiGraph, Graph, NodeIndex};
+use petgraph::visit::DfsPostOrder;
/// Concrete ASG.
///
@@ -136,7 +133,7 @@ where
let index = self.graph.add_node(Some(O::declare(ident)));
self.index_identifier(ident, index);
- ObjectRef(index)
+ ObjectRef::new(index)
})
}
@@ -176,7 +173,7 @@ where
where
F: FnOnce(O) -> TransitionResult<O>,
{
- let node = self.graph.node_weight_mut(identi.0).unwrap();
+ let node = self.graph.node_weight_mut(identi.into()).unwrap();
let obj = node
.take()
@@ -277,7 +274,7 @@ where
#[inline]
fn get<I: Into<ObjectRef<Ix>>>(&self, index: I) -> Option<&O> {
- self.graph.node_weight(index.into().0).map(|node| {
+ self.graph.node_weight(index.into().into()).map(|node| {
node.as_ref()
.expect("internal error: BaseAsg::get missing Node data")
})
@@ -290,16 +287,17 @@ where
self.index
.get(i)
.filter(|ni| ni.index() > 0)
- .map(|ni| ObjectRef(*ni))
+ .map(|ni| ObjectRef::new(*ni))
}
fn add_dep(&mut self, identi: ObjectRef<Ix>, depi: ObjectRef<Ix>) {
- self.graph.update_edge(identi.0, depi.0, Default::default());
+ self.graph
+ .update_edge(identi.into(), depi.into(), Default::default());
}
#[inline]
fn has_dep(&self, ident: ObjectRef<Ix>, dep: ObjectRef<Ix>) -> bool {
- self.graph.contains_edge(ident.0, dep.0)
+ self.graph.contains_edge(ident.into(), dep.into())
}
fn add_dep_lookup(
@@ -310,7 +308,8 @@ where
let identi = self.lookup_or_missing(ident);
let depi = self.lookup_or_missing(dep);
- self.graph.update_edge(identi.0, depi.0, Default::default());
+ self.graph
+ .update_edge(identi.into(), depi.into(), Default::default());
(identi, depi)
}
@@ -367,41 +366,6 @@ where
}
}
-// TODO: encapsulate Petgraph API (N.B. this is untested!)
-impl<'i, O, Ix> Visitable for BaseAsg<O, Ix>
-where
- Ix: IndexType,
-{
- type Map = FixedBitSet;
-
- fn visit_map(&self) -> Self::Map {
- self.graph.visit_map()
- }
-
- fn reset_map(&self, map: &mut Self::Map) {
- self.graph.reset_map(map)
- }
-}
-
-impl<'i, O, Ix> GraphBase for BaseAsg<O, Ix>
-where
- Ix: IndexType,
-{
- type NodeId = NodeIndex<Ix>;
- type EdgeId = EdgeIndex<Ix>;
-}
-
-impl<'a, 'i, O, Ix> IntoNeighbors for &'a BaseAsg<O, Ix>
-where
- Ix: IndexType,
-{
- type Neighbors = Neighbors<'a, AsgEdge, Ix>;
-
- fn neighbors(self, n: Self::NodeId) -> Self::Neighbors {
- self.graph.neighbors(n)
- }
-}
-
#[cfg(test)]
mod test {
use super::super::graph::AsgError;
diff --git a/tamer/src/ir/asg/graph.rs b/tamer/src/ir/asg/graph.rs
index 52078ed..f6f6705 100644
--- a/tamer/src/ir/asg/graph.rs
+++ b/tamer/src/ir/asg/graph.rs
@@ -25,10 +25,14 @@ use super::object::{
};
use super::Sections;
use crate::sym::Symbol;
-use petgraph::graph::{IndexType, NodeIndex};
+use petgraph::graph::NodeIndex;
use std::fmt::Debug;
use std::result::Result;
+/// Datatype representing node and edge indexes.
+pub trait IndexType: petgraph::graph::IndexType {}
+impl<T: petgraph::graph::IndexType> IndexType for T {}
+
/// An abstract semantic graph of [objects][super::object].
///
/// This IR focuses on the definition and manipulation of objects and their
@@ -200,7 +204,13 @@ pub type AsgResult<T, Ix> = Result<T, AsgError<Ix>>;
/// not pointers.
/// See the [module-level documentation][self] for more information.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
-pub struct ObjectRef<Ix>(pub NodeIndex<Ix>);
+pub struct ObjectRef<Ix>(NodeIndex<Ix>);
+
+impl<Ix: IndexType> ObjectRef<Ix> {
+ pub fn new(index: NodeIndex<Ix>) -> Self {
+ Self(index)
+ }
+}
impl<Ix> From<NodeIndex<Ix>> for ObjectRef<Ix>
where
diff --git a/tamer/src/ir/asg/mod.rs b/tamer/src/ir/asg/mod.rs
index d36d4c3..d64024d 100644
--- a/tamer/src/ir/asg/mod.rs
+++ b/tamer/src/ir/asg/mod.rs
@@ -196,7 +196,7 @@ mod ident;
mod object;
mod section;
-pub use graph::{Asg, AsgError, AsgResult, ObjectRef, SortableAsg};
+pub use graph::{Asg, AsgError, AsgResult, IndexType, ObjectRef, SortableAsg};
pub use ident::{DataType, Dim, IdentKind};
pub use object::{
FragmentText, IdentObject, IdentObjectData, IdentObjectState, Source,
diff --git a/tamer/src/obj/xmlo/asg_builder.rs b/tamer/src/obj/xmlo/asg_builder.rs
index ec563ff..6a5047b 100644
--- a/tamer/src/obj/xmlo/asg_builder.rs
+++ b/tamer/src/obj/xmlo/asg_builder.rs
@@ -18,9 +18,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use super::reader::{XmloError, XmloEvent, XmloReader};
-use crate::ir::asg::{Asg, IdentKind, IdentObjectState, ObjectRef, Source};
+use crate::ir::asg::{
+ Asg, IdentKind, IdentObjectState, IndexType, ObjectRef, Source,
+};
use crate::sym::{Interner, Symbol};
-use petgraph::graph::IndexType;
use std::collections::HashSet;
use std::convert::TryInto;
use std::error::Error;