| Title: | Ordered Containers with Integer Keys |
|---|---|
| Description: | Provides a key-value store data structure. The keys are integers and the values can be any R object. This is like a list but indexed by a set of integers, not necessarily contiguous and possibly negative. The implementation uses a 'R6' class. These containers are not faster than lists but their usage can be more convenient for certain situations. |
| Authors: | Stéphane Laurent |
| Maintainer: | Stéphane Laurent <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.0 |
| Built: | 2026-06-04 08:36:35 UTC |
| Source: | https://github.com/stla/intmap |
A map is given by keys and values.
new()
Creates a new intmap object.
intmap$new(keys = NULL, values)
keyskeys, an integer vector without NA value
valuesvalues, a list of R objects; keys and
values must have the same length
An intmap object.
intmap$new() # empty map
intmap$new(
keys = c(4, -2),
values = list(c(1, 2), c("a", "b", "c"))
)
# examples with duplicated keys:
intmap$new(
keys = c(1, 1, 5),
values = list(c(1, 2), c(3, 4), "x")
)
print()
Show instance of an intmap object.
intmap$print(...)
...ignored
size()
Size of the reference map.
intmap$size()
An integer, the number of entries.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$size()
keys()
Get all keys.
intmap$keys()
The keys, an integer vector.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$keys()
values()
Get all values.
intmap$values()
The values, a list of R objects.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$values()
items()
Get all entries of the reference map.
intmap$items()
The entries in a dataframe.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$items()
toList()
Converts the map to a named list.
intmap$toList()
A named list (the names are the keys).
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$toList()
at()
Returns the 'maybe' value corresponding to the given key.
intmap$at(key)
keya key (integer)
A maybe value, either the value corresponding to the key
as a 'Just' maybe value if the key is found, otherwise the
'Nothing' maybe value.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$at(11)
from_just(imap$at(11))
imap$at(4)
get()
Get the value corresponding to the given key or a default value if this key is missing.
intmap$get(key, default = NULL)
keya key (integer)
defaulta R object, the default value
Either the value corresponding to the key if the key is found,
otherwise the default value.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$get(11, default = 999)
imap$get(4, default = 999)
index()
Returns the index of the given key.
intmap$index(key)
keya key (integer)
The index of the key, or NA if it is not found.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$index(11)
imap$index(4)
extract()
Extract a submap from the reference map.
intmap$extract(keys, inplace = FALSE, bydeleting = FALSE)
keyssome keys, an integer vector; those which do not belong to the keys of the reference map will be ignored
inplaceBoolean, whether to update the reference map or to return a new map
bydeletingBoolean, whether to construct the submap by
deleting the keys which are not in keys or by starting
from the empty submap and adding the entries
An intmap object if inplace=FALSE,
otherwise the updated reference map, invisibly.
imap <- intmap$new(
keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X")
)
imap_copy <- imap$copy()
imap$extract(c(11, 3))
imap
imap$extract(c(11, 3), inplace = TRUE)
imap
imap_copy$extract(c(11, 3), bydeleting = TRUE)
imap_copy
imap_copy$extract(c(11, 3), inplace = TRUE, bydeleting = TRUE)
imap_copy
has_key()
Checks whether a key exists in the reference map.
intmap$has_key(key)
keya key (integer)
A Boolean value.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$has_key(11)
imap$has_key(1)
nth()
Returns the n-th entry of the reference map.
intmap$nth(n, stop_if_too_large = TRUE)
nindex, a positive integer
stop_if_too_largea Boolean value, whether to stop if n
is too large, or to use maybe values
A list with the key and the value at index n if
stop_if_too_large=TRUE and n is not too large, otherwise
a maybe value: either this list wrapped in a 'Just' container,
or 'Nothing'.
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$nth(2)
imap$nth(2, stop_if_too_large = FALSE)
imap$nth(9, stop_if_too_large = FALSE)
insert()
Insert a new entry in the reference map.
intmap$insert(key, value, replace = FALSE)
keya key (integer)
valuea value (R object)
replaceBoolean, whether to replace the value if the key is already present
This updates the reference map and this returns a Boolean value:
if replace=FALSE, this returns TRUE if the value has
been inserted (i.e. the given key is new); similarly, if
replace=TRUE, this returns TRUE if the given key is new
(so FALSE means that the value of the existing key has been
replaced).
imap <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap$insert(3, c(6, 7)) # TRUE (insertion)
imap
imap$insert(11, c(8, 9)) # FALSE (no change)
imap
imap$insert(11, c(8, 9), replace = TRUE) # FALSE (replacement)
imap
erase()
Erase the entries of the reference map whose keys are the given ones.
intmap$erase(keys)
keyssome keys, an integer vector; those which do not belong to the keys of the reference map are ignored
The reference map, invisibly.
imap <- intmap$new(
keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X")
)
imap$erase(11)
imap
imap$erase(c(-2, 3))
imap
merge()
Merge the reference map with another map.
intmap$merge(map)
mapan intmap object
The updated reference map, invisibly. Keys of map that
are also keys of the reference map are ignored, i.e. there is no
replacement, only insertions.
imap1 <- intmap$new(
keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5))
)
imap2 <- intmap$new(
keys = c(11, 3), values = list("X", "Z")
)
imap1$merge(imap2)
imap1
copy()
Copy the reference map.
intmap$copy()
A copy of the reference map.
imap <- intmap$new( keys = c(11, 3), values = list(TRUE, "Z") ) true_copy <- imap$copy() true_copy$erase(11) imap naive_copy <- imap naive_copy$erase(11) imap
## ------------------------------------------------ ## Method `intmap$new` ## ------------------------------------------------ intmap$new() # empty map intmap$new( keys = c(4, -2), values = list(c(1, 2), c("a", "b", "c")) ) # examples with duplicated keys: intmap$new( keys = c(1, 1, 5), values = list(c(1, 2), c(3, 4), "x") ) ## ------------------------------------------------ ## Method `intmap$size` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$size() ## ------------------------------------------------ ## Method `intmap$keys` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$keys() ## ------------------------------------------------ ## Method `intmap$values` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$values() ## ------------------------------------------------ ## Method `intmap$items` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$items() ## ------------------------------------------------ ## Method `intmap$toList` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$toList() ## ------------------------------------------------ ## Method `intmap$at` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$at(11) from_just(imap$at(11)) imap$at(4) ## ------------------------------------------------ ## Method `intmap$get` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$get(11, default = 999) imap$get(4, default = 999) ## ------------------------------------------------ ## Method `intmap$index` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$index(11) imap$index(4) ## ------------------------------------------------ ## Method `intmap$extract` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X") ) imap_copy <- imap$copy() imap$extract(c(11, 3)) imap imap$extract(c(11, 3), inplace = TRUE) imap imap_copy$extract(c(11, 3), bydeleting = TRUE) imap_copy imap_copy$extract(c(11, 3), inplace = TRUE, bydeleting = TRUE) imap_copy ## ------------------------------------------------ ## Method `intmap$has_key` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$has_key(11) imap$has_key(1) ## ------------------------------------------------ ## Method `intmap$nth` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$nth(2) imap$nth(2, stop_if_too_large = FALSE) imap$nth(9, stop_if_too_large = FALSE) ## ------------------------------------------------ ## Method `intmap$insert` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$insert(3, c(6, 7)) # TRUE (insertion) imap imap$insert(11, c(8, 9)) # FALSE (no change) imap imap$insert(11, c(8, 9), replace = TRUE) # FALSE (replacement) imap ## ------------------------------------------------ ## Method `intmap$erase` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X") ) imap$erase(11) imap imap$erase(c(-2, 3)) imap ## ------------------------------------------------ ## Method `intmap$merge` ## ------------------------------------------------ imap1 <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap2 <- intmap$new( keys = c(11, 3), values = list("X", "Z") ) imap1$merge(imap2) imap1 ## ------------------------------------------------ ## Method `intmap$copy` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, 3), values = list(TRUE, "Z") ) true_copy <- imap$copy() true_copy$erase(11) imap naive_copy <- imap naive_copy$erase(11) imap## ------------------------------------------------ ## Method `intmap$new` ## ------------------------------------------------ intmap$new() # empty map intmap$new( keys = c(4, -2), values = list(c(1, 2), c("a", "b", "c")) ) # examples with duplicated keys: intmap$new( keys = c(1, 1, 5), values = list(c(1, 2), c(3, 4), "x") ) ## ------------------------------------------------ ## Method `intmap$size` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$size() ## ------------------------------------------------ ## Method `intmap$keys` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$keys() ## ------------------------------------------------ ## Method `intmap$values` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$values() ## ------------------------------------------------ ## Method `intmap$items` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$items() ## ------------------------------------------------ ## Method `intmap$toList` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$toList() ## ------------------------------------------------ ## Method `intmap$at` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$at(11) from_just(imap$at(11)) imap$at(4) ## ------------------------------------------------ ## Method `intmap$get` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$get(11, default = 999) imap$get(4, default = 999) ## ------------------------------------------------ ## Method `intmap$index` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$index(11) imap$index(4) ## ------------------------------------------------ ## Method `intmap$extract` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X") ) imap_copy <- imap$copy() imap$extract(c(11, 3)) imap imap$extract(c(11, 3), inplace = TRUE) imap imap_copy$extract(c(11, 3), bydeleting = TRUE) imap_copy imap_copy$extract(c(11, 3), inplace = TRUE, bydeleting = TRUE) imap_copy ## ------------------------------------------------ ## Method `intmap$has_key` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$has_key(11) imap$has_key(1) ## ------------------------------------------------ ## Method `intmap$nth` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$nth(2) imap$nth(2, stop_if_too_large = FALSE) imap$nth(9, stop_if_too_large = FALSE) ## ------------------------------------------------ ## Method `intmap$insert` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap$insert(3, c(6, 7)) # TRUE (insertion) imap imap$insert(11, c(8, 9)) # FALSE (no change) imap imap$insert(11, c(8, 9), replace = TRUE) # FALSE (replacement) imap ## ------------------------------------------------ ## Method `intmap$erase` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, -2, 3), values = list(c("a", "b"), list(3, 4, 5), "X") ) imap$erase(11) imap imap$erase(c(-2, 3)) imap ## ------------------------------------------------ ## Method `intmap$merge` ## ------------------------------------------------ imap1 <- intmap$new( keys = c(11, -2), values = list(c("a", "b"), list(3, 4, 5)) ) imap2 <- intmap$new( keys = c(11, 3), values = list("X", "Z") ) imap1$merge(imap2) imap1 ## ------------------------------------------------ ## Method `intmap$copy` ## ------------------------------------------------ imap <- intmap$new( keys = c(11, 3), values = list(TRUE, "Z") ) true_copy <- imap$copy() true_copy$erase(11) imap naive_copy <- imap naive_copy$erase(11) imap
The from_just function is imported from the
maybe package.
Follow the link to its documentation:
from_just.
It has been imported for convenient use of the intmap$at method,
which returns a 'Just' value.