After playing with merge sort, and looking at timsort, I realized that tim sort is just merge sort with the difference of how initial subarrays are sorted. And I looked up smoothsort, and it seems easy to implement. So, I thought of an algorithm that use insertion sort and smoothsort depending on subarray size, and then just merge them together (Timsort, but with either insertion sort or smoothsort at the very beginning). This makes me realize that I could make a small sorting algorithm library for the purpose of solving this problem.
The thing is that G’MIC has no built-in way to sort parts of images effectively. I do plan to make a pixelsorting filters that’s a little bit more advanced than the G’MIC stdlib version, so hence why I’m doing this. Another reason is that I might experiment with multi-threaded sorting as merging lends itself to parallelization naturally.
Problem is that I hit a limitation:
foo_test_sort_lib:
5,1,1,3,u ({expr('u',5)})
eval ""${-rep_sort_lib}"insertion_sort(0,0,0,0,0,'x',
1,0,0,0,0,'x',5);"
#@cli rep_sort_lib
#@cli : Return strings which is code of sorting algorithms to be used in math parser.
#@cli : $ echo ${-rep_sort_lib}
rep_sort_lib:
status "
const _x=0;const _y=1;const _z=2;const _c=3;
insertion_sort(destination_ind,destination_x,destination_y,destination_z,destination_c,destination_axis,source_ind,source_x,source_y,source_z,source_c,source_axis,length)=(
if(size(source_axis)==1
,axis_src_choice=source_axis[0]
,run('error invalid_arg_source_axis');
);
if(!inrange(axis_src_choice,0,4,1,0),run('error invalid_arg_source_axis'););
if(size(destination_axis)==1
,axis_des_choice=destination_axis[0]
,run('error invalid_arg_source_axis');
);
if(!inrange(axis_des_choice,0,4,1,0),run('error invalid_arg_source_axis'););
axis_des_choice==_c?(
sorting_array=crop(#source_ind,source_x#,source_y#,source_z#,source_c#,length#,1,1,1);
):
axis_des_choice==_z?(
sorting_array=crop(#source_ind,source_x#,source_y#,source_z#,source_c#,1,length#,1,1);
):
axis_des_choice==_y?(
sorting_array=crop(#source_ind,source_x#,source_y#,source_z#,source_c#,1,1,length#,1);
):
axis_des_choice==_x?(
sorting_array=crop(#source_ind,source_x#,source_y#,source_z#,source_c#,1,1,1,length#);
);
sorting_array;
);
"
C:\Windows\System32>gmic foo_test_sort_lib
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic] *** Error in ./foo_test_sort_lib/ (file 'C:\Users\Miguel\user.gmic', line #11) *** Command 'eval': When substituting function 'insertion_sort()': Function 'crop()': Fifth argument (of type 'const scalar' and value 0) is not a strictly positive integer constant, in expression 'axis_des_choice==_c?( sorting_array=crop(1,(0),(0),(0),(0),(...)'.
[gmic] Command 'eval' has the following description:
eval (+):
expression
Evaluate specified math expression.
* If no command selection is specified, the expression is evaluated once
and its result is set to status.
* If command selection is specified, the evaluation is looped over
selected images. Status is unchanged. In this case, 'eval' is similar to
fill without assigning the image values.