bridge/class-master/depend/pari/src/basemath/bibli2.c
/********************************************************************/
/**                   MERGE & SORT FACTORIZATIONS                  **/
/********************************************************************/
/* merge fx, fy two factorizations, whose 1st column is sorted in strictly
 * increasing order wrt cmp */
MERGE & SORT FACTORIZATIONS
merge fx, fy two factorizations, whose 1st column is sorted in strictly
increasing order wrt cmp
MERGE & SORT FACTORIZATIONS merge fx fy two factorizations whose 1st column is sorted in strictly increasing order wrt cmp
GEN
merge_factor(GEN fx, GEN fy, void *data, int (*cmp)(void *,GEN,GEN))
{
  GEN x = gel(fx,1), e = gel(fx,2), M, E;
  GEN y = gel(fy,1), f = gel(fy,2);
  long ix, iy, m, lx = lg(x), ly = lg(y), l = lx+ly-1;
  M = cgetg(l, t_COL);
  E = cgetg(l, t_COL);
  m = ix = iy = 1;
  while (ix<lx && iy<ly)
  {
    int s = cmp(data, gel(x,ix), gel(y,iy));
    if (s < 0)
    { gel(M,m) = gel(x,ix); gel(E,m) = gel(e,ix); ix++; }
    else if (s == 0)
    {
      GEN z = gel(x,ix), g = addii(gel(e,ix), gel(f,iy));
      iy++; ix++; if (!signe(g)) continue;
      gel(M,m) = z; gel(E,m) = g;
    }
    else
    { gel(M,m) = gel(y,iy); gel(E,m) = gel(f,iy); iy++; }
    m++;
  }
  while (ix<lx) { gel(M,m) = gel(x,ix); gel(E,m) = gel(e,ix); ix++; m++; }
  while (iy<ly) { gel(M,m) = gel(y,iy); gel(E,m) = gel(f,iy); iy++; m++; }
  setlg(M, m);
  setlg(E, m); return mkmat2(M, E);
}
GEN merge_factor ( GEN fx , GEN fy , void * data , int ( * cmp ) ( void * , GEN , GEN ) ) { GEN x = gel ( fx , 1 ) , e = gel ( fx , 2 ) , M , E ; GEN y = gel ( fy , 1 ) , f = gel ( fy , 2 ) ; long ix , iy , m , lx = lg ( x ) , ly = lg ( y ) , l = lx + ly - 1 ; M = cgetg ( l , t_COL ) ; E = cgetg ( l , t_COL ) ; m = ix = iy = 1 ; while ( ix < lx && iy < ly ) { int s = cmp ( data , gel ( x , ix ) , gel ( y , iy ) ) ; if ( s < 0 ) { gel ( M , m ) = gel ( x , ix ) ; gel ( E , m ) = gel ( e , ix ) ; ix ++ ; } else if ( s == 0 ) { GEN z = gel ( x , ix ) , g = addii ( gel ( e , ix ) , gel ( f , iy ) ) ; iy ++ ; ix ++ ; if ( ! signe ( g ) ) continue ; gel ( M , m ) = z ; gel ( E , m ) = g ; } else { gel ( M , m ) = gel ( y , iy ) ; gel ( E , m ) = gel ( f , iy ) ; iy ++ ; } m ++ ; } while ( ix < lx ) { gel ( M , m ) = gel ( x , ix ) ; gel ( E , m ) = gel ( e , ix ) ; ix ++ ; m ++ ; } while ( iy < ly ) { gel ( M , m ) = gel ( y , iy ) ; gel ( E , m ) = gel ( f , iy ) ; iy ++ ; m ++ ; } setlg ( M , m ) ; setlg ( E , m ) ; return mkmat2 ( M , E ) ; }
MERGE & SORT FACTORIZATIONS
merge fx, fy two factorizations, whose 1st column is sorted in strictly
 increasing order wrt cmp
MERGE & SORT FACTORIZATIONS merge fx fy two factorizations whose 1st column is sorted in strictly increasing order wrt cmp