From 9c1348f3240d814c02728a517e9fb7b5ccae5049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 10 Apr 2025 10:53:14 +0200 Subject: [PATCH] perf: Accelerate check if an index sequence corresponds to the entire list of vertices --- R/attributes.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 89fa53e2eed..4dd27d8a241 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -516,9 +516,7 @@ vertex.attributes <- function(graph, index = V(graph)) { res <- .Call(R_igraph_mybracket2_copy, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) if (!missing(index)) { - index_is_natural_sequence <- (length(index) == vcount(graph) && - identical(index, seq(1, vcount(graph)))) - if (!index_is_natural_sequence) { + if (!index_is_natural_sequence(index, graph)) { for (i in seq_along(res)) { res[[i]] <- res[[i]][index] } @@ -556,8 +554,7 @@ set_value_at <- function(value, idx, length_out) { } } - index_is_natural_sequence <- (length(index) == vcount(graph) && all(index == V(graph))) - if (!missing(index) && !index_is_natural_sequence) { + if (!missing(index) && !index_is_natural_sequence(index, graph)) { value <- map(value, set_value_at, idx = index, length_out = length(V(graph))) } @@ -1198,3 +1195,7 @@ assert_named_list <- function(value) { cli::cli_abort("{.arg value} must be a named list with unique names") } } + +index_is_natural_sequence <- function(index, graph) { + length(index) == vcount(graph) && all(index == seq_len(vcount(graph))) +}