Skip to content

Commit 7fb9f96

Browse files
Copilotkrlmlr
andauthored
chore: Use autogenerated _impl functions instead of .Call() (#2471)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: krlmlr <[email protected]> Co-authored-by: Kirill Müller <[email protected]>
1 parent 819d639 commit 7fb9f96

File tree

11 files changed

+153
-437
lines changed

11 files changed

+153
-437
lines changed

R/centrality.R

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -584,43 +584,14 @@ closeness <- function(
584584
normalized = FALSE,
585585
cutoff = -1
586586
) {
587-
# Argument checks
588-
ensure_igraph(graph)
589-
590-
vids <- as_igraph_vs(graph, vids)
591-
mode <- switch(
592-
igraph_match_arg(mode),
593-
"out" = 1,
594-
"in" = 2,
595-
"all" = 3,
596-
"total" = 3
597-
)
598-
if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
599-
weights <- E(graph)$weight
600-
}
601-
if (!is.null(weights) && any(!is.na(weights))) {
602-
weights <- as.numeric(weights)
603-
} else {
604-
weights <- NULL
605-
}
606-
normalized <- as.logical(normalized)
607-
cutoff <- as.numeric(cutoff)
608-
609-
on.exit(.Call(R_igraph_finalizer))
610-
# Function call
611-
res <- .Call(
612-
R_igraph_closeness_cutoff,
613-
graph,
614-
vids - 1,
615-
mode,
616-
weights,
617-
normalized,
618-
cutoff
587+
closeness_cutoff_impl(
588+
graph = graph,
589+
vids = vids,
590+
mode = mode,
591+
weights = weights,
592+
normalized = normalized,
593+
cutoff = cutoff
619594
)$res
620-
if (igraph_opt("add.vertex.names") && is_named(graph)) {
621-
names(res) <- V(graph)$name[vids]
622-
}
623-
res
624595
}
625596

626597
#' Deprecated version of `closeness()`

R/community.R

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -911,36 +911,18 @@ modularity.igraph <- function(
911911
directed = TRUE,
912912
...
913913
) {
914-
# Argument checks
915-
ensure_igraph(x)
916914
if (
917915
is.null(membership) || (!is.numeric(membership) && !is.factor(membership))
918916
) {
919917
cli::cli_abort("Membership is not a numerical vector")
920918
}
921-
membership <- as.numeric(membership)
922-
if (is.null(weights) && "weight" %in% edge_attr_names(x)) {
923-
weights <- E(x)$weight
924-
}
925-
if (!is.null(weights) && any(!is.na(weights))) {
926-
weights <- as.numeric(weights)
927-
} else {
928-
weights <- NULL
929-
}
930-
resolution <- as.numeric(resolution)
931-
directed <- as.logical(directed)
932-
933-
on.exit(.Call(R_igraph_finalizer))
934-
# Function call
935-
res <- .Call(
936-
R_igraph_modularity,
937-
x,
938-
membership - 1,
939-
weights,
940-
resolution,
941-
directed
919+
modularity_impl(
920+
graph = x,
921+
membership = membership,
922+
weights = weights,
923+
resolution = resolution,
924+
directed = directed
942925
)
943-
res
944926
}
945927

946928
#' @rdname communities
@@ -1784,17 +1766,9 @@ cluster_leiden <- function(
17841766
#' g <- make_graph("Zachary")
17851767
#' comms <- cluster_fluid_communities(g, 2)
17861768
cluster_fluid_communities <- function(graph, no.of.communities) {
1787-
# Argument checks
1788-
ensure_igraph(graph)
1789-
1790-
no.of.communities <- as.numeric(no.of.communities)
1791-
1792-
on.exit(.Call(R_igraph_finalizer))
1793-
# Function call
1794-
membership <- .Call(
1795-
R_igraph_community_fluid_communities,
1796-
graph,
1797-
no.of.communities
1769+
membership <- community_fluid_communities_impl(
1770+
graph = graph,
1771+
no_of_communities = no.of.communities
17981772
)
17991773

18001774
res <- list()

R/flow.R

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,20 +434,12 @@ min_cut <- function(
434434
capacity
435435
)
436436
} else {
437-
res <- .Call(
438-
R_igraph_st_mincut,
439-
graph,
440-
as_igraph_vs(graph, source) - 1,
441-
as_igraph_vs(graph, target) - 1,
442-
capacity
437+
res <- st_mincut_impl(
438+
graph = graph,
439+
source = source,
440+
target = target,
441+
capacity = capacity
443442
)
444-
# No need to add +1 here; R_igraph_st_mincut() is autogenerated and
445-
# adds +1 already
446-
if (igraph_opt("return.vs.es")) {
447-
res$cut <- create_es(graph, res$cut)
448-
res$partition1 <- create_vs(graph, res$partition1)
449-
res$partition2 <- create_vs(graph, res$partition2)
450-
}
451443
}
452444
}
453445

R/games.R

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,13 +1073,11 @@ sample_gnm <- function(n, m, directed = FALSE, loops = FALSE) {
10731073
type <- "gnm"
10741074
type1 <- switch(type, "gnp" = 0, "gnm" = 1)
10751075

1076-
on.exit(.Call(R_igraph_finalizer))
1077-
res <- .Call(
1078-
R_igraph_erdos_renyi_game_gnm,
1079-
as.numeric(n),
1080-
as.numeric(m),
1081-
as.logical(directed),
1082-
as.logical(loops)
1076+
res <- erdos_renyi_game_gnm_impl(
1077+
n = n,
1078+
m = m,
1079+
directed = directed,
1080+
loops = loops
10831081
)
10841082

10851083
if (igraph_opt("add.params")) {
@@ -1953,16 +1951,14 @@ sample_pref <- function(
19531951
))
19541952
}
19551953

1956-
on.exit(.Call(R_igraph_finalizer))
1957-
res <- .Call(
1958-
R_igraph_preference_game,
1959-
as.numeric(nodes),
1960-
as.numeric(types),
1961-
as.double(type.dist),
1962-
as.logical(fixed.sizes),
1963-
matrix(as.double(pref.matrix), types, types),
1964-
as.logical(directed),
1965-
as.logical(loops)
1954+
res <- preference_game_impl(
1955+
nodes = nodes,
1956+
types = types,
1957+
type_dist = type.dist,
1958+
fixed_sizes = fixed.sizes,
1959+
pref_matrix = pref.matrix,
1960+
directed = directed,
1961+
loops = loops
19661962
)
19671963
V(res[[1]])$type <- res[[2]] + 1
19681964
if (igraph_opt("add.params")) {
@@ -2004,15 +2000,13 @@ sample_asym_pref <- function(
20042000
))
20052001
}
20062002

2007-
on.exit(.Call(R_igraph_finalizer))
2008-
res <- .Call(
2009-
R_igraph_asymmetric_preference_game,
2010-
as.numeric(nodes),
2011-
as.numeric(types),
2012-
as.numeric(types),
2013-
matrix(as.double(type.dist.matrix), types, types),
2014-
matrix(as.double(pref.matrix), types, types),
2015-
as.logical(loops)
2003+
res <- asymmetric_preference_game_impl(
2004+
nodes = nodes,
2005+
out_types = types,
2006+
in_types = types,
2007+
type_dist_matrix = type.dist.matrix,
2008+
pref_matrix = pref.matrix,
2009+
loops = loops
20162010
)
20172011
V(res[[1]])$outtype <- res[[2]] + 1
20182012
V(res[[1]])$intype <- res[[3]] + 1

R/hrg.R

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -456,34 +456,12 @@ predict_edges <- function(
456456
num.samples = 10000,
457457
num.bins = 25
458458
) {
459-
# Argument checks
460-
ensure_igraph(graph)
461-
if (is.null(hrg)) {
462-
hrg <- list(
463-
left = c(),
464-
right = c(),
465-
prob = c(),
466-
edges = c(),
467-
vertices = c()
468-
)
469-
}
470-
hrg <- lapply(
471-
hrg[c("left", "right", "prob", "edges", "vertices")],
472-
as.numeric
473-
)
474-
start <- as.logical(start)
475-
num.samples <- as.numeric(num.samples)
476-
num.bins <- as.numeric(num.bins)
477-
478-
on.exit(.Call(R_igraph_finalizer))
479-
# Function call
480-
res <- .Call(
481-
R_igraph_hrg_predict,
482-
graph,
483-
hrg,
484-
start,
485-
num.samples,
486-
num.bins
459+
res <- hrg_predict_impl(
460+
graph = graph,
461+
hrg = hrg,
462+
start = start,
463+
num_samples = num.samples,
464+
num_bins = num.bins
487465
)
488466
res$edges <- matrix(res$edges, ncol = 2, byrow = TRUE)
489467
class(res$hrg) <- "igraphHRG"

R/layout.R

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,42 +1287,26 @@ layout_with_dh <- function(
12871287
weight.edge.crossings = 1.0 - sqrt(edge_density(graph)),
12881288
weight.node.edge.dist = 0.2 * (1 - edge_density(graph))
12891289
) {
1290-
# Argument checks
1291-
ensure_igraph(graph)
1292-
if (!is.null(coords)) {
1293-
coords[] <- as.numeric(coords)
1294-
use.seed <- TRUE
1295-
} else {
1290+
if (is.null(coords)) {
12961291
coords <- matrix(NA_real_, ncol = 2, nrow = 0)
12971292
use.seed <- FALSE
1293+
} else {
1294+
use.seed <- TRUE
12981295
}
1299-
maxiter <- as.numeric(maxiter)
1300-
fineiter <- as.numeric(fineiter)
1301-
cool.fact <- as.numeric(cool.fact)
1302-
weight.node.dist <- as.numeric(weight.node.dist)
1303-
weight.border <- as.numeric(weight.border)
1304-
weight.edge.lengths <- as.numeric(weight.edge.lengths)
1305-
weight.edge.crossings <- as.numeric(weight.edge.crossings)
1306-
weight.node.edge.dist <- as.numeric(weight.node.edge.dist)
13071296

1308-
on.exit(.Call(R_igraph_finalizer))
1309-
# Function call
1310-
res <- .Call(
1311-
R_igraph_layout_davidson_harel,
1312-
graph,
1313-
coords,
1314-
use.seed,
1315-
maxiter,
1316-
fineiter,
1317-
cool.fact,
1318-
weight.node.dist,
1319-
weight.border,
1320-
weight.edge.lengths,
1321-
weight.edge.crossings,
1322-
weight.node.edge.dist
1297+
layout_davidson_harel_impl(
1298+
graph = graph,
1299+
res = coords,
1300+
use_seed = use.seed,
1301+
maxiter = maxiter,
1302+
fineiter = fineiter,
1303+
cool_fact = cool.fact,
1304+
weight_node_dist = weight.node.dist,
1305+
weight_border = weight.border,
1306+
weight_edge_lengths = weight.edge.lengths,
1307+
weight_edge_crossings = weight.edge.crossings,
1308+
weight_node_edge_dist = weight.node.edge.dist
13231309
)
1324-
1325-
res
13261310
}
13271311

13281312

@@ -1596,35 +1580,22 @@ layout_with_gem <- function(
15961580
temp.min = 1 / 10,
15971581
temp.init = sqrt(max(vcount(graph), 1))
15981582
) {
1599-
# Argument checks
1600-
ensure_igraph(graph)
1601-
if (!is.null(coords)) {
1602-
coords[] <- as.numeric(coords)
1603-
use.seed <- TRUE
1604-
} else {
1583+
if (is.null(coords)) {
16051584
coords <- matrix(NA_real_, ncol = 2, nrow = 0)
16061585
use.seed <- FALSE
1586+
} else {
1587+
use.seed <- TRUE
16071588
}
16081589

1609-
maxiter <- as.numeric(maxiter)
1610-
temp.max <- as.numeric(temp.max)
1611-
temp.min <- as.numeric(temp.min)
1612-
temp.init <- as.numeric(temp.init)
1613-
1614-
on.exit(.Call(R_igraph_finalizer))
1615-
# Function call
1616-
res <- .Call(
1617-
R_igraph_layout_gem,
1618-
graph,
1619-
coords,
1620-
use.seed,
1621-
maxiter,
1622-
temp.max,
1623-
temp.min,
1624-
temp.init
1590+
layout_gem_impl(
1591+
graph = graph,
1592+
res = coords,
1593+
use_seed = use.seed,
1594+
maxiter = maxiter,
1595+
temp_max = temp.max,
1596+
temp_min = temp.min,
1597+
temp_init = temp.init
16251598
)
1626-
1627-
res
16281599
}
16291600

16301601

@@ -2304,34 +2275,15 @@ layout_with_sugiyama <- function(
23042275
weights = NULL,
23052276
attributes = c("default", "all", "none")
23062277
) {
2307-
# Argument checks
2308-
ensure_igraph(graph)
2309-
if (!is.null(layers)) {
2310-
layers <- as.numeric(layers) - 1
2311-
}
2312-
hgap <- as.numeric(hgap)
2313-
vgap <- as.numeric(vgap)
2314-
maxiter <- as.numeric(maxiter)
2315-
if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
2316-
weights <- E(graph)$weight
2317-
}
2318-
if (!is.null(weights) && any(!is.na(weights))) {
2319-
weights <- as.numeric(weights)
2320-
} else {
2321-
weights <- NULL
2322-
}
23232278
attributes <- igraph_match_arg(attributes)
23242279

2325-
on.exit(.Call(R_igraph_finalizer))
2326-
# Function call
2327-
res <- .Call(
2328-
R_igraph_layout_sugiyama,
2329-
graph,
2330-
layers,
2331-
hgap,
2332-
vgap,
2333-
maxiter,
2334-
weights
2280+
res <- layout_sugiyama_impl(
2281+
graph = graph,
2282+
layers = layers,
2283+
hgap = hgap,
2284+
vgap = vgap,
2285+
maxiter = maxiter,
2286+
weights = weights
23352287
)
23362288

23372289
# Flip the y coordinates, more natural this way

0 commit comments

Comments
 (0)