/****Copyright (c) 1997-2005 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 gmerge1(void *asrc1, void *asrc2, int m, int k, int size, 
                int (*compare)(const void *,const void *))
{
 register char *temp;
 register int i,j,l,M;
 register char *src1,*src2,*SRC,*DES;

 src1=(char *) asrc1;
 src2=(char *) asrc2;


 if (((m<=0)&&(k<=0))||(size<=0))return;

 temp = (char *) malloc((m+k)*size);
#ifdef SANITY_CHECK
 if (temp == NULL) {
   printf("merge: malloc ERROR\n");
   return;	
 }
#endif
  i=j=l=0;
  while ((i < m) && (j < k)) {
    if (compare(&src1[i*size], &src2[j*size]) < 0) {
      MEMCPY(&temp[l*size],&src1[i*size],size);
      i++;l++;
    }
    else {
      MEMCPY(&temp[l*size],&src2[j*size],size);
      j++;l++;
    }
  }

  if (i < m) MEMCPY(&temp[l*size],&src1[i*size],(m-i)*size);
  if (j < k) MEMCPY(&temp[l*size],&src2[j*size],(k-j)*size);
  MEMCPY(src1,temp,m*size);
  MEMCPY(src2,&temp[m*size],k*size);

  free((void *)temp);
}
