Skip to content

Commit

Permalink
refactor: move variables closer to their usage, add assert_character()
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Mar 26, 2024
1 parent 8609d6b commit 916959e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) {

i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE) {
ensure_igraph(graph)
assert_character(name)

if (is.null(value)) {
return(graph)
Expand All @@ -472,19 +473,19 @@ i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE
value <- as.numeric(value)
}

single <- is_single_index(index)
complete <- is_complete_iterator(index)
if (!missing(index) && check) {
index <- as_igraph_vs(graph, index)
}
name <- as.character(name)

vattrs <- .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_vertex)

if (!complete && !(name %in% names(vattrs))) {
complete <- is_complete_iterator(index)
name_available <- (name %in% names(vattrs))
if (!complete && !name_available) {
vattrs[[name]] <- value[rep.int(NA_integer_, vcount(graph))]
}

single <- is_single_index(index)
if (single) {
vattrs[[name]][[index]] <- value
} else {
Expand Down
5 changes: 5 additions & 0 deletions R/utils-assert.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assert_character <- function(x, name) {
if (!inherits(x, "character")) {
cli::cli_abort("{.arg {name}} must be a character, not {.obj_type_friendly {x}}.")
}
}

0 comments on commit 916959e

Please sign in to comment.