/****Copyright (c) 1997-2005 All rights reserved
 ****A. V. Gerbessiotis 
 ****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"


/*gmerge12*/
void gmerge(void *asrc1,void *asrc2,int m1,int m2,int size,int (*compare)(const void*,const void*))
{
 register char *temp,*tmp,*first1,*last1,*first2,*last2,*SRC,*DES;
 register int M;
 register char *src1,*src2;


 if (((m1<= 0)  && (m2<= 0)) || (size <= 0)) return ;
 src1= (char *) asrc1;
 src2= (char *) asrc2;
 tmp = (char *) malloc((m1+m2)*size);
#ifdef SANITY_CHECK
 check_if_null((void *)tmp,"gmerge2","tmp");
#endif
 first1=src1;first2=src2;
 last1 =src1+m1*size;
 last2 =src2+m2*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;
    }
 if ( first1 != last1) 
   MEMCPY(temp,first1,(last1-first1));
 if ( first2 != last2) 
   MEMCPY(temp,first2,(last2-first2));

  MEMCPY(src1,tmp,m1*size) ;
  MEMCPY(src2,&tmp[m1*size],m2*size);

  free((void *)tmp);
}
