# Auto-generated by internal/make-exports.R

#' RcppRoll
#'
#' Efficient windowed / rolling operations. Each function
#' here applies an operation over a moving window of
#' size \code{n}, with (customizable) weights specified
#' through \code{weights}.
#'
#' The functions postfixed with \code{l} and \code{r}
#' are convenience wrappers that set \strong{l}eft
#' / \strong{r}ight alignment of the windowed operations.
#'
#' @name RcppRoll-exports
#' @param x A numeric vector or a numeric matrix.
#' @param n The window size. Ignored when \code{weights} is non-\code{NULL}.
#' @param weights A vector of length \code{n}, giving the weights for each
#'   element within a window. If \code{NULL}, we take unit weights of width \code{n}.
#' @param by Calculate at every \code{by}-th point rather than every point.
#' @param fill Either an empty vector (no fill), or a vector (recycled to)
#'   length 3 giving left, center and right fills.
#' @param partial Compute windows at the edges of \code{x} over however
#'   many elements are in range, rather than filling them? Cannot be
#'   combined with \code{weights}, and \code{fill} does not apply.
#' @param align Align windows on the \code{"left"}, \code{"center"} or
#'   \code{"right"}.
#' @param normalize Normalize window weights, such that they sum to \code{n}.
#' @param na.rm Remove missing values?
NULL

# Argument checking shared by every wrapper below. 'missing_n' and
# 'missing_fill' come from the caller, since missing() only speaks for the frame
# it is called in; conditions are raised against the caller too, so that a user
# sees the roll_* call they made rather than this helper. Returns the window
# size to use, which 'weights' overrides when the two disagree.
checkRollArgs <- function(n, weights, partial, missing_n, missing_fill) {

  call <- sys.call(-1L)

  if (!is.logical(partial) || length(partial) != 1L || is.na(partial)) {
    stop(simpleError("'partial' should be TRUE or FALSE", call))
  }

  if (partial) {
    if (!is.null(weights))
      stop(simpleError(
        "'partial = TRUE' is not supported together with 'weights'", call))
    if (!missing_fill)
      warning(simpleWarning("'fill' is ignored when 'partial = TRUE'", call))
  }

  if (!missing_n && !is.null(weights) && !isTRUE(length(weights) == n)) {
    warning(simpleWarning(sprintf(
      "'n' is ignored when 'weights' is supplied; using 'n = %i' rather than 'n = %i'",
      length(weights), as.integer(n)
    ), call))
    n <- length(weights)
  }

  # checked after 'weights' has had its say, since empty weights would
  # otherwise smuggle in a window of size zero
  n <- as.integer(n)[1L]
  if (!isTRUE(n >= 1L)) {
    stop(simpleError("'n' should be a positive integer", call))
  }

  n
}
#' @rdname RcppRoll-exports
#' @export
roll_mean <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_mean_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_meanr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_mean_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_meanl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_mean_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_median <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_median_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_medianr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_median_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_medianl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_median_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_min <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_min_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_minr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_min_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_minl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_min_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_max <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_max_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_maxr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_max_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_maxl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_max_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_prod <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_prod_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_prodr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_prod_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_prodl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_prod_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_sum <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sum_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_sumr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sum_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_suml <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sum_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_sd <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sd_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_sdr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sd_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_sdl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_sd_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
#' @rdname RcppRoll-exports
#' @export
roll_var <- function(x,
                    n = 1L,
                    weights = NULL,
                    by = 1L,
                    fill = numeric(0),
                    partial = FALSE,
                    align = c("center", "left", "right"),
                    normalize = TRUE,
                    na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_var_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_varr <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "right",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_var_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}

##' @rdname RcppRoll-exports
##' @export
roll_varl <- function(x,
                     n = 1L,
                     weights = NULL,
                     by = 1L,
                     fill = NA,
                     partial = FALSE,
                     align = "left",
                     normalize = TRUE,
                     na.rm = FALSE)
{
  n <- checkRollArgs(n, weights, partial, missing(n), missing(fill))

  .Call(
    C_roll_var_impl,
    x,
    as.integer(n),
    as.numeric(weights),
    as.integer(by),
    as.numeric(fill),
    as.logical(partial),
    as.character(match.arg(align)),
    as.logical(normalize),
    as.logical(na.rm)
  )
}
