Learn R Programming

container (version 0.3.0)

ContainerS3: Container S3 interface

Description

This function creates a container data structure with typical member functions to insert, delete and access objects from the container. It also serves as the base class for objects created with deque, set, and dict.

Usage

container(x = list())

as.container(x)

is.container(x)

add(x, ...)

clear(x)

clone(x, ...)

discard(x, ...)

empty(x)

has(x, ...)

remove(x, ...)

size(x)

type(x)

values(x)

Arguments

x

initial elements passed to constructor or object of class Container passed to member methods.

...

further arguments

S3 methods for class <code>Container</code>

iter(cont)

Create iterator from cont.

add(cont, elem)

Add elem to cont.

clear(cont)

Remove all elements from the cont.

clone(cont)

Create a copy of cont object. For more details see documentation of R6Class.

discard(cont, elem, right=FALSE)

Search for first elem in cont and, if found, remove it. If right is TRUE, search from right to left.

empty(cont)

Return TRUE if the cont is empty, else FALSE.

has(cont, elem)

Return TRUE if cont contains elem else FALSE.

print(cont, list.len, ...)

Print container object representation similar to str

remove(cont, elem, right=FALSE)

Same as discard, but throw an error if elem does not exist.

size(cont)

Return size of the cont.

type(cont)

Return type (or mode) of internal vector containing the elements of the container.

values(cont)

Return a copy of all elements in the same format as they are stored in the object.

Details

The underlying data structure is based on R vectors (or lists), with the mode being set to the mode (or type) of the value passed to the initialize function, which by default is an empty list, in which case the Container object can store objects of mixed and arbitrary types. If the container will only contain objects of one particular type, for example, double values, it will be both more efficient and type safe to initialize the container using this particular type (see Examples section).

See Also

Container, +.Container,

Examples

Run this code
# NOT RUN {
c0 <- container(list(2, "A"))
size(c0)                         # 2
add(c0, 1)
c0$has(2)                        # TRUE
discard(c0, 2)
has(c0, 2)                       # FALSE

# }
# NOT RUN {
c0$remove(2)                     # Error : 2 not in Container
# }
# NOT RUN {
discard(c0, 2)                   # ok (no effect)

type(container(list("A", 1)))    # "list"
type(container(numeric(0)))      # "double"
type(container(0+0i))            # "complex"
type(container(letters[1:3]))    # "character"
values(container(letters[1:3]))  # "a" "b" "c"
type(container(1L))              # "integer"
values(add(container(1L), 2.3))  # since integer type, equals c(1, 2)
# }

Run the code above in your browser using DataLab