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()
.
This class inherits from class Iterable and serves as the base class for Deque, Set, and Dict.
container()
, Iterable, Deque, Set, and Dict
container::Iterable
-> Container
Inherited methods
add()
add element
at()
Same as at2
(see below) but accepts a vector of
indices and always returns a Container
object.
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.
clear()
delete all elements from the Container
count()
Count number of element occurences.
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.
delete_at()
Delete value at given index. If index is not found, an error is signaled.
discard()
Search for occurence(s) of elem
in Container
and
remove first one that is found.
discard_at()
Discard value at given index. If index is not found, the operation is ignored.
get_compare_fun()
Get comparison function used internally by the
Container
object to compare elements.
has()
Determine if Container
has some element.
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.
is_empty()
Check if Container
is empty
length()
Number of elements of the Container
.
names()
Names of the elements.
peek_at()
Same as peek_at2
(see below) but accepts a vector of
indices and always returns a Container
object.
peek_at2()
Peek at index and extract value. If index is invalid,
missing, or not not found, return default
value.
pop()
Get value at index and remove it from Container
.
If index
is not found, raise an error.
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.
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.
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.
remove()
This function is deprecated. Use delete()
instead.
update()
Add elements of other
to this if the name is
not in the Container
and update elements with existing names.
values()
Get Container
values
clone()
The objects of this class are cloneable with this method.
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]