Description Usage Format Details References Immutable Methods Mutable Methods Author(s) See Also Examples
The RQueue reference class implements the data structure queue.
1 |
An object of class R6ClassGenerator
of length 24.
A queue is an ordered list of elements following the First-In-First-Out (FIFO) principle.
The enqueue
method takes elements and add them to the rear terminal position (right) of the queue,
while the dequeue
method returns and removes the element in the queue from the front terminal position (left).
The elements in the queue are not necessarily to be of the same type, and they can be any R objects.
For the details about the queue data structure, see Queue at Wikipedia.
The immutable method does not change the instance.
peekleft()
This method returns the leftmost (front) element in the queue.
It returns NULL
if the queue is empty.
The mutable methods change the instance.
enqueue(..., collapse=NULL)
The enqueue
method enqueues the elements in ...
and collapse
into the queue
(to the right or rear).
Note that you can input multiple elements.
dequeue()
The dequeue
method dequeues (returns and removes) one element (the leftmost or front) fron the queue.
It returns NULL
if the queue is empty.
Yukai Yang, yukai.yang@statistik.uu.se
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 | ### create a new instance
# to create a new instance of the class
queue <- RQueue$new()
# the previous RQueue instance will be removed if you run
queue <- RQueue$new(0, 1, 2, collapse=list(3, 4))
# the following sentence is equivalent to the above
queue <- RQueue$new(0, 1, 2, 3, 4)
# where the numbers 0, 1, 2, 3, 4 are enqueued into the queue
### enqueue elements
# it can be one single element
queue$enqueue(5)
# it can be several elements separated by commas
# note the whole list will be one element of the queue
# because it is not passed through the collapse argument
queue$enqueue(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
queue$enqueue(collapse = list(x=100,y=200))
# they can be used together
queue$enqueue("hurrah", collapse = list("RQueue",300))
### dequeue an element
# dequeue only one element at a time
val <- queue$dequeue()
# then we keep dequeuing!
while(!is.null(val)) val <- queue$dequeue()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.