/****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	gmerge2(void *asrc1,void *asrc2,int m,int k,int size,int (*compare)(const void*,const void*))
{
 register char *temp,*tmp,*first1,*last1,*first2,*last2,*SRC,*DES;
 register int i,j,l,M;
 register char *src1,*src2;

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

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

  tmp = (char *) malloc((m+k)*size);
  if (temp == NULL) {
     printf("merge: malloc ERROR\n"); return;	
  }

  first1=src1;first2=src2;
  last1 =&src1[(m)*size];
  last2 =&src2[(k)*size];
  temp=tmp;

 for (; first1 != last1 && first2 != last2; temp+=size)
    if (compare(first2,first1)<0) {
      MEMCPY(temp,first2,size);
      first2+=size;
    } 
    else {
      MEMCPY(temp,first1,size);
      first1+=size;
    }
/*
    for (; first1 != last1 ; temp+=size,first1+=size) 
         MEMCPY(temp,first1,size);
    for (; first2 != last2 ; temp+=size,first2+=size) 
         MEMCPY(temp,first2,size);
*/
 if ( first1 != last1) 
   MEMCPY(temp,first1,(last1-first1));
 if ( first2 != last2) 
   MEMCPY(temp,first2,(last2-first2));

  MEMCPY(src1,tmp,m*size) ;
  MEMCPY(src2,&tmp[m*size],k*size) ;

  free((void *)tmp);
}
