@@ -223,7 +223,6 @@ get.adjacency.dense <- function(
223223 graph ,
224224 type = c(" both" , " upper" , " lower" ),
225225 attr = NULL ,
226- weights = NULL ,
227226 loops = c(" once" , " twice" , " ignore" ),
228227 names = TRUE
229228) {
@@ -243,20 +242,17 @@ get.adjacency.dense <- function(
243242 )
244243 }
245244 loops <- igraph_match_arg(loops )
246- loops <- switch (loops , " ignore" = 0L , " twice" = 1L , " once" = 2L )
247-
248- if (! is.null(weights )) {
249- weights <- as.numeric(weights )
245+ # Map "ignore" to "none" for get_adjacency_impl
246+ if (loops == " ignore" ) {
247+ loops <- " none"
250248 }
251249
252250 if (is.null(attr )) {
253- on.exit(.Call(Rx_igraph_finalizer ))
254- type <- switch (type , " upper" = 0 , " lower" = 1 , " both" = 2 )
255- res <- .Call(
256- Rx_igraph_get_adjacency ,
251+ # FIXME: Use get_adjacency_impl() also for non-NULL attr
252+ res <- get_adjacency_impl(
257253 graph ,
258- as.numeric( type ) ,
259- weights ,
254+ type ,
255+ weights = numeric () ,
260256 loops
261257 )
262258 } else {
@@ -287,67 +283,33 @@ get.adjacency.sparse <- function(
287283
288284 type <- igraph_match_arg(type )
289285
290- vc <- vcount(graph )
291-
292- el <- as_edgelist(graph , names = FALSE )
293- use.last.ij <- FALSE
294-
295- if (! is.null(attr )) {
286+ # Prepare weights parameter
287+ if (is.null(attr )) {
288+ weights <- numeric ()
289+ } else {
296290 attr <- as.character(attr )
297291 if (! attr %in% edge_attr_names(graph )) {
298292 cli :: cli_abort(" No such edge attribute" , call = call )
299293 }
300- value <- edge_attr(graph , name = attr )
301- if (! is.numeric(value ) && ! is.logical(value )) {
294+ weights <- edge_attr(graph , name = attr )
295+ if (! is.numeric(weights ) && ! is.logical(weights )) {
302296 cli :: cli_abort(
303297 " Matrices must be either numeric or logical, and the edge attribute is not" ,
304298 call = call
305299 )
306300 }
307- } else {
308- value <- rep(1 , nrow(el ))
309301 }
310302
311- if (is_directed(graph )) {
312- res <- Matrix :: sparseMatrix(
313- dims = c(vc , vc ),
314- i = el [, 1 ],
315- j = el [, 2 ],
316- x = value ,
317- use.last.ij = use.last.ij
318- )
319- } else {
320- if (type == " upper" ) {
321- # # upper
322- res <- Matrix :: sparseMatrix(
323- dims = c(vc , vc ),
324- i = pmin(el [, 1 ], el [, 2 ]),
325- j = pmax(el [, 1 ], el [, 2 ]),
326- x = value ,
327- use.last.ij = use.last.ij
328- )
329- } else if (type == " lower" ) {
330- # # lower
331- res <- Matrix :: sparseMatrix(
332- dims = c(vc , vc ),
333- i = pmax(el [, 1 ], el [, 2 ]),
334- j = pmin(el [, 1 ], el [, 2 ]),
335- x = value ,
336- use.last.ij = use.last.ij
337- )
338- } else if (type == " both" ) {
339- # # both
340- res <- Matrix :: sparseMatrix(
341- dims = c(vc , vc ),
342- i = pmin(el [, 1 ], el [, 2 ]),
343- j = pmax(el [, 1 ], el [, 2 ]),
344- x = value ,
345- symmetric = TRUE ,
346- use.last.ij = use.last.ij
347- )
348- res <- as(res , " generalMatrix" )
349- }
350- }
303+ # Use the library implementation
304+ tmp <- get_adjacency_sparse_impl(
305+ graph ,
306+ type ,
307+ weights ,
308+ loops = " once"
309+ )
310+
311+ # Convert to proper Matrix object
312+ res <- igraph.i.spMatrix(tmp )
351313
352314 if (names && " name" %in% vertex_attr_names(graph )) {
353315 colnames(res ) <- rownames(res ) <- V(graph )$ name
@@ -427,7 +389,6 @@ as_adjacency_matrix <- function(
427389 graph ,
428390 type = type ,
429391 attr = attr ,
430- weights = NULL ,
431392 names = names ,
432393 loops = " once"
433394 )
0 commit comments