AddOnlyOrderedMap
instanceThis function initializes a new list-like object using the specified keys and values if provided. The resulting map preserves the order of insertion and does not allow modification or removal of existing entries.
The AddOnlyOrderedMap
can be interacted with exactly like a regular
list, possessing methods for [
, [[
, [<-
, and [[<-
with the same
behaviour, except that NULL cannot be passed in since removal is not
permitted.
The keys()
generic is defined for this class, which will return a list
of the keys in their insertion order. The has_key()
generic is also defined
for this class, returning TRUE/FALSE if a key exists. Lastly, the values()
generic is defined to get all values in insertion order.
AddOnlyOrderedMap(
keys = NULL,
values = NULL,
key_validator = Negate(is.null),
val_validator = Negate(is.null)
)
An AddOnlyOrderedMap
instance
Optional list. A vector of keys to initialize the map with. Can
be any R object. It is assumed that all keys are unique, otherwise the
behaviour is undefined. This CANNOT be a scalar value. If that is desired,
wrap it in a list()
.
Optional list. An iterable vector of values corresponding to
the keys. This CANNOT be a scalar value. If that is desired, wrap it in a
list()
.
Optional function. A function that validates individual keys before insertion, returning TRUE if valid, FALSE otherwise.
Optional function. A function that validates individual values before insertion, returning TRUE if valid, FALSE otherwise.
The average time complexity of all operations are linear with respect to the number of insertion/query inputs, in contrast to R lists which has quadratic time complexity for the same operations.