/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 2003-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Phong Vo <[email protected]> *17* *18***********************************************************************/19#include "vchdr.h"2021/* Like qsort() but allows a struct to hold add'l data describing objects.22**23** Written by Kiem-Phong Vo.24*/2526#define SWAP(le, re, ne) \27do { Vcchar_t *ll = (Vcchar_t*)(le); \28Vcchar_t *rr = (Vcchar_t*)(re); \29ssize_t nn = (ne); \30for(; nn > 0; --nn, ++ll, ++rr) \31{ int ss = *ll; *ll = *rr; *rr = ss; } \32} while(0)3334#if __STD_C35void vcqsort(Void_t* list, ssize_t n, ssize_t size, Vccompare_f comparf, Void_t* disc)36#else37void vcqsort(list, n, size, sortf, disc)38Void_t* list; /* list of objects to be sorted */39ssize_t n; /* number of objects in list[] */40ssize_t size; /* size in byte of each object */41Vccompare_f comparf; /* comparison function */42Void_t* disc; /* adjunct struct for sortf() */43#endif44{45ssize_t l, r;46Vcchar_t *base = (Vcchar_t*)list;4748if(n <= 1)49return;5051if(n == 2)52{ if((*comparf)(base, base+size, disc) > 0)53SWAP(base, base+size, size);54return;55}5657for(l = 1, r = n; l < r; ) /* pivot on element 0 */58{ if((*comparf)(base, base + l*size, disc) >= 0)59l += 1;60else if((r -= 1) > l)61SWAP(base + l*size, base + r*size, size);62}6364if((l -= 1) > 0) /* move the pivot into its final place */65SWAP(base, base + l*size, size);6667if(l > 1)68vcqsort(base, l, size, comparf, disc);6970if((n -= r) > 1)71vcqsort(base + r*size, n, size, comparf, disc);72}737475