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()
.
Roman Pahl
container::Iterable
-> Container
Inherited methods
new()
constructor
Container$new(...)
...
initial elements put into the Container
the Container
object
add()
add element
Container$add(value, name = NULL)
value
value of ANY
type to be added to the Container
.
name
character
optional name attribute of the value.
the Container
object
at()
Same as at2
(see below) but accepts a vector of
indices and always returns a Container
object.
Container$at(index)
index
vector of indices.
Container
object with the extracted elements.
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.
Container$at2(index)
index
Must be a single number > 0 or a string.
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.
clear()
delete all elements from the Container
Container$clear()
the cleared Container
object
count()
Count number of element occurences.
Container$count(elem)
elem
element to be counted.
integer
number of elem
occurences in the Container()
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.
Container$delete(elem)
elem
element to be removed from the Container
.
the Container
object
delete_at()
Delete value at given index. If index is not found, an error is signaled.
Container$delete_at(index)
index
character
or numeric
index
the Container
object
discard()
Search for occurence(s) of elem
in Container
and
remove first one that is found.
Container$discard(elem)
elem
element to be discarded from the Container
. If not
found, the operation is ignored and the object is not altered.
the Container
object
discard_at()
Discard value at given index. If index is not found, the operation is ignored.
Container$discard_at(index)
index
character
or numeric
index
the Container
object
empty()
This function is deprecated. Use is_empty()
instead.
Container$empty()
get_compare_fun()
Get comparison function used internally by the
Container
object to compare elements.
Container$get_compare_fun()
has()
Determine if Container
has some element.
Container$has(elem)
elem
element to search for
TRUE
if Container
contains elem
else FALSE
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.
Container$has_name(name)
name
character
the name
TRUE
if Container
has the name
otherwise FALSE
is_empty()
Check if Container
is empty
Container$is_empty()
TRUE
if the Container
is empty else FALSE
.
integer
length of the Container
, that is, the number of
elements it contains.
character
the names of the elements contained in x
peek_at()
Same as peek_at2
(see below) but accepts a vector of
indices and always returns a Container
object.
Container$peek_at(index, default = NULL)
index
vector of indices.
default
the default value to return in case the value at
index
is not found.
Container
object with the extracted elements.
peek_at2()
Peek at index and extract value. If index is invalid,
missing, or not not found, return default
value.
Container$peek_at2(index, default = NULL)
index
numeric
or character
index to be accessed.
default
the default value to return in case the value at
index
is not found.
the value at the given index or (if not found) the given default value.
pop()
Get value at index and remove it from Container
.
If index
is not found, raise an error.
Container$pop(index)
index
Must be a single number > 0 or a string.
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.
...
further arguments passed to format()
invisibly returns the Container
object
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.
Container$rename(old, new)
old
character
name of key to be renamed.
new
character
new key name.
the Container
object
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.
Container$replace(old, new, add = FALSE)
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.
the Container
object
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.
Container$replace_at(index, value, add = FALSE)
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.
the Container
object
elem
element to be deleted from the Container
. If element
is not found in the Container
, an error is signaled.
the Container
object
the Container
length
type()
This function is deprecated and of no real use anymore.
Container$type()
type (or mode) of internal vector containing the elements
update()
Add elements of other
to this if the name is
not in the Container
and update elements with existing names.
Container$update(other)
other
Iterable
object used to update this.
returns the Container
values()
Get Container
values
Container$values()
elements of the container as a base list
clone()
The objects of this class are cloneable with this method.
Container$clone(deep = FALSE)
deep
Whether to make a deep clone.
This class inherits from class Iterable and serves as the base class for Deque, Set, and Dict.
container()
, Iterable, Deque, Set, and Dict
co = Container$new(1:5, c = Container$new("a", 1), l = list())
co$print()
co$length()
co$names()
co$clear()
# Extract
co = Container$new(a = 1, b = 2, c = 3, d = 4)
co$at(1:2)
co$at(c(1, 4))
co$at(list("d", 2))
co$at2(1)
try(co$at(0:2)) # index must be > 0
co$peek_at(0:2)
co$peek_at(0:2, default = 1)
# Replace
co$replace(4, 9)
co$replace(9, 11)
co$replace_at(1, -1)
try(co$replace_at(11, 1)) # index 11 exceeds length of Container
# Delete
co$delete(-1)
co$delete_at(3)
try(co$delete_at(3)) # index 3 exceeds length of Container
co$discard(3)
co2 = Container$new(b = 0)
co2$add(0, name = "a")
co$update(co2)
co$pop(1)
co
Run the code above in your browser using DataLab