Compare commits
1 Commits
main
..
fixes-major
| Author | SHA1 | Date | |
|---|---|---|---|
| fb9dfe8280 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "grapherity"
|
name = "grapherity"
|
||||||
version = "0.2.2"
|
version = "0.2.1"
|
||||||
authors = ["Stefan Müller"]
|
authors = ["Stefan Müller"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.85.0"
|
rust-version = "1.85.0"
|
||||||
|
|||||||
+22
-36
@@ -122,12 +122,12 @@ where
|
|||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
let mut predecessors = graph.vertex_map(None);
|
let mut predecessors = graph.vertex_map(None);
|
||||||
let result = bfs_impl(graph, source, |neighbor, predecessor| {
|
let (distances, _) = bfs_impl(graph, source, |neighbor, predecessor| {
|
||||||
predecessors[neighbor] = Some(predecessor);
|
predecessors[neighbor] = Some(predecessor);
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
BfsResult {
|
BfsResult {
|
||||||
distances: result.distances,
|
distances,
|
||||||
predecessors,
|
predecessors,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ pub fn bfs_distances<G>(graph: &G, source: G::Vertex) -> VertexMap<G::Vertex, Op
|
|||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
bfs_impl(graph, source, |_, _| true).distances
|
bfs_impl(graph, source, |_, _| true).0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<u32>
|
pub fn bfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<u32>
|
||||||
@@ -154,15 +154,14 @@ where
|
|||||||
if predicate(source) {
|
if predicate(source) {
|
||||||
return Some((source, 0));
|
return Some((source, 0));
|
||||||
}
|
}
|
||||||
bfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).found
|
bfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).1
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BfsImplResult<V: Copy> {
|
fn bfs_impl<G, F>(
|
||||||
distances: VertexMap<V, Option<u32>>,
|
graph: &G,
|
||||||
found: Option<(V, u32)>,
|
source: G::Vertex,
|
||||||
}
|
mut on_discover: F,
|
||||||
|
) -> (VertexMap<G::Vertex, Option<u32>>, Option<(G::Vertex, u32)>)
|
||||||
fn bfs_impl<G, F>(graph: &G, source: G::Vertex, mut on_discover: F) -> BfsImplResult<G::Vertex>
|
|
||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
||||||
@@ -179,19 +178,13 @@ where
|
|||||||
let distance = distances[v].unwrap() + 1;
|
let distance = distances[v].unwrap() + 1;
|
||||||
distances[neighbor] = Some(distance);
|
distances[neighbor] = Some(distance);
|
||||||
if !on_discover(neighbor, v) {
|
if !on_discover(neighbor, v) {
|
||||||
return BfsImplResult {
|
return (distances, Some((neighbor, distance)));
|
||||||
distances,
|
|
||||||
found: Some((neighbor, distance)),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
queue.push_back(neighbor);
|
queue.push_back(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BfsImplResult {
|
(distances, None)
|
||||||
distances,
|
|
||||||
found: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DfsResult<V: Copy> {
|
pub struct DfsResult<V: Copy> {
|
||||||
@@ -204,12 +197,12 @@ where
|
|||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
let mut predecessors = graph.vertex_map(None);
|
let mut predecessors = graph.vertex_map(None);
|
||||||
let result = dfs_impl(graph, source, |neighbor, predecessor| {
|
let (visited, _) = dfs_impl(graph, source, |neighbor, predecessor| {
|
||||||
predecessors[neighbor] = Some(predecessor);
|
predecessors[neighbor] = Some(predecessor);
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
DfsResult {
|
DfsResult {
|
||||||
visited: result.visited,
|
visited,
|
||||||
predecessors,
|
predecessors,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -218,7 +211,7 @@ pub fn dfs_visited<G>(graph: &G, source: G::Vertex) -> VertexMap<G::Vertex, bool
|
|||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
dfs_impl(graph, source, |_, _| true).visited
|
dfs_impl(graph, source, |_, _| true).0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> bool
|
pub fn dfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> bool
|
||||||
@@ -236,15 +229,14 @@ where
|
|||||||
if predicate(source) {
|
if predicate(source) {
|
||||||
return Some(source);
|
return Some(source);
|
||||||
}
|
}
|
||||||
dfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).found
|
dfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).1
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DfsImplResult<V: Copy> {
|
fn dfs_impl<G, F>(
|
||||||
visited: VertexMap<V, bool>,
|
graph: &G,
|
||||||
found: Option<V>,
|
source: G::Vertex,
|
||||||
}
|
mut on_discover: F,
|
||||||
|
) -> (VertexMap<G::Vertex, bool>, Option<G::Vertex>)
|
||||||
fn dfs_impl<G, F>(graph: &G, source: G::Vertex, mut on_discover: F) -> DfsImplResult<G::Vertex>
|
|
||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
||||||
@@ -256,10 +248,7 @@ where
|
|||||||
while let Some((v, predecessor)) = stack.pop() {
|
while let Some((v, predecessor)) = stack.pop() {
|
||||||
if let Some(p) = predecessor {
|
if let Some(p) = predecessor {
|
||||||
if !on_discover(v, p) {
|
if !on_discover(v, p) {
|
||||||
return DfsImplResult {
|
return (visited, Some(v));
|
||||||
visited,
|
|
||||||
found: Some(v),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for neighbor in graph.adjacent_vertices(v) {
|
for neighbor in graph.adjacent_vertices(v) {
|
||||||
@@ -269,10 +258,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DfsImplResult {
|
(visited, None)
|
||||||
visited,
|
|
||||||
found: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dfs_find_path<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<Vec<G::Edge>>
|
pub fn dfs_find_path<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<Vec<G::Edge>>
|
||||||
|
|||||||
+2
-13
@@ -13,17 +13,11 @@ impl<V: Copy, T: Clone> VertexMap<V, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads beyond 'capacity' are valid and return the default value.
|
// Reads beyond capacity() are valid and return the default value.
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.inner.capacity()
|
self.inner.capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deprecated(since = "0.2.2", note = "use 'capacity' instead")]
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.capacity()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
||||||
self.inner.resize(graph.vertex_capacity());
|
self.inner.resize(graph.vertex_capacity());
|
||||||
}
|
}
|
||||||
@@ -54,16 +48,11 @@ impl<E: Copy, T: Clone> EdgeMap<E, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads beyond 'capacity' are valid and return the default value.
|
// Reads beyond capacity() are valid and return the default value.
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.inner.capacity()
|
self.inner.capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deprecated(since = "0.2.2", note = "use 'capacity' instead")]
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.capacity()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sync<G: GraphTopology<Edge = E>>(&mut self, graph: &G) {
|
pub fn sync<G: GraphTopology<Edge = E>>(&mut self, graph: &G) {
|
||||||
self.inner.resize(graph.edge_capacity());
|
self.inner.resize(graph.edge_capacity());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ macro_rules! vertex_map_tests {
|
|||||||
let mut graph = <$T>::new();
|
let mut graph = <$T>::new();
|
||||||
graph.add_vertex();
|
graph.add_vertex();
|
||||||
let mut map = graph.vertex_map(42);
|
let mut map = graph.vertex_map(42);
|
||||||
let capacity_before = map.capacity();
|
let initial_len = map.capacity();
|
||||||
while graph.vertex_capacity() <= capacity_before {
|
while graph.vertex_capacity() <= initial_len {
|
||||||
graph.add_vertex();
|
graph.add_vertex();
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
@@ -197,8 +197,8 @@ macro_rules! edge_map_tests {
|
|||||||
let v2 = graph.add_vertex();
|
let v2 = graph.add_vertex();
|
||||||
graph.add_edge(v1, v2);
|
graph.add_edge(v1, v2);
|
||||||
let mut map = graph.edge_map(42);
|
let mut map = graph.edge_map(42);
|
||||||
let capacity_before = map.capacity();
|
let initial_len = map.capacity();
|
||||||
while graph.edge_capacity() <= capacity_before {
|
while graph.edge_capacity() <= initial_len {
|
||||||
graph.add_edge(v1, v2);
|
graph.add_edge(v1, v2);
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
|
|||||||
Reference in New Issue
Block a user