1 #ifndef _WDL_MERGESORT_H_
2 #define _WDL_MERGESORT_H_
5 static void WDL_mergesort(void *base
, size_t nmemb
, size_t size
, int (*compar
)(const void *, const void *), char *tmpspace
)
10 if (nmemb
< 2) return;
15 b2
= b1
+ (n1
* size
);
19 WDL_mergesort(b1
, n1
, size
, compar
, tmpspace
);
20 WDL_mergesort(b2
, n2
, size
, compar
, tmpspace
);
26 if (compar(b1
, b2
) > 0) // out of order, go to full merge
28 size_t sofar
= b1
-(char*)base
;
29 memcpy(tmpspace
,base
,sofar
);
30 memcpy(tmpspace
+sofar
, b2
, size
);
34 char *writeptr
=tmpspace
+sofar
+size
;
35 while (n1
> 0 && n2
> 0)
37 if (compar(b1
, b2
) > 0)
39 memcpy(writeptr
, b2
, size
);
45 memcpy(writeptr
, b1
, size
);
52 if (n1
> 0) memcpy(writeptr
, b1
, n1
* size
);
53 memcpy(base
, tmpspace
, (nmemb
- n2
) * size
);
58 // in order, just advance
62 while (n1
> 0 && n2
> 0);
65 #endif//_WDL_MERGESORT_H_