/****Copyright (c) 1997-2009 All rights reserved
 ****A. V. Gerbessiotis and C. J. Siniolakis
 ****Permission to use, copy, modify, and distribute this software,
 ****and to incorporate it, in whole or in part, into 
 ****other software,is hereby granted without fee, provided that
 ****  (1) the above copyright notice and this permission 
 ****      notice appear in all copies of the source code, and 
 ****      the above copyright notice appear in clearly visible 
 ****      form on all supporting documentation and distribution 
 ****      media, and
 ****     (2) any redistribution of the software, in original
 ****         or modified form, be without fee and subject to
 ****         these same conditions.
 ****   No guarantee, whatsoever, is offered that the code works as
 ****   advertised or is absent of any, even damaging,
 ****   side-effects. Use this code at your own personal risk.
 ****/
#include "smrg.h"

void gmerge13(void *asrc1, void *asrc2, int m1, int m2, int size, 
                int (*compare)(const void *,const void *))
{
 register char *temp,*SRC,*DES;
 register int i,j,l,M,ip,jp,lp;
 register char *src1,*src2;

 if (((m1<=0)&&(m2<=0))||(size<=0))return;
 src1=(char *) asrc1;
 src2=(char *) asrc2;
 temp = (char *) malloc((m1+m2)*size);
#ifdef SANITY_CHECK
 check_if_null((void *)temp,"gmerge3","temp");
#endif
  i=j=l=0; ip=jp=lp=0;
  for(;((i<m1) && (j<m2));l++,lp+=size){
    if (compare((src1+ip), (src2+jp)) < 0){
      MEMCPY((temp+lp),(src1+ip),size);
      i++; ip+=size;
    }
    else {
      MEMCPY((temp+lp),(src2+jp),size);
      j++; jp+=size;
    }
  }
  if (i < m1) MEMCPY((temp+lp),(src1+ip),(m1-i)*size);
  if (j < m2) MEMCPY((temp+lp),(src2+jp),(m2-j)*size);
  MEMCPY(src1,temp,m1*size);
  MEMCPY(src2,&temp[m1*size],m2*size);

  free((void *)temp);
}
