Blame glib/gqsort.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1991, 1992, 1996, 1997,1999,2004 Free Software Foundation, Inc.
Packit ae235b
 * Copyright (C) 2000 Eazel, Inc.
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <limits.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include "galloca.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
Packit ae235b
#include "gqsort.h"
Packit ae235b
Packit ae235b
#include "gtestutils.h"
Packit ae235b
Packit ae235b
/* This file was originally from stdlib/msort.c in gnu libc, just changed
Packit ae235b
   to build inside glib and to not fall back to an unstable quicksort
Packit ae235b
   for large arrays. */
Packit ae235b
Packit ae235b
/* An alternative to qsort, with an identical interface.
Packit ae235b
   This file is part of the GNU C Library.
Packit ae235b
   Copyright (C) 1992,95-97,99,2000,01,02,04,07 Free Software Foundation, Inc.
Packit ae235b
   Written by Mike Haertel, September 1988.
Packit ae235b
Packit ae235b
   The GNU C Library is free software; you can redistribute it and/or
Packit ae235b
   modify it under the terms of the GNU Lesser General Public
Packit ae235b
   License as published by the Free Software Foundation; either
Packit ae235b
   version 2.1 of the License, or (at your option) any later version.
Packit ae235b
Packit ae235b
   The GNU C Library is distributed in the hope that it will be useful,
Packit ae235b
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
   Lesser General Public License for more details.
Packit ae235b
Packit ae235b
   You should have received a copy of the GNU Lesser General Public
Packit ae235b
   License along with the GNU C Library; if not, see
Packit ae235b
   <http://www.gnu.org/licenses/>.  */
Packit ae235b
Packit ae235b
Packit ae235b
struct msort_param
Packit ae235b
{
Packit ae235b
  size_t s;
Packit ae235b
  size_t var;
Packit ae235b
  GCompareDataFunc cmp;
Packit ae235b
  void *arg;
Packit ae235b
  char *t;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void msort_with_tmp (const struct msort_param *p, void *b, size_t n);
Packit ae235b
Packit ae235b
static void
Packit ae235b
msort_with_tmp (const struct msort_param *p, void *b, size_t n)
Packit ae235b
{
Packit ae235b
  char *b1, *b2;
Packit ae235b
  size_t n1, n2;
Packit ae235b
  char *tmp = p->t;
Packit ae235b
  const size_t s = p->s;
Packit ae235b
  GCompareDataFunc cmp = p->cmp;
Packit ae235b
  void *arg = p->arg;
Packit ae235b
Packit ae235b
  if (n <= 1)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  n1 = n / 2;
Packit ae235b
  n2 = n - n1;
Packit ae235b
  b1 = b;
Packit ae235b
  b2 = (char *) b + (n1 * p->s);
Packit ae235b
Packit ae235b
  msort_with_tmp (p, b1, n1);
Packit ae235b
  msort_with_tmp (p, b2, n2);
Packit ae235b
Packit ae235b
  switch (p->var)
Packit ae235b
    {
Packit ae235b
    case 0:
Packit ae235b
      while (n1 > 0 && n2 > 0)
Packit ae235b
	{
Packit ae235b
	  if ((*cmp) (b1, b2, arg) <= 0)
Packit ae235b
	    {
Packit ae235b
	      *(guint32 *) tmp = *(guint32 *) b1;
Packit ae235b
	      b1 += sizeof (guint32);
Packit ae235b
	      --n1;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      *(guint32 *) tmp = *(guint32 *) b2;
Packit ae235b
	      b2 += sizeof (guint32);
Packit ae235b
	      --n2;
Packit ae235b
	    }
Packit ae235b
	  tmp += sizeof (guint32);
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case 1:
Packit ae235b
      while (n1 > 0 && n2 > 0)
Packit ae235b
	{
Packit ae235b
	  if ((*cmp) (b1, b2, arg) <= 0)
Packit ae235b
	    {
Packit ae235b
	      *(guint64 *) tmp = *(guint64 *) b1;
Packit ae235b
	      b1 += sizeof (guint64);
Packit ae235b
	      --n1;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      *(guint64 *) tmp = *(guint64 *) b2;
Packit ae235b
	      b2 += sizeof (guint64);
Packit ae235b
	      --n2;
Packit ae235b
	    }
Packit ae235b
	  tmp += sizeof (guint64);
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case 2:
Packit ae235b
      while (n1 > 0 && n2 > 0)
Packit ae235b
	{
Packit ae235b
	  unsigned long *tmpl = (unsigned long *) tmp;
Packit ae235b
	  unsigned long *bl;
Packit ae235b
Packit ae235b
	  tmp += s;
Packit ae235b
	  if ((*cmp) (b1, b2, arg) <= 0)
Packit ae235b
	    {
Packit ae235b
	      bl = (unsigned long *) b1;
Packit ae235b
	      b1 += s;
Packit ae235b
	      --n1;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      bl = (unsigned long *) b2;
Packit ae235b
	      b2 += s;
Packit ae235b
	      --n2;
Packit ae235b
	    }
Packit ae235b
	  while (tmpl < (unsigned long *) tmp)
Packit ae235b
	    *tmpl++ = *bl++;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case 3:
Packit ae235b
      while (n1 > 0 && n2 > 0)
Packit ae235b
	{
Packit ae235b
	  if ((*cmp) (*(const void **) b1, *(const void **) b2, arg) <= 0)
Packit ae235b
	    {
Packit ae235b
	      *(void **) tmp = *(void **) b1;
Packit ae235b
	      b1 += sizeof (void *);
Packit ae235b
	      --n1;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      *(void **) tmp = *(void **) b2;
Packit ae235b
	      b2 += sizeof (void *);
Packit ae235b
	      --n2;
Packit ae235b
	    }
Packit ae235b
	  tmp += sizeof (void *);
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      while (n1 > 0 && n2 > 0)
Packit ae235b
	{
Packit ae235b
	  if ((*cmp) (b1, b2, arg) <= 0)
Packit ae235b
	    {
Packit ae235b
	      memcpy (tmp, b1, s);
Packit ae235b
	      tmp += s;
Packit ae235b
	      b1 += s;
Packit ae235b
	      --n1;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      memcpy (tmp, b2, s);
Packit ae235b
	      tmp += s;
Packit ae235b
	      b2 += s;
Packit ae235b
	      --n2;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (n1 > 0)
Packit ae235b
    memcpy (tmp, b1, n1 * s);
Packit ae235b
  memcpy (b, p->t, (n - n2) * s);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
msort_r (void *b, size_t n, size_t s, GCompareDataFunc cmp, void *arg)
Packit ae235b
{
Packit ae235b
  size_t size = n * s;
Packit ae235b
  char *tmp = NULL;
Packit ae235b
  struct msort_param p;
Packit ae235b
Packit ae235b
  /* For large object sizes use indirect sorting.  */
Packit ae235b
  if (s > 32)
Packit ae235b
    size = 2 * n * sizeof (void *) + s;
Packit ae235b
Packit ae235b
  if (size < 1024)
Packit ae235b
    /* The temporary array is small, so put it on the stack.  */
Packit ae235b
    p.t = g_alloca (size);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* It's large, so malloc it.  */
Packit ae235b
      tmp = g_malloc (size);
Packit ae235b
      p.t = tmp;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  p.s = s;
Packit ae235b
  p.var = 4;
Packit ae235b
  p.cmp = cmp;
Packit ae235b
  p.arg = arg;
Packit ae235b
Packit ae235b
  if (s > 32)
Packit ae235b
    {
Packit ae235b
      /* Indirect sorting.  */
Packit ae235b
      char *ip = (char *) b;
Packit ae235b
      void **tp = (void **) (p.t + n * sizeof (void *));
Packit ae235b
      void **t = tp;
Packit ae235b
      void *tmp_storage = (void *) (tp + n);
Packit ae235b
      char *kp;
Packit ae235b
      size_t i;
Packit ae235b
Packit ae235b
      while ((void *) t < tmp_storage)
Packit ae235b
	{
Packit ae235b
	  *t++ = ip;
Packit ae235b
	  ip += s;
Packit ae235b
	}
Packit ae235b
      p.s = sizeof (void *);
Packit ae235b
      p.var = 3;
Packit ae235b
      msort_with_tmp (&p, p.t + n * sizeof (void *), n);
Packit ae235b
Packit ae235b
      /* tp[0] .. tp[n - 1] is now sorted, copy around entries of
Packit ae235b
	 the original array.  Knuth vol. 3 (2nd ed.) exercise 5.2-10.  */
Packit ae235b
      for (i = 0, ip = (char *) b; i < n; i++, ip += s)
Packit ae235b
	if ((kp = tp[i]) != ip)
Packit ae235b
	  {
Packit ae235b
	    size_t j = i;
Packit ae235b
	    char *jp = ip;
Packit ae235b
	    memcpy (tmp_storage, ip, s);
Packit ae235b
Packit ae235b
	    do
Packit ae235b
	      {
Packit ae235b
		size_t k = (kp - (char *) b) / s;
Packit ae235b
		tp[j] = jp;
Packit ae235b
		memcpy (jp, kp, s);
Packit ae235b
		j = k;
Packit ae235b
		jp = kp;
Packit ae235b
		kp = tp[k];
Packit ae235b
	      }
Packit ae235b
	    while (kp != ip);
Packit ae235b
Packit ae235b
	    tp[j] = jp;
Packit ae235b
	    memcpy (jp, tmp_storage, s);
Packit ae235b
	  }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if ((s & (sizeof (guint32) - 1)) == 0
Packit ae235b
	  && ((char *) b - (char *) 0) % ALIGNOF_GUINT32 == 0)
Packit ae235b
	{
Packit ae235b
	  if (s == sizeof (guint32))
Packit ae235b
	    p.var = 0;
Packit ae235b
	  else if (s == sizeof (guint64)
Packit ae235b
		   && ((char *) b - (char *) 0) % ALIGNOF_GUINT64 == 0)
Packit ae235b
	    p.var = 1;
Packit ae235b
	  else if ((s & (sizeof (unsigned long) - 1)) == 0
Packit ae235b
		   && ((char *) b - (char *) 0)
Packit ae235b
		      % ALIGNOF_UNSIGNED_LONG == 0)
Packit ae235b
	    p.var = 2;
Packit ae235b
	}
Packit ae235b
      msort_with_tmp (&p, b, n);
Packit ae235b
    }
Packit ae235b
  g_free (tmp);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_qsort_with_data:
Packit ae235b
 * @pbase: (not nullable): start of array to sort
Packit ae235b
 * @total_elems: elements in the array
Packit ae235b
 * @size: size of each element
Packit ae235b
 * @compare_func: function to compare elements
Packit ae235b
 * @user_data: data to pass to @compare_func
Packit ae235b
 *
Packit ae235b
 * This is just like the standard C qsort() function, but
Packit ae235b
 * the comparison routine accepts a user data argument.
Packit ae235b
 *
Packit ae235b
 * This is guaranteed to be a stable sort since version 2.32.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_qsort_with_data (gconstpointer    pbase,
Packit ae235b
                   gint             total_elems,
Packit ae235b
                   gsize            size,
Packit ae235b
                   GCompareDataFunc compare_func,
Packit ae235b
                   gpointer         user_data)
Packit ae235b
{
Packit ae235b
  msort_r ((gpointer)pbase, total_elems, size, compare_func, user_data);
Packit ae235b
}