Skip to content

Commit 8892ac1

Browse files
Copilotkrlmlr
andauthored
refactor: Switch from .Call() to autogenerated _impl functions (#2443)
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 41c195b commit 8892ac1

File tree

14 files changed

+116
-159
lines changed

14 files changed

+116
-159
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ See `tools/README.md` for guidelines on code generation using the Stimulus frame
120120
Do not commit: `*.d`, `*.o`, `*.so` files in `src/`, and `tests/testthat/testthat-problems.rds`.
121121
These are build artifacts that are regenerated automatically (see `src/README.md` for details on dependency tracking).
122122

123-
Revert all changes to `*.dd` but keep these files version-controlled.
123+
Careful with changes to `*.dd`, keep system headers out, only expect changes if new source files are added or their local dependencies change.
124124

125125
## Testing
126126

R/centralization.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,12 @@ centr_betw <- function(graph, directed = TRUE, normalized = TRUE) {
478478
directed <- as.logical(directed)
479479
normalized <- as.logical(normalized)
480480

481-
on.exit(.Call(R_igraph_finalizer))
482481
# Function call
483-
res <- .Call(R_igraph_centralization_betweenness, graph, directed, normalized)
482+
res <- centralization_betweenness_impl(
483+
graph = graph,
484+
directed = directed,
485+
normalized = normalized
486+
)
484487

485488
res
486489
}

R/community.R

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -984,9 +984,13 @@ modularity_matrix <- function(
984984
resolution <- as.numeric(resolution)
985985
directed <- as.logical(directed)
986986

987-
on.exit(.Call(R_igraph_finalizer))
988987
# Function call
989-
res <- .Call(R_igraph_modularity_matrix, graph, weights, resolution, directed)
988+
res <- modularity_matrix_impl(
989+
graph = graph,
990+
weights = weights,
991+
resolution = resolution,
992+
directed = directed
993+
)
990994

991995
res
992996
}
@@ -2516,9 +2520,12 @@ cluster_louvain <- function(graph, weights = NULL, resolution = 1) {
25162520
}
25172521
resolution <- as.numeric(resolution)
25182522

2519-
on.exit(.Call(R_igraph_finalizer))
25202523
# Function call
2521-
res <- .Call(R_igraph_community_multilevel, graph, weights, resolution)
2524+
res <- community_multilevel_impl(
2525+
graph = graph,
2526+
weights = weights,
2527+
resolution = resolution
2528+
)
25222529
if (igraph_opt("add.vertex.names") && is_named(graph)) {
25232530
res$names <- V(graph)$name
25242531
}
@@ -3047,16 +3054,8 @@ i_compare <- function(
30473054
} else {
30483055
as.numeric(as.factor(comm2))
30493056
}
3050-
method <- switch(
3051-
igraph.match.arg(method),
3052-
vi = 0L,
3053-
nmi = 1L,
3054-
split.join = 2L,
3055-
rand = 3L,
3056-
adjusted.rand = 4L
3057-
)
3058-
on.exit(.Call(R_igraph_finalizer))
3059-
res <- .Call(R_igraph_compare_communities, comm1, comm2, method)
3057+
method <- igraph.match.arg(method)
3058+
res <- compare_communities_impl(comm1 = comm1, comm2 = comm2, method = method)
30603059
res
30613060
}
30623061

@@ -3104,8 +3103,7 @@ split_join_distance <- function(comm1, comm2) {
31043103
} else {
31053104
as.numeric(comm2)
31063105
}
3107-
on.exit(.Call(R_igraph_finalizer))
3108-
res <- .Call(R_igraph_split_join_distance, comm1, comm2)
3106+
res <- split_join_distance_impl(comm1 = comm1, comm2 = comm2)
31093107
unlist(res)
31103108
}
31113109

R/conversion.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -624,17 +624,14 @@ as_undirected <- function(
624624
) {
625625
# Argument checks
626626
ensure_igraph(graph)
627-
mode <- switch(
628-
igraph.match.arg(mode),
629-
"collapse" = 1L,
630-
"each" = 0L,
631-
"mutual" = 2L
632-
)
633-
edge.attr.comb <- igraph.i.attribute.combination(edge.attr.comb)
627+
mode <- igraph.match.arg(mode)
634628

635-
on.exit(.Call(R_igraph_finalizer))
636629
# Function call
637-
res <- .Call(R_igraph_to_undirected, graph, mode, edge.attr.comb)
630+
res <- to_undirected_impl(
631+
graph = graph,
632+
mode = mode,
633+
edge.attr.comb = edge.attr.comb
634+
)
638635

639636
res
640637
}

R/flow.R

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -906,17 +906,10 @@ dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) {
906906
}
907907
root <- as_igraph_vs(graph, root)
908908

909-
mode <- switch(
910-
igraph.match.arg(mode),
911-
"out" = 1,
912-
"in" = 2,
913-
"all" = 3,
914-
"total" = 3
915-
)
909+
mode <- igraph.match.arg(mode)
916910

917-
on.exit(.Call(R_igraph_finalizer))
918911
# Function call
919-
res <- .Call(R_igraph_dominator_tree, graph, root - 1, mode)
912+
res <- dominator_tree_impl(graph = graph, root = root, mode = mode)
920913
if (igraph_opt("return.vs.es")) {
921914
res$leftout <- create_vs(graph, res$leftout)
922915
}

R/hrg.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,8 @@ fit_hrg <- function(graph, hrg = NULL, start = FALSE, steps = 0) {
251251
start <- as.logical(start)
252252
steps <- as.numeric(steps)
253253

254-
on.exit(.Call(R_igraph_finalizer))
255254
# Function call
256-
res <- .Call(R_igraph_hrg_fit, graph, hrg, start, steps)
255+
res <- hrg_fit_impl(graph = graph, hrg = hrg, start = start, steps = steps)
257256

258257
if (igraph_opt("add.vertex.names") && is_named(graph)) {
259258
res$names <- V(graph)$name

R/incidence.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ graph_incidence_build <- function(
8080

8181
# Handle dense unweighted matrices first
8282
if (!inherits(incidence, "Matrix") && is.null(weighted)) {
83-
mode(incidence) <- "double"
84-
on.exit(.Call(R_igraph_finalizer))
85-
86-
mode_num <- switch(mode, "out" = 1, "in" = 2, "all" = 3, "total" = 3)
87-
res <- .Call(R_igraph_biadjacency, incidence, directed, mode_num, multiple)
83+
res <- biadjacency_impl(
84+
incidence = incidence,
85+
directed = directed,
86+
mode = mode,
87+
multiple = multiple
88+
)
8889
return(set_vertex_attr(res$graph, "type", value = res$types))
8990
}
9091

R/make.R

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ line.graph <- function(graph) {
267267
lifecycle::deprecate_soft("2.1.0", "line.graph()", "make_line_graph()")
268268
ensure_igraph(graph)
269269

270-
on.exit(.Call(R_igraph_finalizer))
271-
res <- .Call(R_igraph_linegraph, graph)
270+
res <- linegraph_impl(graph = graph)
272271
if (igraph_opt("add.params")) {
273272
res$name <- "Line graph"
274273
}
@@ -473,8 +472,7 @@ graph.lattice <- function(
473472
graph.kautz <- function(m, n) {
474473
# nocov start
475474
lifecycle::deprecate_soft("2.1.0", "graph.kautz()", "make_kautz_graph()")
476-
on.exit(.Call(R_igraph_finalizer))
477-
res <- .Call(R_igraph_kautz, as.numeric(m), as.numeric(n))
475+
res <- kautz_impl(m = m, n = n)
478476
if (igraph_opt("add.params")) {
479477
res$name <- sprintf("Kautz graph %i-%i", m, n)
480478
res$m <- m
@@ -500,13 +498,8 @@ graph.full.citation <- function(n, directed = TRUE) {
500498
"graph.full.citation()",
501499
"make_full_citation_graph()"
502500
)
503-
# Argument checks
504-
n <- as.numeric(n)
505-
directed <- as.logical(directed)
506-
507-
on.exit(.Call(R_igraph_finalizer))
508501
# Function call
509-
res <- .Call(R_igraph_full_citation, n, directed)
502+
res <- full_citation_impl(n = n, directed = directed)
510503

511504
res <- set_graph_attr(res, "name", "Full citation graph")
512505
res
@@ -545,8 +538,7 @@ graph.full.bipartite <- function(
545538
"total" = 3
546539
)
547540

548-
on.exit(.Call(R_igraph_finalizer))
549-
res <- .Call(R_igraph_full_bipartite, n1, n2, as.logical(directed), mode1)
541+
res <- full_bipartite_impl(n1 = n1, n2 = n2, directed = directed, mode = mode)
550542
if (igraph_opt("add.params")) {
551543
res$graph$name <- "Full bipartite graph"
552544
res$n1 <- n1
@@ -644,13 +636,8 @@ graph.extended.chordal.ring <- function(n, w, directed = FALSE) {
644636
graph.empty <- function(n = 0, directed = TRUE) {
645637
# nocov start
646638
lifecycle::deprecate_soft("2.1.0", "graph.empty()", "make_empty_graph()")
647-
# Argument checks
648-
n <- as.numeric(n)
649-
directed <- as.logical(directed)
650-
651-
on.exit(.Call(R_igraph_finalizer))
652639
# Function call
653-
res <- .Call(R_igraph_empty, n, directed)
640+
res <- empty_impl(n = n, directed = directed)
654641

655642
res
656643
} # nocov end
@@ -672,8 +659,7 @@ graph.de.bruijn <- function(m, n) {
672659
"graph.de.bruijn()",
673660
"make_de_bruijn_graph()"
674661
)
675-
on.exit(.Call(R_igraph_finalizer))
676-
res <- .Call(R_igraph_de_bruijn, as.numeric(m), as.numeric(n))
662+
res <- de_bruijn_impl(m = m, n = n)
677663
if (igraph_opt("add.params")) {
678664
res$name <- sprintf("De-Bruijn graph %i-%i", m, n)
679665
res$m <- m
@@ -743,8 +729,7 @@ graph.bipartite <- function(types, edges, directed = FALSE) {
743729
graph.atlas <- function(n) {
744730
# nocov start
745731
lifecycle::deprecate_soft("2.1.0", "graph.atlas()", "graph_from_atlas()")
746-
on.exit(.Call(R_igraph_finalizer))
747-
res <- .Call(R_igraph_atlas, as.numeric(n))
732+
res <- atlas_impl(number = n)
748733
if (igraph_opt("add.params")) {
749734
res$name <- sprintf("Graph from the Atlas #%i", n)
750735
res$n <- n
@@ -2214,8 +2199,7 @@ from_prufer <- function(...) constructor_spec(make_from_prufer, ...)
22142199
#' graph_from_atlas(sample(0:1252, 1))
22152200
#' graph_from_atlas(sample(0:1252, 1))
22162201
graph_from_atlas <- function(n) {
2217-
on.exit(.Call(R_igraph_finalizer))
2218-
res <- .Call(R_igraph_atlas, as.numeric(n))
2202+
res <- atlas_impl(number = n)
22192203
if (igraph_opt("add.params")) {
22202204
res$name <- sprintf("Graph from the Atlas #%i", n)
22212205
res$n <- n
@@ -2311,8 +2295,7 @@ chordal_ring <- function(...) constructor_spec(make_chordal_ring, ...)
23112295
make_line_graph <- function(graph) {
23122296
ensure_igraph(graph)
23132297

2314-
on.exit(.Call(R_igraph_finalizer))
2315-
res <- .Call(R_igraph_linegraph, graph)
2298+
res <- linegraph_impl(graph = graph)
23162299
if (igraph_opt("add.params")) {
23172300
res$name <- "Line graph"
23182301
}
@@ -2358,8 +2341,7 @@ line_graph <- function(...) constructor_spec(make_line_graph, ...)
23582341
#' make_de_bruijn_graph(2, 2)
23592342
#' make_line_graph(g)
23602343
make_de_bruijn_graph <- function(m, n) {
2361-
on.exit(.Call(R_igraph_finalizer))
2362-
res <- .Call(R_igraph_de_bruijn, as.numeric(m), as.numeric(n))
2344+
res <- de_bruijn_impl(m = m, n = n)
23632345
if (igraph_opt("add.params")) {
23642346
res$name <- sprintf("De-Bruijn graph %i-%i", m, n)
23652347
res$m <- m
@@ -2403,8 +2385,7 @@ de_bruijn_graph <- function(...) constructor_spec(make_de_bruijn_graph, ...)
24032385
#' make_kautz_graph(2, 2)
24042386
#'
24052387
make_kautz_graph <- function(m, n) {
2406-
on.exit(.Call(R_igraph_finalizer))
2407-
res <- .Call(R_igraph_kautz, as.numeric(m), as.numeric(n))
2388+
res <- kautz_impl(m = m, n = n)
24082389
if (igraph_opt("add.params")) {
24092390
res$name <- sprintf("Kautz graph %i-%i", m, n)
24102391
res$m <- m
@@ -2466,8 +2447,7 @@ make_full_bipartite_graph <- function(
24662447
"total" = 3
24672448
)
24682449

2469-
on.exit(.Call(R_igraph_finalizer))
2470-
res <- .Call(R_igraph_full_bipartite, n1, n2, as.logical(directed), mode1)
2450+
res <- full_bipartite_impl(n1 = n1, n2 = n2, directed = directed, mode = mode)
24712451
if (igraph_opt("add.params")) {
24722452
res$graph$name <- "Full bipartite graph"
24732453
res$n1 <- n1
@@ -2581,13 +2561,8 @@ bipartite_graph <- function(...) constructor_spec(make_bipartite_graph, ...)
25812561
#' @examples
25822562
#' print_all(make_full_citation_graph(10))
25832563
make_full_citation_graph <- function(n, directed = TRUE) {
2584-
# Argument checks
2585-
n <- as.numeric(n)
2586-
directed <- as.logical(directed)
2587-
2588-
on.exit(.Call(R_igraph_finalizer))
25892564
# Function call
2590-
res <- .Call(R_igraph_full_citation, n, directed)
2565+
res <- full_citation_impl(n = n, directed = directed)
25912566

25922567
res <- set_graph_attr(res, "name", "Full citation graph")
25932568
res

R/sgm.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ solve_LSAP <- function(x, maximum = FALSE) {
2424
if (maximum) {
2525
x <- max(x) - x
2626
}
27-
storage.mode(x) <- "double"
28-
out <- .Call(R_igraph_solve_lsap, x, as.numeric(nc)) + 1L
27+
out <- solve_lsap_impl(c = x, n = as.numeric(nc)) + 1L
2928
out[seq_len(nr)]
3029
}
3130

R/simple.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ simplify_and_colorize <- function(graph) {
118118
# Argument checks
119119
ensure_igraph(graph)
120120

121-
on.exit(.Call(R_igraph_finalizer))
122121
# Function call
123-
res <- .Call(R_igraph_simplify_and_colorize, graph)
122+
res <- simplify_and_colorize_impl(graph = graph)
124123

125124
V(res$res)$color <- res$vertex_color
126125
E(res$res)$color <- res$edge_color

0 commit comments

Comments
 (0)