
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  local_extrema.r                                                                        #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   is_local_max       ( x, range=10 )                                                                                       #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# ----------------------------------------------------------------------------------- #
is_local_max <- function(x, range=10) {
## returns a vector of length (x)
## TIES:  First occurrance wins

  if (range < 3 || !is.numeric(range))
    stop ("range must be a positive integer with value >= 3")


  L <- length(x)

  if (!L) {
    warning ("x has no length")
    return(logical())
  }

  ret <- rep(FALSE, L)

  if (range >= L)
    ret[which.max(x)] <- TRUE
  else 
    for (i in seq((L + 1) - range)) {
      ret[(i - 1) + which.max(x[seq.int(from=i, by=1, length.out=range)])] <- TRUE
    }
    for (j in setdiff(which(ret), c(1, L))) {
      ret[j] <- all(x[j] >= x[j + c(-1, 1)])
    }



  return(ret)
}
