This class implements a container data structure with typical member functions to insert, delete and access elements from the container. For the standard S3 interface, see container().

Details

This class inherits from class Iterable and serves as the base class for Deque, Set, and Dict.

See also

Author

Roman Pahl

Super class

container::Iterable -> Container

Methods

Inherited methods


Method new()

constructor

Usage

Container$new(...)

Arguments

...

initial elements put into the Container

Returns

the Container object


Method add()

add element

Usage

Container$add(value, name = NULL)

Arguments

value

value of ANY type to be added to the Container.

name

character optional name attribute of the value.

Returns

the Container object


Method at()

Same as at2 (see below) but accepts a vector of indices and always returns a Container object.

Usage

Container$at(index)

Arguments

index

vector of indices.

Returns

Container object with the extracted elements.


Method at2()

Extract value at index. If index is invalid or not found, an error is signaled. If given as a string, the element matching the name is returned. If there are two or more identical names, the value of the first match (i.e. leftmost element) is returned.

Usage

Container$at2(index)

Arguments

index

Must be a single number > 0 or a string.

Returns

If given as a number, the element at the corresponding position, and if given as a string, the element at the corresponding name matching the given string is returned.


Method clear()

delete all elements from the Container

Usage

Container$clear()

Returns

the cleared Container object


Method count()

Count number of element occurences.

Usage

Container$count(elem)

Arguments

elem

element to be counted.

Returns

integer number of elem occurences in the Container()


Method delete()

Search for occurence(s) of elem in Container and remove first one that is found. If elem does not exist, an error is signaled.

Usage

Container$delete(elem)

Arguments

elem

element to be removed from the Container.

Returns

the Container object


Method delete_at()

Delete value at given index. If index is not found, an error is signaled.

Usage

Container$delete_at(index)

Arguments

index

character or numeric index

Returns

the Container object


Method discard()

Search for occurence(s) of elem in Container and remove first one that is found.

Usage

Container$discard(elem)

Arguments

elem

element to be discarded from the Container. If not found, the operation is ignored and the object is not altered.

Returns

the Container object


Method discard_at()

Discard value at given index. If index is not found, the operation is ignored.

Usage

Container$discard_at(index)

Arguments

index

character or numeric index

Returns

the Container object


Method empty()

This function is deprecated. Use is_empty() instead.

Usage

Container$empty()


Method get_compare_fun()

Get comparison function used internally by the Container object to compare elements.

Usage

Container$get_compare_fun()


Method has()

Determine if Container has some element.

Usage

Container$has(elem)

Arguments

elem

element to search for

Returns

TRUE if Container contains elem else FALSE


Method has_name()

Determine if Container object contains an element with the given name. If called with no argument, the function determines whether any element is named.

Usage

Container$has_name(name)

Arguments

name

character the name

Returns

TRUE if Container has the name otherwise FALSE


Method is_empty()

Check if Container is empty

Usage

Container$is_empty()

Returns

TRUE if the Container is empty else FALSE.


Method length()

Number of elements of the Container.

Usage

Container$length()

Returns

integer length of the Container, that is, the number of elements it contains.


Method names()

Names of the elements.

Usage

Container$names()

Returns

character the names of the elements contained in x


Method peek_at()

Same as peek_at2 (see below) but accepts a vector of indices and always returns a Container object.

Usage

Container$peek_at(index, default = NULL)

Arguments

index

vector of indices.

default

the default value to return in case the value at index is not found.

Returns

Container object with the extracted elements.


Method peek_at2()

Peek at index and extract value. If index is invalid, missing, or not not found, return default value.

Usage

Container$peek_at2(index, default = NULL)

Arguments

index

numeric or character index to be accessed.

default

the default value to return in case the value at index is not found.

Returns

the value at the given index or (if not found) the given default value.


Method pop()

Get value at index and remove it from Container. If index is not found, raise an error.

Usage

Container$pop(index)

Arguments

index

Must be a single number > 0 or a string.

Returns

If given as a number, the element at the corresponding position, and if given as a string, the element at the corresponding name matching the given string is returned.


Method print()

Print object representation

Usage

Container$print(...)

Arguments

...

further arguments passed to format()

Returns

invisibly returns the Container object


Method rename()

Rename a key in the Container. An error is signaled, if either the old key is not in the Container or the new key results in a name-clash with an existing key.

Usage

Container$rename(old, new)

Arguments

old

character name of key to be renamed.

new

character new key name.

Returns

the Container object


Method replace()

Replace one element by another element. Search for occurence of old and, if found, replace it by new. If old does not exist, an error is signaled, unless add was set to TRUE, in which case new is added.

Usage

Container$replace(old, new, add = FALSE)

Arguments

old

element to be replaced

new

element to be put instead of old

add

logical if TRUE the new element is added in case old does not exists.

Returns

the Container object


Method replace_at()

Replace value at given index. Replace value at index by given value. If index is not found, an error is signalled, unless add was set to TRUE, in which case new is added.

Usage

Container$replace_at(index, value, add = FALSE)

Arguments

index

character or numeric index

value

ANY new value to replace the old one.

add

logical if TRUE the new value element would be added in case index did not exists.

Returns

the Container object


Method remove()

This function is deprecated. Use delete() instead.

Usage

Container$remove(elem)

Arguments

elem

element to be deleted from the Container. If element is not found in the Container, an error is signaled.

Returns

the Container object


Method size()

This function is deprecated. Use length() instead.

Usage

Container$size()

Returns

the Container length


Method type()

This function is deprecated and of no real use anymore.

Usage

Container$type()

Returns

type (or mode) of internal vector containing the elements


Method update()

Add elements of other to this if the name is not in the Container and update elements with existing names.

Usage

Container$update(other)

Arguments

other

Iterable object used to update this.

Returns

returns the Container


Method values()

Get Container values

Usage

Container$values()

Returns

elements of the container as a base list


Method clone()

The objects of this class are cloneable with this method.

Usage

Container$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

co = Container$new(1:5, c = Container$new("a", 1), l = list())
co$print()
#> [(1L 2L 3L 4L ...), c = ["a", 1], l = list()]
co$length()
#> [1] 3
co$names()
#> [1] ""  "c" "l"
co$clear()
#> []

# Extract
co = Container$new(a = 1, b = 2, c = 3, d = 4)
co$at(1:2)
#> [a = 1, b = 2]
co$at(c(1, 4))
#> [a = 1, d = 4]
co$at(list("d", 2))
#> [d = 4, b = 2]
co$at2(1)
#> [1] 1

try(co$at(0:2))  # index must be > 0
#> Error : index must be > 0

co$peek_at(0:2)
#> [a = 1, b = 2]
co$peek_at(0:2, default = 1)
#> [1, a = 1, b = 2]

# Replace
co$replace(4, 9)
#> [a = 1, b = 2, c = 3, d = 9]
co$replace(9, 11)
#> [a = 1, b = 2, c = 3, d = 11]
co$replace_at(1, -1)
#> [a = -1, b = 2, c = 3, d = 11]

try(co$replace_at(11, 1)) # index 11 exceeds length of Container
#> Error : index 11 exceeds length of Container, which is 4

# Delete
co$delete(-1)
#> [b = 2, c = 3, d = 11]
co$delete_at(3)
#> [b = 2, c = 3]

try(co$delete_at(3))     # index 3 exceeds length of Container
#> Error : index 3 exceeds length of Container, which is 2

co$discard(3)
#> [b = 2]

co2 = Container$new(b = 0)
co2$add(0, name = "a")
#> [b = 0, a = 0]
co$update(co2)
#> [b = 0, a = 0]
co$pop(1)
#> [1] 0
co
#> [a = 0]