A priority queue or PrioQueue is a queue? chapter 7.3.3, which first divides its elements into priority classes and follows the FIFO semantics in each of these classes. Like all classes in the component gb. data, PrioQueue uses the data type Variant for its elements.
You can assign an integer as a priority to each element when you place it in the PrioQueue. The higher this number is, the more important the element is and it is placed further at the beginning of the queue. If two elements have the same priority - are in a priority class - the FIFO principle is applied. The element that will be added to the end of the priority class.
The properties of the PrioQueue correspond to those of the queue:
|.Size||Integer (ReadOnly)||Number of elements of the PrioQueue|
|.IsEmpty||Boolean (ReadOnly)||True, if there are no elements in the PrioQueue, otherwise wrong|
Table 22.214.171.124.1: Properties of the PrioQueue class
Except for the Enq () method, those of the PrioQueue are the same as those of the queue. The Enq () method additionally takes over the priority for the element to be grouped.
|Clear ()||Remove all elements from the stack|
|Enq (vElement, iPrio)||Synonym for Enqueue (vElement, iPrio)|
|Enqueue (vElement, iPrio)||Insertion of a new element with the specified priority|
|Deq ()||Synonym for Dequeue ()|
|Dequeue ()||Remove and return the first element|
|Peek ()||Returns the first element without removing it|
Table 126.96.36.199.1: Methods of the Class PrioQueue
The following code:
Dim hPrioQueue As New PrioQueue hPrioQueue.Enq("x", 2) hPrioQueue.Enq("y", 1) hPrioQueue.Enq("z", 2) Print hPrioQueue.Deq() Print hPrioQueue.Deq() Print hPrioQueue.Deq()
creates this output in the console:
x z y
“x” and “z” are in a priority class (–> 2). Since“ z” was later classified as “x”, it is later in the same class. “y” with lower priority is the last element in the output.
Gambas developer Bruce Bruce Bruen mentioned that he used the PrioQueue class to run 56 SQL scripts in 5 priority classes for integrity checking and statistics generation for a large database. Until then, this functionality had to be implemented and manually maintained in a larger shell script, which became increasingly tedious for occasionally added scripts. Since Gambas 3.2, a very small Gambas program can dynamically determine the priority of the script from the header of the script files and execute all scripts in the intended order.
Based on this case of use, a project will be presented to demonstrate the PrioQueue class, which divides a large number of shell scripts into priority levels (5 scripts in 4 steps are provided in the project) and executes them. The order of execution of the scripts within a priority level is arbitrary? level 3 with 2 scripts. All scripts with a higher priority number have to be executed before those with a lower priority number - which corresponds to the semantics of the PrioQueue. The priority is given in each script file via a line with the syntax:
# PRIO <X>
where <X> has priority[0…9] of a script in the presented project.
This system of classifying scripts into defined priority levels is comparable to the individual runlevels that are run at the start of a Unix operating system http://de.wikipedia.org/wiki/Runlevel.
You must take note of an exception: A PrioQueue cannot be traversed backwards!