/
fastmap2.R
199 lines (186 loc) · 5.01 KB
/
fastmap2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#' @title A Wrapper for \code{fastmap::fastmap}
#' @description \code{\link[fastmap]{fastmap}}
#' provides a key-value store where the keys are strings and the
#' values are any R objects. It differs from normal environment that
#' \code{\link[fastmap]{fastmap}} avoids memory leak. \code{fastmap2}
#' is a wrapper for \code{fastmap}, which provides several generic
#' functions such that it has similar behaviors to lists or
#' environments
#' @param missing_default passed to \code{fastmap::fastmap}
#' @param i,j vector of names
#' @param name name, or key of the value
#' @param value any R object
#' @param x a \code{'fastmap2'} object
#' @param recursive whether to recursively apply \code{\link[base]{as.list}}
#' @param sorted whether to sort names; default is false
#' @param ... passed to other methods
#' @return A list of \code{'fastmap2'} instance
#' @examples
#'
#' ## --------------------------- Basic Usage --------------------------
#' map <- fastmap2()
#' map$a = 1
#' map$b = 2
#' print(map)
#'
#' map[c('a', 'b')]
#' # Alternative way
#' map['a', 'b']
#'
#' map[c('c', 'd')] <- 3:4
#' # or
#' map['e', 'f'] <- 5:6
#'
#' # The order is not guaranteed, unless sort=TRUE
#' as.list(map)
#' as.list(map, sort=TRUE)
#'
#' names(map)
#' length(map)
#'
#' ## ----------------------- NULL value handles -----------------------
#' map$b <- NULL
#' names(map) # 'b' still exists!
#' as.list(map) # 'b' is NULL, but still there
#'
#' # to remove 'b', you have to use `@remove` method
#' map$`@remove`('b')
#'
#' ## ---------------- Native fastmap::fastmap methods -----------------
#'
#' # whether map has 'a'
#' map$`@has`('a')
#'
#' # Remove a name from list
#' map$`@remove`('a')
#'
#' # remove all from list
#' map$`@reset`()
#' print(map)
#'
#' @name fastmap2
#' @export
fastmap2 <- function(missing_default = NULL){
map <- fastmap::fastmap(missing_default = missing_default)
class(map) <- c('fastmap2', 'list')
map
}
#' @title Copy elements to \code{fastmap2}
#' @param li a list or an environment
#' @param map \code{NULL} or a \code{fastmap2} instance
#' @return If \code{map} is not \code{NULL}, elements will be added
#' to \code{map} and return \code{map}, otherwise create a new instance.
#' @export
list_to_fastmap2 <- function(li, map = NULL){
stopifnot2(is.null(map) || inherits(map, 'fastmap2'), msg = 'map must be either NULL or fastmap2')
if(is.null(map)){
map <- fastmap2()
}
for(nm in names(li)){
if(nm != ''){
map[[nm]] <- li[[nm]]
}
}
map
}
#' @title Migrate a \code{fastmap2} object to a new one
#' @param from,to \code{fastmap2} object
#' @param override whether to override keys in \code{to} if they exist
#' @return Map \code{to}
#' @seealso \code{\link{fastmap2}}
update_fastmap2 <- function(from, to, override = TRUE){
if(override){
new_list <- .subset2(from, 'as_list')()
} else{
keys <- .subset2(from, 'keys')()
keys <- keys[!keys %in% .subset2(to, 'keys')()]
new_list <- .subset2(from, 'mget')(keys)
}
if(length(new_list)){
.subset2(to, 'mset')(.list = new_list)
}
return(to)
}
#' @rdname fastmap2
#' @export
`[[.fastmap2` <- function(x, name){
name <- as.character(name)
if( startsWith(name, '@') ){
.subset2(x, substring(name, 2))
}else{
.subset2(x, 'get')(name)
}
}
#' @rdname fastmap2
#' @export
`$.fastmap2` <- `[[.fastmap2`
#' @rdname fastmap2
#' @export
`[[<-.fastmap2` <- function(x, name, value){
.subset2(x, 'set')(as.character(name), value)
return(x)
}
#' @rdname fastmap2
#' @export
`$<-.fastmap2` <- `[[<-.fastmap2`
#' @rdname fastmap2
#' @export
`[.fastmap2` <- function(x, i, j = NULL, ...){
if(missing(i)) {
return( .subset2(x, "as_list")(...) )
} else {
return( .subset2(x, 'mget')(as.character(unlist(c(i, j, ...)))) )
}
}
#' @rdname fastmap2
#' @export
`[<-.fastmap2` <- function(x, i, j = NULL, ..., value){
i <- unlist(c(i, j, ...))
# instead of throwing error,
stopifnot2(length(value) <= 1 || length(value) == length(i),
msg='value must be the same length as name')
if( length(value) == length(i) ){
.subset2(x, 'mset')(.list = structure(as.list(value), names = as.character(i)))
} else {
# set for each key
for(k in i){
.subset2(x, 'set')(as.character(k), value)
}
}
x
}
#' @rdname fastmap2
#' @export
`names.fastmap2` <- function(x){
re <- .subset2(x, 'keys')()
if(!length(re)){ re <- NULL }
re
}
#' @rdname fastmap2
#' @export
`print.fastmap2` <- function(x, ...){
cat('<Map, size=', .subset2(x, 'size')(),
', keys=[', paste(.subset2(x, 'keys')(), collapse = ', '),
']>\n', sep = '')
invisible(x)
}
#' @rdname fastmap2
#' @export
`length.fastmap2` <- function(x){
.subset2(x, 'size')()
}
#' @rdname fastmap2
#' @export
as.list.fastmap2 <- function(x, recursive = FALSE, sorted = FALSE,
...){
re <- .subset2(x, 'as_list')(sort = sorted)
if( recursive ){
for(i in seq_along(re)){
item <- re[[i]]
if(inherits(item, "fastmap2")){
re[[i]] <- as.list(item, recursive = recursive, sorted = sorted, ...)
}
}
}
re
}