R6DS (version 1.1.0)

RDict: The RDict reference class

Description

The RDict reference class implements the data structure dictionary.

Usage

RDict

Arguments

Format

An object of class R6ClassGenerator of length 24.

Immutable Methods

The immutable methods do not change the elements of the instance.

has(key)

The method has returns a boolean indicating if the dictionary contains the element with the key name "key".

Both of the following two sentences are equivalent:

instance$has("keyname")

instance$has(keyname)

get(key)

The method get returns the value of the element whose key is "key". It returns NULL if no element is found.

keys

The method keys returns a vector of the keys in the dictionary.

values

The method values returns a list of the values in the dictionary.

Mutable Methods

The mutable methods changes the elements of the instance.

add(..., key="", val=NULL)

The method add adds new elements into the dictionary. It will not add element with the key which exists already in the dictionary.

The argument ... stands for any input with the form

keyname = value

Therefor, the input can take the form

instance$add(key1=1, key2="hello", key3=list(1))

and the keys of the elements will be strings like "key1", "key2", and "key3", respectively.

If the keyname is missing, the value will not be added.

As an alternative, you can use the latter two arguments to add element.

key=keyname, val=value

Note that any element with the key "" (empty string) will not be added.

delete(key)

The method delete removes the element with the key key in the dictionary.

Suppose that the key name of the element that you want to remove is "keyname". Both of the following two sentences are valid:

instance$delete("keyname")

instance$delete(keyname)

Details

A dictionary is a collection of (key, value) pairs as its elements such that each possible key appears at most once in the collection. The dictionary data structure does not care the order of the elements.

The class RDict inherits the RDLL class, and therefor it has all the methods that RDLL has.

Note that the methods insert_at, appendleft, append in the super class still works without checking if the new element has the key equal to any other keys in the dictionary. Normally they should be depreciated in the RDict class, but this is not done in the current version of the package. It is strongly recommended that the user should use the add method to add a new element when using the RDict class.

The keys of the elements in the dictionary must be strings. However, the values of the elements in the dictionary are not necessarily to be of the same type, and they can even be of function type.

References

For the details about the dictionary data structure, see Dictionary at Wikipedia.

See Also

RDLL and R6DS for the introduction of the reference class and some common methods

Examples

Run this code
# NOT RUN {
### create a new instance

# to create a new instance of the class
dict <- RDict$new()

# of course you can start to add elements when creating the instance
# the previous RDict instance will be removed by doing so
# and the memory allocated for that one will be cleared,
# as now dict has been pointed to another instance of the class.
dict <- RDict$new(id0001=1, id0002=2, collapse=list(id0003=3, id0004=4))
# the following sentence is equivalent to the above
dict <- RDict$new(id0001=1, id0002=2, id0003=3, id0004=4)
# where the three lists are inserted into the dictionary

### immutable methods

dict$keys
dict$values

dict$has(id0001)
dict$has("id0005")
# TRUE as it has the key attribute

dict$get(id0006)
dict$get("id0002")

### mutable methods

dict$add(id0005=5)

dict$add(key="id0006", val=6)
# TRUE

dict$delete(id0001)

# }

Run the code above in your browser using DataCamp Workspace