From c3257436c7d4c10a0ba0874d054b7e7bddffe7e5 Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 22 Sep 2022 21:13:23 +0200 Subject: [PATCH 1/4] Create graph_node_iterator.rs --- src/graph/graph_node_iterator.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/graph/graph_node_iterator.rs diff --git a/src/graph/graph_node_iterator.rs b/src/graph/graph_node_iterator.rs new file mode 100644 index 00000000..6f9b2a47 --- /dev/null +++ b/src/graph/graph_node_iterator.rs @@ -0,0 +1,23 @@ +use super::graph_node::GraphNode; +use super::Graph; + +pub(crate) struct GraphNodeIterator<'a> { + pub(crate) graph: &'a Graph, + pub(crate) index: i64, +} + +impl<'a> Iterator for GraphNodeIterator<'a> { + type Item = GraphNode<'a>; + + fn next(&mut self) -> Option { + if let Some(next) = self.graph.next_node(self.index) { + self.index = next; + return Some(GraphNode { + graph: self.graph, + index: self.index, + }); + } + + None + } +} From 5355c8a9394ddb22f90b0ee3edb16b7ca86a2d42 Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 22 Sep 2022 21:13:26 +0200 Subject: [PATCH 2/4] Update graph_node.rs --- src/graph/graph_node.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/graph/graph_node.rs b/src/graph/graph_node.rs index 136325f7..c436a354 100644 --- a/src/graph/graph_node.rs +++ b/src/graph/graph_node.rs @@ -1,12 +1,14 @@ -#[allow(dead_code)] -pub(crate) struct GraphNode {} +use super::Graph; -#[cfg(test)] -mod tests { - use super::*; +#[allow(dead_code)] +pub(crate) struct GraphNode<'a> { + pub(crate) graph: &'a Graph, + pub(crate) index: i64, +} - #[test] - fn new() { - let _node = GraphNode {}; +#[allow(dead_code)] +impl<'a> GraphNode<'a> { + pub(crate) fn index(&self) -> i64 { + self.index } } From 17957635bc6b93b3ff9d1cf399e1c5829179987f Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 22 Sep 2022 21:13:29 +0200 Subject: [PATCH 3/4] Update graph.rs --- src/graph.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/graph.rs b/src/graph.rs index 46e9020e..8a6e858c 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,8 +1,10 @@ +use self::graph_node_iterator::GraphNodeIterator; use crate::DbError; mod graph_edge; mod graph_element; mod graph_node; +mod graph_node_iterator; pub(crate) struct Graph { from: Vec, @@ -52,6 +54,23 @@ impl Graph { self.node_count as i64 } + pub(crate) fn node_iter(&self) -> GraphNodeIterator { + GraphNodeIterator { + graph: self, + index: 0, + } + } + + fn next_node(&self, index: i64) -> Option { + for i in (index as usize + 1)..self.from_meta.len() { + if 0 <= self.from_meta[i] { + return Some(i as i64); + } + } + + None + } + fn validate_node(&self, index: i64) -> Result<(), DbError> { if let Some(meta) = self.from_meta.get(index as usize) { if 0 <= *meta { @@ -117,4 +136,21 @@ mod tests { assert_eq!(id, 1); } + + #[test] + fn node_iteration() { + let mut graph = Graph::new(); + let expected = vec![ + graph.insert_node(), + graph.insert_node(), + graph.insert_node(), + ]; + let mut nodes = Vec::::new(); + + for node in graph.node_iter() { + nodes.push(node.index()); + } + + assert_eq!(nodes, expected); + } } From f66a2f409a3d9e37f2a26019dcd09b246450aaee Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 22 Sep 2022 21:13:32 +0200 Subject: [PATCH 4/4] Update pr.yaml --- .github/workflows/pr.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 863fac94..76daf13d 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -44,7 +44,7 @@ jobs: - uses: actions/checkout@v3 - uses: taiki-e/install-action@cargo-llvm-cov - run: rustup component add llvm-tools-preview - - run: cargo llvm-cov --fail-uncovered-regions 126 --fail-uncovered-functions 0 --fail-uncovered-lines 1 + - run: cargo llvm-cov --fail-uncovered-regions 127 --fail-uncovered-functions 0 --fail-uncovered-lines 2 test: runs-on: ubuntu-latest