Hi,
Does the array method unique returns the unique value in the array or does it do both returning the unique values and sort them in ascending order ?
Code snippet :
module top;
initial
begin
int f[8] = {9,2,5,8,5,7,2,9};
int d[] = {1,9,12,16};
int q[$] = {2,31,5,14};
int tq[$];
tq = f.unique();
$display("%p",tq);
end
endmodule
I see that in the o/p sorting is happening. Please let me know that cause for it.
The “unique” method just returns all the unique elements. It does not perform a sort implicitly. You can invoke sort explicitly to sort the array in ascending order.
The following code works fine on EDAPlayground with VCS 2014.
module top;
initial
begin
int f[8] = {9,2,5,8,5,7,2,9};
int d[] = {1,9,12,16};
int q[$] = {2,31,5,14};
int tq[$];
int tq1[$];
tq = f.unique();
$display("%p",tq);
tq.sort();
$display("%p",tq);
end
endmodule
// output
'{9, 2, 5, 8, 7}
'{2, 5, 7, 8, 9}
In reply to Ravi007:
The LRM does not define the ordering returned by unique(). Some algorithms are more efficient when the elements are sorted first, but you can’t count on it.