# RSet

##### The RSet reference class

The RSet reference class implements the data structure set.

- Keywords
- RSet

##### Usage

`RSet`

##### Details

A set is a collection of items or elements equipped with the "=" operators such that any two elements in the set cannot be equal. The set data structure does not care the order of the elements.

The class `RSet`

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 equals
any other elements in the set.
Normally they should be depreciated in the `RSet`

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 `RSet`

class.

The elements in the set are not necessarily to be of the same type, and they can even be of function type.

##### Format

An object of class `R6ClassGenerator`

of length 24.

##### Class Method

The class method belongs to the class.

`new(equal, ..., collapse=NULL)`

The

`new`

method creates a new instance of the RSet class containing the values in`...`

and`collapse`

as its elements.The argument

`equal`

takes a function defining the "=" operation, The function set to`equal`

takes two values of the elements in the set and return a boolean. It can be, for example, of the form`equal <- function(x, y) return(x$num == y$num)`

where

`x`

and`y`

are values of two elements in the set with the attribute`num`

.

##### Immutable Methods

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

`has(val)`

The method

`has`

returns a boolean indicating if the set contains`val`

.`union(rset)`

The method

`union`

takes another instance`rset`

of the`RSet`

or`RDLL`

class and returns the union of the two sets.`intersection(rset)`

The method

`intersection`

takes another instance`rset`

of the`RSet`

class and returns the intersection of the two sets.`difference(rset)`

The method

`difference`

takes another instance`rset`

of the`RSet`

class and returns the difference (current instance minus`rset`

) of the two sets.`subset(rset)`

The method

`subset`

takes another instance`rset`

of the`RSet`

class. It returns`TRUE`

if the current instance is a subset of`rset`

.`contains(rset)`

The method

`contains`

takes another instance`rset`

of the`RSet`

or`RDLL`

class. It returns`TRUE`

if the current instance contains`rset`

.

##### Mutable Methods

The mutable methods changes the elements of the instance.

`add(val)`

The method

`add`

adds a new element and returns`TRUE`

showing that the inserting is successful, but if there is one element in current set that is`equal`

to the element, the method will do nothing and return a`FALSE`

showing that the adding fails.`delete(val)`

The method

`delete`

removes the element which is`equal`

to`val`

. If the element is found, then it will be removed and the function returns a`TRUE`

, and if the element is not found, then it will do nothing and returns a`FALSE`

,

##### References

For the details about the set data structure, see Set at Wikipedia.

##### See Also

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

##### Examples

```
# NOT RUN {
### create a new instance
# you have to define "="
equal <- function(x, y) return(x$key == y$key)
# remember that the elements in the set must have the "key" attribute
# to create a new instance of the class
set <- RSet$new(equal=equal)
# of course you can start to add elements when creating the instance
# the previous RSet instance will be removed by doing so
# and the memory allocated for that one will be cleared,
# as now set has been pointed to another instance of the class.
set <- RSet$new(equal=equal,
list(key=5, val="5"), collapse=list(list(key=3,val="3"), list(key=9,val="9")))
# the following sentence is equivalent to the above
set <- RSet$new(equal=equal,
list(key=5, val="5"), list(key=3,val="3"), list(key=9,val="9"))
# where the three lists are inserted into the set
### immutable methods
set$has(list(key=5, num=10))
# TRUE as it has the key attribute
### mutable methods
set$add(list(key=5, num=10))
# FALSE
set$add(list(key=10, val="10"))
# TRUE
set$delete(list(key=10))
# TRUE and list(key=10, val="10") is removed
# union
another_set <- RSet$new(equal=equal,
list(key=5, val="5"), list(key=11,val="11"))
set$union(another_set)$show()
# intersection
set$intersection(another_set)$show()
# difference
set$difference(another_set)$show()
# subset
set$subset(another_set)
# contains
set$contains(another_set)
# }
```

*Documentation reproduced from package R6DS, version 1.1.0, License: GPL-3*