Summarize array values in a constraint

I tried to summaries array values with an help array of indexes.
my purpose is to summarize vals[ on active indexes[i] only]

rand int vals []; rand int indexes [];

int sum_vals = 0;

I have some constraint that generate the 2 queues with below values:
vals[0] = 8;
vals[1] = 64;
vals[2] = 16;
vals[3] = 8;
vals[4] = 8;
vals[5] = 8;
vals[6] = 8;
vals[7] = 8;

indexes[0] = 4;
indexes[1] = 0;
indexes[2] = 2;

I tried to sum it :
sum_vals = vals[indexes[0]] + vals[indexes[1]] + vals[indexes[2]];

this is my implementation :



constraint list_c {

    foreach(indexes[i]){
        indexes[i] inside {[0:7]};
        sum_total_vals(vals[indexes[i]]) == 1;
    }
}

function int sum_total_vals(int curr_val);
    sum_vals += curr_val;
    return 1;
endfunction


This is working for me but :

  1. I think I did it little complex, I will be happy to know if there is a finest way. maybe with “sum with” ( I tried, but didn’t success it. )
  2. in addition I get a warning :
    Function sum_total_vals used in constraint is rosky and has write access to non-local/ persistent variable.

In reply to AL_verif:

It might be easier to have indexes be an array of single bits the same size as you your vals array.

conatraint c_list {
     indexes.sum() with (int'(item)) == 3; // your example showed only 3 indexes "active"
     vals.sum() with {indexes[item.index] ? int'item) : 0 ) == sum_vals;
{

In reply to dave_59:

Thanks a lot! It is nice and clear!