How to find all indices of an associative array

Hi,
I need to list all the indices of an associative array using any of the built in methods. I tried using array.unique(), but dosen’t seem to work.

typedef bit[3:0] warp_id_t;

module test;
  int           in_use[warp_id_t];
  int           index_q[$];
  
  
  
  initial begin
    in_use[4'd10] = 1;
    in_use[4'd15] = 1;
    in_use[4'd1] = 1;
    in_use[4'd5] = 1;
    in_use[4'd4] = 1;
    in_use[4'd7] = 1;
    foreach(in_use[i]) begin
      $display("in_use[%0d]: %0d",i,in_use[i]);
      
    end
    
  
    index_q = in_use.unique();
    
    
    $display("size of index_q : %0d", index_q.size());
    
    foreach (index_q[i]) begin
      $display("Listing elements in index_q[%0d] : %0d",i,index_q[i]);
    end
    
        
    //index_q.shuffle();
    //for ( int j = 0; j < index_q.size; j++ ) begin
    // $display("after shuffle, index element: %0d",index_q[j]);
    //end
    
    
  end
   
   
endmodule

Is there any other way of finding the indices other than pushing the indices into another queue by iterating the array.

Thanks,
Madhu

In reply to mseyunni:

It’s difficult to know what you are trying to accomplish. When you say " to list all the indices of an associative array". Do you mean you want to print a list, or do you want to construct an array containing all the indices

  foreach(in_use[i]) begin
      $display("%0d",i);
    end

index_q = in_use.find_index with (1);

In reply to dave_59:

Hi Dave,

I want to capture the indices into a queue to use one of them (randomly picked) as the id that I want to use in my transaction. find_index with (1) works. However, I had to declare index_q is of type warp_id_t. Does the returned queue should be of type index, right ?

Also, how does find_index with (1) works internally ? ( I see this is something similar to while(1)), but I want to understand the internal working of the search.

Thanks,
Madhu

In reply to mseyunni:

That’s how I do it.

There is, to my knowledge, no function such as ‘keys’.

Ray

In reply to mseyunni:
Yes the returned queue has an element type that matches the associative array index type.

All of the builtin find_ methods require a with (expression). Please see section 7.12.1 Array locator methods in the IEEE 1800-2012 LRM.

In reply to raysalemi:

for some reason .unique, doesn’t seem to work in my original post. Not sure, what was the issue ?

Madhu

In reply to mseyunni:
What does “doesn’t seem to work” mean?

Doesn’t compile - what is the error?

Doesn’t give the the result’s you are expecting. What did you expect? and what did you get?

In reply to dave_59:

I get compile error, if I declare


warp_id_t          index_q[$];

Incompatible complex type assignment
  Type of source expression is incompatible with type of target expression. 
  Mismatching types cannot be used in assignments, initializations and 
  instantiations. The type of the target is 'bit[3:0]$[$]', while the type of 
  the source is 'int$[$]'.
  Source Expression: in_use.unique

on the other hand, if i change the declaration to,


int          index_q[$];

I get a different result than what is expected,

size of index_q : 1
Listing elements in index_q[0] : 1

Now, what I don’t understand the reason for the compile error as returned indices is of type warp_id_t, and it does work with your earlier proposed solution of find_index with (1).

after changing the type to int, the result doesn’t make sense.

can you please explain ?

In reply to mseyunni:
I believe you should be using unique_index(), not unique().

In reply to dave_59:

No. Still the same error even with unique_index(). In fact, the LRM has unique(), but unique_index() didn’t give any compile error though !!!