Skip to content

Commit

Permalink
Add optional argument to math parser function 'sort()' to specify sor…
Browse files Browse the repository at this point in the history
…ting index.
  • Loading branch information
dtschump committed Sep 16, 2024
1 parent c68b1af commit f20e9dd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions CImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22434,23 +22434,28 @@ namespace cimg_library {
_cimg_mp_op("Function 'sort()'");
s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
arg1 = compile(ss5,s1,depth1,0,block_flags);
arg2 = arg4 = 1; arg3 = ~0U;
arg2 = arg4 = 1; arg3 = ~0U; arg5 = 0;
if (s1<se1) {
s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
arg2 = compile(s1,s0,depth1,0,block_flags);
if (s0<se1) {
s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
arg3 = compile(s0,s1,depth1,0,block_flags);
arg4 = s1<se1?compile(++s1,se1,depth1,0,block_flags):1;
if (s1<se1) {
s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
arg4 = compile(s1,s0,depth1,0,block_flags);
arg5 = s0<se1?compile(++s0,se1,depth1,0,block_flags):0;
}
}
}
_cimg_mp_check_type(arg1,1,2,0);
_cimg_mp_check_type(arg2,2,1,0);
if (arg3!=~0U) _cimg_mp_check_type(arg3,3,1,0);
_cimg_mp_check_type(arg4,4,1,0);
_cimg_mp_check_type(arg5,5,1,0);
p1 = size(arg1);
pos = vector(p1);
CImg<ulongT>::vector((ulongT)mp_sort,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
CImg<ulongT>::vector((ulongT)mp_sort,pos,arg1,p1,arg2,arg3,arg4,arg5).move_to(code);
return_comp = true;
_cimg_mp_return(pos);
}
Expand Down Expand Up @@ -28556,15 +28561,16 @@ namespace cimg_library {
const unsigned int
siz = (unsigned int)mp.opcode[3],
nb_elts = mp.opcode[5]==~0U?siz:(unsigned int)_mp_arg(5),
siz_elt = (unsigned int)_mp_arg(6);
siz_elt = (unsigned int)_mp_arg(6),
sort_index = std::min((unsigned int)_mp_arg(7),siz_elt - 1);
const ulongT sn = siz_elt*nb_elts;
if (sn>siz || siz_elt<1)
throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'sort()': "
"Arguments 'nb_elts=%g' and 'siz_elt=%g' are invalid "
"for sorting a vector of size %u.",
mp.imgin.pixel_type(),_mp_arg(5),_mp_arg(6),siz);
CImg<doubleT>(ptrd,siz_elt,nb_elts,1,1,true) = CImg<doubleT>(ptrs,siz_elt,nb_elts,1,1,true).
get_sort(is_increasing,siz_elt>1?'y':0);
shift(-(int)sort_index,0,0,0,2).get_sort(is_increasing,siz_elt>1?'y':0).shift((int)sort_index,0,0,0,2);
if (sn<siz) CImg<doubleT>(ptrd + sn,siz - sn,1,1,1,true) = CImg<doubleT>(ptrs + sn,siz - sn,1,1,1,true);
return cimg::type<double>::nan();
}
Expand Down

0 comments on commit f20e9dd

Please sign in to comment.