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 edge from index #245 #246

Merged
merged 5 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 127 --fail-uncovered-functions 0 --fail-uncovered-lines 2
- run: cargo llvm-cov --fail-uncovered-regions 128 --fail-uncovered-functions 0 --fail-uncovered-lines 3

test:
runs-on: ubuntu-latest
Expand Down
37 changes: 36 additions & 1 deletion src/graph.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use self::graph_edge::GraphEdge;
use self::graph_node::GraphNode;
use self::graph_node_iterator::GraphNodeIterator;
use crate::DbError;

mod graph_edge;
mod graph_element;
mod graph_node;
mod graph_node_iterator;

Expand All @@ -27,6 +27,14 @@ impl Graph {
}
}

pub(crate) fn edge(&self, index: i64) -> Option<GraphEdge> {
if self.validate_edge(index).is_err() {
return None;
}

Some(GraphEdge { graph: self, index })
}

pub(crate) fn insert_edge(&mut self, from: i64, to: i64) -> Result<i64, DbError> {
self.validate_node(from)?;
self.validate_node(to)?;
Expand Down Expand Up @@ -80,6 +88,16 @@ impl Graph {
None
}

fn validate_edge(&self, index: i64) -> Result<(), DbError> {
if let Some(meta) = self.from_meta.get((-index) as usize) {
if 0 <= *meta {
return Ok(());
}
}

Err(DbError::from(format!("'{}' is not a valid edge", index)))
}

fn validate_node(&self, index: i64) -> Result<(), DbError> {
if let Some(meta) = self.from_meta.get(index as usize) {
if 0 <= *meta {
Expand All @@ -95,6 +113,23 @@ impl Graph {
mod tests {
use super::*;

#[test]
fn edge_from_index() {
let mut graph = Graph::new();
let from = graph.insert_node();
let to = graph.insert_node();
let index = graph.insert_edge(from, to).unwrap();

assert_eq!(graph.edge(index).unwrap().index(), index);
}

#[test]
fn edge_from_index_missing() {
let graph = Graph::new();

assert!(graph.edge(-3).is_none());
}

#[test]
fn insert_edge() {
let mut graph = Graph::new();
Expand Down
18 changes: 10 additions & 8 deletions src/graph/graph_edge.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#[allow(dead_code)]
pub(crate) struct GraphEdge {}
use super::Graph;

#[cfg(test)]
mod tests {
use super::*;
#[allow(dead_code)]
pub(crate) struct GraphEdge<'a> {
pub(crate) graph: &'a Graph,
pub(crate) index: i64,
}

#[test]
fn new() {
let _edge = GraphEdge {};
#[allow(dead_code)]
impl<'a> GraphEdge<'a> {
pub(crate) fn index(&self) -> i64 {
self.index
}
}
12 changes: 0 additions & 12 deletions src/graph/graph_element.rs

This file was deleted.