LMMS
Loading...
Searching...
No Matches
mergesort.h
Go to the documentation of this file.
1#ifndef _WDL_MERGESORT_H_
2#define _WDL_MERGESORT_H_
3
4#include "wdltypes.h"
5
6static void WDL_STATICFUNC_UNUSED WDL_mergesort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), char *tmpspace)
7{
8 char *b1,*b2;
9 size_t n1, n2;
10
11 if (nmemb < 2) return;
12
13 n1 = nmemb / 2;
14 b1 = (char *) base;
15 n2 = nmemb - n1;
16 b2 = b1 + (n1 * size);
17
18 if (nmemb>2)
19 {
20 WDL_mergesort(b1, n1, size, compar, tmpspace);
21 WDL_mergesort(b2, n2, size, compar, tmpspace);
22 }
23
24
25 do
26 {
27 if (compar(b1, b2) > 0) // out of order, go to full merge
28 {
29 size_t sofar = b1-(char*)base;
30 memcpy(tmpspace,base,sofar);
31 memcpy(tmpspace+sofar, b2, size);
32 b2 += size;
33 n2--;
34
35 char *writeptr=tmpspace+sofar+size;
36 while (n1 > 0 && n2 > 0)
37 {
38 if (compar(b1, b2) > 0)
39 {
40 memcpy(writeptr, b2, size);
41 b2 += size;
42 n2--;
43 }
44 else
45 {
46 memcpy(writeptr, b1, size);
47 b1 += size;
48 n1--;
49 }
50 writeptr += size;
51 }
52
53 if (n1 > 0) memcpy(writeptr, b1, n1 * size);
54 memcpy(base, tmpspace, (nmemb - n2) * size);
55
56 break;
57 }
58
59 // in order, just advance
60 b1 += size;
61 n1--;
62 }
63 while (n1 > 0 && n2 > 0);
64}
65
66#endif//_WDL_MERGESORT_H_
static void WDL_STATICFUNC_UNUSED WDL_mergesort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *), char *tmpspace)
Definition mergesort.h:6
memcpy(hh, h, RAND_HEAD_LEN)
ulg size
Definition extract.c:2350
#define WDL_STATICFUNC_UNUSED
Definition wdltypes.h:87