1
1
use crate :: mock_graph:: {
2
- arbitrary:: { GuidedArbGraph , Limit , NonUnique , TwoVerticesIn , Uniqueness } ,
2
+ arbitrary:: { GuidedArbGraph , Limit } ,
3
3
MockType , TestGraph ,
4
4
} ;
5
5
use graphene:: {
6
6
algo:: { Bfs , Dfs } ,
7
7
core:: {
8
- property:: { AddEdge , RemoveEdge , VertexInGraph } ,
8
+ property:: { AddEdge , RemoveEdge , VertexIn , VertexInGraph } ,
9
9
Ensure , Graph , GraphDerefMut , Release ,
10
10
} ,
11
11
impl_ensurer,
@@ -16,35 +16,34 @@ use std::{collections::HashSet, fmt::Debug};
16
16
17
17
/// An arbitrary graph and two vertices in it.
18
18
///
19
+ /// Guarantees that the second vertex is reachable from the first.
20
+ ///
19
21
/// Depending on `U`, the two vertices are either allowed to be the same
20
22
/// (`NonUnique`, default), or they must be unique (`Unique`).
21
23
///
22
24
/// Note: All graphs will have at least 1 vertex for non-unique and 2 vertices
23
25
/// for unique, meaning this type never includes the empty graph.
24
26
#[ derive( Clone , Debug ) ]
25
- pub struct TwoReachableVerticesIn < G , U = NonUnique > ( pub TwoVerticesIn < G , U > )
27
+ pub struct TwoReachableVerticesIn < G , const UNIQUE : bool = false > ( pub VertexInGraph < G , 2 , UNIQUE > )
26
28
where
27
29
G : GuidedArbGraph ,
28
30
G :: Graph : TestGraph ,
29
- <G :: Graph as Graph >:: EdgeWeight : MockType ,
30
- U : Uniqueness ;
31
+ <G :: Graph as Graph >:: EdgeWeight : MockType ;
31
32
32
33
impl_ensurer ! {
33
- use <G , U > TwoReachableVerticesIn <G , U >
34
- as ( self . 0 ) : TwoVerticesIn <G , U >
34
+ use <G ; const U : bool > TwoReachableVerticesIn <G , U >
35
+ as ( self . 0 ) : VertexInGraph <G , 2 , U >
35
36
where
36
37
G : GuidedArbGraph ,
37
38
G :: Graph : TestGraph ,
38
39
<G :: Graph as Graph >:: EdgeWeight : MockType ,
39
- U : Uniqueness
40
40
}
41
41
42
- impl < Gr , U > GuidedArbGraph for TwoReachableVerticesIn < Gr , U >
42
+ impl < Gr , const U : bool > GuidedArbGraph for TwoReachableVerticesIn < Gr , U >
43
43
where
44
44
Gr : GuidedArbGraph + GraphDerefMut ,
45
45
Gr :: Graph : TestGraph + RemoveEdge + AddEdge ,
46
46
<Gr :: Graph as Graph >:: EdgeWeight : MockType ,
47
- U : ' static + Uniqueness ,
48
47
{
49
48
fn choose_size < G : Gen > (
50
49
g : & mut G ,
@@ -58,15 +57,13 @@ where
58
57
59
58
// we need at least 1 edge. We'll delegate to TwoVerticesIn to ensure we get
60
59
// at least 1 or 2 vertices (depending on U).
61
- TwoVerticesIn :: < Gr , U > :: choose_size ( g, v_min, v_max, std:: cmp:: max ( e_min, 1 ) , e_max)
60
+ VertexInGraph :: < Gr , 2 , U > :: choose_size ( g, v_min, v_max, std:: cmp:: max ( e_min, 1 ) , e_max)
62
61
}
63
62
64
63
fn arbitrary_fixed < G : Gen > ( g : & mut G , v_count : usize , e_count : usize ) -> Self
65
64
{
66
65
// Create a graph with at least 1 or 2 vertices (1 for non-unique, 2 for Unique)
67
- let graph = TwoVerticesIn :: < Gr , U > :: arbitrary_fixed ( g, v_count, e_count)
68
- . release ( )
69
- . release ( ) ;
66
+ let graph = VertexInGraph :: < Gr , 2 , U > :: arbitrary_fixed ( g, v_count, e_count) . release ( ) ;
70
67
71
68
let mut vert_reachables: Vec < _ > = graph
72
69
. graph ( )
81
78
graph. graph ( ) ,
82
79
[ v. clone ( ) ] ,
83
80
) ) ) ;
84
- if !U :: unique ( ) && graph. graph ( ) . edges_between ( v. clone ( ) , v. clone ( ) ) . count ( ) > 0
81
+ if !U && graph. graph ( ) . edges_between ( v. clone ( ) , v. clone ( ) ) . count ( ) > 0
85
82
{
86
83
reachable. push ( v. clone ( ) ) ;
87
84
}
@@ -100,13 +97,14 @@ where
100
97
// Choose a vertex that ends the path
101
98
let v2 = reachable[ g. gen_range ( 0 , reachable. len ( ) ) ] ;
102
99
103
- Self ( TwoVerticesIn :: new ( graph, v1. clone ( ) , v2) )
100
+ Self ( VertexInGraph :: ensure_unchecked ( graph, [ v1. clone ( ) , v2] ) )
104
101
}
105
102
106
103
fn shrink_guided ( & self , mut limits : HashSet < Limit > ) -> Box < dyn Iterator < Item = Self > >
107
104
{
108
105
let mut result: Vec < Self > = Vec :: new ( ) ;
109
- let ( v1, v2) = self . 0 . get_both ( ) ;
106
+ let v1 = self . 0 . vertex_at :: < 0 > ( ) ;
107
+ let v2 = self . 0 . vertex_at :: < 1 > ( ) ;
110
108
111
109
// First find a path between the vertices
112
110
let g = VertexInGraph :: ensure_unchecked ( self , [ v1] ) ;
@@ -137,7 +135,7 @@ where
137
135
clone
138
136
. 0
139
137
. shrink_guided ( limits)
140
- . map ( |g| Self ( TwoVerticesIn :: new ( g . release ( ) , v1, v2) ) )
138
+ . map ( |g| Self ( VertexInGraph :: ensure_unchecked ( g , [ v1, v2] ) ) )
141
139
} ) ;
142
140
143
141
// Shrink by either removing superfluous edges from last link
@@ -168,8 +166,8 @@ where
168
166
{
169
167
g. add_edge_weighted ( v1, v1, w) . unwrap ( ) ;
170
168
}
171
- g . 1 = v2 ;
172
- result. push ( Self ( g ) ) ;
169
+ let g = g . release ( ) ;
170
+ result. push ( Self ( VertexInGraph :: ensure_unchecked ( g , [ v1 , v2 ] ) ) ) ;
173
171
}
174
172
Box :: new ( result. into_iter ( ) )
175
173
}
0 commit comments