| DequeS3 {container} | R Documentation |
Deque - Double-Ended Queue
Description
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.
Usage
deque(...)
as.deque(x)
is.deque(x)
Arguments
... |
initial elements put into the |
x |
|
Details
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 classDeque
-
as.deque(x)coercesxto a deque.
-
is.deque(x)returnsTRUEifxis of classDequeandFALSEotherwise.
-
x + ycombinesxandyinto a new deque by appendingytox.
-
x - yelement-wise removes all items ofyfromx, given the element was contained inx.
-
addleft(.x, ...)adds (possibly named) elements to left side of.x. -
ref_addleft(.x, ...)same asaddleft(.x, ...)but adds by reference.
-
peek(x, default = NULL)peek at last element. Ifxis empty, returndefault. -
peekleft(x, default = NULL)peek at first element. Ifxis empty, returndefault.
-
ref_pop(.x)pop last element. If.xis empty, an error is given. -
ref_popleft(.x)pop first element. If.xis empty, an error is given.
-
rev(x)andref_rev(x)reverses all elements being done on a copy or in place, respectively.
-
rotate(x, n)rotate all elementsnsteps to the right, Ifnis negative, rotate to the left.
See Also
See container() for all inherited methods. For the full class
documentation see Deque() and it's superclass Container().
Examples
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)