R6DS (version 1.2.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 instance.

has(key)

The method has returns a boolean indicating if the dictionary contains the element with the key "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 (unnamed list).

Mutable Methods

The mutable methods change the instance.

add(key, val)

The method add adds a new element (the pair key and val) into the dictionary. It will not add element with the key which exists already in the dictionary. It returns a boolean showing if the adding is successful.

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

add_multiple(..., collapse=NULL)

The method add_multiple 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

keyname1 = value2, keyname2 = value2, ...

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.

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)

It returns a boolean showing if the element is found and deleted.

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 keys of the elements in the dictionary are stored as strings. The values in the dictionary are not necessarily to be of the same type, and they can be any R objects.

References

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

See Also

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

dict$delete(id0001)

# }

Run the code above in your browser using DataCamp Workspace