Skip to content

Commit 15a6d9a

Browse files
committed
Ensurers without a payload are now 'Guards'.
Updated ensurance system method names so they no longer clash.
1 parent 1f361ae commit 15a6d9a

39 files changed

+244
-233
lines changed

src/algo/tarjan_scc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::{
5353
core::{
5454
property::{ConnectedGraph, HasVertex},
5555
proxy::SubgraphProxy,
56-
Directed, EnsureUnloaded, Graph,
56+
Directed, Graph, Guard,
5757
},
5858
};
5959
use std::cmp::min;
@@ -82,7 +82,7 @@ use std::cmp::min;
8282
/// # algo::TarjanScc,
8383
/// # common::AdjListGraph,
8484
/// # core::{
85-
/// # EnsureUnloaded,
85+
/// # Guard,
8686
/// # property::{
8787
/// # NewVertex, AddEdge, HasVertexGraph, Subgraph
8888
/// # }
@@ -106,7 +106,7 @@ use std::cmp::min;
106106
/// graph.add_edge(&v0,&v2).unwrap();
107107
///
108108
/// // We use `HasVertexGraph` because we don't care where we start
109-
/// let graph = HasVertexGraph::ensure(graph).unwrap();
109+
/// let graph = HasVertexGraph::guard(graph).unwrap();
110110
///
111111
/// // Initialize algorithm
112112
/// let mut tarj = TarjanScc::new(&graph);
@@ -253,7 +253,7 @@ where
253253
}
254254

255255
return Some(
256-
ConnectedGraph::ensure(scc)
256+
ConnectedGraph::guard(scc)
257257
.expect("Tarjans algorithm produced non-strongly-connected subgraph"),
258258
);
259259
// return Some(ConnectedGraph::new(scc));

src/common/ensured.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<G: Ensure> EnsuredGraph<G>
2020
{
2121
if self.0.graph().contains_vertex(v)
2222
{
23-
Some(VertexInGraph::ensure_unvalidated(self.0, v))
23+
Some(VertexInGraph::ensure_unchecked(self.0, v))
2424
}
2525
else
2626
{
@@ -39,7 +39,7 @@ where
3939
) -> Result<VertexInGraph<G>, ()>
4040
{
4141
let v = self.0.graph_mut().new_vertex_weighted(w)?;
42-
Ok(VertexInGraph::ensure_unvalidated(self.0, v))
42+
Ok(VertexInGraph::ensure_unchecked(self.0, v))
4343
}
4444

4545
pub fn new_vertex(self) -> Result<VertexInGraph<G>, ()>

src/core/ensure.rs

+65-46
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ pub trait BaseGraph: Sized + GraphDeref
5858
G::ensure_all(self, p)
5959
}
6060
}
61-
pub trait BaseGraphUnloaded: BaseGraph
61+
pub trait BaseGraphGuard: BaseGraph
6262
{
63-
fn ensure_all<G>(self) -> Result<G, ()>
63+
fn guard_all<G>(self) -> Result<G, ()>
6464
where
6565
G: Ensure<Base = Self>,
6666
G::Payload: Payload<(), Item = ()>,
@@ -72,26 +72,26 @@ pub trait BaseGraphUnloaded: BaseGraph
7272
/// An implementing type ensures a base graph implementation.
7373
///
7474
/// Multiple levels of ensurers are supported.
75-
pub trait Ensure: Release
75+
pub trait Ensure: ReleasePayload
7676
{
77-
fn ensure_unvalidated(
77+
fn ensure_unchecked(
7878
c: Self::Ensured,
79-
p: <Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
79+
p: <Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
8080
) -> Self;
8181

82-
fn validate(
82+
fn can_ensure(
8383
c: &Self::Ensured,
84-
p: &<Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
84+
p: &<Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
8585
) -> bool;
8686

8787
fn ensure(
8888
c: Self::Ensured,
89-
p: <Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
89+
p: <Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
9090
) -> Result<Self, ()>
9191
{
92-
if Self::validate(&c, &p)
92+
if Self::can_ensure(&c, &p)
9393
{
94-
Ok(Self::ensure_unvalidated(c, p))
94+
Ok(Self::ensure_unchecked(c, p))
9595
}
9696
else
9797
{
@@ -105,89 +105,108 @@ pub trait Ensure: Release
105105
Self::ensure(Self::Ensured::ensure_all(g, rest)?, p)
106106
}
107107
}
108-
pub trait EnsureUnloaded: Ensure
108+
pub trait Guard: Ensure
109109
where
110-
<Self as Release>::Payload:
111-
Payload<<<Self as Release>::Ensured as Release>::Payload, Item = ()>,
110+
<Self as ReleasePayload>::Payload:
111+
Payload<<<Self as ReleasePayload>::Ensured as ReleasePayload>::Payload, Item = ()>,
112112
{
113-
fn ensure_unvalidated(c: Self::Ensured) -> Self
113+
fn guard_unchecked(c: Self::Ensured) -> Self
114114
{
115-
<Self as Ensure>::ensure_unvalidated(c, ())
115+
<Self as Ensure>::ensure_unchecked(c, ())
116116
}
117-
fn validate(c: &Self::Ensured) -> bool
117+
fn can_guard(c: &Self::Ensured) -> bool
118118
{
119-
<Self as Ensure>::validate(c, &())
119+
<Self as Ensure>::can_ensure(c, &())
120120
}
121-
fn ensure(c: Self::Ensured) -> Result<Self, ()>
121+
fn guard(c: Self::Ensured) -> Result<Self, ()>
122122
{
123123
<Self as Ensure>::ensure(c, ())
124124
}
125-
fn ensure_all(g: Self::Base) -> Result<Self, ()>
125+
fn guard_all(g: Self::Base) -> Result<Self, ()>
126126
where
127-
<Self as Release>::Payload: Payload<(), Item = ()>,
127+
<Self as ReleasePayload>::Payload: Payload<(), Item = ()>,
128128
{
129-
Ensure::ensure_all(g, <<Self as Release>::Payload>::new((), ()))
129+
Ensure::ensure_all(g, <<Self as ReleasePayload>::Payload>::new((), ()))
130130
}
131131
}
132132

133-
pub trait Release: Sized + GraphDeref
133+
/// Trait for remove one or more layers of ensurers, aka. releasing the
134+
/// properties.
135+
///
136+
/// A _layer_ is an ensurer that ensures some property holds.
137+
/// The base graph does not count as a layer, the ensurer wrapping the base
138+
/// graph is therefore the first layer. Each layer may need a payload. For
139+
/// example, an ensurer guaranteeing that a given vertex exists may have the
140+
/// vertex as a payload.
141+
///
142+
pub trait ReleasePayload: Sized + GraphDeref
134143
{
135144
/// The base graph implementation being ensured
136145
type Base: BaseGraph;
137146

138-
/// The next level of properties.
147+
/// The inner ensurer being further ensured.
139148
type Ensured: Ensure<Base = Self::Base>;
140-
type Payload: Payload<<Self::Ensured as Release>::Payload>;
141149

142-
/// Release only this level's properties, maintaining
143-
/// the next level's properties
150+
/// The payload used to ensure this property holds.
151+
type Payload: Payload<<Self::Ensured as ReleasePayload>::Payload>;
152+
153+
/// ReleasePayload only this level's properties, maintaining
154+
/// the next level's properties and returning the payload released
144155
fn release(
145156
self,
146157
) -> (
147158
Self::Ensured,
148-
<Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
159+
<Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
149160
);
150161

151-
/// Fully release this type, returning the base graph implementation
152-
/// type
162+
/// Fully release all ensurers, returning the base graph and the payload for
163+
/// all levels
153164
fn release_all(self) -> (Self::Base, Self::Payload)
154165
{
155166
let (ensured, payload) = self.release();
156-
let (base, payload_rest) = Release::release_all(ensured);
167+
let (base, payload_rest) = ReleasePayload::release_all(ensured);
157168
(base, Payload::new(payload, payload_rest))
158169
}
159170
}
160-
pub trait ReleaseUnloaded: Release
171+
172+
/// Equivalent to `ReleasePayload` except does not return any payload.
173+
pub trait Release: ReleasePayload
161174
{
162-
/// Release only this level's properties, maintaining
163-
/// the next level's properties
175+
/// ReleasePayload only this level's properties, maintaining
176+
/// the next level's properties and returning the payload released
177+
///
178+
/// Like [ReleasePayload::release], but does not return the payload
179+
/// released.
164180
fn release(self) -> Self::Ensured
165181
{
166-
Release::release(self).0
182+
ReleasePayload::release(self).0
167183
}
168184

169-
/// Fully release this type, returning the base graph implementation
170-
/// type
185+
/// Fully release all ensurers, returning the base graph and the payload for
186+
/// all levels
187+
///
188+
/// Like [ReleasePayload::release_all], but does not return the payloads
189+
/// released.
171190
fn release_all(self) -> Self::Base
172191
{
173-
Release::release_all(self).0
192+
ReleasePayload::release_all(self).0
174193
}
175194
}
176195

177196
impl<G: Graph, D: Deref<Target = G>> BaseGraph for D {}
178197
impl<B: BaseGraph> Ensure for B
179198
{
180-
fn ensure_unvalidated(
199+
fn ensure_unchecked(
181200
c: Self::Ensured,
182-
_: <Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
201+
_: <Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
183202
) -> Self
184203
{
185204
c
186205
}
187206

188-
fn validate(
207+
fn can_ensure(
189208
_: &Self::Ensured,
190-
_: &<Self::Payload as Payload<<Self::Ensured as Release>::Payload>>::Item,
209+
_: &<Self::Payload as Payload<<Self::Ensured as ReleasePayload>::Payload>>::Item,
191210
) -> bool
192211
{
193212
true
@@ -198,7 +217,7 @@ impl<B: BaseGraph> Ensure for B
198217
Ensure::ensure(g, ())
199218
}
200219
}
201-
impl<B: BaseGraph> Release for B
220+
impl<B: BaseGraph> ReleasePayload for B
202221
{
203222
type Base = Self;
204223
type Ensured = Self;
@@ -214,9 +233,9 @@ impl<B: BaseGraph> Release for B
214233
(self, ())
215234
}
216235
}
217-
impl<B: BaseGraph> BaseGraphUnloaded for B {}
218-
impl<E: Ensure> EnsureUnloaded for E where
219-
E::Payload: Payload<<E::Ensured as Release>::Payload, Item = ()>
236+
impl<B: BaseGraph> BaseGraphGuard for B {}
237+
impl<E: Ensure> Guard for E where
238+
E::Payload: Payload<<E::Ensured as ReleasePayload>::Payload, Item = ()>
220239
{
221240
}
222-
impl<E: Release> ReleaseUnloaded for E {}
241+
impl<E: ReleasePayload> Release for E {}

src/core/property/acyclic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ pub struct AcyclicGraph<C: Ensure>(C);
1515

1616
impl<C: Ensure> Ensure for AcyclicGraph<C>
1717
{
18-
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self
18+
fn ensure_unchecked(c: Self::Ensured, _: ()) -> Self
1919
{
2020
Self(c)
2121
}
2222

23-
fn validate(c: &Self::Ensured, _: &()) -> bool
23+
fn can_ensure(c: &Self::Ensured, _: &()) -> bool
2424
{
2525
fn on_visit<G: Graph>(dfs: &mut Dfs<G, (Vec<G::Vertex>, &mut bool)>, v: G::Vertex)
2626
{
@@ -73,7 +73,7 @@ impl<C: Ensure> Ensure for AcyclicGraph<C>
7373
if !done.contains(&v)
7474
{
7575
done.push(v); // not returned by the dfs
76-
let g = VertexInGraph::ensure_unvalidated(c.graph(), v);
76+
let g = VertexInGraph::ensure_unchecked(c.graph(), v);
7777
let dfs = Dfs::new(&g, on_visit, on_exit, on_explore, (Vec::new(), &mut result));
7878

7979
dfs.for_each(|v| {

src/core/property/connected.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait Connected: Unilateral
5656
{
5757
self.all_vertices()
5858
.fold(Self::EdgeWeight::zero(), |max_ecc, v| {
59-
let new_ecc = VertexInGraph::ensure_unvalidated(self, v).eccentricity();
59+
let new_ecc = VertexInGraph::ensure_unchecked(self, v).eccentricity();
6060
if new_ecc > max_ecc
6161
{
6262
new_ecc
@@ -80,7 +80,7 @@ pub trait Connected: Unilateral
8080
{
8181
self.all_vertices()
8282
.fold(Self::EdgeWeight::zero(), |min_ecc, v| {
83-
let new_ecc = VertexInGraph::ensure_unvalidated(self, v).eccentricity();
83+
let new_ecc = VertexInGraph::ensure_unchecked(self, v).eccentricity();
8484
if new_ecc < min_ecc
8585
{
8686
new_ecc
@@ -104,7 +104,7 @@ pub trait Connected: Unilateral
104104
{
105105
let radius = self.radius();
106106
self.all_vertices()
107-
.filter(move |v| VertexInGraph::ensure_unvalidated(self, *v).eccentricity() == radius)
107+
.filter(move |v| VertexInGraph::ensure_unchecked(self, *v).eccentricity() == radius)
108108
}
109109
}
110110

@@ -123,12 +123,12 @@ impl<C: Ensure> ConnectedGraph<C>
123123

124124
impl<C: Ensure> Ensure for ConnectedGraph<C>
125125
{
126-
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self
126+
fn ensure_unchecked(c: Self::Ensured, _: ()) -> Self
127127
{
128128
Self(c)
129129
}
130130

131-
fn validate(c: &Self::Ensured, _: &()) -> bool
131+
fn can_ensure(c: &Self::Ensured, _: &()) -> bool
132132
{
133133
let g = c.graph();
134134
let v_count = g.all_vertices().count();

src/core/property/directedness_ensurers.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ pub struct DirectedGraph<C: Ensure>(C);
77

88
impl<C: Ensure> Ensure for DirectedGraph<C>
99
{
10-
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self
10+
fn ensure_unchecked(c: Self::Ensured, _: ()) -> Self
1111
{
1212
Self(c)
1313
}
1414

15-
fn validate(_: &Self::Ensured, _: &()) -> bool
15+
fn can_ensure(_: &Self::Ensured, _: &()) -> bool
1616
{
1717
<<C::Graph as Graph>::Directedness as Directedness>::directed()
1818
}
@@ -54,12 +54,12 @@ pub struct UndirectedGraph<C: Ensure>(C);
5454

5555
impl<C: Ensure> Ensure for UndirectedGraph<C>
5656
{
57-
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self
57+
fn ensure_unchecked(c: Self::Ensured, _: ()) -> Self
5858
{
5959
Self(c)
6060
}
6161

62-
fn validate(_: &Self::Ensured, _: &()) -> bool
62+
fn can_ensure(_: &Self::Ensured, _: &()) -> bool
6363
{
6464
!<<C::Graph as Graph>::Directedness as Directedness>::directed()
6565
}

src/core/property/has_vertex.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub struct HasVertexGraph<C: Ensure>(C);
2828

2929
impl<C: Ensure> Ensure for HasVertexGraph<C>
3030
{
31-
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self
31+
fn ensure_unchecked(c: Self::Ensured, _: ()) -> Self
3232
{
3333
Self(c)
3434
}
3535

36-
fn validate(c: &Self::Ensured, _: &()) -> bool
36+
fn can_ensure(c: &Self::Ensured, _: &()) -> bool
3737
{
3838
c.graph().all_vertices().next().is_some()
3939
}
@@ -110,12 +110,12 @@ where
110110

111111
impl<C: Ensure> Ensure for VertexInGraph<C>
112112
{
113-
fn ensure_unvalidated(c: Self::Ensured, v: <C::Graph as Graph>::Vertex) -> Self
113+
fn ensure_unchecked(c: Self::Ensured, v: <C::Graph as Graph>::Vertex) -> Self
114114
{
115115
Self(c, v)
116116
}
117117

118-
fn validate(c: &Self::Ensured, p: &<C::Graph as Graph>::Vertex) -> bool
118+
fn can_ensure(c: &Self::Ensured, p: &<C::Graph as Graph>::Vertex) -> bool
119119
{
120120
c.graph().contains_vertex(*p)
121121
}

0 commit comments

Comments
 (0)