R/ufmf_cache.R

Defines functions ufmf_cache_init ufmf_cache_full ufmf_cache_store ufmf_cache_contains ufmf_cache_fetch

# Make an empty ufmf cache
# @param max Maximum number of images to cache
ufmf_cache_init <- function(max=5) {
  e=new.env()
  e$max=5
  e$ims=list()
  e$ids=integer()
  e$accesstimes=numeric()
  e
}

ufmf_cache_full <- function(e) {
  isTRUE(length(e$ims)==e$max)
}

ufmf_cache_store <- function(e, im, id) {
  # nothing to do if already stored
  if(ufmf_cache_contains(e, id))
    return()
  if(ufmf_cache_full(e)){
    # we need to find oldest cached item
    i=which.min(e$accesstimes)
  } else i=length(e$ims)+1
  e$ims[[i]]=im
  e$accesstimes[i] = as.numeric(Sys.time())
  e$ids[i]=id
}

ufmf_cache_contains <- function(e, id) {
  id %in% e$ids
}

ufmf_cache_fetch <- function(e, id) {
  if(ufmf_cache_contains(e, id)){
    i=which(e$ids==id)
    e$accesstimes[i] = as.numeric(Sys.time())
    e$ims[[i]]
  } else NULL
}
jefferis/fmfio documentation built on May 19, 2019, 2:50 a.m.