R/expanding_list.R

Defines functions expanding_list

Documented in expanding_list

#' A linked list for fast collection of values
#' 
expanding_list <- function(capacity = 10) {
  buffer <- vector('list', capacity)
  length <- 0
  
  methods <- list()
  
  methods$double.size <- function() {
    buffer <<- c(buffer, vector('list', capacity))
    capacity <<- capacity * 2
  }
  
  methods$add <- function(val) {
    if(length == capacity) {
      methods$double.size()
    }
    
    length <<- length + 1
    buffer[[length]] <<- val
  }
  
  methods$as.list <- function() {
    b <- buffer[0:length]
    return(b)
  }
  
  methods
}
cleberecht/singaEvaluate documentation built on Jan. 7, 2020, 4:07 p.m.