
Deque - Double-Ended Queue
Source:R/0-DequeS3.R, R/GroupGenericMath.R, R/GroupGenericSummary.R, and 6 more
DequeS3.RdDeques 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.
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)
#> [1] TRUE
print(d)
#> |1, 2, s = "a", v = (1L 2L 3L)|
length(d)
#> [1] 4
names(d)
#> [1] "" "" "s" "v"
as.list(d)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> $s
#> [1] "a"
#>
#> $v
#> [1] 1 2 3
#>
rev(d)
#> |v = (1L 2L 3L), s = "a", 2, 1|
l = list(0, 1)
d2 = as.deque(l)
d + d2
#> |1, 2, s = "a", v = (1L 2L 3L), 0, 1|
c(d, d2) # same as d + d2
#> |1, 2, s = "a", v = (1L 2L 3L), 0, 1|
d2 + d
#> |0, 1, 1, 2, s = "a", v = (1L 2L 3L)|
d - d2
#> |2, s = "a", v = (1L 2L 3L)|
c(d2, d) # same as d2 + d
#> |0, 1, 1, 2, s = "a", v = (1L 2L 3L)|
d2 - d
#> |0|
# Math
d = deque(1, 2, -(3:5))
d
#> |1, 2, (-3L -4L -5L)|
abs(d)
#> |1, 2, 3, 4, 5|
cumsum(d)
#> |1, 3, 0, -4, -9|
round(d)
#> |1, 2, -3, -4, -5|
exp(d)
#> |2.718282, 7.389056, 0.04978707, 0.01831564, 0.006737947|
# Summary
range(d)
#> [1] -5 2
min(d)
#> [1] -5
max(d)
#> [1] 2
d1 = deque(1, 1:2)
d2 = deque(2, 1:2)
d1 + d2 # same as c(d1, d2)
#> |1, (1L 2L), 2, (1L 2L)|
d2 + d1 # same as c(d2, d1)
#> |2, (1L 2L), 1, (1L 2L)|
d1 - d2
#> |1|
d2 - d1
#> |2|
d1 - d1
#> ||
d = deque(0)
add(d, a = 1, b = 2) # |0, a = 1, b = 2|
#> |0, a = 1, b = 2|
addleft(d, a = 1, b = 2) # |b = 2, a = 1, 0|
#> |b = 2, a = 1, 0|
d = deque(1, 2, 3)
peek(d)
#> [1] 3
peekleft(d)
#> [1] 1
peek(deque())
#> NULL
peek(deque(), default = 0)
#> [1] 0
peekleft(deque(), default = 0)
#> [1] 0
d = deque(1, 2, 3)
ref_pop(d)
#> [1] 3
print(d)
#> |1, 2|
ref_popleft(d)
#> [1] 1
print(d)
#> |2|
try({
ref_pop(deque()) # pop at empty Deque
})
#> Error : pop at empty Deque
d = deque(a = 1, b = 2, 3)
rev(d)
#> |3, b = 2, a = 1|
print(d)
#> |a = 1, b = 2, 3|
ref_rev(d)
#> |3, b = 2, a = 1|
print(d)
#> |3, b = 2, a = 1|
d = deque(1, 2, 3, 4)
rotate(d)
#> |4, 1, 2, 3|
rotate(d, n = 2)
#> |3, 4, 1, 2|