Array locator methods
Table of Contents
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:
Method | Description |
---|---|
find() | returns all the elements satisfying the given expression |
find_first() | returns the first element satisfying the given expression |
find_last() | returns the last element satisfying the given expression |
min() | returns the element with the minimum value or whose expression evaluates to a minimum |
max() | returns the element with the maximum value or whose expression evaluates to a maximum |
unique() | returns all elements with unique values or whose expression is unique |
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
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}