My question is about interleaving elements in an array according to their occurrences. I have a queue with some elements representing number of times a particular input is to be generated. So let’s say we have following queue elements:
my_q = {100,50,50,0};
Now, this queue represents the following:
> Input at position-0 should come 2 times > Input at position-1 should come 1 times > Input at position-2 should come 1 times > Input at position-3 should come 0 times
So, the input order should be:
0 (Input-0) — 1 (Input-1) — 0 (Input-0) — 2 (Input-2)
So, I made the following code which works on taking the HCF of number and storing them in a queue:
initial begin int rate[$ ], mod_rate[$ ], final_port_seq[$ ]; int hcf; int num_slot; rate = {100,50,50,0}; hcf = find_hcf(rate); // Finds HCF from elements of rate foreach(rate[i]) begin num_slot += rate[i]/hcf; // Total number of slots mod_rate[i] = rate[i]/hcf; // Get number of occurrences of particular element end $ display("HCF = %0d Number of slots = %0d",hcf, num_slot); // Need to change the following logic: for (int i =0; i< num_slot;) begin foreach (mod_rate[j]) begin if (mod_rate[j] != 0 ) begin // This keeps the sequence 0-1-2-0 mod_rate[j]--; final_port_seq[i] = j; i++; end end end $ display("Final sequence : "); foreach (final_port_seq[i]) begin $ write(" %0d", final_port_seq[i]); end end end // initial function int find_hcf(int q[$ ]); int min[$ ]; int max[$ ]; int q_refined[$ ]; int flag; q_refined = q.find(item) with (item !=0); // 0 is port OFF min = q_refined.min(); max = q_refined.max(); $ display("min = %0d max = %0d",min[0],max[0]); for(int i =min[0];i>0;i--) begin flag = 0; foreach(q[j]) begin if(q[j]%i == 0) flag++; end if(flag == q.size()) begin find_hcf = i; break; end end endfunction
The code works fine as far as slotting is concerned and I got the following output:
output: min = 50 max = 100 HCF = 50 Number of slots = 4 Final sequence : 0 1 2 0
But I want to interleave the queue elements and get the output as follows:
Final sequence : 0 1 0 2
Can anyone tell me what kind of logic I need to implement to have interleaved queue elements? The input rate elements can be any other sequence also.
P.S.: here I have used systemverilog as a language but I am looking for some logic for implementation. Queue is similar to dynamic array.