How to implement an assosiative array functionality using dynamic array

dynamic array to be used and make it work like how an assosiative array works.

Why can’t you just use the SystemVerilog Associative array type?

Regards,

Mark

In reply to Mark Curry:

Why can’t you just use the SystemVerilog Associative array type?

Most likely because it’s a homework/interview question.

In reply to dave_59:

Yes it was an interview question and i haven’t got the answer for the same.

In reply to Suman R:
I’ll give you a hint: It takes two dynamic arrays to implement the functionality of one associative array.

In reply to dave_59:
Hi Dave,
Can you please provide an exact code? I have tried and come up with something like below,


class bus_seq;
  rand int ds_array[]; 
  rand int di_array[]; 
  int darrsize=5;
  constraint size_constriant {
    ds_array.size() == darrsize;
    di_array.size() == darrsize;
  }
endclass : bus_seq
module randm();
  bus_seq bs = new();
  initial begin
    //option 1. this obviously works
    if(!bs.randomize() with {foreach(ds_array[i]) ds_array[i]==i;})
      $error("Failed randomization"); 
    foreach(bs.ds_array[i]) begin
      bs.di_array[i] = i*5;
    end
    $display(" --> after making pair ds_array = %p",bs.ds_array);
    $display(" --> after making pair di_array = %p",bs.di_array);
    foreach(bs.ds_array[i]) begin
      $display("Key Value Pair - using key %0d - bs.di_array[%0d] = %0d", bs.ds_array[i], bs.ds_array[i],bs.di_array[bs.ds_array[i]]);
    end
  end
endmodule : randm

In reply to megamind:

I think the good way is:

  • Use 1 dynamic array to hold “key” of associative array
  • Use 1 dynamic array to hold “value” of associative array