Description Usage Format Details References Class Method Immutable Methods Mutable Methods Author(s) See Also Examples
The RSet reference class implements the data structure set.
1 |
An object of class R6ClassGenerator
of length 24.
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.
It should be noticed that, in your design, if any two elements in the set can be easily compared, by simply, for example, keys, numbers, and etc., the RSet should not be recommended due to efficiency reason. The RSet is suitable for the cases when you have a relatively complex "=" operation between two elements in the set.
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 be any R objects.
For the details about the set data structure, see Set at Wikipedia.
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
.
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
merges the elements in rset
, an instance of some class in the package,
with its elements, and returns a new union set of the two.
intersection(rset)
The method intersection
returns a new intersection set (RSet) of
the current set and rset
, an instance of some class in the package.
difference(rset)
The method difference
returns a new difference set (RSet) of
the current set and rset
, an instance of some class in the package
(current instance minus rset
).
subset(rset)
The method subset
returns a boolean indicating
if the current set is a subset of rset
,
an instance of some class in the package.
contains(rset)
The method contains
returns a boolean indicating
if the current set contains rset
,
an instance of some class in the package.
The mutable methods change the instance.
add(val)
The method add
adds a new element into the set and returns a booleank
showing if the insertion is successful.
add_multiple(..., collapse=NULL)
The method add_multiple
adds new elements in ...
and collapse
into the set.
delete(val)
The method delete
removes the element which is equal
to val
in the set.
It returns a boolean showing if the deletion is successful (if the element is not found in the set).
Yukai Yang, yukai.yang@statistik.uu.se
RDLL and R6DS for the introduction of the reference class and some common methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | ### 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
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.