From 7c4b1543cdd87eb88dac0d64375c35c36846231b Mon Sep 17 00:00:00 2001 From: edzer Date: Tue, 16 Apr 2024 16:30:23 +0200 Subject: [PATCH] add the == and != Ops for sgbp --- R/sgbp.R | 24 +++++++++++++++++------- man/sgbp.Rd | 12 ++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/R/sgbp.R b/R/sgbp.R index 80fbc6dfc..59326c059 100644 --- a/R/sgbp.R +++ b/R/sgbp.R @@ -80,17 +80,27 @@ dim.sgbp = function(x) { c(length(x), attr(x, "ncol")) } +#' @name sgbp +#' @param e1 object of class `sgbp` +#' @param e2 object of class `sgbp` #' @export +#' @details `==` compares only the dimension and index values, not the attributes of two `sgbp` object; use `identical` to check for equality of everything. Ops.sgbp = function(e1, e2) { - if (.Generic != "!") - stop("only ! operator is supported for sgbp objects") - nc = 1:attr(e1, "ncol") - sgbp(lapply(e1, function(x) setdiff(nc, x)), - predicate = paste0("!", attr(e1, "predicate")), - region.id = attr(e1, "region.id"), - ncol = attr(e1, "ncol")) + switch(.Generic, + "!" = { + nc = 1:attr(e1, "ncol") + sgbp(lapply(e1, function(x) setdiff(nc, x)), + predicate = paste0("!", attr(e1, "predicate")), + region.id = attr(e1, "region.id"), + ncol = attr(e1, "ncol")) + }, + "==" = (length(e1) == length(e2)) && all(mapply(function(x,y) identical(x, y), e1, e2)), + "!=" = return(!(e1 == e2)), + stop("only operators !, == and != are supported for sgbp objects") + ) } +#' @name sgbp #' @export as.data.frame.sgbp = function(x, ...) { data.frame(row.id = rep(seq_along(x), lengths(x)), col.id = unlist(x)) diff --git a/man/sgbp.Rd b/man/sgbp.Rd index 9d12e7b41..90a744a54 100644 --- a/man/sgbp.Rd +++ b/man/sgbp.Rd @@ -6,6 +6,8 @@ \alias{t.sgbp} \alias{as.matrix.sgbp} \alias{dim.sgbp} +\alias{Ops.sgbp} +\alias{as.data.frame.sgbp} \title{Methods for dealing with sparse geometry binary predicate lists} \usage{ \method{print}{sgbp}(x, ..., n = 10, max_nb = 10) @@ -15,6 +17,10 @@ \method{as.matrix}{sgbp}(x, ...) \method{dim}{sgbp}(x) + +\method{Ops}{sgbp}(e1, e2) + +\method{as.data.frame}{sgbp}(x, ...) } \arguments{ \item{x}{object of class \code{sgbp}} @@ -24,10 +30,16 @@ \item{n}{integer; maximum number of items to print} \item{max_nb}{integer; maximum number of neighbours to print for each item} + +\item{e1}{object of class \code{sgbp}} + +\item{e2}{object of class \code{sgbp}} } \description{ Methods for dealing with sparse geometry binary predicate lists } \details{ \code{sgbp} are sparse matrices, stored as a list with integer vectors holding the ordered \code{TRUE} indices of each row. This means that for a dense, \eqn{m \times n}{m x n} matrix \code{Q} and a list \code{L}, if \code{Q[i,j]} is \code{TRUE} then \eqn{j} is an element of \code{L[[i]]}. Reversed: when \eqn{k} is the value of \code{L[[i]][j]}, then \code{Q[i,k]} is \code{TRUE}. + +\code{==} compares only the dimension and index values, not the attributes of two \code{sgbp} object; use \code{identical} to check for equality of everything. }