Queue

A queue is a variable-size, ordered collection of homogeneous elements.
•Queues can also grow and shrink similar to dynamic arrays
• But with a queue you can add and remove elements anywhere
• Search and sort can be done easily
• Declared using a $ word subscript i.e., [$]
• Can provide easy sorting and searching
• Allocates extra space to quickly add extra elements
• Does not need the new[ ] operator
• Push and pop operations are efficient
• Can add and remove elements from anywhere
• Can copy contents of fixed or dynamic arrays to the queue


A queue can be bounded or unbounded.
  • bounded queue - queue with the number of entries limited or queue size specified
  • unbounded queue - queue with unlimited entries or queue size not specified


  • Queues are declared using the same syntax as unpacked arrays, but specifying $ as the arra mum size of a queue can be limited by specifying its optional right bound (last index).

    syntax

    Type name_of_queue [$:LIMIT]

    bit queue_1[$]; // queue of bits (unbound queue)

    bit queue_2[$:255]; // A queue whose maximum size is 256 bits

    int queue_int[$]; // queue of int

    byte queue_byte[$]; // A queue of bytes

    byte queue_byte[$:255]; // queue of byte (bounded queue with 256 entries)

    string queue_string[$] = { "Bob" }; // A queue of strings with one element

    integer queue_integer[$] = { 3, 2, 7 }; // An initialized queue of integers


    Initializing queue with elements

    queue_1 = {0,1,2,3};

    queue_int = {10,20,30,40,50};

    queue_string={"verilogHDL", "systemverilog", "OVM", "UVM"};


    Methods in queue

    Queues also provides several built-in methods to access/modify queue. Below we have list down all such methods.
  • size() method returns the number of items in the queue. If the queue is empty, it returns 0.

  • insert() method inserts the given item at the specified index position.

  • delete() limethod deletes the item at the specified index.

  • pop_front() method removes and returns the first element of the queue.

  • pop_back() limethod removes and returns the last element of the queue.

  • push_front() method inserts the given element at the front of the queue.

  • push_back() limethod inserts the given element at the end of the queue.




  • Example :

    module queue_example;
    int queue[$] ;
    task print();
    for( int i=0; i $display("queue[%0d] = %0d",i,queue[i] );
    endtask

    initial begin

    queue = {10,20,30,40 }; $display("----------Insert Into Queue---------------------------");
    queue.insert(0,100); print(); //Insert 0th position position to 100
    $display(" After Insert queue size is = %0d", queue.size() );

    $display("---------- Delete From Queue---------------------------");
    queue.delete(4); print(); // Delete the 5th position of queue
    $display(" After Delete queue size is = %0d", queue.size() );

    $display("---------- push_front to Queue ---------------------------");
    queue.push_front(200); print(); // Delete the 5th position of queue
    $display(" After push_front queue size is = %0d", queue.size() );

    $display("---------- pop_front From Queue ---------------------------");
    queue.pop_front(); print(); // Delete the 5th position of queue
    $display(" After pop_front queue size is = %0d", queue.size() );

    $display("---------- push_back to Queue ---------------------------");
    queue.push_back(300); print(); // Delete the 5th position of queue
    $display(" After push_back queue size is = %0d", queue.size() );

    $display("---------- pop_back From Queue ---------------------------");
    queue.pop_front(); print(); // Delete the 5th position of queue
    $display(" After pop_back queue size is = %0d", queue.size() );

    $finish(1);
    end
    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------

    # ----------Insert Into Queue---------------------------
    # que[0] = 100
    # que[1] = 10
    # que[2] = 20
    # que[3] = 30
    # que[4] = 40
    # After Insert queue size is = 5
    # ---------- Delete From Queue---------------------------
    # que[0] = 100
    # que[1] = 10
    # que[2] = 20
    # que[3] = 30
    # After Delete queue size is = 4
    # ---------- push_front to Queue ---------------------------
    # que[0] = 200
    # que[1] = 100
    # que[2] = 10
    # que[3] = 20
    # que[4] = 30
    # After push_front queue size is = 5
    # ---------- pop_front From Queue ---------------------------
    # que[0] = 100
    # que[1] = 10
    # que[2] = 20
    # que[3] = 30
    # After pop_front queue size is = 4
    # ---------- push_back to Queue ---------------------------
    # que[0] = 100
    # que[1] = 10
    # que[2] = 20
    # que[3] = 30
    # que[4] = 300
    # After push_back queue size is = 5
    # ---------- pop_back From Queue ---------------------------
    # que[0] = 10
    # que[1] = 20
    # que[2] = 30
    # que[3] = 300
    # After pop_back queue size is = 4
    # ------------------------------------------------------

    Advantage:


    - Allocating memory for needed element only (it saves memory)
    - Easy to remove/add new element of a Queue


    Disadvantage:

    - Uncontrollable memory for Queue (we add too much element to a queue can lead to out of memory without warning)
    - Each element of Queue consumes more memory than Array