[SV]SystemVerilog数组定位法(Array Locator methods)

                                     Array locator methods

 

  Array locator methods are useful for finding the index or elements of an array.

  • operate on any unpacked arrays and queues.
  • the return type of these methods is a queue.
  • with an expression, Array elements or indexes can be searched.

  Built-in array locator methods can be classified as, element finder and index finder.

 

一、Element finder methods:

  1. find()                returns all the elements satisfying the given expression
  2. find_first()        returns the first element satisfying the given expression
  3. find_last()        returns the last element satisfying the given expression
  4. min()                returns the element with the minimum value or whose expression evaluates to a minimum
  5. max()               returns the element with the maximum value or whose expression evaluates to a maximum
  6. unique()           returns all elements with unique values or whose expression is unique

二、Index finder methods:

  1. find_index()               returns the indexes of all the elements satisfying the given expression
  2. find_first_index() returns the index of the first element satisfying the given expression
  3. find_last_index()  returns the index of the last element satisfying the given expression
  4. 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

三、Array Index Finder methods

       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.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

四、Array Index Finder methods FIRST_MATCH and LAST_MATCH

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    cnt,qu[$],tmp_var;
   
  initial begin
     
    pkt = new();
    pkt.a = 3;
    pkt.b = 8;
    assoc_array[2] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[5] = pkt;
     
    pkt = new();
    pkt.a = 2;
    pkt.b = 6;
    assoc_array[6] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[8] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[9] = pkt;
     
    //----------------------------------------------------------------------------
    //------- Find First/Last Index Method -------
    //----------------------------------------------------------------------------
     
    //Type-1: returning first matching index
    qu = assoc_array.find_first_index with (item.a == 8);
    cnt = qu.size();
     
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("First Index of Asoc Array for a == 8 is %0d",tmp_var);
    end //}
     
    //Type-2: returning first matching index
    qu = assoc_array.find_first_index with (item.a == 8 && item.b == 3);
    cnt = qu.size();
     
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("First Index of Asoc Array for a == 8,  b == 3 is %0d",tmp_var);
    end //}
     
    //Type-3: returning last matching index
    qu = assoc_array.find_last_index with (item.a == 8 &&  item.b == 3);
    cnt = qu.size();
     
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Last Index of Asoc Array for a == 8,  b == 3 is %0d",tmp_var);
    end //}
     
  end
endmodule

    Simulator Output 

    First Index of Asoc Array for a == 8 is 5
    First Index of Asoc Array for a == 8, b == 3 is 5
    Last Index of Asoc Array for a == 8, b == 3 is 9

五、Array Element Finder methods

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,tmp_var,qu[$];
  int    cnt;
   
  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 Element Method -------
    //----------------------------------------------------------------------------
     
    //Type-1: returning one matching element
    qu = assoc_array.find with (item.a == 'h2);
    cnt = qu.size();
     
    $display("Elements of Asoc Array for a == 2 are,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
     
    //Type-2: returning mutiple matching element
    qu = assoc_array.find with (item.b == 6);
    cnt = qu.size();
     
    $display("Elements of Asoc Array for b == 6 are,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
     
    //Type-3: with multiple conditions
    qu  = assoc_array.find with (item.a == 2 && item.b == 6);
    cnt = qu.size();
     
    $display("Elements of Asoc Array for a == 2,  b == 6,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
  
    //Type-4: with multiple conditions
    qu  = assoc_array.find with (item.a < 2 && item.b > 5);
    cnt = qu.size();
     
    $display("Elements of Asoc Array for a < 2,  b > 5,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
  end
endmodule

Simulator Output 

Elements of Asoc Array for a == 2 are,
Element No: 1
Value of a = 2
Value of b = 6
Elements of Asoc Array for b == 6 are,
Element No: 1
Value of a = 0
Value of b = 6
Element No: 2
Value of a = 2
Value of b = 6
Element No: 3
Value of a = 1
Value of b = 6
Elements of Asoc Array for a == 2, b == 6,
Element No: 1
Value of a = 2
Value of b = 6
Elements of Asoc Array for a < 2, b > 5,
Element No: 1
Value of a = 0
Value of b = 6
Element No: 2
Value of a = 1
Value of b = 6

六、Array Element Finder methods FIND_FIRST and FIND_LAST along ‘with’ clause

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,qu[$],tmp_var;
  int    cnt;
   
  initial begin
     
    pkt = new();
    pkt.a = 3;
    pkt.b = 8;
    assoc_array[2] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[5] = pkt;
     
    pkt = new();
    pkt.a = 2;
    pkt.b = 6;
    assoc_array[6] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[8] = pkt;
     
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    assoc_array[9] = pkt;
     
    //----------------------------------------------------------------------------
    //------- Find First/Last element Method -------
    //----------------------------------------------------------------------------
     
    //Type-1: returning first matching element
    qu = assoc_array.find_first with (item.a == 8);
    cnt = qu.size();
     
    $display("First Element of Asoc Array for a == 8 are,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
     
    //Type-2: returning first matching element
    qu = assoc_array.find_first with (item.a == 8 && item.b == 3);
    cnt = qu.size();
     
    $display("First Element of Asoc Array for a == 8 , b == 3 are,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}
     
    //Type-3: returning last matching element
    qu = assoc_array.find_last with (item.a == 8 &&  item.b == 3);
    cnt = qu.size();
     
    $display("Last Element of Asoc Array for a == 8 , b == 3 are,");
    for(int i=0;i<cnt;i++) begin //{
      tmp_var = qu.pop_front();
      $display("Element No: %0d",i+1);
      tmp_var.display();
    end //}   
  end
endmodule

 Simulator Output 

First Element of Asoc Array for a == 8 are,
Element No: 1
Value of a = 8
Value of b = 3
First Element of Asoc Array for a == 8 , b == 3 are,
Element No: 1
Value of a = 8
Value of b = 3
Last Element of Asoc Array for a == 8 , b == 3 are,
Element No: 1
Value of a = 8
Value of b = 3

七、Array Element Finder methods min and max

module fixedsize_array;
   
  //declaration of array’s
  int array_1[4];
  int temp_value[$];
   
  initial begin
    //array initialization
    array_1  = '{80,20,3,40};
     
    temp_value = array_1.min();
    $display("Min value elenent of array_1 is \t%p",temp_value);
 
    temp_value = array_1.max();
    $display("Max value elenent of array_1 is \t%p",temp_value);      
  end
   
endmodule

       Simulator Output 

       Min value elenent of array_1 is ‘{3}
       Max value elenent of array_1 is ‘{80}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元直数字电路验证

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值