Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 28, 2023
1 parent 500bfc4 commit ff73b86
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
16 changes: 8 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export(auto_set_regexpr_style)
export(check_pkg)
export(combinations)
export(exec)
export(get_cache)
export(get_cache_element)
export(get_cache_item)
export(get_dependencies)
export(get_fun_from_pkg)
export(get_yulab_cache)
export(get_yulab_cache_element)
export(get_yulab_cache_item)
export(initial_yulab_cache)
export(initial_yulab_cache_item)
export(initial_cache)
export(initial_cache_item)
export(install_zip)
export(install_zip_gh)
export(is.installed)
Expand All @@ -27,8 +27,8 @@ export(packageTitle)
export(pload)
export(rbindlist)
export(read.cb)
export(rm_yulab_cache)
export(rm_yulab_cache_item)
export(rm_cache)
export(rm_cache_item)
export(scale_range)
export(scihub_dl)
export(set_PCRE)
Expand All @@ -38,7 +38,7 @@ export(show_in_excel)
export(str_ends)
export(str_starts)
export(str_wrap)
export(update_yulab_cache_item)
export(update_cache_item)
export(use_perl)
export(yread)
export(yread_tsv)
Expand Down
14 changes: 7 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# yulab.utils 0.1.2.001

+ with cache ability (2023-12-27, Wed)
- `initial_yulab_cache()`
- `initial_yulab_cache_item()`
- `get_yulab_cache()`
- `get_yulab_cache_item()`
- `get_yulab_cache_element()`
- `rm_yulab_cache_item()`
- `rm_yulab_cache()`
- `initial_cache()`
- `initial_cache_item()`
- `get_cache()`
- `get_cache_item()`
- `get_cache_element()`
- `rm_cache_item()`
- `rm_cache()`

# yulab.utils 0.1.2

Expand Down
36 changes: 18 additions & 18 deletions R/cache.R
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
##' @rdname yulab-cache
##' @export
initial_yulab_cache <- function() {
initial_cache <- function() {
pos <- 1
envir <- as.environment(pos)
assign(".yulabCache", new.env(), envir = envir)
}

##' @rdname yulab-cache
##' @export
get_yulab_cache <- function() {
get_cache <- function() {
if (!exists(".yulabCache", envir = .GlobalEnv)) {
initial_yulab_cache()
initial_cache()
}
get(".yulabCache", envir = .GlobalEnv)
}

##' @rdname yulab-cache
##' @export
rm_yulab_cache <- function() {
rm_cache <- function() {
if (exists(".yulabCache", envir = .GlobalEnv)) {
rm(".yulabCache", envir = .GlobalEnv)
}
}

##' @rdname yulab-cache
##' @export
initial_yulab_cache_item <- function(item) {
env <- get_yulab_cache()
initial_cache_item <- function(item) {
env <- get_cache()
assign(item, list(), envir = env)
}

##' @rdname yulab-cache
##' @export
get_yulab_cache_item <- function(item) {
env <- get_yulab_cache()
get_cache_item <- function(item) {
env <- get_cache()
if (!exists(item, envir = env)) {
initial_yulab_cache_item(item)
initial_cache_item(item)
}

get(item, envir = env, inherits = FALSE)
}

##' @rdname yulab-cache
##' @export
rm_yulab_cache_item <- function(item) {
env <- get_yulab_cache()
rm_cache_item <- function(item) {
env <- get_cache()
if (exists(item, envir = env)) {
rm(list = item, envir = env)
}
Expand All @@ -70,22 +70,22 @@ rm_yulab_cache_item <- function(item) {
##'
##' fast_fib <- function(x) {
##' if (x < 2) return(1)
##' res <- get_yulab_cache_element('fibonacci', as.character(x))
##' res <- get_cache_element('fibonacci', as.character(x))
##' if (!is.null(res)) {
##' return(res)
##' }
##' res <- fast_fib(x-2) + fast_fib(x-1)
##' e <- list()
##' e[[as.character(x)]] <- res
##' update_yulab_cache_item('fibonacci', e)
##' update_cache_item('fibonacci', e)
##' return(res)
##' }
##'
##' system.time(slow_fib(30))
##' system.time(fast_fib(30))
##'
##' }
update_yulab_cache_item <- function(item, elements) {
update_cache_item <- function(item, elements) {

msg <- "new elements should be stored as a named list"
if (!inherits(elements, 'list')) {
Expand All @@ -100,16 +100,16 @@ update_yulab_cache_item <- function(item, elements) {
stop(msg)
}

env <- get_yulab_cache()
res <- get_yulab_cache_item(item)
env <- get_cache()
res <- get_cache_item(item)
res <- modifyList(res, elements)
assign(item, res, envir = env)
}

##' @rdname yulab-cache
##' @export
get_yulab_cache_element <- function(item, elements) {
x <- get_yulab_cache_item(item)
get_cache_element <- function(item, elements) {
x <- get_cache_item(item)
n <- length(elements)
if (n == 1) return(x[[elements]])

Expand Down
38 changes: 19 additions & 19 deletions man/yulab-cache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff73b86

Please sign in to comment.