DequeS3 | R Documentation |
Deques are a generalization of stacks and queues typically with methods to add, remove and access elements at both sides of the underlying data sequence. As such, the deque can also be used to mimic both stacks and queues.
deque(...) as.deque(x) is.deque(x)
... |
initial elements put into the |
x |
|
Methods that alter Deque objects usually come in two versions
providing either copy or reference semantics where the latter start with
'ref_'
to note the reference semantic, for example, add()
and ref_add()
.
deque(...)
initializes and returns an object of class Deque
as.deque(x)
coerces x
to a deque.
is.deque(x)
returns TRUE
if x
is of class Deque
and FALSE
otherwise.
x + y
combines x
and y
into a new deque by appending y
to x
.
x - y
element-wise removes all items of y
from x
, given
the element was contained in x
.
addleft(.x, ...)
adds (possibly named) elements to left side of .x
.
ref_addleft(.x, ...)
same as addleft(.x, ...)
but adds by reference.
peek(x, default = NULL)
peek at last element. If x
is empty, return
default
.
peekleft(x, default = NULL)
peek at first element. If x
is empty,
return default
.
ref_pop(.x)
pop last element. If .x
is empty, an error is given.
ref_popleft(.x)
pop first element. If .x
is empty, an error is given.
rev(x)
and ref_rev(x)
reverses all elements being done on a copy or in
place, respectively.
rotate(x, n)
rotate all elements n
steps to the right, If n
is
negative, rotate to the left.
See container()
for all inherited methods. For the full class
documentation see Deque()
and it's superclass Container().
d = deque(1, 2, s = "a", v = 1:3) is.deque(d) print(d) length(d) names(d) as.list(d) rev(d) l = list(0, 1) d2 = as.deque(l) d + d2 c(d, d2) # same as d + d2 d2 + d d - d2 c(d2, d) # same as d2 + d d2 - d # Math d = deque(1, 2, -(3:5)) d abs(d) cumsum(d) round(d) exp(d) # Summary range(d) min(d) max(d) d1 = deque(1, 1:2) d2 = deque(2, 1:2) d1 + d2 # same as c(d1, d2) d2 + d1 # same as c(d2, d1) d1 - d2 d2 - d1 d1 - d1 d = deque(0) add(d, a = 1, b = 2) # |0, a = 1, b = 2| addleft(d, a = 1, b = 2) # |b = 2, a = 1, 0| d = deque(1, 2, 3) peek(d) peekleft(d) peek(deque()) peek(deque(), default = 0) peekleft(deque(), default = 0) d = deque(1, 2, 3) ref_pop(d) print(d) ref_popleft(d) print(d) try({ ref_pop(deque()) # pop at empty Deque }) d = deque(a = 1, b = 2, 3) rev(d) print(d) ref_rev(d) print(d) d = deque(1, 2, 3, 4) rotate(d) rotate(d, n = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.