/****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    imerge12(data *asrc1,data *asrc2,int m1,int m2)
{
register data *temp,*tmp,*first1,*last1,*first2,*last2,*SRC,*DES;
register int M;
register data *src1,*src2;


  if ((m1<=0)&&(m2<=0)) return;
  tmp=(data *)malloc((m1+m2)*sizeof(data));
#ifdef SANITY_CHECK
  check_if_null((void *)tmp,"imerge12","tmp");
#endif
  first1=asrc1;first2=asrc2;
  last1=asrc1+m1;
  last2=asrc2+m2;
  temp=tmp;

  for (;((first1!=last1) && (first2!=last2));temp++) {
      if (*first2<*first1) {
        *temp=*first2;
        first2++;
      }
      else {
        *temp=*first1;
        first1++;
      }
  }
 if (first1 != last1)
   IMEMCPY(temp,first1,(last1-first1)*sizeof(data));
 if (first2 != last2)
   IMEMCPY(temp,first2,(last2-first2)*sizeof(data));

  IMEMCPY(asrc1,tmp,m1*sizeof(data)) ;
  IMEMCPY(asrc2,&tmp[m1],m2*sizeof(data)) ;

  free((void *)tmp);
}
