Skip to content

Commit f9a8ecc

Browse files
authored
perf: Accelerate check if an index sequence corresponds to the entire list of vertices (#1818)
2 parents ac6a4bc + 9c1348f commit f9a8ecc

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

R/attributes.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,7 @@ vertex.attributes <- function(graph, index = V(graph)) {
516516
res <- .Call(R_igraph_mybracket2_copy, graph, igraph_t_idx_attr, igraph_attr_idx_vertex)
517517

518518
if (!missing(index)) {
519-
index_is_natural_sequence <- (length(index) == vcount(graph) &&
520-
identical(index, seq(1, vcount(graph))))
521-
if (!index_is_natural_sequence) {
519+
if (!index_is_natural_sequence(index, graph)) {
522520
for (i in seq_along(res)) {
523521
res[[i]] <- res[[i]][index]
524522
}
@@ -556,8 +554,7 @@ set_value_at <- function(value, idx, length_out) {
556554
}
557555
}
558556

559-
index_is_natural_sequence <- (length(index) == vcount(graph) && all(index == V(graph)))
560-
if (!missing(index) && !index_is_natural_sequence) {
557+
if (!missing(index) && !index_is_natural_sequence(index, graph)) {
561558
value <- map(value, set_value_at, idx = index, length_out = length(V(graph)))
562559
}
563560

@@ -1198,3 +1195,7 @@ assert_named_list <- function(value) {
11981195
cli::cli_abort("{.arg value} must be a named list with unique names")
11991196
}
12001197
}
1198+
1199+
index_is_natural_sequence <- function(index, graph) {
1200+
length(index) == vcount(graph) && all(index == seq_len(vcount(graph)))
1201+
}

0 commit comments

Comments
 (0)