Saturday, 31 August 2013

What is the difference between System Verilog Dynamic Array, Associative Array & Queue and their applications?


Ans: The following is the difference between Dynamic Array, Associative Array & Queue.

Dynamic Array:
  • We use dynamic array when we have no idea about the size of the array during compile time and we have to allocate its size for storage during run time.
  • We basically use this array when we have to store a contiguous or Sequential collection of data.
  • The array indexing should be always integer type.
  • To allocate size of a dynamic array, we have to use new[] operator.
            Example:
               int my_array [];
               initial
                   begin
                         my_array = new[4];                              //Allocated 4 elements
                    end
  • To resize a dynamic array, we have to do as follows:
             Example:              
               initial
                  begin
                       my_array = new[18](my_array);     //Resize the Array and Copy
                  end

  • To know the size of the array, we have to use size() operator.
             Example:
              my_array.size();                    //Returns the current size of the array, my_array as an integer.

  • To delete an element from a dynamic array, we have to use delete() operator.
             Example:
              initial
                  begin
                       my_array.delete();    //All the elements of array, my_array will be deleted.
                   end


Associative Array:
  • It is also allocated during run time.
  • This is the array, where data stored in random fashion.
  • It is used when we don’t have to allocate contiguous collection of data, or data in a proper sequence or index.
  • In associative array, the index itself associates the data. So it is called so.
  • Indexing is not regular, can be accessed using indexing like integer or string type or any scalar. 
Example:
my_array ["name"];           // “name”, Index type is a string
my_array[address];           // address, Index type is an integer (here address is an integer).
my_array[my_class];       // my_class, Index type is a class.
my_array[s_array];          // s_array, Index type is an array.
  • It is better to use associative array, when size of the array is unknown & data space is random or irregular or sparse.
  • Following are the methods associated with Associative array. 
          num()returns the number of entries in the Associative array
Eg: my_array.num()
first()assigns the value of the first index in the Associative array to the given index variable Eg:my_array.first(i);
last()assigns the value of the last index in the Associative array to the given index variable Eg:my_array.last(i);
next() — assigns the value of the next index in the Associative array to the given index variable Eg:my_array.next(i);
prev()assigns the value of the previous index in the Associative array to the given index variable Eg:my_array.prev(i);
delete()removes all the elements in the Associative array.
Eg: my_array.delete(i); If the index is specified, then the element at the specified index “i”is deleted
exists()checks if element exists at the specified index “i” in the Associative array Eg:my_array.exists(i);

 Queue:
  • Queue is a variable size, ordered collection of Homogenous Data.
  • It is flexible, as it is variable in size and analogous to an 1-dimensional Unpacked array that can shrink & grow automatically and can be of size zero.
  • The main advantage of queue over dynamic array is that, we don’t need new[] operator to allocate storage space for a queue.
  • The other advantages of queue over dynamic array is that we can manipulate the queue using various queue methods like: push, pop, delete, insert, size.
Example:
module test_example ;
int my_queue[$] = { 1, 2, 3 };
string s_queue [$] = {"first","second","third","fourth"};
string store;
initial
begin
// Use of the size() method/operator
$display("\n size() operator used");
for (int i = 0 ; i < my_queue.size(); i++ )
$display (my_queue[i]);
$display("\n\n Elements of s_queue is :");
for (int i = 0; i < s_queue.size; i++)
$write(s_queue[i]," ");
// Use of insert() method/operator
s_queue.insert(1,"next"); // Previous element 1 is now turned to element 2.
s_queue.insert(2,"somewhere");
$display("\n\n insert() operator used");
for (int i = 0; i < s_queue.size; i++)
$write(s_queue[i]," ");
// Use of delete() method/operator
s_queue.delete(1); // delete the element 1
s_queue.delete(3); // delete the element 3
$display("\n\n delete() operator used");
for (int i = 0; i < s_queue.size; i++)
$write(string_queue[i]," ");
// Use of pop_front() method/operator (it deletes the front of the queue)
store = s_queue.pop_front();
$display("\n\n pop_front() operator used");
$display(" %s",store);
for (int i = 0; i < s_queue.size; i++)
$write(s_queue[i]," ");
// Use of pop_back() method/operator (it deletes the back of the queue)
store= s_queue.pop_back();
$display("\n\n pop_back() operator used");
$display(" %s",store);
for (int i = 0; i < s_queue.size; i++)
$write(s_queue[i]," ");
// Use of push_front() and push_back() method/operator
s_queue.push_front("in-front");
s_queue.push_back("in-back");
$display("\n\n push_front() and push_back() operator used");
for (int i = 0; i < s_queue.size; i++)
$write(s_queue[i]," \n ");
end
endmodule

Result:
size() operator used
1
2
3

Elements of s_queue[$] is:
first second third fourth

insert() operator used
first next somewhere second third fourth

delete() operator used
first somewhere second fourth

pop_front() operator used
first
somewhere second fourth

pop_back() operator used
fourth
somewhere second

push_front() and push_back() operator used
in-front
somewhere
second
in-back

20 comments:


  1. It is really great work and the way in which you are sharing the knowledge is excellent. Thanks for your informative article
    Php course in chennai

    ReplyDelete
  2. Download, install or activate any software or application with the help of verify key technical experts. Find the product keys & get help how to verify or activate products.

    ReplyDelete
  3. Sign in with your MS account from this website office.com/setup Enter your Product Key. Download and Run Office Setup.

    ReplyDelete
  4. Support For AVG Retail Enrollment With the development of the digital world, online protection is crucial. It is extremely important to protect your PCs, Mac, computers as well as mobile devices and tablets with avg activation code.

    ReplyDelete
  5. For Office Setup and Installation go to www.office.com/setup and get started with Installation.

    ReplyDelete
  6. At any time and from anywhere, sign in to My Norton to norton.com/setup manage your subscription, update your profile, change your billing preferences, or renew your Norton protection.

    ReplyDelete
  7. McAfee Virtual Technician identifies and solves many product issues on your PC. If MVT is unable to resolve the issue, the data collected can be used by mcafee activate your support technician to solve the issue.

    ReplyDelete
  8. McAfee Activate indeed offers its free McAfee antivirus download with the www.mcafee.com/activate basic set of virus removal features. Yet, to unlock advanced features, you must have McAfee activate product key.

    ReplyDelete
  9. While Downloading and Installing Office office.com/setup You need to have an active internet connection all the time. You will need to have all the required software like Java runtime to run the office.

    ReplyDelete
  10. Go to the back of subscription card and find your 25 digits code. Use of Norton product key at norton setup to verify your subscription.

    ReplyDelete
  11. Visit www.hulu.com/activate and log in if prompted. Enter the unique activation code that is displayed on your TV screen and within 30 seconds or so you should be logged in.

    ReplyDelete
  12. Present norton antivirus with the best help assembling and keep your PC tainting free. Download norton antivirus with the best help norton.com/setup assembling and dodge ailment assaults.

    ReplyDelete
  13. If you seek proper Norton Antivirus installation, the simpler way is to use the link norton setup In addition to protecting a device after installation, Norton also blocks unauthorized access during installation as well.

    ReplyDelete
  14. Get Started with www.norton.com/setup in order to Secure Your Devices in Simple Steps. Norton is an anti-virus, anti-malware software product. Which stops virus entering in your device at very initial level. It rapidly scans virus, spyware and worms and perform action against it.

    ReplyDelete
  15. Mcafee activation can be easily processed using activation code from mcafee retail card and using it online on mcafee.com/activate you can download and activate security of your device online.

    ReplyDelete