tools/build-pkg.c
static void pkg_sort(PKGIndexEntry * const index, const uint32_t nfiles,
                     const char *namebuf,
                     const uint32_t left, const uint32_t right)
{
#define NAME(a)  (namebuf + PKG_NAMEOFS(index[(a)].nameofs_flags))
#define LESS(a)  (index[(a)].hash < pivot_hash   \
               || (index[(a)].hash == pivot_hash \
                   && stricmp(NAME(a), pivot_name) < 0))
#define SWAP(a,b) do {        \
    PKGIndexEntry tmp;        \
    tmp = index[(a)];         \
    index[(a)] = index[(b)];  \
    index[(b)] = tmp;         \
} while (0)

    if (left >= right) {
        return;
    }
    const uint32_t pivot = (left + right + 1) / 2;
    const uint32_t pivot_hash = index[pivot].hash;
    const char * const pivot_name = NAME(pivot);
    SWAP(pivot, right);

    uint32_t store = left;
    while (store < right && LESS(store)) {
        store++;
    }
    for (uint32_t i = store+1; i <= right-1; i++) {
        if (LESS(i)) {
            SWAP(i, store);
            store++;
        }
    }
    SWAP(right, store);  // right == old pivot

    if (store > 0) {
        pkg_sort(index, nfiles, namebuf, left, store-1);
    }
    pkg_sort(index, nfiles, namebuf, store+1, right);
}
/**
 * pkg_sort:  Sort an array of PKGIndexEntry structures using the
 * quicksort algorithm.
 *
 * [Parameters]
 *     index: Array of PKGIndexEntry structures.
 *     nfiles: Number of entries in index[] (i.e., number of files).
 *     namebuf: Pathname data buffer.
 *     left: Lowest entry number to sort.
 *     right: Highest entry number to sort.
 */
Sort an array of PKGIndexEntry structures using the
quicksort algorithm.

[Parameters]
index: Array of PKGIndexEntry structures.
nfiles: Number of entries in index[] .
namebuf: Pathname data buffer.
left: Lowest entry number to sort.
right: Highest entry number to sort.
Sort an array of PKGIndexEntry structures using the quicksort algorithm . [ Parameters ] index : Array of PKGIndexEntry structures . nfiles : Number of entries in index [] . namebuf : Pathname data buffer . left : Lowest entry number to sort . right : Highest entry number to sort .
static void pkg_sort(PKGIndexEntry * const index, const uint32_t nfiles,
                     const char *namebuf,
                     const uint32_t left, const uint32_t right)
{
#define NAME(a)  (namebuf + PKG_NAMEOFS(index[(a)].nameofs_flags))
#define LESS(a)  (index[(a)].hash < pivot_hash   \
               || (index[(a)].hash == pivot_hash \
                   && stricmp(NAME(a), pivot_name) < 0))
#define SWAP(a,b) do {        \
    PKGIndexEntry tmp;        \
    tmp = index[(a)];         \
    index[(a)] = index[(b)];  \
    index[(b)] = tmp;         \
} while (0)
    if (left >= right) {
        return;
    }
    const uint32_t pivot = (left + right + 1) / 2;
    const uint32_t pivot_hash = index[pivot].hash;
    const char * const pivot_name = NAME(pivot);
    SWAP(pivot, right);
    uint32_t store = left;
    while (store < right && LESS(store)) {
        store++;
    }
    for (uint32_t i = store+1; i <= right-1; i++) {
        if (LESS(i)) {
            SWAP(i, store);
            store++;
        }
    }
    SWAP(right, store);  
    if (store > 0) {
        pkg_sort(index, nfiles, namebuf, left, store-1);
    }
    pkg_sort(index, nfiles, namebuf, store+1, right);
}
static void pkg_sort ( PKGIndexEntry * const index , const uint32_t nfiles , const char * namebuf , const uint32_t left , const uint32_t right ) { #define NAME ( a )   (namebuf + PKG_NAMEOFS(index[(a)].nameofs_flags)) #define LESS ( a )   (index[(a)].hash < pivot_hash   \
               || (index[(a)].hash == pivot_hash \
                   && stricmp(NAME(a), pivot_name) < 0)) #define SWAP ( a , b )  do {        \
    PKGIndexEntry tmp;        \
    tmp = index[(a)];         \
    index[(a)] = index[(b)];  \
    index[(b)] = tmp;         \
} while (0) if ( left >= right ) { return ; } const uint32_t pivot = ( left + right + 1 ) / 2 ; const uint32_t pivot_hash = index [ pivot ] . hash ; const char * const pivot_name = NAME ( pivot ) ; SWAP ( pivot , right ) ; uint32_t store = left ; while ( store < right && LESS ( store ) ) { store ++ ; } for ( uint32_t i = store + 1 ; i <= right - 1 ; i ++ ) { if ( LESS ( i ) ) { SWAP ( i , store ) ; store ++ ; } } SWAP ( right , store ) ; if ( store > 0 ) { pkg_sort ( index , nfiles , namebuf , left , store - 1 ) ; } pkg_sort ( index , nfiles , namebuf , store + 1 , right ) ; }
pkg_sort:  Sort an array of PKGIndexEntry structures using the
 quicksort algorithm.
pkg_sort : Sort an array of PKGIndexEntry structures using the quicksort algorithm .
// right == old pivot