Skip to content
Draft
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
38 changes: 18 additions & 20 deletions R/bipartite.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,41 @@ bipartite_projection <- function(
"false" = 1L,
"true" = 2L
)
if (which != "both" && probe1 != -1) {
if (which != 0L && probe1 != -1) {
cli::cli_warn("{.arg probe1} ignored if only one projection is requested.")
}

on.exit(.Call(Rx_igraph_finalizer))
# Function call
res <- .Call(
Rx_igraph_bipartite_projection,
graph,
types,
as.numeric(probe1),
which
# bipartite_projection_impl always computes both projections
res <- bipartite_projection_impl(
graph = graph,
types = types,
probe1 = probe1
)

if (remove.type) {
if (is_igraph(res[[1]]) && "type" %in% vertex_attr_names(res[[1]])) {
res[[1]] <- delete_vertex_attr(res[[1]], "type")
if (is_igraph(res$proj1) && "type" %in% vertex_attr_names(res$proj1)) {
res$proj1 <- delete_vertex_attr(res$proj1, "type")
}
if (is_igraph(res[[2]]) && "type" %in% vertex_attr_names(res[[2]])) {
res[[2]] <- delete_vertex_attr(res[[2]], "type")
if (is_igraph(res$proj2) && "type" %in% vertex_attr_names(res$proj2)) {
res$proj2 <- delete_vertex_attr(res$proj2, "type")
}
}
if (which == 0L) {
if (multiplicity) {
E(res[[1]])$weight <- res[[3]]
E(res[[2]])$weight <- res[[4]]
E(res$proj1)$weight <- res$multiplicity1
E(res$proj2)$weight <- res$multiplicity2
}
res[1:2]
res[c("proj1", "proj2")]
} else if (which == 1L) {
if (multiplicity) {
E(res[[1]])$weight <- res[[3]]
E(res$proj1)$weight <- res$multiplicity1
}
res[[1]]
res$proj1
} else {
if (multiplicity) {
E(res[[2]])$weight <- res[[4]]
E(res$proj2)$weight <- res$multiplicity2
}
res[[2]]
res$proj2
}
}

Expand Down
48 changes: 18 additions & 30 deletions R/cliques.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,21 @@ count_max_cliques <- function(graph, min = NULL, max = NULL, subset = NULL) {
max <- as.numeric(max)

if (!is.null(subset)) {
subset <- as.numeric(as_igraph_vs(graph, subset) - 1)
# Use maximal_cliques_subset_impl when subset is provided
maximal_cliques_subset_impl(
graph = graph,
subset = subset,
min_size = min,
max_size = max,
details = TRUE
)$no
} else {
maximal_cliques_count_impl(
graph = graph,
min_size = min,
max_size = max
)
}

on.exit(.Call(Rx_igraph_finalizer))
# Function call
res <- .Call(Rx_igraph_maximal_cliques_count, graph, subset, min, max)

res
}

#' @rdname cliques
Expand Down Expand Up @@ -562,30 +569,11 @@ weighted_clique_num <- function(graph, vertex.weights = NULL) {
#'
#' length(max_ivs(g))
ivs <- function(graph, min = NULL, max = NULL) {
ensure_igraph(graph)

if (is.null(min)) {
min <- 0
}

if (is.null(max)) {
max <- 0
}

on.exit(.Call(Rx_igraph_finalizer))
res <- .Call(
Rx_igraph_independent_vertex_sets,
graph,
as.numeric(min),
as.numeric(max)
independent_vertex_sets_impl(
graph = graph,
min_size = min %||% 0,
max_size = max %||% 0
)
res <- lapply(res, `+`, 1)

if (igraph_opt("return.vs.es")) {
res <- lapply(res, unsafe_create_vs, graph = graph, verts = V(graph))
}

res
}

#' @rdname ivs
Expand Down
52 changes: 25 additions & 27 deletions R/community.R
Original file line number Diff line number Diff line change
Expand Up @@ -1311,12 +1311,12 @@ show_trace <- function(communities) {
#####################################################################

community.to.membership2 <- function(merges, vcount, steps) {
mode(merges) <- "numeric"
mode(vcount) <- "numeric"
mode(steps) <- "numeric"
on.exit(.Call(Rx_igraph_finalizer))
res <- .Call(Rx_igraph_community_to_membership2, merges - 1, vcount, steps)
res + 1
res <- community_to_membership_impl(
merges = merges - 1,
nodes = vcount,
steps = steps
)
res$membership + 1
}

#####################################################################
Expand Down Expand Up @@ -1480,19 +1480,18 @@ cluster_spinglass <- function(

on.exit(.Call(Rx_igraph_finalizer))
if (is.null(vertex) || length(vertex) == 0) {
res <- .Call(
Rx_igraph_spinglass_community,
graph,
weights,
as.numeric(spins),
as.logical(parupdate),
as.numeric(start.temp),
as.numeric(stop.temp),
as.numeric(cool.fact),
as.numeric(update.rule),
as.numeric(gamma),
as.numeric(implementation),
as.numeric(gamma.minus)
res <- community_spinglass_impl(
graph = graph,
weights = weights,
spins = spins,
parupdate = parupdate,
starttemp = start.temp,
stoptemp = stop.temp,
coolfact = cool.fact,
update_rule = if (update.rule == 0) "simple" else "config",
gamma = gamma,
implementation = if (implementation == 0) "orig" else "neg",
lambda = gamma.minus
)
res$algorithm <- "spinglass"
res$vcount <- vcount(graph)
Expand All @@ -1502,14 +1501,13 @@ cluster_spinglass <- function(
}
class(res) <- "communities"
} else {
res <- .Call(
Rx_igraph_spinglass_my_community,
graph,
weights,
as_igraph_vs(graph, vertex) - 1,
as.numeric(spins),
as.numeric(update.rule),
as.numeric(gamma)
res <- community_spinglass_single_impl(
graph = graph,
weights = weights,
vertex = as_igraph_vs(graph, vertex) - 1,
spins = spins,
update_rule = if (update.rule == 0) "simple" else "config",
gamma = gamma
)
res$community <- res$community + 1
}
Expand Down
44 changes: 17 additions & 27 deletions R/foreign.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,10 @@ write_graph <- function(
################################################################

read.graph.edgelist <- function(file, n = 0, directed = TRUE) {
on.exit(.Call(Rx_igraph_finalizer))
.Call(
Rx_igraph_read_graph_edgelist,
file,
as.numeric(n),
as.logical(directed)
read_graph_edgelist_impl(
instream = file,
n = n,
directed = directed
)
}

Expand Down Expand Up @@ -593,19 +591,12 @@ read.graph.lgl <- function(
weights = c("auto", "yes", "no"),
directed = FALSE
) {
weights <- switch(
igraph_match_arg(weights),
"no" = 0L,
"yes" = 1L,
"auto" = 2L
)
on.exit(.Call(Rx_igraph_finalizer))
.Call(
Rx_igraph_read_graph_lgl,
file,
as.logical(names),
weights,
as.logical(directed)
weights <- igraph_match_arg(weights)
read_graph_lgl_impl(
instream = file,
names = names,
weights = weights,
directed = directed
)
}

Expand Down Expand Up @@ -656,6 +647,7 @@ write.graph.pajek <- function(graph, file) {
}

read.graph.dimacs <- function(file, directed = TRUE) {
on.exit(.Call(Rx_igraph_finalizer))
res <- .Call(Rx_igraph_read_graph_dimacs, file, as.logical(directed))
if (res[[1]][1] == "max") {
graph <- res[[2]]
Expand Down Expand Up @@ -689,14 +681,12 @@ write.graph.dimacs <- function(
capacity <- E(graph)$capacity
}

on.exit(.Call(Rx_igraph_finalizer))
.Call(
Rx_igraph_write_graph_dimacs,
graph,
file,
as.numeric(source),
as.numeric(target),
as.numeric(capacity)
write_graph_dimacs_flow_impl(
graph = graph,
outstream = file,
source = source,
target = target,
capacity = capacity
)
}

Expand Down
8 changes: 3 additions & 5 deletions R/interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ add_edges <- function(graph, edges, ..., attr = list()) {
}

edges.orig <- ecount(graph)
on.exit(.Call(Rx_igraph_finalizer))
graph <- .Call(
Rx_igraph_add_edges_manual,
graph,
as_igraph_vs(graph, edges) - 1
graph <- add_edges_impl(
graph = graph,
edges = edges
)
edges.new <- ecount(graph)

Expand Down
1 change: 1 addition & 0 deletions R/iterators.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ get_es_graph_id <- get_vs_graph_id <- function(seq) {
#' @export
identical_graphs <- function(g1, g2, attrs = TRUE) {
stopifnot(is_igraph(g1), is_igraph(g2))
on.exit(.Call(Rx_igraph_finalizer))
.Call(Rx_igraph_identical_graphs, g1, g2, as.logical(attrs))
}

Expand Down
Loading