Class Models for Lua

Priority Queue
loop.collection.PriorityQueue
Class of objects that store a set of values with associated weight values so that these values are arranged in the descending order of weight. The values are stored like in the OrderedSet
class, but the values are sorted in descending order of weight. This class is useful for implementing priority queues with no repeated values and which priorities are comparable values like numbers or strings.
Each instance holds an additional table containing the priority of each value inserted in the queue that are used to keep the values properly sorted.
Behavior
Methods
contains(value)
- Return true if a
value
is inserted in the queue or false otherwise. dequeue()
- Removes the first value of the queue and then returns it. If the queue is empty this function has no effect.
empty()
- Return true if there is no value inserted in the queue or false otherwise.
enqueue(value, priority)
- Adds
value
to the queue just before the value with priority larger thanpriority
.value
is returned if successfully added. Ifvalue
is already inserted then the call has no effect and no value is returned. head()
- Returns the value at the start of the queue.
remove(value [, start])
- Searches for
value
and removes it from the queue starting the search after the value ofstart
. Ifstart
is not provided the search is done through the entire set. The removed value and its priority are returned or no value is returned ifvalue
is not found. priority(value)
- Returns the priority associated to the value
value
inserted in the queue. tail()
- Returns the value at the end of the queue.
Remarks
- Instances cannot store the name of class members as strings. To do so, use the class operations over an empty table like in the following example:
PriorityQueue = require "loop.collection.PriorityQueue" queue = {} PriorityQueue.enqueue(queue, "first", 1) PriorityQueue.enqueue(queue, "enqueue", 2) PriorityQueue.enqueue(queue, "head", 3) for word in PriorityQueue.sequence(queue) do io.write(word, " ") end
Copyright (C) 2004-2008 Tecgraf, PUC-RioThis project is currently being maintained by Tecgraf at PUC-Rio.