hset (version 0.1.1)

hset: hset data structure for the R language

Description

Functions to construct and access objects of class "hset", that implements sets and multisets.

Usage

hset(members = NULL, multiplicities = NULL, generalized = FALSE)

is.hset(x) as.hset(x)

is.generalized(hset) as.generalized(hset, suppress.warning = FALSE) as.not.generalized(hset, suppress.warning = FALSE)

clone.of.hset(current.hset, generalized = NA_integer_) refer.to.hset(current.hset, generalized = NA_integer_)

members(hset) multiplicities(hset) size.support(hset) cardinality(hset)

Value

Functions hset, as.hset, as.generalized, as.not.generalized, clone.of.hset, and refer.to.hset return an object of class "hset"; is.hset and is.generalized return a one dimensional logical value; members and multiplicities return a vector of class "character"; size.support and cardinality return a number.

Arguments

members

collection of elements of the set.

multiplicities

collection of multiplicities of the elements in members, argument used only when generalized is TRUE.

generalized

logical value indicating whether the returned object is a set, or a multiset.

x

object to be coerced or tested.

hset

object of class "hset".

suppress.warning

avoid warning when hset is already generalized, or is already not generalized.

current.hset

object of class "hset" to be copied, or referred to

Details

hset is the constructor of a set or a multiset, implemented using the hash table from package "hash".

For multisets, the arguments members and multiplicities have to be compatible. If the latter is NULL all members have multiplicity 1 by default, otherwise the two arguments must have the same length. If multiplicities is not NULL, it must be a vector of class "numeric" with the same length of members. The valid classes for the argument members are "NULL", "hset", or "vector", in the third case, members can be of sub-class "atomic", or of sub-class "list" containing objects of the classes indicated so far. The recursive definition of "list" objects allows the definition of elements of a set or a multiset that are themselves sets.

The function is.hset is used to check whether the object x is of class "hset", whereas as.hset is used to coerce x to an "hset" object.

The function is.generalized is used to check whether an object of class "hset" is a set or a multiset, as.generalized and as.not.generalized convert a set to a multiset and viceversa.

The functions clone.of.hset and refer.to.hset are used to copy an object of class "hset", and refer to it, respectively.

The functions members and multiplicities return a vector of elements, with their corresponding multiplicities, respectively. The functions size.support and cardinality return the number of elements and the sum of the multiplicities, respectively.

Examples

Run this code
## create an empty set and an empty multiset
hset() # equivalent to hset(NULL), as.hset(list()), or hset(list())
hset(generalized = TRUE)

## create set {1,3,4} and multiset {1[2], 3[1], 4[.5]}
hset(c(1,3,4)) # equivalent to hset(c(1,1,3,4)) or hset(c(1,4,3))
hset(c(1,3,4), c(2,1,.5)) # equivalent to hset(c(1,1,3,4), c(1,1,1,.5))

## recursive definition of sets
hset(hset()) # equivalent to hset(list(list()))
hset(list(1, list(1,list()) )) # {{{},1},1}

## check and coerce hset objects
is.hset(hset())
as.hset(list())
# note that hset(hset()) and as.hset(hset()) are not equivalent

## canonical map from sets to multisets, and vice versa
hs <- hset(); is.generalized(hs)
as.generalized(hs); is.generalized(hs)
as.not.generalized(hs); is.generalized(hs)
# note reference semantic of hs

## value and reference semantics
hs <- hset(c(1,3,4))
hsc <- clone.of.hset(hs)
hsc <- union(hsc, hset(c(4,5)))
hsc; hs # value semantic
hsr <- refer.to.hset(hs) # equivalent to: hsr <- hs, or hsr = hs
hsc <- union(hsc, hset(c(4,5)))
hsr; hs # they refer to the same updated object in memory

## information extraction about hset objects
hm <- hset(c(1,4,3), c(2,.5,1), generalized = TRUE)
members(hm)
multiplicities(hm)
size.support(hm)
cardinality(hm)

Run the code above in your browser using DataLab