1
- use crate :: core:: { property:: RemoveVertex , Ensure , Graph , GraphDerefMut } ;
1
+ use crate :: core:: {
2
+ property:: { RemoveVertex , Rooted } ,
3
+ Ensure , Graph , GraphDerefMut ,
4
+ } ;
2
5
use std:: {
3
6
borrow:: Borrow ,
4
7
fmt:: { Debug , Error , Formatter } ,
5
8
} ;
6
9
7
10
/// A marker trait for graphs with at least 1 vertex.
8
- pub trait HasVertex : Graph
11
+ pub trait HasVertex < const V : usize = 1 > : Graph
9
12
{
13
+ /// Ensures this trait cannot be implemented with V=0.
14
+ ///
15
+ /// Add a call to this associated type in the implementing type's
16
+ /// constructor to ensure if that type ever gets v=0, compilation will
17
+ /// fail.
18
+ ///
19
+ /// Example:
20
+ /// ```compile_fail, E0080
21
+ /// # use std::borrow::Borrow;
22
+ /// # use graphene::{
23
+ /// # common::AdjListGraph,
24
+ /// # core::{Directed, Graph, property::HasVertex}
25
+ /// # };
26
+ /// # impl<const V: usize> Graph for Struct<V> {
27
+ /// # type Vertex = ();
28
+ /// # type VertexWeight = ();
29
+ /// # type EdgeWeight = ();
30
+ /// # type EdgeWeightRef<'a> = () where Self: 'a;
31
+ /// # type Directedness = Directed;
32
+ /// #
33
+ /// # fn all_vertices_weighted(&self) -> impl Iterator<Item=(Self::Vertex, &Self::VertexWeight)>
34
+ /// # {
35
+ /// # std::iter::empty()
36
+ /// # }
37
+ /// #
38
+ /// # fn edges_between(&self, source: impl Borrow<Self::Vertex>, sink: impl Borrow<Self::Vertex>)
39
+ /// # -> impl Iterator<Item=Self::EdgeWeightRef<'_>>
40
+ /// # {
41
+ /// # std::iter::empty()
42
+ /// # }
43
+ /// #
44
+ /// # }
45
+ /// struct Struct<const V: usize>(usize);
46
+ ///
47
+ /// impl<const V: usize> HasVertex<V> for Struct<V> {
48
+ /// fn get_vertex_at<const N:usize>(&self) -> Self::Vertex {
49
+ /// ()
50
+ /// }
51
+ /// }
52
+ ///
53
+ /// impl<const V: usize> Struct<V> {
54
+ /// fn new() -> Self {
55
+ /// _ = Self::ASSERT_NOT_0; // This ensures errors are thrown if V = 0
56
+ /// Struct(V)
57
+ /// }
58
+ /// }
59
+ ///
60
+ /// let _ = Struct::<0>::new(); // Will cause a compile error
61
+ /// let _ = Struct::<1>::new(); // Will compile successfully
62
+ /// ```
63
+ const ASSERT_NOT_0 : ( ) = assert ! ( V > 0 , "Found type implementing HasVertex<0>" ) ;
64
+
10
65
/// Returns a vertex in the graph.
11
66
///
12
67
/// Successive calls do not have to return the same vertex,
@@ -16,7 +71,13 @@ pub trait HasVertex: Graph
16
71
/// to ensure that "wrapping" ensurers don't accidentally use it, instead
17
72
/// of actively delegating to the inner class, who might have its own
18
73
/// implementation.
19
- fn get_vertex ( & self ) -> Self :: Vertex ;
74
+ fn get_vertex ( & self ) -> Self :: Vertex
75
+ {
76
+ _ = Self :: ASSERT_NOT_0 ;
77
+ self . get_vertex_at :: < 0 > ( )
78
+ }
79
+
80
+ fn get_vertex_at < const I : usize > ( & self ) -> Self :: Vertex ;
20
81
}
21
82
22
83
/// Ensures the underlying graph has at least 1 vertex.
56
117
}
57
118
}
58
119
59
- impl < C : Ensure > HasVertex for HasVertexGraph < C >
120
+ impl < C : Ensure , const V : usize > HasVertex < V > for HasVertexGraph < C >
60
121
{
61
- fn get_vertex ( & self ) -> Self :: Vertex
122
+ fn get_vertex_at < const N : usize > ( & self ) -> Self :: Vertex
62
123
{
63
124
self . all_vertices ( )
64
125
. next ( )
@@ -75,16 +136,36 @@ impl_ensurer! {
75
136
///
76
137
/// That vertex is guaranteed to be returned by any call to `get_vertex` and
77
138
/// cannot be removed from the graph.
139
+
78
140
#[ derive( Clone ) ]
79
- pub struct VertexInGraph < C : Ensure > ( C , <C :: Graph as Graph >:: Vertex ) ;
141
+ pub struct VertexInGraph < C : Ensure , const V : usize = 1 > ( C , [ <C :: Graph as Graph >:: Vertex ; V ] ) ;
80
142
81
- impl < C : Ensure > VertexInGraph < C >
143
+ impl < C : Ensure , const V : usize > VertexInGraph < C , V >
82
144
{
83
- pub fn set_vertex ( & mut self , v : impl Borrow < <C :: Graph as Graph >:: Vertex > ) -> Result < ( ) , ( ) >
145
+ /// ```compile_fail, E0080
146
+ /// use graphene::common::AdjListGraph;
147
+ /// use graphene::core::property::VertexInGraph;
148
+ /// use graphene::core::Ensure;
149
+ ///
150
+ /// let _ = VertexInGraph::<_, 0>::ensure_unchecked(AdjListGraph::<(), ()>::new(), []);
151
+ /// ```
152
+ fn new ( c : C , vs : [ <C :: Graph as Graph >:: Vertex ; V ] ) -> Self
84
153
{
85
- if self . 0 . graph ( ) . contains_vertex ( v. borrow ( ) )
154
+ _ = Self :: ASSERT_NOT_0 ;
155
+ Self ( c, vs)
156
+ }
157
+
158
+ pub fn set_vertex (
159
+ & mut self ,
160
+ replacements : impl Borrow < [ <C :: Graph as Graph >:: Vertex ; V ] > ,
161
+ ) -> Result < ( ) , ( ) >
162
+ {
163
+ if replacements
164
+ . borrow ( )
165
+ . iter ( )
166
+ . all ( |v| self . 0 . graph ( ) . contains_vertex ( v) )
86
167
{
87
- self . 1 = v . borrow ( ) . clone ( ) ;
168
+ self . 1 = replacements . borrow ( ) . clone ( ) ;
88
169
Ok ( ( ) )
89
170
}
90
171
else
@@ -94,7 +175,7 @@ impl<C: Ensure> VertexInGraph<C>
94
175
}
95
176
}
96
177
97
- impl < C : Ensure > Debug for VertexInGraph < C >
178
+ impl < C : Ensure , const V : usize > Debug for VertexInGraph < C , V >
98
179
where
99
180
C : Debug ,
100
181
<C :: Graph as Graph >:: Vertex : Debug ,
@@ -108,26 +189,26 @@ where
108
189
}
109
190
}
110
191
111
- impl < C : Ensure > Ensure for VertexInGraph < C >
192
+ impl < C : Ensure , const V : usize > Ensure for VertexInGraph < C , V >
112
193
{
113
- fn ensure_unchecked ( c : Self :: Ensured , v : <C :: Graph as Graph >:: Vertex ) -> Self
194
+ fn ensure_unchecked ( c : Self :: Ensured , v : [ <C :: Graph as Graph >:: Vertex ; V ] ) -> Self
114
195
{
115
- Self ( c, v)
196
+ Self :: new ( c, v)
116
197
}
117
198
118
- fn can_ensure ( c : & Self :: Ensured , p : & <C :: Graph as Graph >:: Vertex ) -> bool
199
+ fn can_ensure ( c : & Self :: Ensured , p : & [ <C :: Graph as Graph >:: Vertex ; V ] ) -> bool
119
200
{
120
- c. graph ( ) . contains_vertex ( * p )
201
+ p . iter ( ) . all ( |v| c. graph ( ) . contains_vertex ( v ) )
121
202
}
122
203
}
123
204
124
- impl < C : Ensure + GraphDerefMut > RemoveVertex for VertexInGraph < C >
205
+ impl < C : Ensure + GraphDerefMut , const V : usize > RemoveVertex for VertexInGraph < C , V >
125
206
where
126
207
C :: Graph : RemoveVertex ,
127
208
{
128
209
fn remove_vertex ( & mut self , v : impl Borrow < Self :: Vertex > ) -> Result < Self :: VertexWeight , ( ) >
129
210
{
130
- if self . 1 . borrow ( ) != v. borrow ( )
211
+ if self . 1 . iter ( ) . all ( |v2| v2 != v. borrow ( ) )
131
212
{
132
213
self . 0 . graph_mut ( ) . remove_vertex ( v)
133
214
}
@@ -138,16 +219,32 @@ where
138
219
}
139
220
}
140
221
141
- impl < C : Ensure > HasVertex for VertexInGraph < C >
222
+ impl < C : Ensure , const V : usize > HasVertex < V > for VertexInGraph < C , V >
142
223
{
143
- fn get_vertex ( & self ) -> Self :: Vertex
224
+ fn get_vertex_at < const N : usize > ( & self ) -> Self :: Vertex
225
+ {
226
+ // assert!(N < V);
227
+ self . 1 [ N ]
228
+ }
229
+ }
230
+
231
+ impl < C : Ensure > Rooted for VertexInGraph < C >
232
+ where
233
+ C :: Graph : Rooted ,
234
+ {
235
+ fn root ( & self ) -> Self :: Vertex
236
+ {
237
+ self . get_vertex ( )
238
+ }
239
+
240
+ fn set_root ( & mut self , v : impl Borrow < Self :: Vertex > ) -> Result < ( ) , ( ) >
144
241
{
145
- self . 1
242
+ self . set_vertex ( [ * v . borrow ( ) ] )
146
243
}
147
244
}
148
245
149
246
impl_ensurer ! {
150
- use <C > VertexInGraph <C >: Ensure , HasVertex , RemoveVertex
247
+ use <C ; const V : usize > VertexInGraph <C , V >: Ensure , HasVertex , RemoveVertex , Rooted
151
248
as ( self . 0 ) : C
152
- as ( self . 1 ) : <C :: Graph as Graph >:: Vertex
249
+ as ( self . 1 ) : [ <C :: Graph as Graph >:: Vertex ; V ]
153
250
}
0 commit comments