Back to index

php5  5.3.10
Defines | Functions
mergesort.c File Reference
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "php.h"

Go to the source code of this file.

Defines

#define NATURAL
#define THRESHOLD   16 /* Best choice for natural merge cut-off. */
#define ISIZE   sizeof(int)
#define PSIZE   sizeof(u_char *)
#define ICOPY_LIST(src, dst, last)
#define ICOPY_ELT(src, dst, i)
#define CCOPY_LIST(src, dst, last)
#define CCOPY_ELT(src, dst, i)
#define EVAL(p)
#define swap(a, b)
#define reverse(bot, top)

Functions

static void setup (u_char *list1, u_char *list2, size_t n, size_t size, int(*cmp)(const void *, const void *TSRMLS_DC) TSRMLS_DC)
static void insertionsort (u_char *a, size_t n, size_t size, int(*cmp)(const void *, const void *TSRMLS_DC) TSRMLS_DC)
PHPAPI int php_mergesort (void *base, size_t nmemb, size_t size, int(*cmp)(const void *, const void *TSRMLS_DC) TSRMLS_DC)

Define Documentation

#define CCOPY_ELT (   src,
  dst,
 
)
Value:
do                                 \
              *dst++ = *src++;            \
       while (i -= 1)

Definition at line 87 of file mergesort.c.

#define CCOPY_LIST (   src,
  dst,
  last 
)
Value:
do                                 \
              *dst++ = *src++;            \
       while (src < last)

Definition at line 83 of file mergesort.c.

#define EVAL (   p)
Value:
(u_char **)                                      \
              ((u_char *)0 +                                                 \
           (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))

Definition at line 98 of file mergesort.c.

#define ICOPY_ELT (   src,
  dst,
 
)
Value:
do                                               \
       *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE;  \
       while (i -= ISIZE)

Definition at line 78 of file mergesort.c.

#define ICOPY_LIST (   src,
  dst,
  last 
)
Value:
do                                               \
       *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE;    \
       while(src < last)

Definition at line 74 of file mergesort.c.

#define ISIZE   sizeof(int)

Definition at line 72 of file mergesort.c.

#define NATURAL

Definition at line 50 of file mergesort.c.

#define PSIZE   sizeof(u_char *)

Definition at line 73 of file mergesort.c.

#define reverse (   bot,
  top 
)
Value:
{                           \
       s = top;                                  \
       do {                                      \
              i = size;                          \
              do {                               \
                     tmp = *bot; *bot++ = *s; *s++ = tmp; \
              } while (--i);                            \
              s -= size2;                        \
       } while(bot < s);                         \
}

Definition at line 248 of file mergesort.c.

#define swap (   a,
 
)
Value:
{                                  \
              s = b;                             \
              i = size;                          \
              do {                               \
                     tmp = *a; *a++ = *s; *s++ = tmp; \
              } while (--i);                            \
              a -= size;                         \
       }

Definition at line 240 of file mergesort.c.

#define THRESHOLD   16 /* Best choice for natural merge cut-off. */

Definition at line 51 of file mergesort.c.


Function Documentation

static void insertionsort ( u_char a,
size_t  n,
size_t  size,
int(*)(const void *, const void *TSRMLS_DC) TSRMLS_DC  cmp 
) [static]

Definition at line 336 of file mergesort.c.

{
       u_char *ai, *s, *t, *u, tmp;
       int i;

       for (ai = a+size; --n >= 1; ai += size)
              for (t = ai; t > a; t -= size) {
                     u = t - size;
                     if (cmp(u, t TSRMLS_CC) <= 0)
                            break;
                     swap(u, t);
              }
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_mergesort ( void *  base,
size_t  nmemb,
size_t  size,
int(*)(const void *, const void *TSRMLS_DC) TSRMLS_DC  cmp 
)

Definition at line 105 of file mergesort.c.

{
       register unsigned int i;
       register int sense;
       int big, iflag;
       register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
       u_char *list2, *list1, *p2, *p, *last, **p1;

       if (size < PSIZE / 2) {            /* Pointers must fit into 2 * size. */
              errno = EINVAL;
              return (-1);
       }

       if (nmemb == 0)
              return (0);

       /*
        * XXX
        * Stupid subtraction for the Cray.
        */
       iflag = 0;
       if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
              iflag = 1;

       if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
              return (-1);

       list1 = base;
       setup(list1, list2, nmemb, size, cmp TSRMLS_CC);
       last = list2 + nmemb * size;
       i = big = 0;
       while (*EVAL(list2) != last) {
           l2 = list1;
           p1 = EVAL(list1);
           for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
              p2 = *EVAL(p2);
              f1 = l2;
              f2 = l1 = list1 + (p2 - list2);
              if (p2 != last)
                     p2 = *EVAL(p2);
              l2 = list1 + (p2 - list2);
              while (f1 < l1 && f2 < l2) {
                     if ((*cmp)(f1, f2 TSRMLS_CC) <= 0) {
                            q = f2;
                            b = f1, t = l1;
                            sense = -1;
                     } else {
                            q = f1;
                            b = f2, t = l2;
                            sense = 0;
                     }
                     if (!big) {   /* here i = 0 */
                            while ((b += size) < t && cmp(q, b TSRMLS_CC) >sense)
                                   if (++i == 6) {
                                          big = 1;
                                          goto EXPONENTIAL;
                                   }
                     } else {
EXPONENTIAL:                for (i = size; ; i <<= 1)
                                   if ((p = (b + i)) >= t) {
                                          if ((p = t - size) > b &&
                                              (*cmp)(q, p TSRMLS_CC) <= sense)
                                                 t = p;
                                          else
                                                 b = p;
                                          break;
                                   } else if ((*cmp)(q, p TSRMLS_CC) <= sense) {
                                          t = p;
                                          if (i == size)
                                                 big = 0;
                                          goto FASTCASE;
                                   } else
                                          b = p;
                            while (t > b+size) {
                                   i = (((t - b) / size) >> 1) * size;
                                   if ((*cmp)(q, p = b + i TSRMLS_CC) <= sense)
                                          t = p;
                                   else
                                          b = p;
                            }
                            goto COPY;
FASTCASE:                   while (i > size)
                                   if ((*cmp)(q,
                                          p = b + (i >>= 1) TSRMLS_CC) <= sense)
                                          t = p;
                                   else
                                          b = p;
COPY:                       b = t;
                     }
                     i = size;
                     if (q == f1) {
                            if (iflag) {
                                   ICOPY_LIST(f2, tp2, b);
                                   ICOPY_ELT(f1, tp2, i);
                            } else {
                                   CCOPY_LIST(f2, tp2, b);
                                   CCOPY_ELT(f1, tp2, i);
                            }
                     } else {
                            if (iflag) {
                                   ICOPY_LIST(f1, tp2, b);
                                   ICOPY_ELT(f2, tp2, i);
                            } else {
                                   CCOPY_LIST(f1, tp2, b);
                                   CCOPY_ELT(f2, tp2, i);
                            }
                     }
              }
              if (f2 < l2) {
                     if (iflag)
                            ICOPY_LIST(f2, tp2, l2);
                     else
                            CCOPY_LIST(f2, tp2, l2);
              } else if (f1 < l1) {
                     if (iflag)
                            ICOPY_LIST(f1, tp2, l1);
                     else
                            CCOPY_LIST(f1, tp2, l1);
              }
              *p1 = l2;
           }
           tp2 = list1;     /* swap list1, list2 */
           list1 = list2;
           list2 = tp2;
           last = list2 + nmemb*size;
       }
       if (base == list2) {
              memmove(list2, list1, nmemb*size);
              list2 = list1;
       }
       free(list2);
       return (0);
}

Here is the call graph for this function:

static void setup ( u_char list1,
u_char list2,
size_t  n,
size_t  size,
int(*)(const void *, const void *TSRMLS_DC) TSRMLS_DC  cmp 
) [static]

Definition at line 265 of file mergesort.c.

{
       int i, length, size2, tmp, sense;
       u_char *f1, *f2, *s, *l2, *last, *p2;

       size2 = size*2;
       if (n <= 5) {
              insertionsort(list1, n, size, cmp TSRMLS_CC);
              *EVAL(list2) = (u_char*) list2 + n*size;
              return;
       }
       /*
        * Avoid running pointers out of bounds; limit n to evens
        * for simplicity.
        */
       i = 4 + (n & 1);
       insertionsort(list1 + (n - i) * size, i, size, cmp TSRMLS_CC);
       last = list1 + size * (n - i);
       *EVAL(list2 + (last - list1)) = list2 + n * size;

#ifdef NATURAL
       p2 = list2;
       f1 = list1;
       sense = (cmp(f1, f1 + size TSRMLS_CC) > 0);
       for (; f1 < last; sense = !sense) {
              length = 2;
                                   /* Find pairs with same sense. */
              for (f2 = f1 + size2; f2 < last; f2 += size2) {
                     if ((cmp(f2, f2+ size TSRMLS_CC) > 0) != sense)
                            break;
                     length += 2;
              }
              if (length < THRESHOLD) {          /* Pairwise merge */
                     do {
                            p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
                            if (sense > 0)
                                   swap (f1, f1 + size);
                     } while ((f1 += size2) < f2);
              } else {                           /* Natural merge */
                     l2 = f2;
                     for (f2 = f1 + size2; f2 < l2; f2 += size2) {
                            if ((cmp(f2-size, f2 TSRMLS_CC) > 0) != sense) {
                                   p2 = *EVAL(p2) = f2 - list1 + list2;
                                   if (sense > 0)
                                          reverse(f1, f2-size);
                                   f1 = f2;
                            }
                     }
                     if (sense > 0)
                            reverse (f1, f2-size);
                     f1 = f2;
                     if (f2 < last || cmp(f2 - size, f2 TSRMLS_CC) > 0)
                            p2 = *EVAL(p2) = f2 - list1 + list2;
                     else
                            p2 = *EVAL(p2) = list2 + n*size;
              }
       }
#else         /* pairwise merge only. */
       for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
              p2 = *EVAL(p2) = p2 + size2;
              if (cmp (f1, f1 + size TSRMLS_CC) > 0)
                     swap(f1, f1 + size);
       }
#endif /* NATURAL */
}

Here is the call graph for this function:

Here is the caller graph for this function: