Skip to content

Commit

Permalink
Added core::property::Rooted and acompanying ensurer graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emoun committed Dec 6, 2020
1 parent d20d857 commit 2e44c1f
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 114 deletions.
18 changes: 17 additions & 1 deletion src/core/property/has_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait HasVertex: Graph
///
/// Gives no guarantees on which vertex is returned by any given call to
/// `get_vertex` if the the graph has multiple vertices.
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct HasVertexGraph<C: Ensure>(C);

impl<C: Ensure> Ensure for HasVertexGraph<C>
Expand Down Expand Up @@ -78,6 +78,22 @@ impl_ensurer! {
#[derive(Clone)]
pub struct VertexInGraph<C: Ensure>(C, <C::Graph as Graph>::Vertex);

impl<C: Ensure> VertexInGraph<C>
{
pub fn set_vertex(&mut self, v: impl Borrow<<C::Graph as Graph>::Vertex>) -> Result<(), ()>
{
if self.0.graph().contains_vertex(v.borrow())
{
self.1 = v.borrow().clone();
Ok(())
}
else
{
Err(())
}
}
}

impl<C: Ensure> Debug for VertexInGraph<C>
where
C: Debug,
Expand Down
27 changes: 27 additions & 0 deletions src/core/property/impl_ensurer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,33 @@ macro_rules! impl_properties {
}
}
}

// Rooted
$crate::impl_properties!{
@struct [ $struct ]
@generic [ $($generics)* ]
@delegate [ $delegate_type ]
$(@exclude [ $($exclude_props)* ])?
$(@include [ $($include_props)* ])?
@bounds [
$delegate_type: $crate::core::GraphDerefMut,
<$delegate_type as $crate::core::GraphDeref>::Graph:
$crate::core::property::Rooted,
$($bounds)*
]
@trait_id Rooted [$crate::core::property]
@implement {
delegate::delegate! {
to $crate::core::GraphDeref::graph(&self$($delegate)+) {
fn root(&self) -> Self::Vertex;
}
to $crate::core::GraphDerefMut::graph_mut(&mut self$($delegate)+) {
fn set_root(&mut self, v: impl std::borrow::Borrow<Self::Vertex>)
-> std::result::Result<(), ()>;
}
}
}
}
};

{
Expand Down
4 changes: 3 additions & 1 deletion src/core/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ mod directedness_ensurers;
mod has_vertex;
mod no_loops;
mod reflexive;
mod rooted;
mod subgraph;
mod unilateral;
mod unique;
mod weak;

pub use self::{
acyclic::*, base_props::*, connected::*, directedness_ensurers::*, has_vertex::*,
impl_ensurer::*, no_loops::*, reflexive::*, subgraph::*, unilateral::*, unique::*, weak::*,
impl_ensurer::*, no_loops::*, reflexive::*, rooted::*, subgraph::*, unilateral::*, unique::*,
weak::*,
};
use crate::core::{
proxy::{EdgeProxyGraph, ProxyVertex, VertexProxyGraph},
Expand Down
95 changes: 95 additions & 0 deletions src/core/property/rooted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::core::{
property::{HasVertex, VertexInGraph},
Ensure, Graph, Release,
};
use std::borrow::Borrow;

/// A maker trait for graphs that are rooted.
///
/// A rooted graph has a distinguished node, called the root of the graph.
/// A rooted graph always has a root, which cannot be removed (unless another
/// vertex is designated as the root first).
///
/// Even though rooted graphs always implement `HasVertex`, the `get_vertex`
/// method is not required to always return the root of the graph.
/// To always get the root, the `root` method can be used.
pub trait Rooted: HasVertex
{
/// Returns the root of the graph.
///
/// If the root of the graph changes between successive calls to this
/// method, so does the vertex returned.
/// However, if the root doesn't change, the vertex is guaranteed to be
/// returned by successive calls.
fn root(&self) -> Self::Vertex;

/// Designates the given vertex is the root of the graph.
///
/// Returns error if it was unable to change the root of the graph.
/// E.g. if the given vertex is not in the graph.
fn set_root(&mut self, v: impl Borrow<Self::Vertex>) -> Result<(), ()>;

/// Return true of the given vertex is the root of the graph.
/// Otherwise returns false.
fn is_root(&self, v: impl Borrow<Self::Vertex>) -> bool
{
self.root() == *v.borrow()
}
}

/// Ensures the a specific vertex is the root of the graph.
pub struct RootedGraph<C: Ensure>(VertexInGraph<C>);

impl<C: Ensure> Clone for RootedGraph<C>
where
VertexInGraph<C>: Clone,
{
fn clone(&self) -> Self
{
Self(self.0.clone())
}
}

impl<C: Ensure> Release for RootedGraph<C>
{
type Base = C::Base;
type Ensured = C;
type Payload = (<C::Graph as Graph>::Vertex, C::Payload);

fn release(self) -> (Self::Ensured, <C::Graph as Graph>::Vertex)
{
self.0.release()
}
}

impl<C: Ensure> Ensure for RootedGraph<C>
{
fn ensure_unvalidated(c: Self::Ensured, v: <C::Graph as Graph>::Vertex) -> Self
{
Self(VertexInGraph::ensure_unvalidated(c, v))
}

fn validate(c: &Self::Ensured, p: &<C::Graph as Graph>::Vertex) -> bool
{
VertexInGraph::<C>::validate(c, p)
}
}

impl<C: Ensure> Rooted for RootedGraph<C>
{
fn root(&self) -> Self::Vertex
{
self.0.get_vertex()
}

fn set_root(&mut self, v: impl Borrow<Self::Vertex>) -> Result<(), ()>
{
self.0.set_vertex(v)
}
}

impl_ensurer! {
use<C> RootedGraph<C>: Release, Ensure, Rooted
as (self.0) : VertexInGraph<C>
where C: Ensure
}
111 changes: 0 additions & 111 deletions tests/core/property/has_vertex.rs

This file was deleted.

Loading

0 comments on commit 2e44c1f

Please sign in to comment.