| 1 |
list_builder <- function() { |
| 2 |
(function() { |
| 3 |
capacity_ <- 1024 |
| 4 |
index_ <- 0 |
| 5 |
data_ <- vector("list", capacity_) |
| 6 |
|
| 7 |
append <- function(data) { |
| 8 |
|
| 9 |
# increment index and check capacity |
| 10 |
index_ <<- index_ + 1 |
| 11 |
if (index_ > capacity_) { |
| 12 |
capacity_ <<- capacity_ * 2 |
| 13 |
data_[capacity_] <<- list(NULL) |
| 14 |
} |
| 15 |
|
| 16 |
# append data |
| 17 |
if (is.null(data)) |
| 18 |
data_[index_] <<- list(NULL) |
| 19 |
else |
| 20 |
data_[[index_]] <<- data |
| 21 |
} |
| 22 |
|
| 23 |
data <- function() { |
| 24 |
data_[seq_len(index_)] |
| 25 |
} |
| 26 |
|
| 27 |
clear <- function() { |
| 28 |
capacity_ <<- 1024 |
| 29 |
index_ <<- 0 |
| 30 |
data_ <<- vector("list", capacity_) |
| 31 |
} |
| 32 |
|
| 33 |
empty <- function() { |
| 34 |
index_ == 0 |
| 35 |
} |
| 36 |
|
| 37 |
list( |
| 38 |
append = append, |
| 39 |
clear = clear, |
| 40 |
empty = empty, |
| 41 |
data = data |
| 42 |
) |
| 43 |
|
| 44 |
})() |
| 45 |
} |