[DEV-7084] TAMER: Finish encapsulating petgraph

This will allow us to migrate away from Petgraph in the future should we
choose to do so.
master
Mike Gerwitz 2020-04-09 11:34:30 -04:00
parent 0f423f3b24
commit 9220de4769
4 changed files with 28 additions and 53 deletions

View File

@ -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;

View File

@ -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

View File

@ -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,

View File

@ -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;