Description Usage Format Details References Immutable Methods Mutable Methods Author(s) See Also Examples
The RDeque reference class implements the data structure double-ended queue (deque).
1 |
An object of class R6ClassGenerator
of length 24.
A deque is an ordered list of elements generalizing the queue data structure. One can append and pop (return and remove) elements from both sides (left and right, front and rear) of the deque.
The elements in the deque are not necessarily to be of the same type, and they can be any R objects.
For the details about the deque data structure, see Deque at Wikipedia.
The immutable methods do not change the instance.
peekleft()
This method returns the leftmost (front) element of the deque.
It returns NULL
if the deque is empty.
peek()
This method returns the rightmost (rear) element of the deque.
It returns NULL
if the deque is empty.
The mutable methods change the instance.
appendleft(..., collapse=NULL)
The appendleft
method appends the elements in ...
and collapse
into the deque to the left (front).
Note that if you append elements in this order:
instance$appendleft(elem1, elem2, elem3)
The order of them inside the deque will be
elem3, elem2, elem1, ...
and elem3
will be the new front of the deque.
append(..., collapse=NULL)
The append
method appends the elements in ...
and collapse
into the deque to the right (rear).
popleft()
The popleft
method returns and removes the leftmost (front) element in the deque.
It returns NULL
if the deque is empty.
pop()
The pop
method returns and removes the rightmost (rear) element in the deque.
It returns NULL
if the deque is empty.
Yukai Yang, yukai.yang@statistik.uu.se
RStack, RQueue, and R6DS for the introduction of the reference class and some common methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | ### create a new instance
# to create a new instance of the class
deque <- RDeque$new()
# the previous RDeque instance will be removed if you run
deque <- RDeque$new(0, 1, 2, collapse=list(3, 4))
# the following sentence is equivalent to the above
deque <- RDeque$new(0, 1, 2, 3, 4)
# where the numbers 0, 1, 2, 3, 4 are enqueued into the deque
### append and appendleft
# it can be one single element
deque$append(5)
# it can be several elements separated by commas
# note the whole list will be one element of the deque
# because it is not passed through the collapse argument
deque$append(list(a=10,b=20), "Hello world!")
# the collapse argument takes a list whose elements will be collapsed
# but the elements' names will not be saved
deque$append(collapse = list(x=100,y=200))
# they can be used together
deque$append("hurrah", collapse = list("RDeque",300))
# this string will be the new head
deque$appendleft("a string")
# we can update the head by
deque$appendleft("string3","string2","string1")
# "string1" will be the leftmost
### peekleft and peek
deque$peekleft()
# "string1"
deque$peek()
# 300
### popleft and pop
val <- deque$popleft()
# "string1"
val <- deque$pop()
# 300
# then we keep dequeuing!
while(!is.null(val)) val <- deque$pop()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.