Count unique values in an array or a queue

Hi All,
I want to get the number of unique values in an array / a queue.

This method does not work


int values[$] = '{1,2,3,4,1,2,3,4};
int unique_value_count;

unique_value_count = values.unique().size();

It gives the following error message

Member operator “.” cannot be used on object of type int.
Expression: this.values.unique
Source info: values.\unique ().size

Following method works


int values[$] = '{1,2,3,4,1,2,3,4};
int unique_value_count;
int unique_values[$];

unique_values = values.unique();
unique_value_count = unique_values.size();

First implementation is more concise and I prefer it more than second one.
Could someone please help me to figure out why the first method does not work?

My full requirement is to constraint the number of unique values in a queue.


constraint values_c{
    values.size() == 5;
    values.unique().size() == 2;
  }

Thanks in advance.

In reply to tharindu:

The first method is known as function chaining. This syntax was just added to the upcoming IEEE 1800-2023 SystemVerilog LRM. Some tools, like Questa, already support it. Check with your tool vendor.

In reply to dave_59:

Hi @dave_59,

Thank you very much for the information. I am using VCS 2023.03 version. I tested in EDA playground. Yes, the first method works well with Mentor Questa 2021.3. I will use the second method for the moment as VCS does’t support function chaining yet.

Thanks again!

In reply to dave_59:


I tried the followin code in Questa, i am getting constraint solver failure. 

module tb();
  
  class demo; 
    
    int values[];
    
    constraint values_c{
      values.size() == 5;
      values.unique().size() == 2;
    }
    
    
  endclass 
  
  
  demo demo_h = new();
  
  initial begin 
    demo_h.randomize();
    $display("values %0p",demo_h.values);
  end
  
endmodule


In reply to uvm_novice:

Hi @uvm_novice,

Yes, that’s true. It says that there are conflicts. But anyway Questa seems to support function chaining outside the constraints like below.


module tb();
 
  class demo; 
    int values[$] = '{1,2,3,4,1,2,3,4};
    int unique_value_count;

    function void demo_f;
      unique_value_count = values.unique().size();    
    endfunction

  endclass 
 
 
  demo demo_h = new();
 
  initial begin 
    demo_h.demo_f;
    $display("unique_value_count: %0d", demo_h.unique_value_count);
    
  end
 
endmodule