[Roxygen-commits] r240 - in pkg: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 11 18:05:28 CET 2009


Author: manuel
Date: 2009-11-11 18:05:27 +0100 (Wed, 11 Nov 2009)
New Revision: 240

Added:
   pkg/.Rbuildignore
   pkg/R/memoize.R
Modified:
   pkg/DESCRIPTION
   pkg/R/parse.R
   pkg/man/parse.file.Rd
   pkg/man/parse.ref.srcref.Rd
Log:
Hadley's "Cache parsing of srcrefs" patch; Roxygen mailinglist 2009-09-10.

Added: pkg/.Rbuildignore
===================================================================
--- pkg/.Rbuildignore	                        (rev 0)
+++ pkg/.Rbuildignore	2009-11-11 17:05:27 UTC (rev 240)
@@ -0,0 +1,2 @@
+sandbox
+patches
\ No newline at end of file

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2009-11-11 15:18:05 UTC (rev 239)
+++ pkg/DESCRIPTION	2009-11-11 17:05:27 UTC (rev 240)
@@ -5,11 +5,13 @@
     collation, namespace and callgraphs.
 Title: Literate Programming in R
 Author: Peter Danenberg <pcd at roxygen.org>, Manuel Eugster
-    <Manuel.Eugster at stat.uni-muenchen.de>
+    <Manuel.Eugster at stat.uni-muenchen.de> with contributions
+    from Hadley Wickham <hadley at rice.edu>
 Maintainer: Peter Danenberg <pcd at roxygen.org>
 URL: http://roxygen.org
+Depends: digest
 Suggests: Rgraphviz (>= 1.19.2), tools (>= 2.9.1)
-Collate: 'functional.R' 'list.R' 'roxygen.R' 'string.R' 'parse.R'
-    'parseS4.R' 'roclet.R' 'callgraph.R' 'description.R' 'collate.R' 
+Collate: 'functional.R' 'list.R' 'roxygen.R' 'string.R' 'memoize.R' 'parse.R'
+    'parseS4.R' 'roclet.R' 'callgraph.R' 'description.R' 'collate.R'
     'namespace.R' 'Rd.R' 'Rdmerge.R' 'Rdapi.R' 'Rdtank.R' 'Rd2.R'
     'roxygenize.R'

Added: pkg/R/memoize.R
===================================================================
--- pkg/R/memoize.R	                        (rev 0)
+++ pkg/R/memoize.R	2009-11-11 17:05:27 UTC (rev 240)
@@ -0,0 +1,46 @@
+library(digest)
+
+new_cache <- function() {
+  
+  cache <- NULL
+  cache_reset <- function() {
+    cache <<- new.env(TRUE, emptyenv())
+  }
+  
+  cache_set <- function(key, value) {
+    assign(key, value, env = cache)
+  }
+  
+  cache_get <- function(key) {
+    get(key, env = cache, inherits = FALSE)
+  }
+  
+  cache_has_key <- function(key) {
+    exists(key, env = cache, inherits = FALSE)
+  }
+  
+  cache_reset()
+  list(
+    reset = cache_reset, 
+    set = cache_set, 
+    get = cache_get,
+    has_key = cache_has_key,
+    keys = function() ls(cache)
+  )
+}
+
+memoize <- function(f) {
+  cache <- new_cache()
+  
+  function(...) {
+    hash <- digest(list(...))
+    
+    if (cache$has_key(hash)) {
+      cache$get(hash)
+    } else {
+      res <- f(...)
+      cache$set(hash, res)
+      res
+    }
+  }
+}
\ No newline at end of file

Modified: pkg/R/parse.R
===================================================================
--- pkg/R/parse.R	2009-11-11 15:18:05 UTC (rev 239)
+++ pkg/R/parse.R	2009-11-11 17:05:27 UTC (rev 240)
@@ -2,6 +2,7 @@
 #' @include functional.R
 #' @include string.R
 #' @include list.R
+#' @include memoize.R
 roxygen()
 
 #' Sequence that distinguishes roxygen comment from normal comment.
@@ -80,7 +81,7 @@
   for (key in c(...))
     register.parser(table, key, parser)
 }
-  
+
 #' Register many preref parsers at once.
 #' @param parser the parser to register
 #' @param \dots the keys upon which to register
@@ -170,7 +171,7 @@
   else
     parse.default(key, rest)
 }
-  
+
 #' Parse an element containing a mandatory name
 #' and description (such as \code{@@param}).
 #' @param key the parsing key
@@ -300,7 +301,7 @@
                               if (is.null.string(description)) NULL
                               else parse.description(description))
   }
-} 
+}
 
 #' Recursively walk an expression (as returned by \code{parse}) in
 #' preorder.
@@ -398,13 +399,16 @@
   }
 }
 
-#' Parse a srcref
+#' Parse a srcref;
+#' with the help of memoization (by Hadley Wickham).
+#' param ref the srcref to be parsed
+#' param \dots ignored
 #' @method parse.ref srcref
-#' @param ref the srcref to be parsed
+#' @param ... the srcref to be parsed
 #' @param \dots ignored
 #' @return List containing the parsed srcref
 #' @export
-parse.ref.srcref <- function(ref, ...) {
+parse.ref.srcref <- memoize(function(ref, ...) {
   srcfile <- attributes(ref)$srcfile
   srcref <- list(srcref=list(filename=srcfile$filename,
                    lloc=as.vector(ref)))
@@ -414,7 +418,7 @@
   if (is.call(car(expressions)))
     parsed <- parse.call(expressions)
   append(parsed, srcref)
-}
+})
 
 #' Parse each of a list of preref/srcref pairs.
 #' @param preref.srcrefs list of preref/srcref pairs
@@ -422,13 +426,15 @@
 parse.refs <- function(preref.srcrefs)
   Map(parse.ref, preref.srcrefs)
 
-#' Parse a source file containing roxygen directives.
-#' @param file string naming file to be parsed
+#' Parse a source file containing roxygen directives;
+#' with the help of memoization (by Hadley Wickham).
+#' param file string naming file to be parsed
+#' @param ... string naming file to be parsed
 #' @return List containing parsed directives
 #' @export
 #' @callGraph
 #' @callGraphDepth 3
-parse.file <- function(file) {
+parse.file <- memoize(function(file) {
   srcfile <- srcfile(file)
   srcrefs <- attributes(parse(srcfile$filename,
                               srcfile=srcfile))$srcref
@@ -436,7 +442,7 @@
     parse.refs(zip.list(prerefs(srcfile, srcrefs), srcrefs))
   else
     nil
-}
+})
 
 #' Parse many files at one.
 #' @param \dots files to be parsed

Modified: pkg/man/parse.file.Rd
===================================================================
--- pkg/man/parse.file.Rd	2009-11-11 15:18:05 UTC (rev 239)
+++ pkg/man/parse.file.Rd	2009-11-11 17:05:27 UTC (rev 240)
@@ -1,7 +1,9 @@
 \name{parse.file}
 \alias{parse.file}
 \title{Parse a source file containing roxygen directives.}
-\usage{parse.file(file)}
+\usage{### parse.file(file)
+parse.file(...)
+}
 \description{Parse a source file containing roxygen directives.}
 \value{List containing parsed directives}
-\arguments{\item{file}{string naming file to be parsed}}
+\arguments{\item{...}{string naming file to be parsed}}

Modified: pkg/man/parse.ref.srcref.Rd
===================================================================
--- pkg/man/parse.ref.srcref.Rd	2009-11-11 15:18:05 UTC (rev 239)
+++ pkg/man/parse.ref.srcref.Rd	2009-11-11 17:05:27 UTC (rev 240)
@@ -1,8 +1,10 @@
 \name{parse.ref.srcref}
 \alias{parse.ref.srcref}
 \title{Parse a srcref...}
-\usage{\method{parse.ref}{srcref} (ref, ...)}
+\usage{
+### parse.ref.srcref(ref, ...)
+parse.ref.srcref(...)
+}
 \description{Parse a srcref}
 \value{List containing the parsed srcref}
-\arguments{\item{ref}{the srcref to be parsed}
-\item{\dots}{ignored}}
+\arguments{\item{...}{the srcref to be parsed}}



More information about the Roxygen-commits mailing list