Skip to content

Commit

Permalink
Merge pull request #39 from andyquinterom/T38
Browse files Browse the repository at this point in the history
feat: Adds subset with limit
  • Loading branch information
cristhian-ixpantia authored Jan 27, 2025
2 parents 3c8f8c4 + 545b714 commit 77dfd82
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orbweaver
Title: Fast and Efficient Graph Data Structures
Version: 0.17.1
Version: 0.18.0
Authors@R:
c(person(given = "ixpantia, SRL",
role = "cph",
Expand Down
8 changes: 4 additions & 4 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ DirectedGraph$get_all_roots <- function() .Call(wrap__DirectedGraph__get_all_roo

DirectedGraph$get_roots_over <- function(node_ids) .Call(wrap__DirectedGraph__get_roots_over, self, node_ids)

DirectedGraph$subset <- function(node_id) .Call(wrap__DirectedGraph__subset, self, node_id)

DirectedGraph$subset_multi <- function(node_ids) .Call(wrap__DirectedGraph__subset_multi, self, node_ids)

DirectedGraph$subset_multi_with_limit <- function(node_ids, limit) .Call(wrap__DirectedGraph__subset_multi_with_limit, self, node_ids, limit)

DirectedGraph$print <- function() invisible(.Call(wrap__DirectedGraph__print, self))

DirectedGraph$to_bin_disk <- function(path) .Call(wrap__DirectedGraph__to_bin_disk, self, path)
Expand Down Expand Up @@ -84,10 +84,10 @@ DirectedAcyclicGraph$get_all_roots <- function() .Call(wrap__DirectedAcyclicGrap

DirectedAcyclicGraph$get_roots_over <- function(node_ids) .Call(wrap__DirectedAcyclicGraph__get_roots_over, self, node_ids)

DirectedAcyclicGraph$subset <- function(node_id) .Call(wrap__DirectedAcyclicGraph__subset, self, node_id)

DirectedAcyclicGraph$subset_multi <- function(node_ids) .Call(wrap__DirectedAcyclicGraph__subset_multi, self, node_ids)

DirectedAcyclicGraph$subset_multi_with_limit <- function(node_ids, limit) .Call(wrap__DirectedAcyclicGraph__subset_multi_with_limit, self, node_ids, limit)

DirectedAcyclicGraph$print <- function() invisible(.Call(wrap__DirectedAcyclicGraph__print, self))

DirectedAcyclicGraph$to_bin_disk <- function(path) .Call(wrap__DirectedAcyclicGraph__to_bin_disk, self, path)
Expand Down
14 changes: 8 additions & 6 deletions R/subset.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
NO_LIMIT <- -1L

#' @export
subset.DirectedGraph <- function(x, ...) {
subset.DirectedGraph <- function(x, ..., limit = NO_LIMIT) {
arguments <- c(...)
if (length(arguments) > 1) {
if (limit == NO_LIMIT) {
return(throw_if_error(x$subset_multi(arguments)))
}
throw_if_error(x$subset(arguments))
return(throw_if_error(x$subset_multi_with_limit(arguments, limit)))
}

#' @export
subset.DirectedAcyclicGraph <- function(x, ...) {
subset.DirectedAcyclicGraph <- function(x, ..., limit = NO_LIMIT) {
arguments <- c(...)
if (length(arguments) > 1) {
if (limit == NO_LIMIT) {
return(throw_if_error(x$subset_multi(arguments)))
}
throw_if_error(x$subset(arguments))
return(throw_if_error(x$subset_multi_with_limit(arguments, limit)))
}
6 changes: 3 additions & 3 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = 'orbweaver_r'
version = '0.16.0'
version = '0.18.0'
edition = '2021'

[lib]
crate-type = [ 'staticlib', 'lib' ]
name = 'orbweaver_r'

[dependencies]
orbweaver = { version = "0.17.1" }
orbweaver = { version = "0.18.0" }
extendr-api = { version = "0.7", features = ["serde", "result_condition"] }

# This will help us filter the platforms
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ pub trait RImplDirectedGraph: Sized {
fn get_leaves_under(&self, nodes: RNodesIn) -> Result<NodeVec>;
fn get_all_roots(&self) -> NodeVec;
fn get_roots_over(&self, node_ids: RNodesIn) -> Result<NodeVec>;
fn subset(&self, node_id: &str) -> Result<Self>;
fn subset_multi(&self, node_id: RNodesIn) -> Result<Self>;
fn subset_multi_with_limit(&self, node_id: RNodesIn, limit: i32) -> Result<Self>;
fn print(&self);
fn find_all_paths(&self, from: &str, to: &str) -> Result<List>;
fn to_bin_disk(&self, path: &str) -> Result<()>;
Expand Down
17 changes: 14 additions & 3 deletions src/rust/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,25 @@ macro_rules! impl_directed_graph {
.map_err(to_r_error)
.map(NodeVec)
}
fn subset(&self, node_id: &str) -> Result<Self> {
Ok(Self(self.0.subset(node_id).map_err(to_r_error)?))
}
fn subset_multi(&self, node_ids: RNodesIn) -> Result<Self> {
Ok(Self(
self.0.subset_multi(node_ids.iter()).map_err(to_r_error)?,
))
}
fn subset_multi_with_limit(&self, node_ids: RNodesIn, limit: i32) -> Result<Self> {
if limit <= 0 {
return Err("Limit cannot be negative".into());
}

// This is safe because we checked right before
let limit = unsafe { std::num::NonZeroUsize::new_unchecked(limit as usize) };

Ok(Self(
self.0
.subset_multi_with_limit(node_ids.iter(), limit)
.map_err(to_r_error)?,
))
}
fn print(&self) {
println!("{:?}", self.0)
}
Expand Down
Binary file modified src/rust/vendor.tar.xz
Binary file not shown.

0 comments on commit 77dfd82

Please sign in to comment.