Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/vcqsort.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vchdr.h"
21
22
/* Like qsort() but allows a struct to hold add'l data describing objects.
23
**
24
** Written by Kiem-Phong Vo.
25
*/
26
27
#define SWAP(le, re, ne) \
28
do { Vcchar_t *ll = (Vcchar_t*)(le); \
29
Vcchar_t *rr = (Vcchar_t*)(re); \
30
ssize_t nn = (ne); \
31
for(; nn > 0; --nn, ++ll, ++rr) \
32
{ int ss = *ll; *ll = *rr; *rr = ss; } \
33
} while(0)
34
35
#if __STD_C
36
void vcqsort(Void_t* list, ssize_t n, ssize_t size, Vccompare_f comparf, Void_t* disc)
37
#else
38
void vcqsort(list, n, size, sortf, disc)
39
Void_t* list; /* list of objects to be sorted */
40
ssize_t n; /* number of objects in list[] */
41
ssize_t size; /* size in byte of each object */
42
Vccompare_f comparf; /* comparison function */
43
Void_t* disc; /* adjunct struct for sortf() */
44
#endif
45
{
46
ssize_t l, r;
47
Vcchar_t *base = (Vcchar_t*)list;
48
49
if(n <= 1)
50
return;
51
52
if(n == 2)
53
{ if((*comparf)(base, base+size, disc) > 0)
54
SWAP(base, base+size, size);
55
return;
56
}
57
58
for(l = 1, r = n; l < r; ) /* pivot on element 0 */
59
{ if((*comparf)(base, base + l*size, disc) >= 0)
60
l += 1;
61
else if((r -= 1) > l)
62
SWAP(base + l*size, base + r*size, size);
63
}
64
65
if((l -= 1) > 0) /* move the pivot into its final place */
66
SWAP(base, base + l*size, size);
67
68
if(l > 1)
69
vcqsort(base, l, size, comparf, disc);
70
71
if((n -= r) > 1)
72
vcqsort(base + r*size, n, size, comparf, disc);
73
}
74
75