G'MIC exercises

Some news about the management of dynamic arrays in the math parser.
I’ve added new functions in command math_lib to ease handling this type of structure.

Basically, you first define a 1-column image with the number of channels you want (filled with 0). It will be used to represent your dynamic array. One example is worth a thousand words :

test :

  1,1,1,3  # A dynamic array of RGB colors (initially empty).

  v -
  eval ${-math_lib}"

    print(dar_size(#0)); # Print number of elements -> '0'

    # 500 insertions at random positions.
    for (k = 0, k<500, ++k,
      dar_insert(#0,u([255,255,255]),round(u(dar_size(#0))));
      ext('w[0] 200,100%,0');  # Display array
    );

    print(dar_size(#0)); # Print number of elements -> '500'

    # 200 removal at random positions.
    for (k = 0, k<200, ++k,
      dar_remove(#0,round(u(dar_size(#0)-1)));
      ext('w[0] 200,100%,0'); # Display array
    );

    print(dar_size(#0)); # Print number of elements -> '300'

    # Access to individual element by I[#ind,pos] :

    dar_insert(#0,[1,2,3],125);
    print(I[#0,125]);
  "
  v +

Maybe @garagecoder will be able to use those functions ? :slight_smile:

EDIT: How this internally work ?
The trick here is to store the current number of element in the image, at position [w*h*d-1], and to have a image height that is always greater or equal than dar_size() + 1. The function dar_insert() resizes the image when needed, and the function dar_remove() also resizes to save memory usage.
The height of an image is not equal to the number of elements in your dynamic arrays, that’s why there is a function dar_size() to get this information.