.packageName <- "spc"
# Computation of CUSUM steady-state ARLs (mean monitoring)
xcusum.ad <- function(k, h, mu1, mu0 = 0, sided = "one", r = 30) {
  if (k<0) 
    stop("k has to be non-negative")
  if (h<=0) 
    stop("h has to be positive")
  if (r<4) 
    stop("r is too small")
  if (r>30 & r<=50 & sided=="two") 
    warning("computation needs some time")
  if (r>50 & sided=="two") 
    warning("ought to be restricted to very fast CPUs")
  ctyp <- pmatch(sided, c("one", "two", "Crosier")) - 1
  if (is.na(ctyp)) 
    stop("invalid cusum type")
  ad <- .C("xcusum_ad",as.integer(ctyp),as.double(k),
           as.double(h),as.double(mu0),as.double(mu1),as.integer(r),
           ans=double(length=1),PACKAGE="spc")$ans 
  names(ad) <- "ad"
  return(ad)
}
# Computation of CUSUM ARLs (mean monitoring)
xcusum.arl <- function(k, h, mu, hs = 0, sided = "one", r = 30) {
  if (k<0) 
    stop("k has to be non-negative")
  if (h<=0) 
    stop("h has to be positive")
  if ( hs<0 | (sided=="two" & hs>h/2+k) 
            | (sided=="one" & hs>h/2+k) ) 
    stop("wrong headstart")
  if (r<4) 
    stop("r is too small")
  ctyp <- pmatch(sided, c("one", "two", "Crosier")) - 1
  if (is.na(ctyp)) 
    stop("invalid cusum type")
  arl <- .C("xcusum_arl",as.integer(ctyp),as.double(k),
            as.double(h),as.double(hs),as.double(mu),as.integer(r),
            ans=double(length=1),PACKAGE="spc")$ans
  names(arl) <- "arl"
  return (arl)
}
# Computation of CUSUM decision limits for given ARL (mean monitoring)
xcusum.crit <- function(k, L0, mu0 = 0, hs = 0, sided = "one", r = 30) {
  if (k<0)
    stop("k has to be non-negative")
  if (L0<1) 
    stop("L0 is too small")
  if (hs<0) 
    stop("wrong headstart")
  if (r<4) 
    stop("r is too small")
  ctyp <- pmatch(sided, c("one", "two", "Crosier")) - 1
  if (is.na(ctyp)) 
    stop("invalid cusum type")
  h <- .C("xcusum_crit",as.integer(ctyp),as.double(k),
          as.double(L0),as.double(hs),as.double(mu0),as.integer(r),
          ans=double(length=1),PACKAGE="spc")$ans 
  names(h) <- "h"
  return (h)
}

# Computation of EWMA steady-state ARLs (mean monitoring)
xewma.ad <- function(l,c,mu1,mu0=0,zr=0,sided="one",limits="fix",r=40) { 
  if (l<=0 || l>1) 
    stop("l has to be between 0 and 1")
  if (c<=0) 
    stop("c has to be positive")
  if (zr>c & sided=="one") 
    stop("wrong reflexion border")
  if (r<4) 
    stop("r is too small")
  ctyp <- pmatch(sided, c("one", "two")) - 1
  if (is.na(ctyp)) 
    stop("invalid ewma type")
  ltyp <- pmatch(limits, c("fix","vacl","fir","both","Steiner","Knoth")) - 1
  if (is.na(ltyp)) 
    stop("invalid limits type")
  ad <- .C("xewma_ad",as.integer(ctyp),as.double(l),
           as.double(c),as.double(zr),as.double(mu0),as.double(mu1),
           as.integer(ltyp),as.integer(r),
           ans=double(length=1),,PACKAGE="spc")$ans 
  names(ad) <- "ad"
  return (ad)
}
# Computation of EWMA ARLs (mean monitoring)
xewma.arl <- function(l,c,mu,zr=0,hs=0,sided="one",limits="fix",r=40) { 
  if (l<=0 || l>1) 
    stop("l has to be between 0 and 1")
  if (c<=0) 
    stop("c has to be positive")
  if (zr>c & sided=="one") 
    stop("wrong reflexion border")
  if ( (sided=="two" & abs(hs)>c) | (sided=="one" & (hs<zr | hs>c)) ) 
    stop("wrong headstart")
  if (r<4) 
    stop("r is too small")
  ctyp <- pmatch(sided, c("one", "two")) - 1
  if (is.na(ctyp)) 
    stop("invalid ewma type")
  ltyp <- pmatch(limits, c("fix","vacl","fir","both","Steiner","Knoth")) - 1
  if (is.na(ltyp)) 
    stop("invalid limits type")
  arl <- .C("xewma_arl",as.integer(ctyp),as.double(l),
            as.double(c),as.double(zr),as.double(hs),
            as.double(mu),as.integer(ltyp),as.integer(r),
            ans=double(length=1),PACKAGE="spc")$ans 
  names(arl) <- "arl"
  return (arl)
}
# Computation of EWMA critical values for given ARL (mean monitoring)
xewma.crit <- function(l,L0,mu0=0,zr=0,hs=0,sided="one",limits="fix",r=40) {
  if (l<=0 || l>1) 
    stop("l has to be between 0 and 1")
  if (L0<1) 
    stop("L0 is too small")
  if (r<4) 
    stop("r is too small")
  if (sided=="one" & hs<zr)
    stop("headstart hs smaller than reflexion border zr")
  ctyp <- pmatch(sided, c("one", "two")) - 1
  if (is.na(ctyp)) 
    stop("invalid ewma type")
  ltyp <- pmatch(limits, c("fix","vacl","fir","both","Steiner","Knoth")) - 1
  if (is.na(ltyp)) 
    stop("invalid limits type")
  c <- .C("xewma_crit",as.integer(ctyp),as.double(l),
          as.double(L0),as.double(zr),as.double(hs),
          as.double(mu0),as.integer(ltyp),as.integer(r),
          ans=double(length=1),PACKAGE="spc")$ans 
  names(c) <- "c"
  return (c)
}

.First.lib <- function(lib, pkg)
{
    library.dynam("spc", pkg, lib)
}

