systemverilog associative array find index

SystemVerilog associative array find_index method

SystemVerilog array Index finder method shall return single or multiple indexes which satisfies the condition.

The condition also shall be single or multiple conditions. multiple conditions can be written on using conditional expressions. example: &&, || etc.

index finder methods

Method Description
find_index() returns the indexes of all the elements satisfying the given expression
find_first_index() returns the index of the first element satisfying the given expression
find_last_index() returns the index of the last element satisfying the given expression
unique_index() returns the indexes of all elements with unique values or whose expression is unique

‘with’ clause is optional for min,max,unique and unique_index methods

associative array find_index example

Below example shows the return of single and multiple index return.

class packet;
  int a;
  int b;
  
  function void display();
    $display("\tValue of a = %0d",a);
    $display("\tValue of b = %0d",b);
  endfunction
endclass


module assoc_array;
  
  //declaration of array
  packet assoc_array[*];
  packet pkt;
  int    count,qu[$],tmp_var;
  
  initial begin
    
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[2] = pkt;
    
    pkt = new();
    pkt.a = 0;
    pkt.b = 6;
    assoc_array[5] = pkt;
    
    pkt = new();
    pkt.a = 2;
    pkt.b = 6;
    assoc_array[6] = pkt;
        
    pkt = new();
    pkt.a = 1;
    pkt.b = 6;
    assoc_array[9] = pkt;
    //----------------------------------------------------------------------------
    //------- Find Index Method -------
    //----------------------------------------------------------------------------
    
    //Type-1: returning one matching index
    qu = assoc_array.find_index with (item.a == 'h2);
    count = qu.size();
    
    for(int i=0;i<count;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Index of Asoc Array for a == 2 is %0d",tmp_var);
    end //}
    
    //Type-2: returning mutiple matching index
    qu = assoc_array.find_index with (item.b == 6);
    count = qu.size();
    
    for(int i=0;i<count;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Index of Asoc Array for b == 6 is %0d",tmp_var);
    end //}
    
    //Type-3: with multiple conditions
    qu  = assoc_array.find_index with (item.a == 2 && item.b == 6);
    count = qu.size();
    
    for(int i=0;i<count;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Index of Asoc Array for a == 2,  b == 6 is %0d",tmp_var);
    end //}
    
    //Type-4: with multiple conditions
    qu  = assoc_array.find_index with (item.a < 2 && item.b > 5);
    count = qu.size();
    
    for(int i=0;i<count;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Index of Asoc Array for a < 2,  b > 5 is %0d",tmp_var);
    end //}
  end
endmodule

Simulator Output

Index of Asoc Array for a == 2 is 6
Index of Asoc Array for b == 6 is 5
Index of Asoc Array for b == 6 is 6
Index of Asoc Array for b == 6 is 9
Index of Asoc Array for a == 2, b == 6 is 6
Index of Asoc Array for a < 2, b > 5 is 5
Index of Asoc Array for a < 2, b > 5 is 9

Click to execute on   

For more array manipulation method examples refer to Array Manipulation Methods