Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[graph] Add node iteration #240 #241

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
Expand Down Expand Up @@ -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<i64> {
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 {
Expand Down Expand Up @@ -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::<i64>::new();

for node in graph.node_iter() {
nodes.push(node.index());
}

assert_eq!(nodes, expected);
}
}
18 changes: 10 additions & 8 deletions src/graph/graph_node.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 23 additions & 0 deletions src/graph/graph_node_iterator.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Item> {
if let Some(next) = self.graph.next_node(self.index) {
self.index = next;
return Some(GraphNode {
graph: self.graph,
index: self.index,
});
}

None
}
}