Learn R Programming

einops (version 0.2.1)

AddOnlyOrderedMap: Create an AddOnlyOrderedMap instance

Description

This 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.

Usage

AddOnlyOrderedMap(
  keys = NULL,
  values = NULL,
  key_validator = Negate(is.null),
  val_validator = Negate(is.null)
)

Value

An AddOnlyOrderedMap instance

Arguments

keys

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().

values

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().

key_validator

Optional function. A function that validates individual keys before insertion, returning TRUE if valid, FALSE otherwise.

val_validator

Optional function. A function that validates individual values before insertion, returning TRUE if valid, FALSE otherwise.

Details

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.