bridge/class-master/depend/pari/src/basemath/bibli2.c
/* Sort the vector x in place, using cmp to compare entries */
Sort the vector x in place, using cmp to compare entries
Sort the vector x in place using cmp to compare entries
void
gen_sort_inplace(GEN x, void *E, int (*cmp)(void*,GEN,GEN), GEN *perm)
{
  long tx, lx, i;
  pari_sp av = avma;
  GEN y;
  init_sort(&x, &tx, &lx);
  if (lx<=2)
  {
    if (perm) *perm = lx == 1? cgetg(1, t_VECSMALL): mkvecsmall(1);
    return;
  }
  y = gen_sortspec(x,lx-1, E, cmp);
  if (perm)
  {
    GEN z = new_chunk(lx);
    for (i=1; i<lx; i++) gel(z,i) = gel(x,y[i]);
    for (i=1; i<lx; i++) gel(x,i) = gel(z,i);
    *perm = y;
    avma = (pari_sp)y;
  } else {
    for (i=1; i<lx; i++) gel(y,i) = gel(x,y[i]);
    for (i=1; i<lx; i++) gel(x,i) = gel(y,i);
    avma = av;
  }
}
void gen_sort_inplace ( GEN x , void * E , int ( * cmp ) ( void * , GEN , GEN ) , GEN * perm ) { long tx , lx , i ; pari_sp av = avma ; GEN y ; init_sort ( & x , & tx , & lx ) ; if ( lx <= 2 ) { if ( perm ) * perm = lx == 1 ? cgetg ( 1 , t_VECSMALL ) : mkvecsmall ( 1 ) ; return ; } y = gen_sortspec ( x , lx - 1 , E , cmp ) ; if ( perm ) { GEN z = new_chunk ( lx ) ; for ( i = 1 ; i < lx ; i ++ ) gel ( z , i ) = gel ( x , y [ i ] ) ; for ( i = 1 ; i < lx ; i ++ ) gel ( x , i ) = gel ( z , i ) ; * perm = y ; avma = ( pari_sp ) y ; } else { for ( i = 1 ; i < lx ; i ++ ) gel ( y , i ) = gel ( x , y [ i ] ) ; for ( i = 1 ; i < lx ; i ++ ) gel ( x , i ) = gel ( y , i ) ; avma = av ; } }
Sort the vector x in place, using cmp to compare entries
Sort the vector x in place using cmp to compare entries