@@ -8,6 +8,10 @@ use std::{
8
8
} ;
9
9
10
10
/// A marker trait for graphs with at least 1 vertex.
11
+ ///
12
+ /// `V` is the number of vertices that are guaranteed to be in the graph.
13
+ /// If `Unique` is true (default) then indexed vertices methods will never return duplicate
14
+ /// vertices.
11
15
pub trait HasVertex < const V : usize = 1 > : Graph
12
16
{
13
17
/// Ensures this trait cannot be implemented with V=0.
@@ -45,7 +49,7 @@ pub trait HasVertex<const V: usize = 1>: Graph
45
49
/// struct Struct<const V: usize>(usize);
46
50
///
47
51
/// impl<const V: usize> HasVertex<V> for Struct<V> {
48
- /// fn get_vertex_at<const N:usize> (&self) -> Self::Vertex {
52
+ /// fn get_vertex_idx (&self, idx: usize ) -> Self::Vertex {
49
53
/// ()
50
54
/// }
51
55
/// }
@@ -64,20 +68,32 @@ pub trait HasVertex<const V: usize = 1>: Graph
64
68
65
69
/// Returns a vertex in the graph.
66
70
///
67
- /// Successive calls do not have to return the same vertex,
71
+ /// Successive calls do not guarantee to return the same vertex,
68
72
/// even though the graph hasn't changed.
69
- ///
70
- /// This trait doesn't provide a default implementation for `get_vertex`
71
- /// to ensure that "wrapping" ensurers don't accidentally use it, instead
72
- /// of actively delegating to the inner class, who might have its own
73
- /// implementation.
74
73
fn get_vertex ( & self ) -> Self :: Vertex
75
74
{
76
75
_ = Self :: ASSERT_NOT_0 ;
77
76
self . get_vertex_at :: < 0 > ( )
78
77
}
79
-
80
- fn get_vertex_at < const I : usize > ( & self ) -> Self :: Vertex ;
78
+
79
+ /// Returns the I'th vertex guaranteed to be in the vertex.
80
+ ///
81
+ /// The vertex ordering (i.e. the I) is significant and does not change unless
82
+ /// the underlying graph type changes it.
83
+ fn get_vertex_at < const I : usize > ( & self ) -> Self :: Vertex {
84
+ _ = Self :: ASSERT_NOT_0 ;
85
+ const {
86
+ assert ! ( I < V )
87
+ }
88
+ assert ! ( I < V , "I out of bounds" ) ;
89
+ self . get_vertex_idx ( I )
90
+ }
91
+
92
+ /// Returns vertex guaranteed to be in the vertex at the given index in the ordering.
93
+ ///
94
+ /// The vertex ordering (i.e. the idx) is significant and does not change unless
95
+ /// the underlying graph type changes it.
96
+ fn get_vertex_idx ( & self , idx : usize ) -> Self :: Vertex ;
81
97
}
82
98
83
99
/// Ensures the underlying graph has at least 1 vertex.
@@ -119,8 +135,9 @@ where
119
135
120
136
impl < C : Ensure , const V : usize > HasVertex < V > for HasVertexGraph < C >
121
137
{
122
- fn get_vertex_at < const N : usize > ( & self ) -> Self :: Vertex
138
+ fn get_vertex_idx ( & self , idx : usize ) -> Self :: Vertex
123
139
{
140
+ assert ! ( idx < V ) ;
124
141
self . all_vertices ( )
125
142
. next ( )
126
143
. expect ( "HasVertexGraph has no vertices." )
@@ -132,11 +149,9 @@ impl_ensurer! {
132
149
as ( self . 0 ) : C
133
150
}
134
151
135
- /// Ensures a specific vertex is in the underlying graph.
152
+ /// Ensures a specific vertex or vertices is in the underlying graph.
136
153
///
137
- /// That vertex is guaranteed to be returned by any call to `get_vertex` and
138
- /// cannot be removed from the graph.
139
-
154
+ /// The designated vertices cannot be removed from the graph.
140
155
#[ derive( Clone ) ]
141
156
pub struct VertexInGraph < C : Ensure , const V : usize = 1 > ( C , [ <C :: Graph as Graph >:: Vertex ; V ] ) ;
142
157
@@ -221,10 +236,10 @@ where
221
236
222
237
impl < C : Ensure , const V : usize > HasVertex < V > for VertexInGraph < C , V >
223
238
{
224
- fn get_vertex_at < const N : usize > ( & self ) -> Self :: Vertex
239
+ fn get_vertex_idx ( & self , idx : usize ) -> Self :: Vertex
225
240
{
226
- // assert!(N < V);
227
- self . 1 [ N ]
241
+ assert ! ( idx < V ) ;
242
+ self . 1 [ idx ]
228
243
}
229
244
}
230
245
0 commit comments