In the below snippet, The expected output from the first $display is “unique indices are '{0, 1, 2, 4, 6}”. And I am able to get the proper output from the first $display statement.
The expected output from the second $display is “unique indices with > 3 condition are '{2, 4, 6}”. But, I am not getting the expected output from the second $display statement. Instead, I am getting “unique indices with > 3 condition are '{0, 2}” as an output from the second $display statement. Please explain, what went wrong with my analysis?
module test();
int q[$];
int da[] = {1,3,5,5,7,3,9};
initial
begin
q = da.unique_index();
$display("unique indices are %p", q);
q.delete();
q = da.unique_index() with (item > 3);
$display("unique indices with > 3 condition are %p", q);
end
endmodule
unique_index() returns the indices of all elements with unique values or whose expression evaluates to a unique value. The queue returned contains one and only one entry for each of the values found in the array. The ordering of the returned elements is unrelated to the ordering of the original array. The index returned for duplicate valued entries may be the index for one of the duplicates.
When applying a ‘with’ clause, the unique_index() takes into account the boolean return value as the ‘unique’ criteria.
For index 0, ‘1 > 3’ evaluates as false, hence it is unique. For index 1, ‘3 > 3’ also evaluates as false, which has already been identified. For index 2, ‘5 > 3’ evaluates as true. Since this is the first true, the index is unique and added to the return queue. All of the remaining indexes also return true, so they are not unique.
This would have to split into two operations along with constructing an associative array.
Untested:
module test();
int q[$];
int da[] = {1,3,5,5,7,3,9};
int aa[int];
initial
begin
q = da.find_index() with (item >3);
foreach(q[idx]) aa[idx] == da[idx];
q = aa.unique_index();
$display("unique indices with > 3 condition are %p", q);
end
endmodule
module test();
int q[$];
int da[] = {1,3,5,5,7,3,9};
int aa[int];
initial
begin
q = da.find_index() with (item >3);
$display("indices with > 3 condition are %p", q);
foreach(q[idx]) aa[q[idx]] = da[q[idx]];
$display("indices with > 3 condition are %p", aa);
q = aa.unique_index();
$display("unique indices with > 3 condition are %p", q);
end
endmodule
indices with > 3 condition are '{2, 3, 4, 6}
indices with > 3 condition are '{2:5, 3:5, 4:7, 6:9}
unique indices with > 3 condition are '{2, 4, 6}