From f4ba3edfe94b7990a79bff03a73369b584e9a72e Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 4 Jun 2011 12:06:24 -0600 Subject: ENH: Rename _sortmodule.c.src and move it into a new sorting directory. Fix the build to deal with the new name and location. --- numpy/core/SConscript | 2 +- numpy/core/setup.py | 2 +- numpy/core/src/_sortmodule.c.src | 1041 ------------------------------- numpy/core/src/sorting/sortmodule.c.src | 1041 +++++++++++++++++++++++++++++++ 4 files changed, 1043 insertions(+), 1043 deletions(-) delete mode 100644 numpy/core/src/_sortmodule.c.src create mode 100644 numpy/core/src/sorting/sortmodule.c.src (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index 0baea3d0c..055f3c297 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,7 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -sortmodule_src = env.GenerateFromTemplate(pjoin('src', '_sortmodule.c.src')) +sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'sorting','sortmodule.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 87e4e4f90..f53186b42 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -668,7 +668,7 @@ def configuration(parent_package='',top_path=None): config.add_data_dir('include/numpy/fenv') config.add_extension('_sort', - sources=[join('src','_sortmodule.c.src'), + sources=[join('src','sorting','sortmodule.c.src'), generate_config_h, generate_numpyconfig_h, generate_numpy_api, diff --git a/numpy/core/src/_sortmodule.c.src b/numpy/core/src/_sortmodule.c.src deleted file mode 100644 index 527d0c402..000000000 --- a/numpy/core/src/_sortmodule.c.src +++ /dev/null @@ -1,1041 +0,0 @@ -/* -*- c -*- */ - -/* - * The purpose of this module is to add faster sort functions - * that are type-specific. This is done by altering the - * function table for the builtin descriptors. - * - * These sorting functions are copied almost directly from numarray - * with a few modifications (complex comparisons compare the imaginary - * part if the real parts are equal, for example), and the names - * are changed. - * - * The original sorting code is due to Charles R. Harris who wrote - * it for numarray. - */ - -/* - * Quick sort is usually the fastest, but the worst case scenario can - * be slower than the merge and heap sorts. The merge sort requires - * extra memory and so for large arrays may not be useful. - * - * The merge sort is *stable*, meaning that equal components - * are unmoved from their entry versions, so it can be used to - * implement lexigraphic sorting on multiple keys. - * - * The heap sort is included for completeness. - */ - - -#include "Python.h" -#include "numpy/noprefix.h" -#include "numpy/npy_math.h" -#include "numpy/halffloat.h" - -#include "npy_config.h" - -#define NOT_USED NPY_UNUSED(unused) -#define PYA_QS_STACK 100 -#define SMALL_QUICKSORT 15 -#define SMALL_MERGESORT 20 -#define SMALL_STRING 16 - -/* - ***************************************************************************** - ** SWAP MACROS ** - ***************************************************************************** - */ - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, - * CDOUBLE,CLONGDOUBLE, INTP# - * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble, npy_intp# - */ -#define @TYPE@_SWAP(a,b) {@type@ tmp = (b); (b)=(a); (a) = tmp;} - -/**end repeat**/ - -/* - ***************************************************************************** - ** COMPARISON FUNCTIONS ** - ***************************************************************************** - */ - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG# - * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - return a < b; -} -/**end repeat**/ - - -/**begin repeat - * - * #TYPE = FLOAT, DOUBLE, LONGDOUBLE# - * #type = float, double, longdouble# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - return a < b || (b != b && a == a); -} -/**end repeat**/ - -NPY_INLINE static int -HALF_LT(npy_half a, npy_half b) -{ - int ret; - - if (npy_half_isnan(b)) { - ret = !npy_half_isnan(a); - } else { - ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); - } - - return ret; -} - -/* - * For inline functions SUN recommends not using a return in the then part - * of an if statement. It's a SUN compiler thing, so assign the return value - * to a variable instead. - */ - -/**begin repeat - * - * #TYPE = CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = cfloat, cdouble, clongdouble# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} -/**end repeat**/ - - -/* The PyObject functions are stubs for later use */ -NPY_INLINE static int -PyObject_LT(PyObject *pa, PyObject *pb) -{ - return 0; -} - - -NPY_INLINE static void -STRING_COPY(char *s1, char *s2, size_t len) -{ - memcpy(s1, s2, len); -} - - -NPY_INLINE static void -STRING_SWAP(char *s1, char *s2, size_t len) -{ - while(len--) { - const char t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -STRING_LT(char *s1, char *s2, size_t len) -{ - const unsigned char *c1 = (unsigned char *)s1; - const unsigned char *c2 = (unsigned char *)s2; - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (c1[i] != c2[i]) { - ret = c1[i] < c2[i]; - break; - } - } - return ret; -} - - -NPY_INLINE static void -UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - *s1++ = *s2++; - } -} - - -NPY_INLINE static void -UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - const npy_ucs4 t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (s1[i] != s2[i]) { - ret = s1[i] < s2[i]; - break; - } - } - return ret; -} - - -/* - ***************************************************************************** - ** NUMERIC SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong, ushort, float, double, longdouble, - * cfloat, cdouble, clongdouble# - */ - - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl = start; - @type@ *pr = start + num - 1; - @type@ vp; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - vp = *pm; - pi = pl; - pj = pr - 1; - @TYPE@_SWAP(*pm, *pj); - for (;;) { - do ++pi; while (@TYPE@_LT(*pi, vp)); - do --pj; while (@TYPE@_LT(vp, *pj)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(*pi,*pj); - } - pk = pr - 1; - @TYPE@_SWAP(*pi, *pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) -{ - @type@ vp; - npy_intp *pl, *pr; - npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; - - pl = tosort; - pr = tosort + num - 1; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - vp = v[*pm]; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v[*pi],vp)); - do --pj; while (@TYPE@_LT(vp,v[*pj])); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v[*pk])) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) -{ - @type@ tmp, *a; - npy_intp i,j,l; - - /* The array needs to be offset by one for heapsort indexing */ - a = start - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j += 1; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j++; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) -{ - npy_intp *a, i,j,l, tmp; - /* The arrays need to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j += 1; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j++; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) -{ - @type@ vp, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_mergesort0(pl, pm, pw); - @TYPE@_mergesort0(pm, pr, pw); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(*pm,*pj)) { - *pk = *pm++; - } - else { - *pk = *pj++; - } - pk++; - } - while(pj < pi) { - *pk++ = *pj++; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl, *pr, *pw; - - pl = start; - pr = pl + num; - pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_mergesort0(pl, pr, pw); - - PyDataMem_FREE(pw); - return 0; -} - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) -{ - @type@ vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl + 1)>>1); - @TYPE@_amergesort0(pl,pm-1,v,pw); - @TYPE@_amergesort0(pm,pr,v,pw); - for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { - *pi = *pj; - } - for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { - if (@TYPE@_LT(v[*pj],v[*pk])) { - *pm = *pj; - ++pj; - } - else { - *pm = *pk; - ++pk; - } - } - for (; pk < pi; ++pm, ++pk) { - *pm = *pk; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { - *pj = *pk; - } - *pj = vi; - } - } -} - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) -{ - npy_intp *pl, *pr, *pw; - - pl = tosort; pr = pl + num - 1; - pw = PyDimMem_NEW((1+num/2)); - - if (!pw) { - PyErr_NoMemory(); - return -1; - } - - @TYPE@_amergesort0(pl, pr, v, pw); - PyDimMem_FREE(pw); - - return 0; -} - - -/**end repeat**/ - -/* - ***************************************************************************** - ** STRING SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = STRING, UNICODE# - * #type = char, PyArray_UCS4# - */ - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) -{ - @type@ *pi, *pj, *pk, *pm; - - if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { - /* merge sort */ - pm = pl + (((pr - pl)/len) >> 1)*len; - @TYPE@_mergesort0(pl, pm, pw, vp, len); - @TYPE@_mergesort0(pm, pr, pw, vp, len); - @TYPE@_COPY(pw, pl, pm - pl); - pi = pw + (pm - pl); - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(pm, pj, len)) { - @TYPE@_COPY(pk, pm, len); - pm += len; - } - else { - @TYPE@_COPY(pk, pj, len); - pj += len; - } - pk += len; - } - @TYPE@_COPY(pk, pj, pi - pj); - } - else { - /* insertion sort */ - for (pi = pl + len; pi < pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - @type@ *pl, *pr, *pw, *vp; - int err = 0; - - pl = start; - pr = pl + num*len; - pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); - if (!pw) { - PyErr_NoMemory(); - err = -1; - goto fail_0; - } - vp = (@type@ *) PyDataMem_NEW(elsize); - if (!vp) { - PyErr_NoMemory(); - err = -1; - goto fail_1; - } - @TYPE@_mergesort0(pl, pr, pw, vp, len); - - PyDataMem_FREE(vp); -fail_1: - PyDataMem_FREE(pw); -fail_0: - return err; -} - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp = malloc(arr->descr->elsize); - @type@ *pl = start; - @type@ *pr = start + (num - 1)*len; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { - /* quicksort partition */ - pm = pl + (((pr - pl)/len) >> 1)*len; - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - @TYPE@_COPY(vp, pm, len); - pi = pl; - pj = pr - len; - @TYPE@_SWAP(pm, pj, len); - for (;;) { - do pi += len; while (@TYPE@_LT(pi, vp, len)); - do pj -= len; while (@TYPE@_LT(vp, pj, len)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(pi, pj, len); - } - pk = pr - len; - @TYPE@_SWAP(pi, pk, len); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + len; - *sptr++ = pr; - pr = pi - len; - } - else { - *sptr++ = pl; - *sptr++ = pi - len; - pl = pi + len; - } - } - - /* insertion sort */ - for (pi = pl + len; pi <= pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - free(vp); - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *tmp = malloc(arr->descr->elsize); - @type@ *a = start - len; - npy_intp i,j,l; - - for (l = n>>1; l > 0; --l) { - @TYPE@_COPY(tmp, a + l*len, len); - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j += 1; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - for (; n > 1;) { - @TYPE@_COPY(tmp, a + n*len, len); - @TYPE@_COPY(a + n*len, a + len, len); - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j++; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - free(tmp); - return 0; -} - - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - npy_intp *a, i,j,l, tmp; - - /* The array needs to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j += 1; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j++; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp; - npy_intp *pl = tosort; - npy_intp *pr = tosort + num - 1; - npy_intp *stack[PYA_QS_STACK]; - npy_intp **sptr=stack; - npy_intp *pm, *pi, *pj, *pk, vi; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - vp = v + (*pm)*len; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); - do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) -{ - @type@ *vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_amergesort0(pl,pm,v,pw,len); - @TYPE@_amergesort0(pm,pr,v,pw,len); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { - *pk = *pm++; - } else { - *pk = *pj++; - } - pk++; - } - while (pj < pi) { - *pk++ = *pj++; - } - } else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - } -} - - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - npy_intp *pl, *pr, *pw; - - pl = tosort; - pr = pl + num; - pw = PyDimMem_NEW(num/2); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_amergesort0(pl, pr, v, pw, len); - - PyDimMem_FREE(pw); - return 0; -} -/**end repeat**/ - -static void -add_sortfuncs(void) -{ - PyArray_Descr *descr; - - /**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - */ - descr = PyArray_DescrFromType(PyArray_@TYPE@); - descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)@TYPE@_quicksort; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aquicksort; - descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)@TYPE@_heapsort; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aheapsort; - descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)@TYPE@_mergesort; - descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)@TYPE@_amergesort; - /**end repeat**/ - -} - -static struct PyMethodDef methods[] = { - {NULL, NULL, 0, NULL} -}; - - -#if defined(NPY_PY3K) -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_sort", - NULL, - -1, - methods, - NULL, - NULL, - NULL, - NULL -}; -#endif - -/* Initialization function for the module */ -#if defined(NPY_PY3K) -PyObject *PyInit__sort(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - if (!m) { - return NULL; - } - import_array(); - add_sortfuncs(); - return m; -} -#else -PyMODINIT_FUNC -init_sort(void) { - Py_InitModule("_sort", methods); - - import_array(); - add_sortfuncs(); -} -#endif diff --git a/numpy/core/src/sorting/sortmodule.c.src b/numpy/core/src/sorting/sortmodule.c.src new file mode 100644 index 000000000..527d0c402 --- /dev/null +++ b/numpy/core/src/sorting/sortmodule.c.src @@ -0,0 +1,1041 @@ +/* -*- c -*- */ + +/* + * The purpose of this module is to add faster sort functions + * that are type-specific. This is done by altering the + * function table for the builtin descriptors. + * + * These sorting functions are copied almost directly from numarray + * with a few modifications (complex comparisons compare the imaginary + * part if the real parts are equal, for example), and the names + * are changed. + * + * The original sorting code is due to Charles R. Harris who wrote + * it for numarray. + */ + +/* + * Quick sort is usually the fastest, but the worst case scenario can + * be slower than the merge and heap sorts. The merge sort requires + * extra memory and so for large arrays may not be useful. + * + * The merge sort is *stable*, meaning that equal components + * are unmoved from their entry versions, so it can be used to + * implement lexigraphic sorting on multiple keys. + * + * The heap sort is included for completeness. + */ + + +#include "Python.h" +#include "numpy/noprefix.h" +#include "numpy/npy_math.h" +#include "numpy/halffloat.h" + +#include "npy_config.h" + +#define NOT_USED NPY_UNUSED(unused) +#define PYA_QS_STACK 100 +#define SMALL_QUICKSORT 15 +#define SMALL_MERGESORT 20 +#define SMALL_STRING 16 + +/* + ***************************************************************************** + ** SWAP MACROS ** + ***************************************************************************** + */ + +/**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, + * CDOUBLE,CLONGDOUBLE, INTP# + * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble, npy_intp# + */ +#define @TYPE@_SWAP(a,b) {@type@ tmp = (b); (b)=(a); (a) = tmp;} + +/**end repeat**/ + +/* + ***************************************************************************** + ** COMPARISON FUNCTIONS ** + ***************************************************************************** + */ + +/**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG# + * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, + * longlong, ulonglong# + */ +NPY_INLINE static int +@TYPE@_LT(@type@ a, @type@ b) +{ + return a < b; +} +/**end repeat**/ + + +/**begin repeat + * + * #TYPE = FLOAT, DOUBLE, LONGDOUBLE# + * #type = float, double, longdouble# + */ +NPY_INLINE static int +@TYPE@_LT(@type@ a, @type@ b) +{ + return a < b || (b != b && a == a); +} +/**end repeat**/ + +NPY_INLINE static int +HALF_LT(npy_half a, npy_half b) +{ + int ret; + + if (npy_half_isnan(b)) { + ret = !npy_half_isnan(a); + } else { + ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); + } + + return ret; +} + +/* + * For inline functions SUN recommends not using a return in the then part + * of an if statement. It's a SUN compiler thing, so assign the return value + * to a variable instead. + */ + +/**begin repeat + * + * #TYPE = CFLOAT, CDOUBLE, CLONGDOUBLE# + * #type = cfloat, cdouble, clongdouble# + */ +NPY_INLINE static int +@TYPE@_LT(@type@ a, @type@ b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} +/**end repeat**/ + + +/* The PyObject functions are stubs for later use */ +NPY_INLINE static int +PyObject_LT(PyObject *pa, PyObject *pb) +{ + return 0; +} + + +NPY_INLINE static void +STRING_COPY(char *s1, char *s2, size_t len) +{ + memcpy(s1, s2, len); +} + + +NPY_INLINE static void +STRING_SWAP(char *s1, char *s2, size_t len) +{ + while(len--) { + const char t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +STRING_LT(char *s1, char *s2, size_t len) +{ + const unsigned char *c1 = (unsigned char *)s1; + const unsigned char *c2 = (unsigned char *)s2; + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (c1[i] != c2[i]) { + ret = c1[i] < c2[i]; + break; + } + } + return ret; +} + + +NPY_INLINE static void +UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + *s1++ = *s2++; + } +} + + +NPY_INLINE static void +UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + const npy_ucs4 t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (s1[i] != s2[i]) { + ret = s1[i] < s2[i]; + break; + } + } + return ret; +} + + +/* + ***************************************************************************** + ** NUMERIC SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE# + * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, + * longlong, ulonglong, ushort, float, double, longdouble, + * cfloat, cdouble, clongdouble# + */ + + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl = start; + @type@ *pr = start + num - 1; + @type@ vp; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + vp = *pm; + pi = pl; + pj = pr - 1; + @TYPE@_SWAP(*pm, *pj); + for (;;) { + do ++pi; while (@TYPE@_LT(*pi, vp)); + do --pj; while (@TYPE@_LT(vp, *pj)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(*pi,*pj); + } + pk = pr - 1; + @TYPE@_SWAP(*pi, *pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) +{ + @type@ vp; + npy_intp *pl, *pr; + npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; + + pl = tosort; + pr = tosort + num - 1; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + vp = v[*pm]; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v[*pi],vp)); + do --pj; while (@TYPE@_LT(vp,v[*pj])); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v[*pk])) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) +{ + @type@ tmp, *a; + npy_intp i,j,l; + + /* The array needs to be offset by one for heapsort indexing */ + a = start - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j += 1; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j++; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) +{ + npy_intp *a, i,j,l, tmp; + /* The arrays need to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j += 1; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j++; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) +{ + @type@ vp, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_mergesort0(pl, pm, pw); + @TYPE@_mergesort0(pm, pr, pw); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(*pm,*pj)) { + *pk = *pm++; + } + else { + *pk = *pj++; + } + pk++; + } + while(pj < pi) { + *pk++ = *pj++; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl, *pr, *pw; + + pl = start; + pr = pl + num; + pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_mergesort0(pl, pr, pw); + + PyDataMem_FREE(pw); + return 0; +} + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) +{ + @type@ vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl + 1)>>1); + @TYPE@_amergesort0(pl,pm-1,v,pw); + @TYPE@_amergesort0(pm,pr,v,pw); + for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { + *pi = *pj; + } + for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { + if (@TYPE@_LT(v[*pj],v[*pk])) { + *pm = *pj; + ++pj; + } + else { + *pm = *pk; + ++pk; + } + } + for (; pk < pi; ++pm, ++pk) { + *pm = *pk; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { + *pj = *pk; + } + *pj = vi; + } + } +} + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) +{ + npy_intp *pl, *pr, *pw; + + pl = tosort; pr = pl + num - 1; + pw = PyDimMem_NEW((1+num/2)); + + if (!pw) { + PyErr_NoMemory(); + return -1; + } + + @TYPE@_amergesort0(pl, pr, v, pw); + PyDimMem_FREE(pw); + + return 0; +} + + +/**end repeat**/ + +/* + ***************************************************************************** + ** STRING SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = STRING, UNICODE# + * #type = char, PyArray_UCS4# + */ + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) +{ + @type@ *pi, *pj, *pk, *pm; + + if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { + /* merge sort */ + pm = pl + (((pr - pl)/len) >> 1)*len; + @TYPE@_mergesort0(pl, pm, pw, vp, len); + @TYPE@_mergesort0(pm, pr, pw, vp, len); + @TYPE@_COPY(pw, pl, pm - pl); + pi = pw + (pm - pl); + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(pm, pj, len)) { + @TYPE@_COPY(pk, pm, len); + pm += len; + } + else { + @TYPE@_COPY(pk, pj, len); + pj += len; + } + pk += len; + } + @TYPE@_COPY(pk, pj, pi - pj); + } + else { + /* insertion sort */ + for (pi = pl + len; pi < pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + @type@ *pl, *pr, *pw, *vp; + int err = 0; + + pl = start; + pr = pl + num*len; + pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); + if (!pw) { + PyErr_NoMemory(); + err = -1; + goto fail_0; + } + vp = (@type@ *) PyDataMem_NEW(elsize); + if (!vp) { + PyErr_NoMemory(); + err = -1; + goto fail_1; + } + @TYPE@_mergesort0(pl, pr, pw, vp, len); + + PyDataMem_FREE(vp); +fail_1: + PyDataMem_FREE(pw); +fail_0: + return err; +} + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp = malloc(arr->descr->elsize); + @type@ *pl = start; + @type@ *pr = start + (num - 1)*len; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { + /* quicksort partition */ + pm = pl + (((pr - pl)/len) >> 1)*len; + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + @TYPE@_COPY(vp, pm, len); + pi = pl; + pj = pr - len; + @TYPE@_SWAP(pm, pj, len); + for (;;) { + do pi += len; while (@TYPE@_LT(pi, vp, len)); + do pj -= len; while (@TYPE@_LT(vp, pj, len)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(pi, pj, len); + } + pk = pr - len; + @TYPE@_SWAP(pi, pk, len); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + len; + *sptr++ = pr; + pr = pi - len; + } + else { + *sptr++ = pl; + *sptr++ = pi - len; + pl = pi + len; + } + } + + /* insertion sort */ + for (pi = pl + len; pi <= pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + free(vp); + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *tmp = malloc(arr->descr->elsize); + @type@ *a = start - len; + npy_intp i,j,l; + + for (l = n>>1; l > 0; --l) { + @TYPE@_COPY(tmp, a + l*len, len); + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j += 1; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + for (; n > 1;) { + @TYPE@_COPY(tmp, a + n*len, len); + @TYPE@_COPY(a + n*len, a + len, len); + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j++; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + free(tmp); + return 0; +} + + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + npy_intp *a, i,j,l, tmp; + + /* The array needs to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j += 1; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j++; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp; + npy_intp *pl = tosort; + npy_intp *pr = tosort + num - 1; + npy_intp *stack[PYA_QS_STACK]; + npy_intp **sptr=stack; + npy_intp *pm, *pi, *pj, *pk, vi; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + vp = v + (*pm)*len; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); + do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) +{ + @type@ *vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_amergesort0(pl,pm,v,pw,len); + @TYPE@_amergesort0(pm,pr,v,pw,len); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { + *pk = *pm++; + } else { + *pk = *pj++; + } + pk++; + } + while (pj < pi) { + *pk++ = *pj++; + } + } else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + } +} + + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + npy_intp *pl, *pr, *pw; + + pl = tosort; + pr = pl + num; + pw = PyDimMem_NEW(num/2); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_amergesort0(pl, pr, v, pw, len); + + PyDimMem_FREE(pw); + return 0; +} +/**end repeat**/ + +static void +add_sortfuncs(void) +{ + PyArray_Descr *descr; + + /**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# + */ + descr = PyArray_DescrFromType(PyArray_@TYPE@); + descr->f->sort[PyArray_QUICKSORT] = + (PyArray_SortFunc *)@TYPE@_quicksort; + descr->f->argsort[PyArray_QUICKSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aquicksort; + descr->f->sort[PyArray_HEAPSORT] = + (PyArray_SortFunc *)@TYPE@_heapsort; + descr->f->argsort[PyArray_HEAPSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aheapsort; + descr->f->sort[PyArray_MERGESORT] = + (PyArray_SortFunc *)@TYPE@_mergesort; + descr->f->argsort[PyArray_MERGESORT] = + (PyArray_ArgSortFunc *)@TYPE@_amergesort; + /**end repeat**/ + +} + +static struct PyMethodDef methods[] = { + {NULL, NULL, 0, NULL} +}; + + +#if defined(NPY_PY3K) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_sort", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +/* Initialization function for the module */ +#if defined(NPY_PY3K) +PyObject *PyInit__sort(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + import_array(); + add_sortfuncs(); + return m; +} +#else +PyMODINIT_FUNC +init_sort(void) { + Py_InitModule("_sort", methods); + + import_array(); + add_sortfuncs(); +} +#endif -- cgit v1.2.1 From 60054e398e52c690a3769dea9a25883bf4cae9f4 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 5 Jun 2011 23:03:17 -0600 Subject: ENH: Move comparison functions into an include file. Make include file for the future sorting library. Use prefixed numpy types. --- numpy/core/include/numpy/npy_sort.h | 128 +++++++++++++ numpy/core/setup.py | 1 + numpy/core/src/sorting/npy_sort_common.h | 306 +++++++++++++++++++++++++++++++ numpy/core/src/sorting/sortmodule.c.src | 200 +------------------- 4 files changed, 444 insertions(+), 191 deletions(-) create mode 100644 numpy/core/include/numpy/npy_sort.h create mode 100644 numpy/core/src/sorting/npy_sort_common.h (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h new file mode 100644 index 000000000..c31155705 --- /dev/null +++ b/numpy/core/include/numpy/npy_sort.h @@ -0,0 +1,128 @@ +#ifndef __NPY_SORT_H__ +#define __NPY_SORT_H__ + +/* Python include is for future object sorts */ +#include +#include + + +int BOOL_aquicksort(npy_bool *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int BOOL_aheapsort(npy_bool *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void BOOL_amergesort0(npy_intp *pl, npy_intp *pr, npy_bool *v, npy_intp *pw); +int BOOL_amergesort(npy_bool *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int BYTE_aquicksort(npy_byte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int BYTE_aheapsort(npy_byte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void BYTE_amergesort0(npy_intp *pl, npy_intp *pr, npy_byte *v, npy_intp *pw); +int BYTE_amergesort(npy_byte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int UBYTE_aquicksort(npy_ubyte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int UBYTE_aheapsort(npy_ubyte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void UBYTE_amergesort0(npy_intp *pl, npy_intp *pr, npy_ubyte *v, npy_intp *pw); +int UBYTE_amergesort(npy_ubyte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int SHORT_aquicksort(npy_short *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int SHORT_aheapsort(npy_short *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void SHORT_amergesort0(npy_intp *pl, npy_intp *pr, npy_short *v, npy_intp *pw); +int SHORT_amergesort(npy_short *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int USHORT_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int USHORT_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void USHORT_amergesort0(npy_intp *pl, npy_intp *pr, npy_ushort *v, npy_intp *pw); +int USHORT_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int INT_aquicksort(npy_int *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int INT_aheapsort(npy_int *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void INT_amergesort0(npy_intp *pl, npy_intp *pr, npy_int *v, npy_intp *pw); +int INT_amergesort(npy_int *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int UINT_aquicksort(npy_uint *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int UINT_aheapsort(npy_uint *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void UINT_amergesort0(npy_intp *pl, npy_intp *pr, npy_uint *v, npy_intp *pw); +int UINT_amergesort(npy_uint *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int LONG_aquicksort(npy_long *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int LONG_aheapsort(npy_long *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void LONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_long *v, npy_intp *pw); +int LONG_amergesort(npy_long *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int ULONG_aquicksort(npy_ulong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int ULONG_aheapsort(npy_ulong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void ULONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_ulong *v, npy_intp *pw); +int ULONG_amergesort(npy_ulong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int LONGLONG_aquicksort(npy_longlong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int LONGLONG_aheapsort(npy_longlong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void LONGLONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_longlong *v, npy_intp *pw); +int LONGLONG_amergesort(npy_longlong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int ULONGLONG_aquicksort(npy_ulonglong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int ULONGLONG_aheapsort(npy_ulonglong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void ULONGLONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_ulonglong *v, npy_intp *pw); +int ULONGLONG_amergesort(npy_ulonglong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int HALF_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int HALF_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void HALF_amergesort0(npy_intp *pl, npy_intp *pr, npy_ushort *v, npy_intp *pw); +int HALF_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int FLOAT_aquicksort(npy_float *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int FLOAT_aheapsort(npy_float *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void FLOAT_amergesort0(npy_intp *pl, npy_intp *pr, npy_float *v, npy_intp *pw); +int FLOAT_amergesort(npy_float *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int DOUBLE_aquicksort(npy_double *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int DOUBLE_aheapsort(npy_double *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void DOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_double *v, npy_intp *pw); +int DOUBLE_amergesort(npy_double *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int LONGDOUBLE_aquicksort(npy_longdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int LONGDOUBLE_aheapsort(npy_longdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void LONGDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_longdouble *v, npy_intp *pw); +int LONGDOUBLE_amergesort(npy_longdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int CFLOAT_aquicksort(npy_cfloat *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int CFLOAT_aheapsort(npy_cfloat *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void CFLOAT_amergesort0(npy_intp *pl, npy_intp *pr, npy_cfloat *v, npy_intp *pw); +int CFLOAT_amergesort(npy_cfloat *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int CDOUBLE_aquicksort(npy_cdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int CDOUBLE_aheapsort(npy_cdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void CDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_cdouble *v, npy_intp *pw); +int CDOUBLE_amergesort(npy_cdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int CLONGDOUBLE_aquicksort(npy_clongdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); +int CLONGDOUBLE_aheapsort(npy_clongdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); +void CLONGDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_clongdouble *v, npy_intp *pw); +int CLONGDOUBLE_amergesort(npy_clongdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); + + +int STRING_aheapsort(char *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); +int STRING_aquicksort(char *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); +void STRING_amergesort0(npy_intp *pl, npy_intp *pr, char *v, npy_intp *pw, int len); +int STRING_amergesort(char *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); + + +int UNICODE_aheapsort(npy_ucs4 *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); +int UNICODE_aquicksort(npy_ucs4 *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); +void UNICODE_amergesort0(npy_intp *pl, npy_intp *pr, npy_ucs4 *v, npy_intp *pw, int len); +int UNICODE_amergesort(npy_ucs4 *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); + +#endif diff --git a/numpy/core/setup.py b/numpy/core/setup.py index f53186b42..ab4f913c0 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -653,6 +653,7 @@ def configuration(parent_package='',top_path=None): config.add_include_dirs(join('src', 'npymath')) config.add_include_dirs(join('src', 'multiarray')) config.add_include_dirs(join('src', 'umath')) + config.add_include_dirs(join('src', 'sorting')) config.numpy_include_dirs.extend(config.paths('include')) diff --git a/numpy/core/src/sorting/npy_sort_common.h b/numpy/core/src/sorting/npy_sort_common.h new file mode 100644 index 000000000..d4b3a0b3e --- /dev/null +++ b/numpy/core/src/sorting/npy_sort_common.h @@ -0,0 +1,306 @@ +#ifndef __NPY_SORT_COMMON_H__ +#define __NPY_SORT_COMMON_H__ + + +#include "Python.h" +#include "numpy/npy_common.h" +#include "numpy/npy_math.h" +#include "numpy/halffloat.h" +#include "npy_config.h" + + +/* + ***************************************************************************** + ** SWAP MACROS ** + ***************************************************************************** + */ + +#define BOOL_SWAP(a,b) {npy_bool tmp = (b); (b)=(a); (a) = tmp;} +#define BYTE_SWAP(a,b) {npy_byte tmp = (b); (b)=(a); (a) = tmp;} +#define UBYTE_SWAP(a,b) {npy_ubyte tmp = (b); (b)=(a); (a) = tmp;} +#define SHORT_SWAP(a,b) {npy_short tmp = (b); (b)=(a); (a) = tmp;} +#define USHORT_SWAP(a,b) {npy_ushort tmp = (b); (b)=(a); (a) = tmp;} +#define INT_SWAP(a,b) {npy_int tmp = (b); (b)=(a); (a) = tmp;} +#define UINT_SWAP(a,b) {npy_uint tmp = (b); (b)=(a); (a) = tmp;} +#define LONG_SWAP(a,b) {npy_long tmp = (b); (b)=(a); (a) = tmp;} +#define ULONG_SWAP(a,b) {npy_ulong tmp = (b); (b)=(a); (a) = tmp;} +#define LONGLONG_SWAP(a,b) {npy_longlong tmp = (b); (b)=(a); (a) = tmp;} +#define ULONGLONG_SWAP(a,b) {npy_ulonglong tmp = (b); (b)=(a); (a) = tmp;} +#define HALF_SWAP(a,b) {npy_half tmp = (b); (b)=(a); (a) = tmp;} +#define FLOAT_SWAP(a,b) {npy_float tmp = (b); (b)=(a); (a) = tmp;} +#define DOUBLE_SWAP(a,b) {npy_double tmp = (b); (b)=(a); (a) = tmp;} +#define LONGDOUBLE_SWAP(a,b) {npy_longdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} +#define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} +#define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} + +/* + ***************************************************************************** + ** COMPARISON FUNCTIONS ** + ***************************************************************************** + */ + +NPY_INLINE static int +BOOL_LT(npy_bool a, npy_bool b) +{ + return a < b; +} + + +NPY_INLINE static int +BYTE_LT(npy_byte a, npy_byte b) +{ + return a < b; +} + + +NPY_INLINE static int +UBYTE_LT(npy_ubyte a, npy_ubyte b) +{ + return a < b; +} + + +NPY_INLINE static int +SHORT_LT(npy_short a, npy_short b) +{ + return a < b; +} + + +NPY_INLINE static int +USHORT_LT(npy_ushort a, npy_ushort b) +{ + return a < b; +} + + +NPY_INLINE static int +INT_LT(npy_int a, npy_int b) +{ + return a < b; +} + + +NPY_INLINE static int +UINT_LT(npy_uint a, npy_uint b) +{ + return a < b; +} + + +NPY_INLINE static int +LONG_LT(npy_long a, npy_long b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONG_LT(npy_ulong a, npy_ulong b) +{ + return a < b; +} + + +NPY_INLINE static int +LONGLONG_LT(npy_longlong a, npy_longlong b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONGLONG_LT(npy_ulonglong a, npy_ulonglong b) +{ + return a < b; +} + + +NPY_INLINE static int +FLOAT_LT(npy_float a, npy_float b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +DOUBLE_LT(npy_double a, npy_double b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +HALF_LT(npy_half a, npy_half b) +{ + int ret; + + if (npy_half_isnan(b)) { + ret = !npy_half_isnan(a); + } else { + ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); + } + + return ret; +} + +/* + * For inline functions SUN recommends not using a return in the then part + * of an if statement. It's a SUN compiler thing, so assign the return value + * to a variable instead. + */ +NPY_INLINE static int +CFLOAT_LT(npy_cfloat a, npy_cfloat b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CDOUBLE_LT(npy_cdouble a, npy_cdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CLONGDOUBLE_LT(npy_clongdouble a, npy_clongdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +/* The PyObject functions are stubs for later use */ +NPY_INLINE static int +PyObject_LT(PyObject *pa, PyObject *pb) +{ + return 0; +} + + +NPY_INLINE static void +STRING_COPY(char *s1, char *s2, size_t len) +{ + memcpy(s1, s2, len); +} + + +NPY_INLINE static void +STRING_SWAP(char *s1, char *s2, size_t len) +{ + while(len--) { + const char t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +STRING_LT(char *s1, char *s2, size_t len) +{ + const unsigned char *c1 = (unsigned char *)s1; + const unsigned char *c2 = (unsigned char *)s2; + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (c1[i] != c2[i]) { + ret = c1[i] < c2[i]; + break; + } + } + return ret; +} + + +NPY_INLINE static void +UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + *s1++ = *s2++; + } +} + + +NPY_INLINE static void +UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + const npy_ucs4 t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (s1[i] != s2[i]) { + ret = s1[i] < s2[i]; + break; + } + } + return ret; +} + +#endif diff --git a/numpy/core/src/sorting/sortmodule.c.src b/numpy/core/src/sorting/sortmodule.c.src index 527d0c402..f242b3393 100644 --- a/numpy/core/src/sorting/sortmodule.c.src +++ b/numpy/core/src/sorting/sortmodule.c.src @@ -26,13 +26,7 @@ * The heap sort is included for completeness. */ - -#include "Python.h" -#include "numpy/noprefix.h" -#include "numpy/npy_math.h" -#include "numpy/halffloat.h" - -#include "npy_config.h" +#include "npy_sort_common.h" #define NOT_USED NPY_UNUSED(unused) #define PYA_QS_STACK 100 @@ -40,186 +34,6 @@ #define SMALL_MERGESORT 20 #define SMALL_STRING 16 -/* - ***************************************************************************** - ** SWAP MACROS ** - ***************************************************************************** - */ - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, - * CDOUBLE,CLONGDOUBLE, INTP# - * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble, npy_intp# - */ -#define @TYPE@_SWAP(a,b) {@type@ tmp = (b); (b)=(a); (a) = tmp;} - -/**end repeat**/ - -/* - ***************************************************************************** - ** COMPARISON FUNCTIONS ** - ***************************************************************************** - */ - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG# - * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - return a < b; -} -/**end repeat**/ - - -/**begin repeat - * - * #TYPE = FLOAT, DOUBLE, LONGDOUBLE# - * #type = float, double, longdouble# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - return a < b || (b != b && a == a); -} -/**end repeat**/ - -NPY_INLINE static int -HALF_LT(npy_half a, npy_half b) -{ - int ret; - - if (npy_half_isnan(b)) { - ret = !npy_half_isnan(a); - } else { - ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); - } - - return ret; -} - -/* - * For inline functions SUN recommends not using a return in the then part - * of an if statement. It's a SUN compiler thing, so assign the return value - * to a variable instead. - */ - -/**begin repeat - * - * #TYPE = CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = cfloat, cdouble, clongdouble# - */ -NPY_INLINE static int -@TYPE@_LT(@type@ a, @type@ b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} -/**end repeat**/ - - -/* The PyObject functions are stubs for later use */ -NPY_INLINE static int -PyObject_LT(PyObject *pa, PyObject *pb) -{ - return 0; -} - - -NPY_INLINE static void -STRING_COPY(char *s1, char *s2, size_t len) -{ - memcpy(s1, s2, len); -} - - -NPY_INLINE static void -STRING_SWAP(char *s1, char *s2, size_t len) -{ - while(len--) { - const char t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -STRING_LT(char *s1, char *s2, size_t len) -{ - const unsigned char *c1 = (unsigned char *)s1; - const unsigned char *c2 = (unsigned char *)s2; - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (c1[i] != c2[i]) { - ret = c1[i] < c2[i]; - break; - } - } - return ret; -} - - -NPY_INLINE static void -UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - *s1++ = *s2++; - } -} - - -NPY_INLINE static void -UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - const npy_ucs4 t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (s1[i] != s2[i]) { - ret = s1[i] < s2[i]; - break; - } - } - return ret; -} - /* ***************************************************************************** @@ -233,9 +47,10 @@ UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong, ushort, float, double, longdouble, - * cfloat, cdouble, clongdouble# + * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble# */ @@ -598,7 +413,7 @@ static int /**begin repeat * * #TYPE = STRING, UNICODE# - * #type = char, PyArray_UCS4# + * #type = char, npy_ucs4# */ static void @@ -971,6 +786,9 @@ static int } /**end repeat**/ +/* just for the module bit */ +#include "numpy/noprefix.h" + static void add_sortfuncs(void) { -- cgit v1.2.1 From aa6f11da12873ae8ce1cfde08aa7484ecafdf761 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 7 Jun 2011 13:48:02 -0600 Subject: ENH: Rename the sorting directory to npysort. --- numpy/core/SConscript | 2 +- numpy/core/setup.py | 4 +- numpy/core/src/npysort/npy_sort_common.h | 306 +++++++++++ numpy/core/src/npysort/sortmodule.c.src | 859 +++++++++++++++++++++++++++++++ numpy/core/src/sorting/npy_sort_common.h | 306 ----------- numpy/core/src/sorting/sortmodule.c.src | 859 ------------------------------- 6 files changed, 1168 insertions(+), 1168 deletions(-) create mode 100644 numpy/core/src/npysort/npy_sort_common.h create mode 100644 numpy/core/src/npysort/sortmodule.c.src delete mode 100644 numpy/core/src/sorting/npy_sort_common.h delete mode 100644 numpy/core/src/sorting/sortmodule.c.src (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index 055f3c297..b495af824 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,7 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'sorting','sortmodule.c.src')) +sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', diff --git a/numpy/core/setup.py b/numpy/core/setup.py index ab4f913c0..5883b09ee 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -653,7 +653,7 @@ def configuration(parent_package='',top_path=None): config.add_include_dirs(join('src', 'npymath')) config.add_include_dirs(join('src', 'multiarray')) config.add_include_dirs(join('src', 'umath')) - config.add_include_dirs(join('src', 'sorting')) + config.add_include_dirs(join('src', 'npysort')) config.numpy_include_dirs.extend(config.paths('include')) @@ -669,7 +669,7 @@ def configuration(parent_package='',top_path=None): config.add_data_dir('include/numpy/fenv') config.add_extension('_sort', - sources=[join('src','sorting','sortmodule.c.src'), + sources=[join('src','npysort','sortmodule.c.src'), generate_config_h, generate_numpyconfig_h, generate_numpy_api, diff --git a/numpy/core/src/npysort/npy_sort_common.h b/numpy/core/src/npysort/npy_sort_common.h new file mode 100644 index 000000000..d4b3a0b3e --- /dev/null +++ b/numpy/core/src/npysort/npy_sort_common.h @@ -0,0 +1,306 @@ +#ifndef __NPY_SORT_COMMON_H__ +#define __NPY_SORT_COMMON_H__ + + +#include "Python.h" +#include "numpy/npy_common.h" +#include "numpy/npy_math.h" +#include "numpy/halffloat.h" +#include "npy_config.h" + + +/* + ***************************************************************************** + ** SWAP MACROS ** + ***************************************************************************** + */ + +#define BOOL_SWAP(a,b) {npy_bool tmp = (b); (b)=(a); (a) = tmp;} +#define BYTE_SWAP(a,b) {npy_byte tmp = (b); (b)=(a); (a) = tmp;} +#define UBYTE_SWAP(a,b) {npy_ubyte tmp = (b); (b)=(a); (a) = tmp;} +#define SHORT_SWAP(a,b) {npy_short tmp = (b); (b)=(a); (a) = tmp;} +#define USHORT_SWAP(a,b) {npy_ushort tmp = (b); (b)=(a); (a) = tmp;} +#define INT_SWAP(a,b) {npy_int tmp = (b); (b)=(a); (a) = tmp;} +#define UINT_SWAP(a,b) {npy_uint tmp = (b); (b)=(a); (a) = tmp;} +#define LONG_SWAP(a,b) {npy_long tmp = (b); (b)=(a); (a) = tmp;} +#define ULONG_SWAP(a,b) {npy_ulong tmp = (b); (b)=(a); (a) = tmp;} +#define LONGLONG_SWAP(a,b) {npy_longlong tmp = (b); (b)=(a); (a) = tmp;} +#define ULONGLONG_SWAP(a,b) {npy_ulonglong tmp = (b); (b)=(a); (a) = tmp;} +#define HALF_SWAP(a,b) {npy_half tmp = (b); (b)=(a); (a) = tmp;} +#define FLOAT_SWAP(a,b) {npy_float tmp = (b); (b)=(a); (a) = tmp;} +#define DOUBLE_SWAP(a,b) {npy_double tmp = (b); (b)=(a); (a) = tmp;} +#define LONGDOUBLE_SWAP(a,b) {npy_longdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} +#define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} +#define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} + +/* + ***************************************************************************** + ** COMPARISON FUNCTIONS ** + ***************************************************************************** + */ + +NPY_INLINE static int +BOOL_LT(npy_bool a, npy_bool b) +{ + return a < b; +} + + +NPY_INLINE static int +BYTE_LT(npy_byte a, npy_byte b) +{ + return a < b; +} + + +NPY_INLINE static int +UBYTE_LT(npy_ubyte a, npy_ubyte b) +{ + return a < b; +} + + +NPY_INLINE static int +SHORT_LT(npy_short a, npy_short b) +{ + return a < b; +} + + +NPY_INLINE static int +USHORT_LT(npy_ushort a, npy_ushort b) +{ + return a < b; +} + + +NPY_INLINE static int +INT_LT(npy_int a, npy_int b) +{ + return a < b; +} + + +NPY_INLINE static int +UINT_LT(npy_uint a, npy_uint b) +{ + return a < b; +} + + +NPY_INLINE static int +LONG_LT(npy_long a, npy_long b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONG_LT(npy_ulong a, npy_ulong b) +{ + return a < b; +} + + +NPY_INLINE static int +LONGLONG_LT(npy_longlong a, npy_longlong b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONGLONG_LT(npy_ulonglong a, npy_ulonglong b) +{ + return a < b; +} + + +NPY_INLINE static int +FLOAT_LT(npy_float a, npy_float b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +DOUBLE_LT(npy_double a, npy_double b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +HALF_LT(npy_half a, npy_half b) +{ + int ret; + + if (npy_half_isnan(b)) { + ret = !npy_half_isnan(a); + } else { + ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); + } + + return ret; +} + +/* + * For inline functions SUN recommends not using a return in the then part + * of an if statement. It's a SUN compiler thing, so assign the return value + * to a variable instead. + */ +NPY_INLINE static int +CFLOAT_LT(npy_cfloat a, npy_cfloat b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CDOUBLE_LT(npy_cdouble a, npy_cdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CLONGDOUBLE_LT(npy_clongdouble a, npy_clongdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +/* The PyObject functions are stubs for later use */ +NPY_INLINE static int +PyObject_LT(PyObject *pa, PyObject *pb) +{ + return 0; +} + + +NPY_INLINE static void +STRING_COPY(char *s1, char *s2, size_t len) +{ + memcpy(s1, s2, len); +} + + +NPY_INLINE static void +STRING_SWAP(char *s1, char *s2, size_t len) +{ + while(len--) { + const char t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +STRING_LT(char *s1, char *s2, size_t len) +{ + const unsigned char *c1 = (unsigned char *)s1; + const unsigned char *c2 = (unsigned char *)s2; + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (c1[i] != c2[i]) { + ret = c1[i] < c2[i]; + break; + } + } + return ret; +} + + +NPY_INLINE static void +UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + *s1++ = *s2++; + } +} + + +NPY_INLINE static void +UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + const npy_ucs4 t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (s1[i] != s2[i]) { + ret = s1[i] < s2[i]; + break; + } + } + return ret; +} + +#endif diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src new file mode 100644 index 000000000..f242b3393 --- /dev/null +++ b/numpy/core/src/npysort/sortmodule.c.src @@ -0,0 +1,859 @@ +/* -*- c -*- */ + +/* + * The purpose of this module is to add faster sort functions + * that are type-specific. This is done by altering the + * function table for the builtin descriptors. + * + * These sorting functions are copied almost directly from numarray + * with a few modifications (complex comparisons compare the imaginary + * part if the real parts are equal, for example), and the names + * are changed. + * + * The original sorting code is due to Charles R. Harris who wrote + * it for numarray. + */ + +/* + * Quick sort is usually the fastest, but the worst case scenario can + * be slower than the merge and heap sorts. The merge sort requires + * extra memory and so for large arrays may not be useful. + * + * The merge sort is *stable*, meaning that equal components + * are unmoved from their entry versions, so it can be used to + * implement lexigraphic sorting on multiple keys. + * + * The heap sort is included for completeness. + */ + +#include "npy_sort_common.h" + +#define NOT_USED NPY_UNUSED(unused) +#define PYA_QS_STACK 100 +#define SMALL_QUICKSORT 15 +#define SMALL_MERGESORT 20 +#define SMALL_STRING 16 + + +/* + ***************************************************************************** + ** NUMERIC SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE# + * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble# + */ + + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl = start; + @type@ *pr = start + num - 1; + @type@ vp; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + vp = *pm; + pi = pl; + pj = pr - 1; + @TYPE@_SWAP(*pm, *pj); + for (;;) { + do ++pi; while (@TYPE@_LT(*pi, vp)); + do --pj; while (@TYPE@_LT(vp, *pj)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(*pi,*pj); + } + pk = pr - 1; + @TYPE@_SWAP(*pi, *pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) +{ + @type@ vp; + npy_intp *pl, *pr; + npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; + + pl = tosort; + pr = tosort + num - 1; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + vp = v[*pm]; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v[*pi],vp)); + do --pj; while (@TYPE@_LT(vp,v[*pj])); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v[*pk])) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) +{ + @type@ tmp, *a; + npy_intp i,j,l; + + /* The array needs to be offset by one for heapsort indexing */ + a = start - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j += 1; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j++; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) +{ + npy_intp *a, i,j,l, tmp; + /* The arrays need to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j += 1; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j++; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) +{ + @type@ vp, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_mergesort0(pl, pm, pw); + @TYPE@_mergesort0(pm, pr, pw); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(*pm,*pj)) { + *pk = *pm++; + } + else { + *pk = *pj++; + } + pk++; + } + while(pj < pi) { + *pk++ = *pj++; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl, *pr, *pw; + + pl = start; + pr = pl + num; + pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_mergesort0(pl, pr, pw); + + PyDataMem_FREE(pw); + return 0; +} + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) +{ + @type@ vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl + 1)>>1); + @TYPE@_amergesort0(pl,pm-1,v,pw); + @TYPE@_amergesort0(pm,pr,v,pw); + for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { + *pi = *pj; + } + for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { + if (@TYPE@_LT(v[*pj],v[*pk])) { + *pm = *pj; + ++pj; + } + else { + *pm = *pk; + ++pk; + } + } + for (; pk < pi; ++pm, ++pk) { + *pm = *pk; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { + *pj = *pk; + } + *pj = vi; + } + } +} + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) +{ + npy_intp *pl, *pr, *pw; + + pl = tosort; pr = pl + num - 1; + pw = PyDimMem_NEW((1+num/2)); + + if (!pw) { + PyErr_NoMemory(); + return -1; + } + + @TYPE@_amergesort0(pl, pr, v, pw); + PyDimMem_FREE(pw); + + return 0; +} + + +/**end repeat**/ + +/* + ***************************************************************************** + ** STRING SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = STRING, UNICODE# + * #type = char, npy_ucs4# + */ + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) +{ + @type@ *pi, *pj, *pk, *pm; + + if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { + /* merge sort */ + pm = pl + (((pr - pl)/len) >> 1)*len; + @TYPE@_mergesort0(pl, pm, pw, vp, len); + @TYPE@_mergesort0(pm, pr, pw, vp, len); + @TYPE@_COPY(pw, pl, pm - pl); + pi = pw + (pm - pl); + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(pm, pj, len)) { + @TYPE@_COPY(pk, pm, len); + pm += len; + } + else { + @TYPE@_COPY(pk, pj, len); + pj += len; + } + pk += len; + } + @TYPE@_COPY(pk, pj, pi - pj); + } + else { + /* insertion sort */ + for (pi = pl + len; pi < pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + @type@ *pl, *pr, *pw, *vp; + int err = 0; + + pl = start; + pr = pl + num*len; + pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); + if (!pw) { + PyErr_NoMemory(); + err = -1; + goto fail_0; + } + vp = (@type@ *) PyDataMem_NEW(elsize); + if (!vp) { + PyErr_NoMemory(); + err = -1; + goto fail_1; + } + @TYPE@_mergesort0(pl, pr, pw, vp, len); + + PyDataMem_FREE(vp); +fail_1: + PyDataMem_FREE(pw); +fail_0: + return err; +} + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp = malloc(arr->descr->elsize); + @type@ *pl = start; + @type@ *pr = start + (num - 1)*len; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { + /* quicksort partition */ + pm = pl + (((pr - pl)/len) >> 1)*len; + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + @TYPE@_COPY(vp, pm, len); + pi = pl; + pj = pr - len; + @TYPE@_SWAP(pm, pj, len); + for (;;) { + do pi += len; while (@TYPE@_LT(pi, vp, len)); + do pj -= len; while (@TYPE@_LT(vp, pj, len)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(pi, pj, len); + } + pk = pr - len; + @TYPE@_SWAP(pi, pk, len); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + len; + *sptr++ = pr; + pr = pi - len; + } + else { + *sptr++ = pl; + *sptr++ = pi - len; + pl = pi + len; + } + } + + /* insertion sort */ + for (pi = pl + len; pi <= pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + free(vp); + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *tmp = malloc(arr->descr->elsize); + @type@ *a = start - len; + npy_intp i,j,l; + + for (l = n>>1; l > 0; --l) { + @TYPE@_COPY(tmp, a + l*len, len); + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j += 1; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + for (; n > 1;) { + @TYPE@_COPY(tmp, a + n*len, len); + @TYPE@_COPY(a + n*len, a + len, len); + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j++; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + free(tmp); + return 0; +} + + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + npy_intp *a, i,j,l, tmp; + + /* The array needs to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j += 1; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j++; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp; + npy_intp *pl = tosort; + npy_intp *pr = tosort + num - 1; + npy_intp *stack[PYA_QS_STACK]; + npy_intp **sptr=stack; + npy_intp *pm, *pi, *pj, *pk, vi; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + vp = v + (*pm)*len; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); + do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) +{ + @type@ *vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_amergesort0(pl,pm,v,pw,len); + @TYPE@_amergesort0(pm,pr,v,pw,len); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { + *pk = *pm++; + } else { + *pk = *pj++; + } + pk++; + } + while (pj < pi) { + *pk++ = *pj++; + } + } else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + } +} + + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + npy_intp *pl, *pr, *pw; + + pl = tosort; + pr = pl + num; + pw = PyDimMem_NEW(num/2); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_amergesort0(pl, pr, v, pw, len); + + PyDimMem_FREE(pw); + return 0; +} +/**end repeat**/ + +/* just for the module bit */ +#include "numpy/noprefix.h" + +static void +add_sortfuncs(void) +{ + PyArray_Descr *descr; + + /**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# + */ + descr = PyArray_DescrFromType(PyArray_@TYPE@); + descr->f->sort[PyArray_QUICKSORT] = + (PyArray_SortFunc *)@TYPE@_quicksort; + descr->f->argsort[PyArray_QUICKSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aquicksort; + descr->f->sort[PyArray_HEAPSORT] = + (PyArray_SortFunc *)@TYPE@_heapsort; + descr->f->argsort[PyArray_HEAPSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aheapsort; + descr->f->sort[PyArray_MERGESORT] = + (PyArray_SortFunc *)@TYPE@_mergesort; + descr->f->argsort[PyArray_MERGESORT] = + (PyArray_ArgSortFunc *)@TYPE@_amergesort; + /**end repeat**/ + +} + +static struct PyMethodDef methods[] = { + {NULL, NULL, 0, NULL} +}; + + +#if defined(NPY_PY3K) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_sort", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +/* Initialization function for the module */ +#if defined(NPY_PY3K) +PyObject *PyInit__sort(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + import_array(); + add_sortfuncs(); + return m; +} +#else +PyMODINIT_FUNC +init_sort(void) { + Py_InitModule("_sort", methods); + + import_array(); + add_sortfuncs(); +} +#endif diff --git a/numpy/core/src/sorting/npy_sort_common.h b/numpy/core/src/sorting/npy_sort_common.h deleted file mode 100644 index d4b3a0b3e..000000000 --- a/numpy/core/src/sorting/npy_sort_common.h +++ /dev/null @@ -1,306 +0,0 @@ -#ifndef __NPY_SORT_COMMON_H__ -#define __NPY_SORT_COMMON_H__ - - -#include "Python.h" -#include "numpy/npy_common.h" -#include "numpy/npy_math.h" -#include "numpy/halffloat.h" -#include "npy_config.h" - - -/* - ***************************************************************************** - ** SWAP MACROS ** - ***************************************************************************** - */ - -#define BOOL_SWAP(a,b) {npy_bool tmp = (b); (b)=(a); (a) = tmp;} -#define BYTE_SWAP(a,b) {npy_byte tmp = (b); (b)=(a); (a) = tmp;} -#define UBYTE_SWAP(a,b) {npy_ubyte tmp = (b); (b)=(a); (a) = tmp;} -#define SHORT_SWAP(a,b) {npy_short tmp = (b); (b)=(a); (a) = tmp;} -#define USHORT_SWAP(a,b) {npy_ushort tmp = (b); (b)=(a); (a) = tmp;} -#define INT_SWAP(a,b) {npy_int tmp = (b); (b)=(a); (a) = tmp;} -#define UINT_SWAP(a,b) {npy_uint tmp = (b); (b)=(a); (a) = tmp;} -#define LONG_SWAP(a,b) {npy_long tmp = (b); (b)=(a); (a) = tmp;} -#define ULONG_SWAP(a,b) {npy_ulong tmp = (b); (b)=(a); (a) = tmp;} -#define LONGLONG_SWAP(a,b) {npy_longlong tmp = (b); (b)=(a); (a) = tmp;} -#define ULONGLONG_SWAP(a,b) {npy_ulonglong tmp = (b); (b)=(a); (a) = tmp;} -#define HALF_SWAP(a,b) {npy_half tmp = (b); (b)=(a); (a) = tmp;} -#define FLOAT_SWAP(a,b) {npy_float tmp = (b); (b)=(a); (a) = tmp;} -#define DOUBLE_SWAP(a,b) {npy_double tmp = (b); (b)=(a); (a) = tmp;} -#define LONGDOUBLE_SWAP(a,b) {npy_longdouble tmp = (b); (b)=(a); (a) = tmp;} -#define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} -#define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} -#define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} -#define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} - -/* - ***************************************************************************** - ** COMPARISON FUNCTIONS ** - ***************************************************************************** - */ - -NPY_INLINE static int -BOOL_LT(npy_bool a, npy_bool b) -{ - return a < b; -} - - -NPY_INLINE static int -BYTE_LT(npy_byte a, npy_byte b) -{ - return a < b; -} - - -NPY_INLINE static int -UBYTE_LT(npy_ubyte a, npy_ubyte b) -{ - return a < b; -} - - -NPY_INLINE static int -SHORT_LT(npy_short a, npy_short b) -{ - return a < b; -} - - -NPY_INLINE static int -USHORT_LT(npy_ushort a, npy_ushort b) -{ - return a < b; -} - - -NPY_INLINE static int -INT_LT(npy_int a, npy_int b) -{ - return a < b; -} - - -NPY_INLINE static int -UINT_LT(npy_uint a, npy_uint b) -{ - return a < b; -} - - -NPY_INLINE static int -LONG_LT(npy_long a, npy_long b) -{ - return a < b; -} - - -NPY_INLINE static int -ULONG_LT(npy_ulong a, npy_ulong b) -{ - return a < b; -} - - -NPY_INLINE static int -LONGLONG_LT(npy_longlong a, npy_longlong b) -{ - return a < b; -} - - -NPY_INLINE static int -ULONGLONG_LT(npy_ulonglong a, npy_ulonglong b) -{ - return a < b; -} - - -NPY_INLINE static int -FLOAT_LT(npy_float a, npy_float b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -DOUBLE_LT(npy_double a, npy_double b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -HALF_LT(npy_half a, npy_half b) -{ - int ret; - - if (npy_half_isnan(b)) { - ret = !npy_half_isnan(a); - } else { - ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); - } - - return ret; -} - -/* - * For inline functions SUN recommends not using a return in the then part - * of an if statement. It's a SUN compiler thing, so assign the return value - * to a variable instead. - */ -NPY_INLINE static int -CFLOAT_LT(npy_cfloat a, npy_cfloat b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -NPY_INLINE static int -CDOUBLE_LT(npy_cdouble a, npy_cdouble b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -NPY_INLINE static int -CLONGDOUBLE_LT(npy_clongdouble a, npy_clongdouble b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -/* The PyObject functions are stubs for later use */ -NPY_INLINE static int -PyObject_LT(PyObject *pa, PyObject *pb) -{ - return 0; -} - - -NPY_INLINE static void -STRING_COPY(char *s1, char *s2, size_t len) -{ - memcpy(s1, s2, len); -} - - -NPY_INLINE static void -STRING_SWAP(char *s1, char *s2, size_t len) -{ - while(len--) { - const char t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -STRING_LT(char *s1, char *s2, size_t len) -{ - const unsigned char *c1 = (unsigned char *)s1; - const unsigned char *c2 = (unsigned char *)s2; - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (c1[i] != c2[i]) { - ret = c1[i] < c2[i]; - break; - } - } - return ret; -} - - -NPY_INLINE static void -UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - *s1++ = *s2++; - } -} - - -NPY_INLINE static void -UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - const npy_ucs4 t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (s1[i] != s2[i]) { - ret = s1[i] < s2[i]; - break; - } - } - return ret; -} - -#endif diff --git a/numpy/core/src/sorting/sortmodule.c.src b/numpy/core/src/sorting/sortmodule.c.src deleted file mode 100644 index f242b3393..000000000 --- a/numpy/core/src/sorting/sortmodule.c.src +++ /dev/null @@ -1,859 +0,0 @@ -/* -*- c -*- */ - -/* - * The purpose of this module is to add faster sort functions - * that are type-specific. This is done by altering the - * function table for the builtin descriptors. - * - * These sorting functions are copied almost directly from numarray - * with a few modifications (complex comparisons compare the imaginary - * part if the real parts are equal, for example), and the names - * are changed. - * - * The original sorting code is due to Charles R. Harris who wrote - * it for numarray. - */ - -/* - * Quick sort is usually the fastest, but the worst case scenario can - * be slower than the merge and heap sorts. The merge sort requires - * extra memory and so for large arrays may not be useful. - * - * The merge sort is *stable*, meaning that equal components - * are unmoved from their entry versions, so it can be used to - * implement lexigraphic sorting on multiple keys. - * - * The heap sort is included for completeness. - */ - -#include "npy_sort_common.h" - -#define NOT_USED NPY_UNUSED(unused) -#define PYA_QS_STACK 100 -#define SMALL_QUICKSORT 15 -#define SMALL_MERGESORT 20 -#define SMALL_STRING 16 - - -/* - ***************************************************************************** - ** NUMERIC SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble# - */ - - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl = start; - @type@ *pr = start + num - 1; - @type@ vp; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - vp = *pm; - pi = pl; - pj = pr - 1; - @TYPE@_SWAP(*pm, *pj); - for (;;) { - do ++pi; while (@TYPE@_LT(*pi, vp)); - do --pj; while (@TYPE@_LT(vp, *pj)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(*pi,*pj); - } - pk = pr - 1; - @TYPE@_SWAP(*pi, *pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) -{ - @type@ vp; - npy_intp *pl, *pr; - npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; - - pl = tosort; - pr = tosort + num - 1; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - vp = v[*pm]; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v[*pi],vp)); - do --pj; while (@TYPE@_LT(vp,v[*pj])); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v[*pk])) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) -{ - @type@ tmp, *a; - npy_intp i,j,l; - - /* The array needs to be offset by one for heapsort indexing */ - a = start - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j += 1; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j++; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) -{ - npy_intp *a, i,j,l, tmp; - /* The arrays need to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j += 1; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j++; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) -{ - @type@ vp, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_mergesort0(pl, pm, pw); - @TYPE@_mergesort0(pm, pr, pw); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(*pm,*pj)) { - *pk = *pm++; - } - else { - *pk = *pj++; - } - pk++; - } - while(pj < pi) { - *pk++ = *pj++; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl, *pr, *pw; - - pl = start; - pr = pl + num; - pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_mergesort0(pl, pr, pw); - - PyDataMem_FREE(pw); - return 0; -} - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) -{ - @type@ vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl + 1)>>1); - @TYPE@_amergesort0(pl,pm-1,v,pw); - @TYPE@_amergesort0(pm,pr,v,pw); - for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { - *pi = *pj; - } - for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { - if (@TYPE@_LT(v[*pj],v[*pk])) { - *pm = *pj; - ++pj; - } - else { - *pm = *pk; - ++pk; - } - } - for (; pk < pi; ++pm, ++pk) { - *pm = *pk; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { - *pj = *pk; - } - *pj = vi; - } - } -} - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) -{ - npy_intp *pl, *pr, *pw; - - pl = tosort; pr = pl + num - 1; - pw = PyDimMem_NEW((1+num/2)); - - if (!pw) { - PyErr_NoMemory(); - return -1; - } - - @TYPE@_amergesort0(pl, pr, v, pw); - PyDimMem_FREE(pw); - - return 0; -} - - -/**end repeat**/ - -/* - ***************************************************************************** - ** STRING SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = STRING, UNICODE# - * #type = char, npy_ucs4# - */ - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) -{ - @type@ *pi, *pj, *pk, *pm; - - if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { - /* merge sort */ - pm = pl + (((pr - pl)/len) >> 1)*len; - @TYPE@_mergesort0(pl, pm, pw, vp, len); - @TYPE@_mergesort0(pm, pr, pw, vp, len); - @TYPE@_COPY(pw, pl, pm - pl); - pi = pw + (pm - pl); - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(pm, pj, len)) { - @TYPE@_COPY(pk, pm, len); - pm += len; - } - else { - @TYPE@_COPY(pk, pj, len); - pj += len; - } - pk += len; - } - @TYPE@_COPY(pk, pj, pi - pj); - } - else { - /* insertion sort */ - for (pi = pl + len; pi < pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - @type@ *pl, *pr, *pw, *vp; - int err = 0; - - pl = start; - pr = pl + num*len; - pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); - if (!pw) { - PyErr_NoMemory(); - err = -1; - goto fail_0; - } - vp = (@type@ *) PyDataMem_NEW(elsize); - if (!vp) { - PyErr_NoMemory(); - err = -1; - goto fail_1; - } - @TYPE@_mergesort0(pl, pr, pw, vp, len); - - PyDataMem_FREE(vp); -fail_1: - PyDataMem_FREE(pw); -fail_0: - return err; -} - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp = malloc(arr->descr->elsize); - @type@ *pl = start; - @type@ *pr = start + (num - 1)*len; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { - /* quicksort partition */ - pm = pl + (((pr - pl)/len) >> 1)*len; - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - @TYPE@_COPY(vp, pm, len); - pi = pl; - pj = pr - len; - @TYPE@_SWAP(pm, pj, len); - for (;;) { - do pi += len; while (@TYPE@_LT(pi, vp, len)); - do pj -= len; while (@TYPE@_LT(vp, pj, len)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(pi, pj, len); - } - pk = pr - len; - @TYPE@_SWAP(pi, pk, len); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + len; - *sptr++ = pr; - pr = pi - len; - } - else { - *sptr++ = pl; - *sptr++ = pi - len; - pl = pi + len; - } - } - - /* insertion sort */ - for (pi = pl + len; pi <= pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - free(vp); - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *tmp = malloc(arr->descr->elsize); - @type@ *a = start - len; - npy_intp i,j,l; - - for (l = n>>1; l > 0; --l) { - @TYPE@_COPY(tmp, a + l*len, len); - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j += 1; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - for (; n > 1;) { - @TYPE@_COPY(tmp, a + n*len, len); - @TYPE@_COPY(a + n*len, a + len, len); - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j++; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - free(tmp); - return 0; -} - - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - npy_intp *a, i,j,l, tmp; - - /* The array needs to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j += 1; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j++; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp; - npy_intp *pl = tosort; - npy_intp *pr = tosort + num - 1; - npy_intp *stack[PYA_QS_STACK]; - npy_intp **sptr=stack; - npy_intp *pm, *pi, *pj, *pk, vi; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - vp = v + (*pm)*len; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); - do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) -{ - @type@ *vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_amergesort0(pl,pm,v,pw,len); - @TYPE@_amergesort0(pm,pr,v,pw,len); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { - *pk = *pm++; - } else { - *pk = *pj++; - } - pk++; - } - while (pj < pi) { - *pk++ = *pj++; - } - } else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - } -} - - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - npy_intp *pl, *pr, *pw; - - pl = tosort; - pr = pl + num; - pw = PyDimMem_NEW(num/2); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_amergesort0(pl, pr, v, pw, len); - - PyDimMem_FREE(pw); - return 0; -} -/**end repeat**/ - -/* just for the module bit */ -#include "numpy/noprefix.h" - -static void -add_sortfuncs(void) -{ - PyArray_Descr *descr; - - /**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - */ - descr = PyArray_DescrFromType(PyArray_@TYPE@); - descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)@TYPE@_quicksort; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aquicksort; - descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)@TYPE@_heapsort; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aheapsort; - descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)@TYPE@_mergesort; - descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)@TYPE@_amergesort; - /**end repeat**/ - -} - -static struct PyMethodDef methods[] = { - {NULL, NULL, 0, NULL} -}; - - -#if defined(NPY_PY3K) -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_sort", - NULL, - -1, - methods, - NULL, - NULL, - NULL, - NULL -}; -#endif - -/* Initialization function for the module */ -#if defined(NPY_PY3K) -PyObject *PyInit__sort(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - if (!m) { - return NULL; - } - import_array(); - add_sortfuncs(); - return m; -} -#else -PyMODINIT_FUNC -init_sort(void) { - Py_InitModule("_sort", methods); - - import_array(); - add_sortfuncs(); -} -#endif -- cgit v1.2.1 From 8343f3885bffce54bf99b19d81112bf913746d83 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 7 Jun 2011 21:28:42 -0600 Subject: ENH: Break out the sort functions into a separate file. --- numpy/core/SConscript | 3 +- numpy/core/setup.py | 141 +++--- numpy/core/src/npysort/sort.c.src | 820 ++++++++++++++++++++++++++++++ numpy/core/src/npysort/sortmodule.c | 70 +++ numpy/core/src/npysort/sortmodule.c.src | 859 -------------------------------- 5 files changed, 977 insertions(+), 916 deletions(-) create mode 100644 numpy/core/src/npysort/sort.c.src create mode 100644 numpy/core/src/npysort/sortmodule.c delete mode 100644 numpy/core/src/npysort/sortmodule.c.src (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index b495af824..844b16776 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,7 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) +sort_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sort.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', @@ -480,6 +480,7 @@ env.DistutilsPythonExtension('multiarray_tests', source=multiarray_tests_src) #------------------ # Build sort module #------------------ +sortmodule_src = [pjoin('src', 'npysort','sortmodule.c')] sort = env.DistutilsPythonExtension('_sort', source = sortmodule_src) #------------------- diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 5883b09ee..e1d35ac20 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -592,6 +592,7 @@ def configuration(parent_package='',top_path=None): config.add_include_dirs(join(local_dir, "src", "private")) config.add_include_dirs(join(local_dir, "src")) config.add_include_dirs(join(local_dir)) + # Multiarray version: this function is needed to build foo.c from foo.c.src # when foo.c is included in another file and as such not in the src # argument of build_ext command @@ -608,10 +609,8 @@ def configuration(parent_package='',top_path=None): # numpy.distutils generate .c from .c.src in weird directories, we have # to add them there as they depend on the build_dir config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') cmd.ensure_finalized() - cmd.template_sources(sources, ext) # umath version: this function is needed to build foo.c from foo.c.src @@ -629,14 +628,12 @@ def configuration(parent_package='',top_path=None): # numpy.distutils generate .c from .c.src in weird directories, we have # to add them there as they depend on the build_dir config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') cmd.ensure_finalized() - cmd.template_sources(sources, ext) - def generate_umath_c(ext,build_dir): + def generate_umath_c(ext, build_dir): target = join(build_dir,header_dir,'__umath_generated.c') dir = os.path.dirname(target) if not os.path.exists(dir): @@ -668,14 +665,6 @@ def configuration(parent_package='',top_path=None): if sys.platform == 'cygwin': config.add_data_dir('include/numpy/fenv') - config.add_extension('_sort', - sources=[join('src','npysort','sortmodule.c.src'), - generate_config_h, - generate_numpyconfig_h, - generate_numpy_api, - ], - libraries=['npymath']) - # npymath needs the config.h and numpyconfig.h files to be generated, but # build_clib cannot handle generate_config_h and generate_numpyconfig_h # (don't ask). Because clib are generated before extensions, we have to @@ -742,53 +731,93 @@ def configuration(parent_package='',top_path=None): join('src', 'multiarray', 'usertypes.h'), join('src', 'private', 'lowlevel_strided_loops.h')] - multiarray_src = [join('src', 'multiarray', 'multiarraymodule.c'), - join('src', 'multiarray', 'hashdescr.c'), - join('src', 'multiarray', 'arrayobject.c'), - join('src', 'multiarray', 'numpymemoryview.c'), - join('src', 'multiarray', 'buffer.c'), - join('src', 'multiarray', 'datetime.c'), - join('src', 'multiarray', 'datetime_busday.c'), - join('src', 'multiarray', 'datetime_busdaycal.c'), - join('src', 'multiarray', 'numpyos.c'), - join('src', 'multiarray', 'conversion_utils.c'), - join('src', 'multiarray', 'flagsobject.c'), - join('src', 'multiarray', 'descriptor.c'), - join('src', 'multiarray', 'iterators.c'), - join('src', 'multiarray', 'mapping.c'), - join('src', 'multiarray', 'number.c'), - join('src', 'multiarray', 'getset.c'), - join('src', 'multiarray', 'sequence.c'), - join('src', 'multiarray', 'methods.c'), - join('src', 'multiarray', 'ctors.c'), - join('src', 'multiarray', 'convert_datatype.c'), - join('src', 'multiarray', 'convert.c'), - join('src', 'multiarray', 'shape.c'), - join('src', 'multiarray', 'item_selection.c'), - join('src', 'multiarray', 'calculation.c'), - join('src', 'multiarray', 'common.c'), - join('src', 'multiarray', 'usertypes.c'), - join('src', 'multiarray', 'scalarapi.c'), - join('src', 'multiarray', 'refcount.c'), - join('src', 'multiarray', 'arraytypes.c.src'), - join('src', 'multiarray', 'scalartypes.c.src'), - join('src', 'multiarray', 'nditer.c.src'), - join('src', 'multiarray', 'lowlevel_strided_loops.c.src'), - join('src', 'multiarray', 'dtype_transfer.c'), - join('src', 'multiarray', 'nditer_pywrap.c'), - join('src', 'multiarray', 'einsum.c.src')] + multiarray_src = [ + join('src', 'multiarray', 'multiarraymodule.c'), + join('src', 'multiarray', 'hashdescr.c'), + join('src', 'multiarray', 'arrayobject.c'), + join('src', 'multiarray', 'numpymemoryview.c'), + join('src', 'multiarray', 'buffer.c'), + join('src', 'multiarray', 'datetime.c'), + join('src', 'multiarray', 'datetime_busday.c'), + join('src', 'multiarray', 'datetime_busdaycal.c'), + join('src', 'multiarray', 'numpyos.c'), + join('src', 'multiarray', 'conversion_utils.c'), + join('src', 'multiarray', 'flagsobject.c'), + join('src', 'multiarray', 'descriptor.c'), + join('src', 'multiarray', 'iterators.c'), + join('src', 'multiarray', 'mapping.c'), + join('src', 'multiarray', 'number.c'), + join('src', 'multiarray', 'getset.c'), + join('src', 'multiarray', 'sequence.c'), + join('src', 'multiarray', 'methods.c'), + join('src', 'multiarray', 'ctors.c'), + join('src', 'multiarray', 'convert_datatype.c'), + join('src', 'multiarray', 'convert.c'), + join('src', 'multiarray', 'shape.c'), + join('src', 'multiarray', 'item_selection.c'), + join('src', 'multiarray', 'calculation.c'), + join('src', 'multiarray', 'common.c'), + join('src', 'multiarray', 'usertypes.c'), + join('src', 'multiarray', 'scalarapi.c'), + join('src', 'multiarray', 'refcount.c'), + join('src', 'multiarray', 'arraytypes.c.src'), + join('src', 'multiarray', 'scalartypes.c.src'), + join('src', 'multiarray', 'nditer.c.src'), + join('src', 'multiarray', 'lowlevel_strided_loops.c.src'), + join('src', 'multiarray', 'dtype_transfer.c'), + join('src', 'multiarray', 'nditer_pywrap.c'), + join('src', 'multiarray', 'einsum.c.src')] if PYTHON_HAS_UNICODE_WIDE: multiarray_src.append(join('src', 'multiarray', 'ucsnarrow.c')) - umath_src = [join('src', 'umath', 'umathmodule.c.src'), + umath_src = [ + join('src', 'umath', 'umathmodule.c.src'), join('src', 'umath', 'funcs.inc.src'), join('src', 'umath', 'loops.c.src'), join('src', 'umath', 'ufunc_object.c')] - umath_deps = [generate_umath_py, + umath_deps = [ + generate_umath_py, join(codegen_dir,'generate_ufunc_api.py')] + ####################################################################### + # Sort Module # + ####################################################################### + + # _sort version: this function is needed to build foo.c from foo.c.src + # when foo.c is included in another file and as such not in the src + # argument of build_ext command + def generate_sort_templated_sources(ext, build_dir): + from numpy.distutils.misc_util import get_cmd + + subpath = join('src', 'npysort') + sources = [join(local_dir, subpath, 'sort.c.src')] + + # numpy.distutils generate .c from .c.src in weird directories, we have + # to add them there as they depend on the build_dir + config.add_include_dirs(join(build_dir, subpath)) + cmd = get_cmd('build_src') + cmd.ensure_finalized() + cmd.template_sources(sources, ext) + + sort_deps = [ + join('src', 'npysort', 'npy_sort_common.h'), + join('src', 'npysort', 'sort.c.src'), + join('src', 'npysort', 'sortmodule.c')] + + sort_src = [ + join('src', 'npysort', 'sortmodule.c'), + generate_sort_templated_sources, + generate_config_h, + generate_numpyconfig_h, + generate_numpy_api] + + config.add_extension('_sort', + sources = sort_src, + depends = deps + sort_deps, + libraries=['npymath']) +# if not ENABLE_SEPARATE_COMPILATION: multiarray_deps.extend(multiarray_src) multiarray_src = [join('src', 'multiarray', 'multiarraymodule_onefile.c')] @@ -801,7 +830,7 @@ def configuration(parent_package='',top_path=None): config.add_extension('multiarray', sources = multiarray_src + - [generate_config_h, + [generate_config_h, generate_numpyconfig_h, generate_numpy_api, join(codegen_dir,'generate_numpy_api.py'), @@ -810,11 +839,11 @@ def configuration(parent_package='',top_path=None): libraries=['npymath']) config.add_extension('umath', - sources = [generate_config_h, - generate_numpyconfig_h, - generate_umath_c, - generate_ufunc_api, - ] + umath_src, + sources = umath_src + + [generate_config_h, + generate_numpyconfig_h, + generate_umath_c, + generate_ufunc_api], depends = deps + umath_deps, libraries=['npymath'], ) diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src new file mode 100644 index 000000000..551f73510 --- /dev/null +++ b/numpy/core/src/npysort/sort.c.src @@ -0,0 +1,820 @@ +/* -*- c -*- */ + +/* + * The purpose of this module is to add faster sort functions + * that are type-specific. This is done by altering the + * function table for the builtin descriptors. + * + * These sorting functions are copied almost directly from numarray + * with a few modifications (complex comparisons compare the imaginary + * part if the real parts are equal, for example), and the names + * are changed. + * + * The original sorting code is due to Charles R. Harris who wrote + * it for numarray. + */ + +/* + * Quick sort is usually the fastest, but the worst case scenario can + * be slower than the merge and heap sorts. The merge sort requires + * extra memory and so for large arrays may not be useful. + * + * The merge sort is *stable*, meaning that equal components + * are unmoved from their entry versions, so it can be used to + * implement lexigraphic sorting on multiple keys. + * + * The heap sort is included for completeness. + */ + +#include "npy_sort_common.h" + +#define NOT_USED NPY_UNUSED(unused) +#define PYA_QS_STACK 100 +#define SMALL_QUICKSORT 15 +#define SMALL_MERGESORT 20 +#define SMALL_STRING 16 + + +/* + ***************************************************************************** + ** NUMERIC SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE# + * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble# + */ + + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl = start; + @type@ *pr = start + num - 1; + @type@ vp; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); + if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); + vp = *pm; + pi = pl; + pj = pr - 1; + @TYPE@_SWAP(*pm, *pj); + for (;;) { + do ++pi; while (@TYPE@_LT(*pi, vp)); + do --pj; while (@TYPE@_LT(vp, *pj)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(*pi,*pj); + } + pk = pr - 1; + @TYPE@_SWAP(*pi, *pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) +{ + @type@ vp; + npy_intp *pl, *pr; + npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; + + pl = tosort; + pr = tosort + num - 1; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); + if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); + vp = v[*pm]; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v[*pi],vp)); + do --pj; while (@TYPE@_LT(vp,v[*pj])); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v[*pk])) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) +{ + @type@ tmp, *a; + npy_intp i,j,l; + + /* The array needs to be offset by one for heapsort indexing */ + a = start - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j += 1; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a[j], a[j+1])) { + j++; + } + if (@TYPE@_LT(tmp, a[j])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) +{ + npy_intp *a, i,j,l, tmp; + /* The arrays need to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j += 1; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { + j++; + } + if (@TYPE@_LT(v[tmp], v[a[j]])) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) +{ + @type@ vp, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_mergesort0(pl, pm, pw); + @TYPE@_mergesort0(pm, pr, pw); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(*pm,*pj)) { + *pk = *pm++; + } + else { + *pk = *pj++; + } + pk++; + } + while(pj < pi) { + *pk++ = *pj++; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vp = *pi; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, *pk)) { + *pj-- = *pk--; + } + *pj = vp; + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) +{ + @type@ *pl, *pr, *pw; + + pl = start; + pr = pl + num; + pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_mergesort0(pl, pr, pw); + + PyDataMem_FREE(pw); + return 0; +} + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) +{ + @type@ vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl + 1)>>1); + @TYPE@_amergesort0(pl,pm-1,v,pw); + @TYPE@_amergesort0(pm,pr,v,pw); + for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { + *pi = *pj; + } + for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { + if (@TYPE@_LT(v[*pj],v[*pk])) { + *pm = *pj; + ++pj; + } + else { + *pm = *pk; + ++pk; + } + } + for (; pk < pi; ++pm, ++pk) { + *pm = *pk; + } + } + else { + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v[vi]; + for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { + *pj = *pk; + } + *pj = vi; + } + } +} + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) +{ + npy_intp *pl, *pr, *pw; + + pl = tosort; pr = pl + num - 1; + pw = PyDimMem_NEW((1+num/2)); + + if (!pw) { + PyErr_NoMemory(); + return -1; + } + + @TYPE@_amergesort0(pl, pr, v, pw); + PyDimMem_FREE(pw); + + return 0; +} + + +/**end repeat**/ + +/* + ***************************************************************************** + ** STRING SORTS ** + ***************************************************************************** + */ + + +/**begin repeat + * + * #TYPE = STRING, UNICODE# + * #type = char, npy_ucs4# + */ + +static void +@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) +{ + @type@ *pi, *pj, *pk, *pm; + + if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { + /* merge sort */ + pm = pl + (((pr - pl)/len) >> 1)*len; + @TYPE@_mergesort0(pl, pm, pw, vp, len); + @TYPE@_mergesort0(pm, pr, pw, vp, len); + @TYPE@_COPY(pw, pl, pm - pl); + pi = pw + (pm - pl); + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(pm, pj, len)) { + @TYPE@_COPY(pk, pm, len); + pm += len; + } + else { + @TYPE@_COPY(pk, pj, len); + pj += len; + } + pk += len; + } + @TYPE@_COPY(pk, pj, pi - pj); + } + else { + /* insertion sort */ + for (pi = pl + len; pi < pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + } +} + +static int +@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + @type@ *pl, *pr, *pw, *vp; + int err = 0; + + pl = start; + pr = pl + num*len; + pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); + if (!pw) { + PyErr_NoMemory(); + err = -1; + goto fail_0; + } + vp = (@type@ *) PyDataMem_NEW(elsize); + if (!vp) { + PyErr_NoMemory(); + err = -1; + goto fail_1; + } + @TYPE@_mergesort0(pl, pr, pw, vp, len); + + PyDataMem_FREE(vp); +fail_1: + PyDataMem_FREE(pw); +fail_0: + return err; +} + +static int +@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) +{ + const size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp = malloc(arr->descr->elsize); + @type@ *pl = start; + @type@ *pr = start + (num - 1)*len; + @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; + + for (;;) { + while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { + /* quicksort partition */ + pm = pl + (((pr - pl)/len) >> 1)*len; + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); + if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); + @TYPE@_COPY(vp, pm, len); + pi = pl; + pj = pr - len; + @TYPE@_SWAP(pm, pj, len); + for (;;) { + do pi += len; while (@TYPE@_LT(pi, vp, len)); + do pj -= len; while (@TYPE@_LT(vp, pj, len)); + if (pi >= pj) { + break; + } + @TYPE@_SWAP(pi, pj, len); + } + pk = pr - len; + @TYPE@_SWAP(pi, pk, len); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + len; + *sptr++ = pr; + pr = pi - len; + } + else { + *sptr++ = pl; + *sptr++ = pi - len; + pl = pi + len; + } + } + + /* insertion sort */ + for (pi = pl + len; pi <= pr; pi += len) { + @TYPE@_COPY(vp, pi, len); + pj = pi; + pk = pi - len; + while (pj > pl && @TYPE@_LT(vp, pk, len)) { + @TYPE@_COPY(pj, pk, len); + pj -= len; + pk -= len; + } + @TYPE@_COPY(pj, vp, len); + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + free(vp); + return 0; +} + + +static int +@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *tmp = malloc(arr->descr->elsize); + @type@ *a = start - len; + npy_intp i,j,l; + + for (l = n>>1; l > 0; --l) { + @TYPE@_COPY(tmp, a + l*len, len); + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j += 1; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + for (; n > 1;) { + @TYPE@_COPY(tmp, a + n*len, len); + @TYPE@_COPY(a + n*len, a + len, len); + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) + j++; + if (@TYPE@_LT(tmp, a + j*len, len)) { + @TYPE@_COPY(a + i*len, a + j*len, len); + i = j; + j += j; + } + else { + break; + } + } + @TYPE@_COPY(a + i*len, tmp, len); + } + + free(tmp); + return 0; +} + + +static int +@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + npy_intp *a, i,j,l, tmp; + + /* The array needs to be offset by one for heapsort indexing */ + a = tosort - 1; + + for (l = n>>1; l > 0; --l) { + tmp = a[l]; + for (i = l, j = l<<1; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j += 1; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + for (; n > 1;) { + tmp = a[n]; + a[n] = a[1]; + n -= 1; + for (i = 1, j = 2; j <= n;) { + if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) + j++; + if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { + a[i] = a[j]; + i = j; + j += j; + } + else { + break; + } + } + a[i] = tmp; + } + + return 0; +} + + +static int +@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) +{ + size_t len = arr->descr->elsize/sizeof(@type@); + @type@ *vp; + npy_intp *pl = tosort; + npy_intp *pr = tosort + num - 1; + npy_intp *stack[PYA_QS_STACK]; + npy_intp **sptr=stack; + npy_intp *pm, *pi, *pj, *pk, vi; + + for (;;) { + while ((pr - pl) > SMALL_QUICKSORT) { + /* quicksort partition */ + pm = pl + ((pr - pl) >> 1); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); + if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); + vp = v + (*pm)*len; + pi = pl; + pj = pr - 1; + INTP_SWAP(*pm,*pj); + for (;;) { + do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); + do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); + if (pi >= pj) { + break; + } + INTP_SWAP(*pi,*pj); + } + pk = pr - 1; + INTP_SWAP(*pi,*pk); + /* push largest partition on stack */ + if (pi - pl < pr - pi) { + *sptr++ = pi + 1; + *sptr++ = pr; + pr = pi - 1; + } + else { + *sptr++ = pl; + *sptr++ = pi - 1; + pl = pi + 1; + } + } + + /* insertion sort */ + for (pi = pl + 1; pi <= pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi - 1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + if (sptr == stack) { + break; + } + pr = *(--sptr); + pl = *(--sptr); + } + + return 0; +} + + +static void +@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) +{ + @type@ *vp; + npy_intp vi, *pi, *pj, *pk, *pm; + + if (pr - pl > SMALL_MERGESORT) { + /* merge sort */ + pm = pl + ((pr - pl) >> 1); + @TYPE@_amergesort0(pl,pm,v,pw,len); + @TYPE@_amergesort0(pm,pr,v,pw,len); + for (pi = pw, pj = pl; pj < pm;) { + *pi++ = *pj++; + } + pj = pw; + pk = pl; + while (pj < pi && pm < pr) { + if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { + *pk = *pm++; + } else { + *pk = *pj++; + } + pk++; + } + while (pj < pi) { + *pk++ = *pj++; + } + } else { + /* insertion sort */ + for (pi = pl + 1; pi < pr; ++pi) { + vi = *pi; + vp = v + vi*len; + pj = pi; + pk = pi -1; + while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { + *pj-- = *pk--; + } + *pj = vi; + } + } +} + + +static int +@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) +{ + const size_t elsize = arr->descr->elsize; + const size_t len = elsize / sizeof(@type@); + npy_intp *pl, *pr, *pw; + + pl = tosort; + pr = pl + num; + pw = PyDimMem_NEW(num/2); + if (!pw) { + PyErr_NoMemory(); + return -1; + } + @TYPE@_amergesort0(pl, pr, v, pw, len); + + PyDimMem_FREE(pw); + return 0; +} +/**end repeat**/ + +/* just for the module bit */ +#include "numpy/noprefix.h" + +static void +add_sortfuncs(void) +{ + PyArray_Descr *descr; + + /**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# + */ + descr = PyArray_DescrFromType(PyArray_@TYPE@); + descr->f->sort[PyArray_QUICKSORT] = + (PyArray_SortFunc *)@TYPE@_quicksort; + descr->f->argsort[PyArray_QUICKSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aquicksort; + descr->f->sort[PyArray_HEAPSORT] = + (PyArray_SortFunc *)@TYPE@_heapsort; + descr->f->argsort[PyArray_HEAPSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aheapsort; + descr->f->sort[PyArray_MERGESORT] = + (PyArray_SortFunc *)@TYPE@_mergesort; + descr->f->argsort[PyArray_MERGESORT] = + (PyArray_ArgSortFunc *)@TYPE@_amergesort; + /**end repeat**/ + +} + + diff --git a/numpy/core/src/npysort/sortmodule.c b/numpy/core/src/npysort/sortmodule.c new file mode 100644 index 000000000..8bd6ed1fa --- /dev/null +++ b/numpy/core/src/npysort/sortmodule.c @@ -0,0 +1,70 @@ +/* -*- c -*- */ + +/* + * The purpose of this module is to add faster sort functions + * that are type-specific. This is done by altering the + * function table for the builtin descriptors. + * + * These sorting functions are copied almost directly from numarray + * with a few modifications (complex comparisons compare the imaginary + * part if the real parts are equal, for example), and the names + * are changed. + * + * The original sorting code is due to Charles R. Harris who wrote + * it for numarray. + */ + +/* + * Quick sort is usually the fastest, but the worst case scenario can + * be slower than the merge and heap sorts. The merge sort requires + * extra memory and so for large arrays may not be useful. + * + * The merge sort is *stable*, meaning that equal components + * are unmoved from their entry versions, so it can be used to + * implement lexigraphic sorting on multiple keys. + * + * The heap sort is included for completeness. + */ + +#include "sort.c" + +static struct PyMethodDef methods[] = { + {NULL, NULL, 0, NULL} +}; + + +#if defined(NPY_PY3K) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_sort", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +/* Initialization function for the module */ +#if defined(NPY_PY3K) +PyObject *PyInit__sort(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + import_array(); + add_sortfuncs(); + return m; +} +#else +PyMODINIT_FUNC +init_sort(void) { + Py_InitModule("_sort", methods); + + import_array(); + add_sortfuncs(); +} +#endif diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src deleted file mode 100644 index f242b3393..000000000 --- a/numpy/core/src/npysort/sortmodule.c.src +++ /dev/null @@ -1,859 +0,0 @@ -/* -*- c -*- */ - -/* - * The purpose of this module is to add faster sort functions - * that are type-specific. This is done by altering the - * function table for the builtin descriptors. - * - * These sorting functions are copied almost directly from numarray - * with a few modifications (complex comparisons compare the imaginary - * part if the real parts are equal, for example), and the names - * are changed. - * - * The original sorting code is due to Charles R. Harris who wrote - * it for numarray. - */ - -/* - * Quick sort is usually the fastest, but the worst case scenario can - * be slower than the merge and heap sorts. The merge sort requires - * extra memory and so for large arrays may not be useful. - * - * The merge sort is *stable*, meaning that equal components - * are unmoved from their entry versions, so it can be used to - * implement lexigraphic sorting on multiple keys. - * - * The heap sort is included for completeness. - */ - -#include "npy_sort_common.h" - -#define NOT_USED NPY_UNUSED(unused) -#define PYA_QS_STACK 100 -#define SMALL_QUICKSORT 15 -#define SMALL_MERGESORT 20 -#define SMALL_STRING 16 - - -/* - ***************************************************************************** - ** NUMERIC SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE# - * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble# - */ - - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl = start; - @type@ *pr = start + num - 1; - @type@ vp; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - if (@TYPE@_LT(*pr, *pm)) @TYPE@_SWAP(*pr, *pm); - if (@TYPE@_LT(*pm, *pl)) @TYPE@_SWAP(*pm, *pl); - vp = *pm; - pi = pl; - pj = pr - 1; - @TYPE@_SWAP(*pm, *pj); - for (;;) { - do ++pi; while (@TYPE@_LT(*pi, vp)); - do --pj; while (@TYPE@_LT(vp, *pj)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(*pi,*pj); - } - pk = pr - 1; - @TYPE@_SWAP(*pi, *pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) -{ - @type@ vp; - npy_intp *pl, *pr; - npy_intp *stack[PYA_QS_STACK], **sptr=stack, *pm, *pi, *pj, *pk, vi; - - pl = tosort; - pr = tosort + num - 1; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - if (@TYPE@_LT(v[*pr],v[*pm])) INTP_SWAP(*pr,*pm); - if (@TYPE@_LT(v[*pm],v[*pl])) INTP_SWAP(*pm,*pl); - vp = v[*pm]; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v[*pi],vp)); - do --pj; while (@TYPE@_LT(vp,v[*pj])); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v[*pk])) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) -{ - @type@ tmp, *a; - npy_intp i,j,l; - - /* The array needs to be offset by one for heapsort indexing */ - a = start - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j += 1; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a[j], a[j+1])) { - j++; - } - if (@TYPE@_LT(tmp, a[j])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) -{ - npy_intp *a, i,j,l, tmp; - /* The arrays need to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j += 1; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v[a[j]], v[a[j+1]])) { - j++; - } - if (@TYPE@_LT(v[tmp], v[a[j]])) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) -{ - @type@ vp, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_mergesort0(pl, pm, pw); - @TYPE@_mergesort0(pm, pr, pw); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(*pm,*pj)) { - *pk = *pm++; - } - else { - *pk = *pj++; - } - pk++; - } - while(pj < pi) { - *pk++ = *pj++; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vp = *pi; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, *pk)) { - *pj-- = *pk--; - } - *pj = vp; - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) -{ - @type@ *pl, *pr, *pw; - - pl = start; - pr = pl + num; - pw = (@type@ *) PyDataMem_NEW((num/2)*sizeof(@type@)); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_mergesort0(pl, pr, pw); - - PyDataMem_FREE(pw); - return 0; -} - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) -{ - @type@ vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl + 1)>>1); - @TYPE@_amergesort0(pl,pm-1,v,pw); - @TYPE@_amergesort0(pm,pr,v,pw); - for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { - *pi = *pj; - } - for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { - if (@TYPE@_LT(v[*pj],v[*pk])) { - *pm = *pj; - ++pj; - } - else { - *pm = *pk; - ++pk; - } - } - for (; pk < pi; ++pm, ++pk) { - *pm = *pk; - } - } - else { - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v[vi]; - for (pj = pi, pk = pi - 1; pj > pl && @TYPE@_LT(vp, v[*pk]); --pj, --pk) { - *pj = *pk; - } - *pj = vi; - } - } -} - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) -{ - npy_intp *pl, *pr, *pw; - - pl = tosort; pr = pl + num - 1; - pw = PyDimMem_NEW((1+num/2)); - - if (!pw) { - PyErr_NoMemory(); - return -1; - } - - @TYPE@_amergesort0(pl, pr, v, pw); - PyDimMem_FREE(pw); - - return 0; -} - - -/**end repeat**/ - -/* - ***************************************************************************** - ** STRING SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = STRING, UNICODE# - * #type = char, npy_ucs4# - */ - -static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) -{ - @type@ *pi, *pj, *pk, *pm; - - if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { - /* merge sort */ - pm = pl + (((pr - pl)/len) >> 1)*len; - @TYPE@_mergesort0(pl, pm, pw, vp, len); - @TYPE@_mergesort0(pm, pr, pw, vp, len); - @TYPE@_COPY(pw, pl, pm - pl); - pi = pw + (pm - pl); - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(pm, pj, len)) { - @TYPE@_COPY(pk, pm, len); - pm += len; - } - else { - @TYPE@_COPY(pk, pj, len); - pj += len; - } - pk += len; - } - @TYPE@_COPY(pk, pj, pi - pj); - } - else { - /* insertion sort */ - for (pi = pl + len; pi < pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - } -} - -static int -@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - @type@ *pl, *pr, *pw, *vp; - int err = 0; - - pl = start; - pr = pl + num*len; - pw = (@type@ *) PyDataMem_NEW((num/2)*elsize); - if (!pw) { - PyErr_NoMemory(); - err = -1; - goto fail_0; - } - vp = (@type@ *) PyDataMem_NEW(elsize); - if (!vp) { - PyErr_NoMemory(); - err = -1; - goto fail_1; - } - @TYPE@_mergesort0(pl, pr, pw, vp, len); - - PyDataMem_FREE(vp); -fail_1: - PyDataMem_FREE(pw); -fail_0: - return err; -} - -static int -@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) -{ - const size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp = malloc(arr->descr->elsize); - @type@ *pl = start; - @type@ *pr = start + (num - 1)*len; - @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; - - for (;;) { - while ((size_t)(pr - pl) > SMALL_QUICKSORT*len) { - /* quicksort partition */ - pm = pl + (((pr - pl)/len) >> 1)*len; - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - if (@TYPE@_LT(pr, pm, len)) @TYPE@_SWAP(pr, pm, len); - if (@TYPE@_LT(pm, pl, len)) @TYPE@_SWAP(pm, pl, len); - @TYPE@_COPY(vp, pm, len); - pi = pl; - pj = pr - len; - @TYPE@_SWAP(pm, pj, len); - for (;;) { - do pi += len; while (@TYPE@_LT(pi, vp, len)); - do pj -= len; while (@TYPE@_LT(vp, pj, len)); - if (pi >= pj) { - break; - } - @TYPE@_SWAP(pi, pj, len); - } - pk = pr - len; - @TYPE@_SWAP(pi, pk, len); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + len; - *sptr++ = pr; - pr = pi - len; - } - else { - *sptr++ = pl; - *sptr++ = pi - len; - pl = pi + len; - } - } - - /* insertion sort */ - for (pi = pl + len; pi <= pr; pi += len) { - @TYPE@_COPY(vp, pi, len); - pj = pi; - pk = pi - len; - while (pj > pl && @TYPE@_LT(vp, pk, len)) { - @TYPE@_COPY(pj, pk, len); - pj -= len; - pk -= len; - } - @TYPE@_COPY(pj, vp, len); - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - free(vp); - return 0; -} - - -static int -@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *tmp = malloc(arr->descr->elsize); - @type@ *a = start - len; - npy_intp i,j,l; - - for (l = n>>1; l > 0; --l) { - @TYPE@_COPY(tmp, a + l*len, len); - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j += 1; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - for (; n > 1;) { - @TYPE@_COPY(tmp, a + n*len, len); - @TYPE@_COPY(a + n*len, a + len, len); - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(a + j*len, a + (j+1)*len, len)) - j++; - if (@TYPE@_LT(tmp, a + j*len, len)) { - @TYPE@_COPY(a + i*len, a + j*len, len); - i = j; - j += j; - } - else { - break; - } - } - @TYPE@_COPY(a + i*len, tmp, len); - } - - free(tmp); - return 0; -} - - -static int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - npy_intp *a, i,j,l, tmp; - - /* The array needs to be offset by one for heapsort indexing */ - a = tosort - 1; - - for (l = n>>1; l > 0; --l) { - tmp = a[l]; - for (i = l, j = l<<1; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j += 1; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - for (; n > 1;) { - tmp = a[n]; - a[n] = a[1]; - n -= 1; - for (i = 1, j = 2; j <= n;) { - if (j < n && @TYPE@_LT(v + a[j]*len, v + a[j+1]*len, len)) - j++; - if (@TYPE@_LT(v + tmp*len, v + a[j]*len, len)) { - a[i] = a[j]; - i = j; - j += j; - } - else { - break; - } - } - a[i] = tmp; - } - - return 0; -} - - -static int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) -{ - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp; - npy_intp *pl = tosort; - npy_intp *pr = tosort + num - 1; - npy_intp *stack[PYA_QS_STACK]; - npy_intp **sptr=stack; - npy_intp *pm, *pi, *pj, *pk, vi; - - for (;;) { - while ((pr - pl) > SMALL_QUICKSORT) { - /* quicksort partition */ - pm = pl + ((pr - pl) >> 1); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - if (@TYPE@_LT(v + (*pr)*len, v + (*pm)*len, len)) INTP_SWAP(*pr, *pm); - if (@TYPE@_LT(v + (*pm)*len, v + (*pl)*len, len)) INTP_SWAP(*pm, *pl); - vp = v + (*pm)*len; - pi = pl; - pj = pr - 1; - INTP_SWAP(*pm,*pj); - for (;;) { - do ++pi; while (@TYPE@_LT(v + (*pi)*len, vp, len)); - do --pj; while (@TYPE@_LT(vp, v + (*pj)*len, len)); - if (pi >= pj) { - break; - } - INTP_SWAP(*pi,*pj); - } - pk = pr - 1; - INTP_SWAP(*pi,*pk); - /* push largest partition on stack */ - if (pi - pl < pr - pi) { - *sptr++ = pi + 1; - *sptr++ = pr; - pr = pi - 1; - } - else { - *sptr++ = pl; - *sptr++ = pi - 1; - pl = pi + 1; - } - } - - /* insertion sort */ - for (pi = pl + 1; pi <= pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi - 1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - if (sptr == stack) { - break; - } - pr = *(--sptr); - pl = *(--sptr); - } - - return 0; -} - - -static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) -{ - @type@ *vp; - npy_intp vi, *pi, *pj, *pk, *pm; - - if (pr - pl > SMALL_MERGESORT) { - /* merge sort */ - pm = pl + ((pr - pl) >> 1); - @TYPE@_amergesort0(pl,pm,v,pw,len); - @TYPE@_amergesort0(pm,pr,v,pw,len); - for (pi = pw, pj = pl; pj < pm;) { - *pi++ = *pj++; - } - pj = pw; - pk = pl; - while (pj < pi && pm < pr) { - if (@TYPE@_LT(v + (*pm)*len, v + (*pj)*len, len)) { - *pk = *pm++; - } else { - *pk = *pj++; - } - pk++; - } - while (pj < pi) { - *pk++ = *pj++; - } - } else { - /* insertion sort */ - for (pi = pl + 1; pi < pr; ++pi) { - vi = *pi; - vp = v + vi*len; - pj = pi; - pk = pi -1; - while (pj > pl && @TYPE@_LT(vp, v + (*pk)*len, len)) { - *pj-- = *pk--; - } - *pj = vi; - } - } -} - - -static int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) -{ - const size_t elsize = arr->descr->elsize; - const size_t len = elsize / sizeof(@type@); - npy_intp *pl, *pr, *pw; - - pl = tosort; - pr = pl + num; - pw = PyDimMem_NEW(num/2); - if (!pw) { - PyErr_NoMemory(); - return -1; - } - @TYPE@_amergesort0(pl, pr, v, pw, len); - - PyDimMem_FREE(pw); - return 0; -} -/**end repeat**/ - -/* just for the module bit */ -#include "numpy/noprefix.h" - -static void -add_sortfuncs(void) -{ - PyArray_Descr *descr; - - /**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - */ - descr = PyArray_DescrFromType(PyArray_@TYPE@); - descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)@TYPE@_quicksort; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aquicksort; - descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)@TYPE@_heapsort; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aheapsort; - descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)@TYPE@_mergesort; - descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)@TYPE@_amergesort; - /**end repeat**/ - -} - -static struct PyMethodDef methods[] = { - {NULL, NULL, 0, NULL} -}; - - -#if defined(NPY_PY3K) -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_sort", - NULL, - -1, - methods, - NULL, - NULL, - NULL, - NULL -}; -#endif - -/* Initialization function for the module */ -#if defined(NPY_PY3K) -PyObject *PyInit__sort(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - if (!m) { - return NULL; - } - import_array(); - add_sortfuncs(); - return m; -} -#else -PyMODINIT_FUNC -init_sort(void) { - Py_InitModule("_sort", methods); - - import_array(); - add_sortfuncs(); -} -#endif -- cgit v1.2.1 From 39a420ca5a42b7450f2ff242cd7f6c78f3832b72 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 9 Jun 2011 11:20:14 -0600 Subject: STY: Reorganize core/setup.py a bit. --- numpy/core/setup.py | 228 +++++++++++++++++++++++++++++----------------------- 1 file changed, 129 insertions(+), 99 deletions(-) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index e1d35ac20..24acc49c8 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -593,59 +593,6 @@ def configuration(parent_package='',top_path=None): config.add_include_dirs(join(local_dir, "src")) config.add_include_dirs(join(local_dir)) - # Multiarray version: this function is needed to build foo.c from foo.c.src - # when foo.c is included in another file and as such not in the src - # argument of build_ext command - def generate_multiarray_templated_sources(ext, build_dir): - from numpy.distutils.misc_util import get_cmd - - subpath = join('src', 'multiarray') - sources = [join(local_dir, subpath, 'scalartypes.c.src'), - join(local_dir, subpath, 'arraytypes.c.src'), - join(local_dir, subpath, 'nditer.c.src'), - join(local_dir, subpath, 'lowlevel_strided_loops.c.src'), - join(local_dir, subpath, 'einsum.c.src')] - - # numpy.distutils generate .c from .c.src in weird directories, we have - # to add them there as they depend on the build_dir - config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') - cmd.ensure_finalized() - cmd.template_sources(sources, ext) - - # umath version: this function is needed to build foo.c from foo.c.src - # when foo.c is included in another file and as such not in the src - # argument of build_ext command - def generate_umath_templated_sources(ext, build_dir): - from numpy.distutils.misc_util import get_cmd - - subpath = join('src', 'umath') - # NOTE: For manual template conversion of loops.h.src, read the note - # in that file. - sources = [join(local_dir, subpath, 'loops.c.src'), - join(local_dir, subpath, 'umathmodule.c.src')] - - # numpy.distutils generate .c from .c.src in weird directories, we have - # to add them there as they depend on the build_dir - config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') - cmd.ensure_finalized() - cmd.template_sources(sources, ext) - - - def generate_umath_c(ext, build_dir): - target = join(build_dir,header_dir,'__umath_generated.c') - dir = os.path.dirname(target) - if not os.path.exists(dir): - os.makedirs(dir) - script = generate_umath_py - if newer(script,target): - f = open(target,'w') - f.write(generate_umath.make_code(generate_umath.defdict, - generate_umath.__file__)) - f.close() - return [] - config.add_data_files('include/numpy/*.h') config.add_include_dirs(join('src', 'npymath')) config.add_include_dirs(join('src', 'multiarray')) @@ -665,6 +612,10 @@ def configuration(parent_package='',top_path=None): if sys.platform == 'cygwin': config.add_data_dir('include/numpy/fenv') + ####################################################################### + # npymath library # + ####################################################################### + # npymath needs the config.h and numpyconfig.h files to be generated, but # build_clib cannot handle generate_config_h and generate_numpyconfig_h # (don't ask). Because clib are generated before extensions, we have to @@ -703,6 +654,67 @@ def configuration(parent_package='',top_path=None): config.add_npy_pkg_config("mlib.ini.in", "lib/npy-pkg-config", subst_dict) + ####################################################################### + # sort module # + ####################################################################### + + # _sort version: this function is needed to build foo.c from foo.c.src + # when foo.c is included in another file and as such not in the src + # argument of build_ext command + def generate_sort_templated_sources(ext, build_dir): + from numpy.distutils.misc_util import get_cmd + + subpath = join('src', 'npysort') + sources = [join(local_dir, subpath, 'sort.c.src')] + + # numpy.distutils generate .c from .c.src in weird directories, we have + # to add them there as they depend on the build_dir + config.add_include_dirs(join(build_dir, subpath)) + cmd = get_cmd('build_src') + cmd.ensure_finalized() + cmd.template_sources(sources, ext) + + sort_deps = [ + join('src', 'npysort', 'npy_sort_common.h'), + join('src', 'npysort', 'sort.c.src'), + join('src', 'npysort', 'sortmodule.c')] + + sort_src = [ + join('src', 'npysort', 'sortmodule.c'), + generate_sort_templated_sources, + generate_config_h, + generate_numpyconfig_h, + generate_numpy_api] + + config.add_extension('_sort', + sources = sort_src, + depends = deps + sort_deps, + libraries = ['npymath']) + + ####################################################################### + # multiarray module # + ####################################################################### + + # Multiarray version: this function is needed to build foo.c from foo.c.src + # when foo.c is included in another file and as such not in the src + # argument of build_ext command + def generate_multiarray_templated_sources(ext, build_dir): + from numpy.distutils.misc_util import get_cmd + + subpath = join('src', 'multiarray') + sources = [join(local_dir, subpath, 'scalartypes.c.src'), + join(local_dir, subpath, 'arraytypes.c.src'), + join(local_dir, subpath, 'nditer.c.src'), + join(local_dir, subpath, 'lowlevel_strided_loops.c.src'), + join(local_dir, subpath, 'einsum.c.src')] + + # numpy.distutils generate .c from .c.src in weird directories, we have + # to add them there as they depend on the build_dir + config.add_include_dirs(join(build_dir, subpath)) + cmd = get_cmd('build_src') + cmd.ensure_finalized() + cmd.template_sources(sources, ext) + multiarray_deps = [ join('src', 'multiarray', 'arrayobject.h'), join('src', 'multiarray', 'arraytypes.h'), @@ -771,28 +783,36 @@ def configuration(parent_package='',top_path=None): if PYTHON_HAS_UNICODE_WIDE: multiarray_src.append(join('src', 'multiarray', 'ucsnarrow.c')) - umath_src = [ - join('src', 'umath', 'umathmodule.c.src'), - join('src', 'umath', 'funcs.inc.src'), - join('src', 'umath', 'loops.c.src'), - join('src', 'umath', 'ufunc_object.c')] + if not ENABLE_SEPARATE_COMPILATION: + multiarray_deps.extend(multiarray_src) + multiarray_src = [join('src', 'multiarray', 'multiarraymodule_onefile.c')] + multiarray_src.append(generate_multiarray_templated_sources) - umath_deps = [ - generate_umath_py, - join(codegen_dir,'generate_ufunc_api.py')] + config.add_extension('multiarray', + sources = multiarray_src + + [generate_config_h, + generate_numpyconfig_h, + generate_numpy_api, + join(codegen_dir,'generate_numpy_api.py'), + join('*.py')], + depends = deps + multiarray_deps, + libraries = ['npymath']) ####################################################################### - # Sort Module # + # umath module # ####################################################################### - # _sort version: this function is needed to build foo.c from foo.c.src + # umath version: this function is needed to build foo.c from foo.c.src # when foo.c is included in another file and as such not in the src # argument of build_ext command - def generate_sort_templated_sources(ext, build_dir): + def generate_umath_templated_sources(ext, build_dir): from numpy.distutils.misc_util import get_cmd - subpath = join('src', 'npysort') - sources = [join(local_dir, subpath, 'sort.c.src')] + subpath = join('src', 'umath') + # NOTE: For manual template conversion of loops.h.src, read the note + # in that file. + sources = [join(local_dir, subpath, 'loops.c.src'), + join(local_dir, subpath, 'umathmodule.c.src')] # numpy.distutils generate .c from .c.src in weird directories, we have # to add them there as they depend on the build_dir @@ -801,43 +821,36 @@ def configuration(parent_package='',top_path=None): cmd.ensure_finalized() cmd.template_sources(sources, ext) - sort_deps = [ - join('src', 'npysort', 'npy_sort_common.h'), - join('src', 'npysort', 'sort.c.src'), - join('src', 'npysort', 'sortmodule.c')] - sort_src = [ - join('src', 'npysort', 'sortmodule.c'), - generate_sort_templated_sources, - generate_config_h, - generate_numpyconfig_h, - generate_numpy_api] + def generate_umath_c(ext, build_dir): + target = join(build_dir,header_dir,'__umath_generated.c') + dir = os.path.dirname(target) + if not os.path.exists(dir): + os.makedirs(dir) + script = generate_umath_py + if newer(script,target): + f = open(target,'w') + f.write(generate_umath.make_code(generate_umath.defdict, + generate_umath.__file__)) + f.close() + return [] - config.add_extension('_sort', - sources = sort_src, - depends = deps + sort_deps, - libraries=['npymath']) -# - if not ENABLE_SEPARATE_COMPILATION: - multiarray_deps.extend(multiarray_src) - multiarray_src = [join('src', 'multiarray', 'multiarraymodule_onefile.c')] - multiarray_src.append(generate_multiarray_templated_sources) + umath_src = [ + join('src', 'umath', 'umathmodule.c.src'), + join('src', 'umath', 'funcs.inc.src'), + join('src', 'umath', 'loops.c.src'), + join('src', 'umath', 'ufunc_object.c')] + umath_deps = [ + generate_umath_py, + join(codegen_dir,'generate_ufunc_api.py')] + + if not ENABLE_SEPARATE_COMPILATION: umath_deps.extend(umath_src) umath_src = [join('src', 'umath', 'umathmodule_onefile.c')] umath_src.append(generate_umath_templated_sources) umath_src.append(join('src', 'umath', 'funcs.inc.src')) - config.add_extension('multiarray', - sources = multiarray_src + - [generate_config_h, - generate_numpyconfig_h, - generate_numpy_api, - join(codegen_dir,'generate_numpy_api.py'), - join('*.py')], - depends = deps + multiarray_deps, - libraries=['npymath']) - config.add_extension('umath', sources = umath_src + [generate_config_h, @@ -845,18 +858,27 @@ def configuration(parent_package='',top_path=None): generate_umath_c, generate_ufunc_api], depends = deps + umath_deps, - libraries=['npymath'], + libraries = ['npymath'], ) + ####################################################################### + # scalarmath module # + ####################################################################### + config.add_extension('scalarmath', - sources=[join('src','scalarmathmodule.c.src'), + sources = [join('src','scalarmathmodule.c.src'), generate_config_h, generate_numpyconfig_h, generate_numpy_api, generate_ufunc_api], - libraries=['npymath'], + depends = deps, + libraries = ['npymath'], ) + ####################################################################### + # _dotblas module # + ####################################################################### + # Configure blasdot blas_info = get_info('blas_opt',0) #blas_info = {} @@ -869,16 +891,24 @@ def configuration(parent_package='',top_path=None): config.add_extension('_dotblas', sources = [get_dotblas_sources], - depends=[join('blasdot','_dotblas.c'), + depends = [join('blasdot','_dotblas.c'), join('blasdot','cblas.h'), ], include_dirs = ['blasdot'], extra_info = blas_info ) + ####################################################################### + # umath_tests module # + ####################################################################### + config.add_extension('umath_tests', sources = [join('src','umath', 'umath_tests.c.src')]) + ####################################################################### + # multiarray_tests module # + ####################################################################### + config.add_extension('multiarray_tests', sources = [join('src', 'multiarray', 'multiarray_tests.c.src')]) -- cgit v1.2.1 From 527bedde2858173ef8eff3b4d572dd5090ec9070 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 12 Jun 2011 22:39:56 -0600 Subject: STY: Move npy_intp definitions and associated formats into npy_common.h. --- numpy/core/include/numpy/ndarraytypes.h | 89 ------------------------------- numpy/core/include/numpy/npy_common.h | 93 +++++++++++++++++++++++++++++++-- numpy/core/include/numpy/npy_sort.h | 2 +- numpy/core/setup.py | 40 ++++++++------ numpy/core/src/npysort/sortmodule.c | 3 +- 5 files changed, 117 insertions(+), 110 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index d3e28fe93..701afee82 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -99,12 +99,6 @@ enum NPY_TYPES { NPY_BOOL=0, /* How many floating point types are there (excluding half) */ #define NPY_NUM_FLOATTYPE 3 -/* - * We need to match npy_intp to a signed integer of the same size as a - * pointer variable. npy_uintp to the equivalent unsigned integer - */ - - /* * These characters correspond to the array type and the struct * module @@ -290,89 +284,6 @@ typedef enum { NPY_BUSDAY_RAISE } NPY_BUSDAY_ROLL; -/* - * This is to typedef npy_intp to the appropriate pointer size for - * this platform. Py_intptr_t, Py_uintptr_t are defined in pyport.h. - */ -typedef Py_intptr_t npy_intp; -typedef Py_uintptr_t npy_uintp; -#define NPY_SIZEOF_INTP NPY_SIZEOF_PY_INTPTR_T -#define NPY_SIZEOF_UINTP NPY_SIZEOF_PY_INTPTR_T - -#ifdef constchar -#undef constchar -#endif - -#if (PY_VERSION_HEX < 0x02050000) - #ifndef PY_SSIZE_T_MIN - typedef int Py_ssize_t; - #define PY_SSIZE_T_MAX INT_MAX - #define PY_SSIZE_T_MIN INT_MIN - #endif -#define NPY_SSIZE_T_PYFMT "i" -#undef PyIndex_Check -#define constchar const char -#define PyIndex_Check(op) 0 -#else -#define NPY_SSIZE_T_PYFMT "n" -#define constchar char -#endif - -/* NPY_INTP_FMT Note: - * Unlike the other NPY_*_FMT macros which are used with - * PyOS_snprintf, NPY_INTP_FMT is used with PyErr_Format and - * PyString_Format. These functions use different formatting - * codes which are portably specified according to the Python - * documentation. See ticket #1795. - * - * On Windows x64, the LONGLONG formatter should be used, but - * in Python 2.6 the %lld formatter is not supported. In this - * case we work around the problem by using the %zd formatter. - */ -#if NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_INT - #define NPY_INTP NPY_INT - #define NPY_UINTP NPY_UINT - #define PyIntpArrType_Type PyIntArrType_Type - #define PyUIntpArrType_Type PyUIntArrType_Type - #define NPY_MAX_INTP NPY_MAX_INT - #define NPY_MIN_INTP NPY_MIN_INT - #define NPY_MAX_UINTP NPY_MAX_UINT - #define NPY_INTP_FMT "d" -#elif NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONG - #define NPY_INTP NPY_LONG - #define NPY_UINTP NPY_ULONG - #define PyIntpArrType_Type PyLongArrType_Type - #define PyUIntpArrType_Type PyULongArrType_Type - #define NPY_MAX_INTP NPY_MAX_LONG - #define NPY_MIN_INTP MIN_LONG - #define NPY_MAX_UINTP NPY_MAX_ULONG - #define NPY_INTP_FMT "ld" -#elif defined(PY_LONG_LONG) && (NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONGLONG) - #define NPY_INTP NPY_LONGLONG - #define NPY_UINTP NPY_ULONGLONG - #define PyIntpArrType_Type PyLongLongArrType_Type - #define PyUIntpArrType_Type PyULongLongArrType_Type - #define NPY_MAX_INTP NPY_MAX_LONGLONG - #define NPY_MIN_INTP NPY_MIN_LONGLONG - #define NPY_MAX_UINTP NPY_MAX_ULONGLONG - #if (PY_VERSION_HEX >= 0x02070000) - #define NPY_INTP_FMT "lld" - #else - #define NPY_INTP_FMT "zd" - #endif -#endif - -/* - * We can only use C99 formats for npy_int_p if it is the same as - * intp_t, hence the condition on HAVE_UNITPTR_T - */ -#if (NPY_USE_C99_FORMATS) == 1 \ - && (defined HAVE_UINTPTR_T) \ - && (defined HAVE_INTTYPES_H) - #include - #undef NPY_INTP_FMT - #define NPY_INTP_FMT PRIdPTR -#endif #define NPY_ERR(str) fprintf(stderr, #str); fflush(stderr); #define NPY_ERR2(str) fprintf(stderr, str); fflush(stderr); diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 145fe2142..0c92fb494 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -1,7 +1,7 @@ #ifndef _NPY_COMMON_H_ #define _NPY_COMMON_H_ -/* This is auto-generated */ +/* numpconfig.h is auto-generated */ #include "numpyconfig.h" #if defined(_MSC_VER) @@ -23,10 +23,95 @@ enum { NPY_CPU_BIG }; -/* Some platforms don't define bool, long long, or long double. - Handle that here. -*/ +/* + * This is to typedef npy_intp to the appropriate pointer size for + * this platform. Py_intptr_t, Py_uintptr_t are defined in pyport.h. + */ +typedef Py_intptr_t npy_intp; +typedef Py_uintptr_t npy_uintp; +#define NPY_SIZEOF_INTP NPY_SIZEOF_PY_INTPTR_T +#define NPY_SIZEOF_UINTP NPY_SIZEOF_PY_INTPTR_T + +#ifdef constchar +#undef constchar +#endif + +#if (PY_VERSION_HEX < 0x02050000) + #ifndef PY_SSIZE_T_MIN + typedef int Py_ssize_t; + #define PY_SSIZE_T_MAX INT_MAX + #define PY_SSIZE_T_MIN INT_MIN + #endif +#define NPY_SSIZE_T_PYFMT "i" +#undef PyIndex_Check +#define constchar const char +#define PyIndex_Check(op) 0 +#else +#define NPY_SSIZE_T_PYFMT "n" +#define constchar char +#endif + +/* NPY_INTP_FMT Note: + * Unlike the other NPY_*_FMT macros which are used with + * PyOS_snprintf, NPY_INTP_FMT is used with PyErr_Format and + * PyString_Format. These functions use different formatting + * codes which are portably specified according to the Python + * documentation. See ticket #1795. + * + * On Windows x64, the LONGLONG formatter should be used, but + * in Python 2.6 the %lld formatter is not supported. In this + * case we work around the problem by using the %zd formatter. + */ +#if NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_INT + #define NPY_INTP NPY_INT + #define NPY_UINTP NPY_UINT + #define PyIntpArrType_Type PyIntArrType_Type + #define PyUIntpArrType_Type PyUIntArrType_Type + #define NPY_MAX_INTP NPY_MAX_INT + #define NPY_MIN_INTP NPY_MIN_INT + #define NPY_MAX_UINTP NPY_MAX_UINT + #define NPY_INTP_FMT "d" +#elif NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONG + #define NPY_INTP NPY_LONG + #define NPY_UINTP NPY_ULONG + #define PyIntpArrType_Type PyLongArrType_Type + #define PyUIntpArrType_Type PyULongArrType_Type + #define NPY_MAX_INTP NPY_MAX_LONG + #define NPY_MIN_INTP MIN_LONG + #define NPY_MAX_UINTP NPY_MAX_ULONG + #define NPY_INTP_FMT "ld" +#elif defined(PY_LONG_LONG) && (NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONGLONG) + #define NPY_INTP NPY_LONGLONG + #define NPY_UINTP NPY_ULONGLONG + #define PyIntpArrType_Type PyLongLongArrType_Type + #define PyUIntpArrType_Type PyULongLongArrType_Type + #define NPY_MAX_INTP NPY_MAX_LONGLONG + #define NPY_MIN_INTP NPY_MIN_LONGLONG + #define NPY_MAX_UINTP NPY_MAX_ULONGLONG + #if (PY_VERSION_HEX >= 0x02070000) + #define NPY_INTP_FMT "lld" + #else + #define NPY_INTP_FMT "zd" + #endif +#endif + +/* + * We can only use C99 formats for npy_int_p if it is the same as + * intp_t, hence the condition on HAVE_UNITPTR_T + */ +#if (NPY_USE_C99_FORMATS) == 1 \ + && (defined HAVE_UINTPTR_T) \ + && (defined HAVE_INTTYPES_H) + #include + #undef NPY_INTP_FMT + #define NPY_INTP_FMT PRIdPTR +#endif + +/* + * Some platforms don't define bool, long long, or long double. + * Handle that here. + */ #define NPY_BYTE_FMT "hhd" #define NPY_UBYTE_FMT "hhu" #define NPY_SHORT_FMT "hd" diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h index c31155705..c844e9fdf 100644 --- a/numpy/core/include/numpy/npy_sort.h +++ b/numpy/core/include/numpy/npy_sort.h @@ -3,7 +3,7 @@ /* Python include is for future object sorts */ #include -#include +#include int BOOL_aquicksort(npy_bool *v, npy_intp* tosort, npy_intp num, void *NOT_USED); diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 24acc49c8..63aca2847 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -626,7 +626,7 @@ def configuration(parent_package='',top_path=None): def get_mathlib_info(*args): # Another ugly hack: the mathlib info is known once build_src is run, # but we cannot use add_installed_pkg_config here either, so we only - # updated the substition dictionary during npymath build + # update the substition dictionary during npymath build config_cmd = config.get_config_cmd() # Check that the toolchain works, to fail early if it doesn't @@ -654,6 +654,16 @@ def configuration(parent_package='',top_path=None): config.add_npy_pkg_config("mlib.ini.in", "lib/npy-pkg-config", subst_dict) + ####################################################################### + # sort library # + ####################################################################### + + config.add_installed_library('npysort', + sources = [join('src', 'npysort', 'sort.c.src')], + install_dir = 'lib') + config.add_npy_pkg_config("npysort.ini.in", "lib/npy-pkg-config", + subst_dict) + ####################################################################### # sort module # ####################################################################### @@ -661,18 +671,18 @@ def configuration(parent_package='',top_path=None): # _sort version: this function is needed to build foo.c from foo.c.src # when foo.c is included in another file and as such not in the src # argument of build_ext command - def generate_sort_templated_sources(ext, build_dir): - from numpy.distutils.misc_util import get_cmd - - subpath = join('src', 'npysort') - sources = [join(local_dir, subpath, 'sort.c.src')] - - # numpy.distutils generate .c from .c.src in weird directories, we have - # to add them there as they depend on the build_dir - config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') - cmd.ensure_finalized() - cmd.template_sources(sources, ext) +# def generate_sort_templated_sources(ext, build_dir): +# from numpy.distutils.misc_util import get_cmd +# +# subpath = join('src', 'npysort') +# sources = [join(local_dir, subpath, 'sort.c.src')] +# +# # numpy.distutils generate .c from .c.src in weird directories, we have +# # to add them there as they depend on the build_dir +# config.add_include_dirs(join(build_dir, subpath)) +# cmd = get_cmd('build_src') +# cmd.ensure_finalized() +# cmd.template_sources(sources, ext) sort_deps = [ join('src', 'npysort', 'npy_sort_common.h'), @@ -681,7 +691,7 @@ def configuration(parent_package='',top_path=None): sort_src = [ join('src', 'npysort', 'sortmodule.c'), - generate_sort_templated_sources, +# generate_sort_templated_sources, generate_config_h, generate_numpyconfig_h, generate_numpy_api] @@ -689,7 +699,7 @@ def configuration(parent_package='',top_path=None): config.add_extension('_sort', sources = sort_src, depends = deps + sort_deps, - libraries = ['npymath']) + libraries = ['npymath', 'npysort']) ####################################################################### # multiarray module # diff --git a/numpy/core/src/npysort/sortmodule.c b/numpy/core/src/npysort/sortmodule.c index 8bd6ed1fa..f6c90cbe9 100644 --- a/numpy/core/src/npysort/sortmodule.c +++ b/numpy/core/src/npysort/sortmodule.c @@ -26,7 +26,8 @@ * The heap sort is included for completeness. */ -#include "sort.c" +/*#include "sort.c"*/ +#include "numpy/npy_sort.h" static struct PyMethodDef methods[] = { {NULL, NULL, 0, NULL} -- cgit v1.2.1 From d1155a365d755c5b28c383bd2cd45203a8fa060f Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 14 Jun 2011 13:26:09 -0600 Subject: ENH: Move sorting functions into a library. --- numpy/core/include/numpy/npy_sort.h | 83 ++++++++++++++++++------- numpy/core/npysort.ini.in | 18 ++++++ numpy/core/setup.py | 47 +++++++++------ numpy/core/src/dummymodule.c | 46 ++++++++++++++ numpy/core/src/npysort/npy_sort_common.h | 32 +++++++++- numpy/core/src/npysort/sort.c.src | 59 +++++------------- numpy/core/src/npysort/sortmodule.c | 71 ---------------------- numpy/core/src/npysort/sortmodule.c.src | 100 +++++++++++++++++++++++++++++++ 8 files changed, 299 insertions(+), 157 deletions(-) create mode 100644 numpy/core/npysort.ini.in create mode 100644 numpy/core/src/dummymodule.c delete mode 100644 numpy/core/src/npysort/sortmodule.c create mode 100644 numpy/core/src/npysort/sortmodule.c.src (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h index c844e9fdf..60f2b047a 100644 --- a/numpy/core/include/numpy/npy_sort.h +++ b/numpy/core/include/numpy/npy_sort.h @@ -3,126 +3,167 @@ /* Python include is for future object sorts */ #include -#include +#include +#include +int BOOL_quicksort(npy_bool *start, npy_intp num, void *NOT_USED); +int BOOL_heapsort(npy_bool *start, npy_intp n, void *NOT_USED); +int BOOL_mergesort(npy_bool *start, npy_intp num, void *NOT_USED); int BOOL_aquicksort(npy_bool *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int BOOL_aheapsort(npy_bool *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void BOOL_amergesort0(npy_intp *pl, npy_intp *pr, npy_bool *v, npy_intp *pw); int BOOL_amergesort(npy_bool *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int BYTE_quicksort(npy_byte *start, npy_intp num, void *NOT_USED); +int BYTE_heapsort(npy_byte *start, npy_intp n, void *NOT_USED); +int BYTE_mergesort(npy_byte *start, npy_intp num, void *NOT_USED); int BYTE_aquicksort(npy_byte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int BYTE_aheapsort(npy_byte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void BYTE_amergesort0(npy_intp *pl, npy_intp *pr, npy_byte *v, npy_intp *pw); int BYTE_amergesort(npy_byte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int UBYTE_quicksort(npy_ubyte *start, npy_intp num, void *NOT_USED); +int UBYTE_heapsort(npy_ubyte *start, npy_intp n, void *NOT_USED); +int UBYTE_mergesort(npy_ubyte *start, npy_intp num, void *NOT_USED); int UBYTE_aquicksort(npy_ubyte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int UBYTE_aheapsort(npy_ubyte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void UBYTE_amergesort0(npy_intp *pl, npy_intp *pr, npy_ubyte *v, npy_intp *pw); int UBYTE_amergesort(npy_ubyte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int SHORT_quicksort(npy_short *start, npy_intp num, void *NOT_USED); +int SHORT_heapsort(npy_short *start, npy_intp n, void *NOT_USED); +int SHORT_mergesort(npy_short *start, npy_intp num, void *NOT_USED); int SHORT_aquicksort(npy_short *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int SHORT_aheapsort(npy_short *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void SHORT_amergesort0(npy_intp *pl, npy_intp *pr, npy_short *v, npy_intp *pw); int SHORT_amergesort(npy_short *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int USHORT_quicksort(npy_ushort *start, npy_intp num, void *NOT_USED); +int USHORT_heapsort(npy_ushort *start, npy_intp n, void *NOT_USED); +int USHORT_mergesort(npy_ushort *start, npy_intp num, void *NOT_USED); int USHORT_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int USHORT_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void USHORT_amergesort0(npy_intp *pl, npy_intp *pr, npy_ushort *v, npy_intp *pw); int USHORT_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int INT_quicksort(npy_int *start, npy_intp num, void *NOT_USED); +int INT_heapsort(npy_int *start, npy_intp n, void *NOT_USED); +int INT_mergesort(npy_int *start, npy_intp num, void *NOT_USED); int INT_aquicksort(npy_int *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int INT_aheapsort(npy_int *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void INT_amergesort0(npy_intp *pl, npy_intp *pr, npy_int *v, npy_intp *pw); int INT_amergesort(npy_int *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int UINT_quicksort(npy_uint *start, npy_intp num, void *NOT_USED); +int UINT_heapsort(npy_uint *start, npy_intp n, void *NOT_USED); +int UINT_mergesort(npy_uint *start, npy_intp num, void *NOT_USED); int UINT_aquicksort(npy_uint *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int UINT_aheapsort(npy_uint *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void UINT_amergesort0(npy_intp *pl, npy_intp *pr, npy_uint *v, npy_intp *pw); int UINT_amergesort(npy_uint *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int LONG_quicksort(npy_long *start, npy_intp num, void *NOT_USED); +int LONG_heapsort(npy_long *start, npy_intp n, void *NOT_USED); +int LONG_mergesort(npy_long *start, npy_intp num, void *NOT_USED); int LONG_aquicksort(npy_long *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int LONG_aheapsort(npy_long *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void LONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_long *v, npy_intp *pw); int LONG_amergesort(npy_long *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int ULONG_quicksort(npy_ulong *start, npy_intp num, void *NOT_USED); +int ULONG_heapsort(npy_ulong *start, npy_intp n, void *NOT_USED); +int ULONG_mergesort(npy_ulong *start, npy_intp num, void *NOT_USED); int ULONG_aquicksort(npy_ulong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int ULONG_aheapsort(npy_ulong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void ULONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_ulong *v, npy_intp *pw); int ULONG_amergesort(npy_ulong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); int LONGLONG_aquicksort(npy_longlong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int LONGLONG_aheapsort(npy_longlong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void LONGLONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_longlong *v, npy_intp *pw); int LONGLONG_amergesort(npy_longlong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int LONGLONG_quicksort(npy_longlong *start, npy_intp num, void *NOT_USED); +int LONGLONG_heapsort(npy_longlong *start, npy_intp n, void *NOT_USED); +int LONGLONG_mergesort(npy_longlong *start, npy_intp num, void *NOT_USED); +int ULONGLONG_quicksort(npy_ulonglong *start, npy_intp num, void *NOT_USED); +int ULONGLONG_heapsort(npy_ulonglong *start, npy_intp n, void *NOT_USED); +int ULONGLONG_mergesort(npy_ulonglong *start, npy_intp num, void *NOT_USED); int ULONGLONG_aquicksort(npy_ulonglong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int ULONGLONG_aheapsort(npy_ulonglong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void ULONGLONG_amergesort0(npy_intp *pl, npy_intp *pr, npy_ulonglong *v, npy_intp *pw); int ULONGLONG_amergesort(npy_ulonglong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int HALF_quicksort(npy_ushort *start, npy_intp num, void *NOT_USED); +int HALF_heapsort(npy_ushort *start, npy_intp n, void *NOT_USED); +int HALF_mergesort(npy_ushort *start, npy_intp num, void *NOT_USED); int HALF_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int HALF_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void HALF_amergesort0(npy_intp *pl, npy_intp *pr, npy_ushort *v, npy_intp *pw); int HALF_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int FLOAT_quicksort(npy_float *start, npy_intp num, void *NOT_USED); +int FLOAT_heapsort(npy_float *start, npy_intp n, void *NOT_USED); +int FLOAT_mergesort(npy_float *start, npy_intp num, void *NOT_USED); int FLOAT_aquicksort(npy_float *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int FLOAT_aheapsort(npy_float *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void FLOAT_amergesort0(npy_intp *pl, npy_intp *pr, npy_float *v, npy_intp *pw); int FLOAT_amergesort(npy_float *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int DOUBLE_quicksort(npy_double *start, npy_intp num, void *NOT_USED); +int DOUBLE_heapsort(npy_double *start, npy_intp n, void *NOT_USED); +int DOUBLE_mergesort(npy_double *start, npy_intp num, void *NOT_USED); int DOUBLE_aquicksort(npy_double *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int DOUBLE_aheapsort(npy_double *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void DOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_double *v, npy_intp *pw); int DOUBLE_amergesort(npy_double *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int LONGDOUBLE_quicksort(npy_longdouble *start, npy_intp num, void *NOT_USED); +int LONGDOUBLE_heapsort(npy_longdouble *start, npy_intp n, void *NOT_USED); +int LONGDOUBLE_mergesort(npy_longdouble *start, npy_intp num, void *NOT_USED); int LONGDOUBLE_aquicksort(npy_longdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int LONGDOUBLE_aheapsort(npy_longdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void LONGDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_longdouble *v, npy_intp *pw); int LONGDOUBLE_amergesort(npy_longdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int CFLOAT_quicksort(npy_cfloat *start, npy_intp num, void *NOT_USED); +int CFLOAT_heapsort(npy_cfloat *start, npy_intp n, void *NOT_USED); +int CFLOAT_mergesort(npy_cfloat *start, npy_intp num, void *NOT_USED); int CFLOAT_aquicksort(npy_cfloat *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int CFLOAT_aheapsort(npy_cfloat *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void CFLOAT_amergesort0(npy_intp *pl, npy_intp *pr, npy_cfloat *v, npy_intp *pw); int CFLOAT_amergesort(npy_cfloat *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int CDOUBLE_quicksort(npy_cdouble *start, npy_intp num, void *NOT_USED); +int CDOUBLE_heapsort(npy_cdouble *start, npy_intp n, void *NOT_USED); +int CDOUBLE_mergesort(npy_cdouble *start, npy_intp num, void *NOT_USED); int CDOUBLE_aquicksort(npy_cdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int CDOUBLE_aheapsort(npy_cdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void CDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_cdouble *v, npy_intp *pw); int CDOUBLE_amergesort(npy_cdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int CLONGDOUBLE_quicksort(npy_clongdouble *start, npy_intp num, void *NOT_USED); +int CLONGDOUBLE_heapsort(npy_clongdouble *start, npy_intp n, void *NOT_USED); +int CLONGDOUBLE_mergesort(npy_clongdouble *start, npy_intp num, void *NOT_USED); int CLONGDOUBLE_aquicksort(npy_clongdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); int CLONGDOUBLE_aheapsort(npy_clongdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -void CLONGDOUBLE_amergesort0(npy_intp *pl, npy_intp *pr, npy_clongdouble *v, npy_intp *pw); int CLONGDOUBLE_amergesort(npy_clongdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int STRING_mergesort(char *start, npy_intp num, PyArrayObject *arr); +int STRING_quicksort(char *start, npy_intp num, PyArrayObject *arr); +int STRING_heapsort(char *start, npy_intp n, PyArrayObject *arr); int STRING_aheapsort(char *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); int STRING_aquicksort(char *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); -void STRING_amergesort0(npy_intp *pl, npy_intp *pr, char *v, npy_intp *pw, int len); int STRING_amergesort(char *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); +int UNICODE_mergesort(npy_ucs4 *start, npy_intp num, PyArrayObject *arr); +int UNICODE_quicksort(npy_ucs4 *start, npy_intp num, PyArrayObject *arr); +int UNICODE_heapsort(npy_ucs4 *start, npy_intp n, PyArrayObject *arr); int UNICODE_aheapsort(npy_ucs4 *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); int UNICODE_aquicksort(npy_ucs4 *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); -void UNICODE_amergesort0(npy_intp *pl, npy_intp *pr, npy_ucs4 *v, npy_intp *pw, int len); int UNICODE_amergesort(npy_ucs4 *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); #endif diff --git a/numpy/core/npysort.ini.in b/numpy/core/npysort.ini.in new file mode 100644 index 000000000..66e08ccfa --- /dev/null +++ b/numpy/core/npysort.ini.in @@ -0,0 +1,18 @@ +[meta] +Name = npysort +Description = Core sort library for Numpy +Version = 0.1 + +[variables] +pkgname = @pkgname@ +prefix = ${pkgdir} +libdir = ${prefix}@sep@lib +includedir = ${prefix}@sep@include + +[default] +Libs = -L${libdir} -lnpysort +Cflags = -I${includedir} + +[msvc] +Libs = /LIBPATH:${libdir} npysort.lib +Cflags = /INCLUDE:${includedir} diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 63aca2847..0cc04b4a8 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -613,7 +613,7 @@ def configuration(parent_package='',top_path=None): config.add_data_dir('include/numpy/fenv') ####################################################################### - # npymath library # + # dummy module # ####################################################################### # npymath needs the config.h and numpyconfig.h files to be generated, but @@ -622,6 +622,17 @@ def configuration(parent_package='',top_path=None): # explicitly add an extension which has generate_config_h and # generate_numpyconfig_h as sources *before* adding npymath. + config.add_extension('_dummy', + sources = [join('src','dummymodule.c'), + generate_config_h, + generate_numpyconfig_h, + generate_numpy_api] + ) + + ####################################################################### + # npymath library # + ####################################################################### + subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) def get_mathlib_info(*args): # Another ugly hack: the mathlib info is known once build_src is run, @@ -658,6 +669,7 @@ def configuration(parent_package='',top_path=None): # sort library # ####################################################################### + #subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) config.add_installed_library('npysort', sources = [join('src', 'npysort', 'sort.c.src')], install_dir = 'lib') @@ -671,27 +683,26 @@ def configuration(parent_package='',top_path=None): # _sort version: this function is needed to build foo.c from foo.c.src # when foo.c is included in another file and as such not in the src # argument of build_ext command -# def generate_sort_templated_sources(ext, build_dir): -# from numpy.distutils.misc_util import get_cmd -# -# subpath = join('src', 'npysort') -# sources = [join(local_dir, subpath, 'sort.c.src')] -# -# # numpy.distutils generate .c from .c.src in weird directories, we have -# # to add them there as they depend on the build_dir -# config.add_include_dirs(join(build_dir, subpath)) -# cmd = get_cmd('build_src') -# cmd.ensure_finalized() -# cmd.template_sources(sources, ext) + def generate_sort_templated_sources(ext, build_dir): + from numpy.distutils.misc_util import get_cmd + + subpath = join('src', 'npysort') + sources = [join(local_dir, subpath, 'sortmodule.c.src')] + + # numpy.distutils generate .c from .c.src in weird directories, we have + # to add them there as they depend on the build_dir + config.add_include_dirs(join(build_dir, subpath)) + cmd = get_cmd('build_src') + cmd.ensure_finalized() + cmd.template_sources(sources, ext) sort_deps = [ join('src', 'npysort', 'npy_sort_common.h'), - join('src', 'npysort', 'sort.c.src'), - join('src', 'npysort', 'sortmodule.c')] + join('src', 'npysort', 'sort.c.src')] sort_src = [ - join('src', 'npysort', 'sortmodule.c'), -# generate_sort_templated_sources, + join('src', 'npysort', 'sortmodule.c.src'), + generate_sort_templated_sources, generate_config_h, generate_numpyconfig_h, generate_numpy_api] @@ -699,7 +710,7 @@ def configuration(parent_package='',top_path=None): config.add_extension('_sort', sources = sort_src, depends = deps + sort_deps, - libraries = ['npymath', 'npysort']) + libraries = ['npysort']) ####################################################################### # multiarray module # diff --git a/numpy/core/src/dummymodule.c b/numpy/core/src/dummymodule.c new file mode 100644 index 000000000..b4eb735d4 --- /dev/null +++ b/numpy/core/src/dummymodule.c @@ -0,0 +1,46 @@ + +/* -*- c -*- */ + +/* + * This is a dummy module whose purpose is to get distutils to generate the + * configuration files before the libraries are made. + */ + +#include +#include + +static struct PyMethodDef methods[] = { + {NULL, NULL, 0, NULL} +}; + + +#if defined(NPY_PY3K) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "dummy", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +/* Initialization function for the module */ +#if defined(NPY_PY3K) +PyObject *PyInit__dummy(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + return m; +} +#else +PyMODINIT_FUNC +init_sort(void) { + Py_InitModule("_dummy", methods); +} +#endif diff --git a/numpy/core/src/npysort/npy_sort_common.h b/numpy/core/src/npysort/npy_sort_common.h index d4b3a0b3e..c785b5e71 100644 --- a/numpy/core/src/npysort/npy_sort_common.h +++ b/numpy/core/src/npysort/npy_sort_common.h @@ -1,12 +1,13 @@ #ifndef __NPY_SORT_COMMON_H__ #define __NPY_SORT_COMMON_H__ - +/* #include "Python.h" #include "numpy/npy_common.h" #include "numpy/npy_math.h" -#include "numpy/halffloat.h" #include "npy_config.h" +*/ +#include /* @@ -139,6 +140,33 @@ LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) } +NPY_INLINE static int +npy_half_isnan(npy_half h) +{ + return ((h&0x7c00u) == 0x7c00u) && ((h&0x03ffu) != 0x0000u); +} + + +NPY_INLINE static int +npy_half_lt_nonan(npy_half h1, npy_half h2) +{ + if (h1&0x8000u) { + if (h2&0x8000u) { + return (h1&0x7fffu) > (h2&0x7fffu); + } else { + /* Signed zeros are equal, have to check for it */ + return (h1 != 0x8000u) || (h2 != 0x0000u); + } + } else { + if (h2&0x8000u) { + return 0; + } else { + return (h1&0x7fffu) < (h2&0x7fffu); + } + } +} + + NPY_INLINE static int HALF_LT(npy_half a, npy_half b) { diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index 551f73510..90a44a3cd 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -26,6 +26,8 @@ * The heap sort is included for completeness. */ +#include +#include #include "npy_sort_common.h" #define NOT_USED NPY_UNUSED(unused) @@ -54,7 +56,7 @@ */ -static int +int @TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) { @type@ *pl = start; @@ -116,7 +118,7 @@ static int return 0; } -static int +int @TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) { @type@ vp; @@ -182,7 +184,7 @@ static int } -static int +int @TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) { @type@ tmp, *a; @@ -232,7 +234,7 @@ static int return 0; } -static int +int @TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) { npy_intp *a, i,j,l, tmp; @@ -322,7 +324,7 @@ static void } } -static int +int @TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) { @type@ *pl, *pr, *pw; @@ -381,7 +383,7 @@ static void } } -static int +int @TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) { npy_intp *pl, *pr, *pw; @@ -459,7 +461,7 @@ static void } } -static int +int @TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) { const size_t elsize = arr->descr->elsize; @@ -490,7 +492,7 @@ fail_0: return err; } -static int +int @TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) { const size_t len = arr->descr->elsize/sizeof(@type@); @@ -557,7 +559,7 @@ static int } -static int +int @TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); @@ -606,7 +608,7 @@ static int } -static int +int @TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); @@ -655,7 +657,7 @@ static int } -static int +int @TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); @@ -765,7 +767,7 @@ static void } -static int +int @TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) { const size_t elsize = arr->descr->elsize; @@ -785,36 +787,3 @@ static int return 0; } /**end repeat**/ - -/* just for the module bit */ -#include "numpy/noprefix.h" - -static void -add_sortfuncs(void) -{ - PyArray_Descr *descr; - - /**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - */ - descr = PyArray_DescrFromType(PyArray_@TYPE@); - descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)@TYPE@_quicksort; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aquicksort; - descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)@TYPE@_heapsort; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aheapsort; - descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)@TYPE@_mergesort; - descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)@TYPE@_amergesort; - /**end repeat**/ - -} - - diff --git a/numpy/core/src/npysort/sortmodule.c b/numpy/core/src/npysort/sortmodule.c deleted file mode 100644 index f6c90cbe9..000000000 --- a/numpy/core/src/npysort/sortmodule.c +++ /dev/null @@ -1,71 +0,0 @@ -/* -*- c -*- */ - -/* - * The purpose of this module is to add faster sort functions - * that are type-specific. This is done by altering the - * function table for the builtin descriptors. - * - * These sorting functions are copied almost directly from numarray - * with a few modifications (complex comparisons compare the imaginary - * part if the real parts are equal, for example), and the names - * are changed. - * - * The original sorting code is due to Charles R. Harris who wrote - * it for numarray. - */ - -/* - * Quick sort is usually the fastest, but the worst case scenario can - * be slower than the merge and heap sorts. The merge sort requires - * extra memory and so for large arrays may not be useful. - * - * The merge sort is *stable*, meaning that equal components - * are unmoved from their entry versions, so it can be used to - * implement lexigraphic sorting on multiple keys. - * - * The heap sort is included for completeness. - */ - -/*#include "sort.c"*/ -#include "numpy/npy_sort.h" - -static struct PyMethodDef methods[] = { - {NULL, NULL, 0, NULL} -}; - - -#if defined(NPY_PY3K) -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_sort", - NULL, - -1, - methods, - NULL, - NULL, - NULL, - NULL -}; -#endif - -/* Initialization function for the module */ -#if defined(NPY_PY3K) -PyObject *PyInit__sort(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - if (!m) { - return NULL; - } - import_array(); - add_sortfuncs(); - return m; -} -#else -PyMODINIT_FUNC -init_sort(void) { - Py_InitModule("_sort", methods); - - import_array(); - add_sortfuncs(); -} -#endif diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src new file mode 100644 index 000000000..9aabc1100 --- /dev/null +++ b/numpy/core/src/npysort/sortmodule.c.src @@ -0,0 +1,100 @@ +/* -*- c -*- */ + +/* + * The purpose of this module is to add faster sort functions + * that are type-specific. This is done by altering the + * function table for the builtin descriptors. + * + * These sorting functions are copied almost directly from numarray + * with a few modifications (complex comparisons compare the imaginary + * part if the real parts are equal, for example), and the names + * are changed. + * + * The original sorting code is due to Charles R. Harris who wrote + * it for numarray. + */ + +/* + * Quick sort is usually the fastest, but the worst case scenario can + * be slower than the merge and heap sorts. The merge sort requires + * extra memory and so for large arrays may not be useful. + * + * The merge sort is *stable*, meaning that equal components + * are unmoved from their entry versions, so it can be used to + * implement lexigraphic sorting on multiple keys. + * + * The heap sort is included for completeness. + */ + +#include +#include +#include + +static void +add_sortfuncs(void) +{ + PyArray_Descr *descr; + + /**begin repeat + * + * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, + * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, + * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# + */ + descr = PyArray_DescrFromType(PyArray_@TYPE@); + descr->f->sort[PyArray_QUICKSORT] = + (PyArray_SortFunc *)@TYPE@_quicksort; + descr->f->argsort[PyArray_QUICKSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aquicksort; + descr->f->sort[PyArray_HEAPSORT] = + (PyArray_SortFunc *)@TYPE@_heapsort; + descr->f->argsort[PyArray_HEAPSORT] = + (PyArray_ArgSortFunc *)@TYPE@_aheapsort; + descr->f->sort[PyArray_MERGESORT] = + (PyArray_SortFunc *)@TYPE@_mergesort; + descr->f->argsort[PyArray_MERGESORT] = + (PyArray_ArgSortFunc *)@TYPE@_amergesort; + /**end repeat**/ + +} + +static struct PyMethodDef methods[] = { + {NULL, NULL, 0, NULL} +}; + + +#if defined(NPY_PY3K) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_sort", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +/* Initialization function for the module */ +#if defined(NPY_PY3K) +PyObject *PyInit__sort(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + import_array(); + add_sortfuncs(); + return m; +} +#else +PyMODINIT_FUNC +init_sort(void) { + Py_InitModule("_sort", methods); + + import_array(); + add_sortfuncs(); +} +#endif -- cgit v1.2.1 From 16704fd870047d9b43334b7cef5ea98bcf5faeb3 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 14 Jun 2011 14:27:59 -0600 Subject: ENH: Make scons build the npysort library. --- numpy/core/SConscript | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index 844b16776..5532ced7e 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,7 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -sort_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sort.c.src')) +sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', @@ -431,6 +431,23 @@ mlib_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'mlib.ini'), env.Install('$distutils_installdir/lib/npy-pkg-config', mlib_ini) env.Install('$distutils_installdir/lib/npy-pkg-config', npymath_ini) +# npysort core lib +npysort_src = [env.GenerateFromTemplate(pjoin('src', 'npysort', 'sort.c.src'))] +env.DistutilsInstalledStaticExtLibrary("npysort", npysort_src, install_dir='lib') +env.Prepend(LIBS=["npysort"]) +env.Prepend(LIBPATH=["."]) + +subst_dict = {'@prefix@': '$distutils_install_prefix', + '@pkgname@': 'numpy.core', '@sep@': os.path.sep} +npysort_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'npysort.ini'), + 'npysort.ini.in', SUBST_DICT=subst_dict) + +#subst_dict = {'@posix_mathlib@': " ".join(['-l%s' % l for l in mlib]), +# '@msvc_mathlib@': " ".join(['%s.mlib' % l for l in mlib])} +#mlib_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'mlib.ini'), +# 'mlib.ini.in', SUBST_DICT=subst_dict) +env.Install('$distutils_installdir/lib/npy-pkg-config', npysort_ini) + #----------------- # Build multiarray #----------------- @@ -480,7 +497,6 @@ env.DistutilsPythonExtension('multiarray_tests', source=multiarray_tests_src) #------------------ # Build sort module #------------------ -sortmodule_src = [pjoin('src', 'npysort','sortmodule.c')] sort = env.DistutilsPythonExtension('_sort', source = sortmodule_src) #------------------- -- cgit v1.2.1 From 7efe96fc04f969119d1c3c09daddffe64d518b52 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 14 Jun 2011 22:33:58 -0600 Subject: ENH: Rename the sort functions using lower case suffix instead of upper case prefix. The name are now of the form _. --- numpy/core/include/numpy/npy_common.h | 5 +- numpy/core/include/numpy/npy_sort.h | 288 ++++++++++++++++---------------- numpy/core/src/npysort/sort.c.src | 68 ++++---- numpy/core/src/npysort/sortmodule.c.src | 20 ++- 4 files changed, 195 insertions(+), 186 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 0c92fb494..6ca83cc29 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -178,11 +178,12 @@ typedef unsigned int npy_uint; typedef unsigned long npy_ulong; /* These are for completeness */ -typedef float npy_float; -typedef double npy_double; +typedef char npy_char; typedef short npy_short; typedef int npy_int; typedef long npy_long; +typedef float npy_float; +typedef double npy_double; /* * Disabling C99 complex usage: a lot of C code in numpy/scipy rely on being diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h index 60f2b047a..0c0b77bcf 100644 --- a/numpy/core/include/numpy/npy_sort.h +++ b/numpy/core/include/numpy/npy_sort.h @@ -7,163 +7,163 @@ #include -int BOOL_quicksort(npy_bool *start, npy_intp num, void *NOT_USED); -int BOOL_heapsort(npy_bool *start, npy_intp n, void *NOT_USED); -int BOOL_mergesort(npy_bool *start, npy_intp num, void *NOT_USED); -int BOOL_aquicksort(npy_bool *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int BOOL_aheapsort(npy_bool *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int BOOL_amergesort(npy_bool *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); +int heapsort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); +int mergesort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); +int aquicksort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int BYTE_quicksort(npy_byte *start, npy_intp num, void *NOT_USED); -int BYTE_heapsort(npy_byte *start, npy_intp n, void *NOT_USED); -int BYTE_mergesort(npy_byte *start, npy_intp num, void *NOT_USED); -int BYTE_aquicksort(npy_byte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int BYTE_aheapsort(npy_byte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int BYTE_amergesort(npy_byte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int UBYTE_quicksort(npy_ubyte *start, npy_intp num, void *NOT_USED); -int UBYTE_heapsort(npy_ubyte *start, npy_intp n, void *NOT_USED); -int UBYTE_mergesort(npy_ubyte *start, npy_intp num, void *NOT_USED); -int UBYTE_aquicksort(npy_ubyte *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int UBYTE_aheapsort(npy_ubyte *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int UBYTE_amergesort(npy_ubyte *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); +int heapsort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); +int mergesort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); +int aquicksort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int heapsort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int mergesort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int aquicksort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int SHORT_quicksort(npy_short *start, npy_intp num, void *NOT_USED); -int SHORT_heapsort(npy_short *start, npy_intp n, void *NOT_USED); -int SHORT_mergesort(npy_short *start, npy_intp num, void *NOT_USED); -int SHORT_aquicksort(npy_short *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int SHORT_aheapsort(npy_short *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int SHORT_amergesort(npy_short *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int USHORT_quicksort(npy_ushort *start, npy_intp num, void *NOT_USED); -int USHORT_heapsort(npy_ushort *start, npy_intp n, void *NOT_USED); -int USHORT_mergesort(npy_ushort *start, npy_intp num, void *NOT_USED); -int USHORT_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int USHORT_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int USHORT_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_short(npy_short *vec, npy_intp cnt, void *null); +int heapsort_npy_short(npy_short *vec, npy_intp cnt, void *null); +int mergesort_npy_short(npy_short *vec, npy_intp cnt, void *null); +int aquicksort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int INT_quicksort(npy_int *start, npy_intp num, void *NOT_USED); -int INT_heapsort(npy_int *start, npy_intp n, void *NOT_USED); -int INT_mergesort(npy_int *start, npy_intp num, void *NOT_USED); -int INT_aquicksort(npy_int *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int INT_aheapsort(npy_int *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int INT_amergesort(npy_int *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int UINT_quicksort(npy_uint *start, npy_intp num, void *NOT_USED); -int UINT_heapsort(npy_uint *start, npy_intp n, void *NOT_USED); -int UINT_mergesort(npy_uint *start, npy_intp num, void *NOT_USED); -int UINT_aquicksort(npy_uint *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int UINT_aheapsort(npy_uint *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int UINT_amergesort(npy_uint *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int LONG_quicksort(npy_long *start, npy_intp num, void *NOT_USED); -int LONG_heapsort(npy_long *start, npy_intp n, void *NOT_USED); -int LONG_mergesort(npy_long *start, npy_intp num, void *NOT_USED); -int LONG_aquicksort(npy_long *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int LONG_aheapsort(npy_long *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int LONG_amergesort(npy_long *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int ULONG_quicksort(npy_ulong *start, npy_intp num, void *NOT_USED); -int ULONG_heapsort(npy_ulong *start, npy_intp n, void *NOT_USED); -int ULONG_mergesort(npy_ulong *start, npy_intp num, void *NOT_USED); -int ULONG_aquicksort(npy_ulong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int ULONG_aheapsort(npy_ulong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int ULONG_amergesort(npy_ulong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int LONGLONG_aquicksort(npy_longlong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int LONGLONG_aheapsort(npy_longlong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int LONGLONG_amergesort(npy_longlong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); -int LONGLONG_quicksort(npy_longlong *start, npy_intp num, void *NOT_USED); -int LONGLONG_heapsort(npy_longlong *start, npy_intp n, void *NOT_USED); -int LONGLONG_mergesort(npy_longlong *start, npy_intp num, void *NOT_USED); - - -int ULONGLONG_quicksort(npy_ulonglong *start, npy_intp num, void *NOT_USED); -int ULONGLONG_heapsort(npy_ulonglong *start, npy_intp n, void *NOT_USED); -int ULONGLONG_mergesort(npy_ulonglong *start, npy_intp num, void *NOT_USED); -int ULONGLONG_aquicksort(npy_ulonglong *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int ULONGLONG_aheapsort(npy_ulonglong *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int ULONGLONG_amergesort(npy_ulonglong *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int HALF_quicksort(npy_ushort *start, npy_intp num, void *NOT_USED); -int HALF_heapsort(npy_ushort *start, npy_intp n, void *NOT_USED); -int HALF_mergesort(npy_ushort *start, npy_intp num, void *NOT_USED); -int HALF_aquicksort(npy_ushort *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int HALF_aheapsort(npy_ushort *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int HALF_amergesort(npy_ushort *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int FLOAT_quicksort(npy_float *start, npy_intp num, void *NOT_USED); -int FLOAT_heapsort(npy_float *start, npy_intp n, void *NOT_USED); -int FLOAT_mergesort(npy_float *start, npy_intp num, void *NOT_USED); -int FLOAT_aquicksort(npy_float *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int FLOAT_aheapsort(npy_float *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int FLOAT_amergesort(npy_float *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_int(npy_int *vec, npy_intp cnt, void *null); +int heapsort_npy_int(npy_int *vec, npy_intp cnt, void *null); +int mergesort_npy_int(npy_int *vec, npy_intp cnt, void *null); +int aquicksort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); +int heapsort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); +int mergesort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); +int aquicksort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_long(npy_long *vec, npy_intp cnt, void *null); +int heapsort_npy_long(npy_long *vec, npy_intp cnt, void *null); +int mergesort_npy_long(npy_long *vec, npy_intp cnt, void *null); +int aquicksort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int heapsort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int mergesort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int aquicksort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int heapsort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int mergesort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int aquicksort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int heapsort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int mergesort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int aquicksort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_float(npy_float *vec, npy_intp cnt, void *null); +int heapsort_npy_float(npy_float *vec, npy_intp cnt, void *null); +int mergesort_npy_float(npy_float *vec, npy_intp cnt, void *null); +int aquicksort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int DOUBLE_quicksort(npy_double *start, npy_intp num, void *NOT_USED); -int DOUBLE_heapsort(npy_double *start, npy_intp n, void *NOT_USED); -int DOUBLE_mergesort(npy_double *start, npy_intp num, void *NOT_USED); -int DOUBLE_aquicksort(npy_double *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int DOUBLE_aheapsort(npy_double *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int DOUBLE_amergesort(npy_double *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int LONGDOUBLE_quicksort(npy_longdouble *start, npy_intp num, void *NOT_USED); -int LONGDOUBLE_heapsort(npy_longdouble *start, npy_intp n, void *NOT_USED); -int LONGDOUBLE_mergesort(npy_longdouble *start, npy_intp num, void *NOT_USED); -int LONGDOUBLE_aquicksort(npy_longdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int LONGDOUBLE_aheapsort(npy_longdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int LONGDOUBLE_amergesort(npy_longdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_double(npy_double *vec, npy_intp cnt, void *null); +int heapsort_npy_double(npy_double *vec, npy_intp cnt, void *null); +int mergesort_npy_double(npy_double *vec, npy_intp cnt, void *null); +int aquicksort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int heapsort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int mergesort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int aquicksort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int CFLOAT_quicksort(npy_cfloat *start, npy_intp num, void *NOT_USED); -int CFLOAT_heapsort(npy_cfloat *start, npy_intp n, void *NOT_USED); -int CFLOAT_mergesort(npy_cfloat *start, npy_intp num, void *NOT_USED); -int CFLOAT_aquicksort(npy_cfloat *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int CFLOAT_aheapsort(npy_cfloat *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int CFLOAT_amergesort(npy_cfloat *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int CDOUBLE_quicksort(npy_cdouble *start, npy_intp num, void *NOT_USED); -int CDOUBLE_heapsort(npy_cdouble *start, npy_intp n, void *NOT_USED); -int CDOUBLE_mergesort(npy_cdouble *start, npy_intp num, void *NOT_USED); -int CDOUBLE_aquicksort(npy_cdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int CDOUBLE_aheapsort(npy_cdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int CDOUBLE_amergesort(npy_cdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); +int quicksort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int heapsort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int mergesort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int aquicksort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int heapsort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int mergesort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int aquicksort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int CLONGDOUBLE_quicksort(npy_clongdouble *start, npy_intp num, void *NOT_USED); -int CLONGDOUBLE_heapsort(npy_clongdouble *start, npy_intp n, void *NOT_USED); -int CLONGDOUBLE_mergesort(npy_clongdouble *start, npy_intp num, void *NOT_USED); -int CLONGDOUBLE_aquicksort(npy_clongdouble *v, npy_intp* tosort, npy_intp num, void *NOT_USED); -int CLONGDOUBLE_aheapsort(npy_clongdouble *v, npy_intp *tosort, npy_intp n, void *NOT_USED); -int CLONGDOUBLE_amergesort(npy_clongdouble *v, npy_intp *tosort, npy_intp num, void *NOT_USED); - - -int STRING_mergesort(char *start, npy_intp num, PyArrayObject *arr); -int STRING_quicksort(char *start, npy_intp num, PyArrayObject *arr); -int STRING_heapsort(char *start, npy_intp n, PyArrayObject *arr); -int STRING_aheapsort(char *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); -int STRING_aquicksort(char *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); -int STRING_amergesort(char *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); +int quicksort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int heapsort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int mergesort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int aquicksort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int UNICODE_mergesort(npy_ucs4 *start, npy_intp num, PyArrayObject *arr); -int UNICODE_quicksort(npy_ucs4 *start, npy_intp num, PyArrayObject *arr); -int UNICODE_heapsort(npy_ucs4 *start, npy_intp n, PyArrayObject *arr); -int UNICODE_aheapsort(npy_ucs4 *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr); -int UNICODE_aquicksort(npy_ucs4 *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr); -int UNICODE_amergesort(npy_ucs4 *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr); +int quicksort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); #endif diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index 90a44a3cd..3e2f320d4 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -49,15 +49,18 @@ * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE# + * #suff = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble# * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, * npy_cdouble, npy_clongdouble# */ - int -@TYPE@_quicksort(@type@ *start, npy_intp num, void *NOT_USED) +quicksort_@suff@(@type@ *start, npy_intp num, void *NOT_USED) { @type@ *pl = start; @type@ *pr = start + num - 1; @@ -119,7 +122,7 @@ int } int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) +aquicksort_@suff@(@type@ *v, npy_intp* tosort, npy_intp num, void *NOT_USED) { @type@ vp; npy_intp *pl, *pr; @@ -185,7 +188,7 @@ int int -@TYPE@_heapsort(@type@ *start, npy_intp n, void *NOT_USED) +heapsort_@suff@(@type@ *start, npy_intp n, void *NOT_USED) { @type@ tmp, *a; npy_intp i,j,l; @@ -235,7 +238,7 @@ int } int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) +aheapsort_@suff@(@type@ *v, npy_intp *tosort, npy_intp n, void *NOT_USED) { npy_intp *a, i,j,l, tmp; /* The arrays need to be offset by one for heapsort indexing */ @@ -283,22 +286,22 @@ int } static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw) +mergesort0_@suff@(@type@ *pl, @type@ *pr, @type@ *pw) { @type@ vp, *pi, *pj, *pk, *pm; if (pr - pl > SMALL_MERGESORT) { /* merge sort */ pm = pl + ((pr - pl) >> 1); - @TYPE@_mergesort0(pl, pm, pw); - @TYPE@_mergesort0(pm, pr, pw); + mergesort0_@suff@(pl, pm, pw); + mergesort0_@suff@(pm, pr, pw); for (pi = pw, pj = pl; pj < pm;) { *pi++ = *pj++; } pj = pw; pk = pl; while (pj < pi && pm < pr) { - if (@TYPE@_LT(*pm,*pj)) { + if (@TYPE@_LT(*pm, *pj)) { *pk = *pm++; } else { @@ -325,7 +328,7 @@ static void } int -@TYPE@_mergesort(@type@ *start, npy_intp num, void *NOT_USED) +mergesort_@suff@(@type@ *start, npy_intp num, void *NOT_USED) { @type@ *pl, *pr, *pw; @@ -336,14 +339,14 @@ int PyErr_NoMemory(); return -1; } - @TYPE@_mergesort0(pl, pr, pw); + mergesort0_@suff@(pl, pr, pw); PyDataMem_FREE(pw); return 0; } static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) +amergesort0_@suff@(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw) { @type@ vp; npy_intp vi, *pi, *pj, *pk, *pm; @@ -351,13 +354,13 @@ static void if (pr - pl > SMALL_MERGESORT) { /* merge sort */ pm = pl + ((pr - pl + 1)>>1); - @TYPE@_amergesort0(pl,pm-1,v,pw); - @TYPE@_amergesort0(pm,pr,v,pw); + amergesort0_@suff@(pl, pm-1, v, pw); + amergesort0_@suff@(pm, pr, v, pw); for (pi = pw, pj = pl; pj < pm; ++pi, ++pj) { *pi = *pj; } for (pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) { - if (@TYPE@_LT(v[*pj],v[*pk])) { + if (@TYPE@_LT(v[*pj], v[*pk])) { *pm = *pj; ++pj; } @@ -384,7 +387,7 @@ static void } int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) +amergesort_@suff@(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) { npy_intp *pl, *pr, *pw; @@ -396,7 +399,7 @@ int return -1; } - @TYPE@_amergesort0(pl, pr, v, pw); + amergesort0_@suff@(pl, pr, v, pw); PyDimMem_FREE(pw); return 0; @@ -415,19 +418,20 @@ int /**begin repeat * * #TYPE = STRING, UNICODE# - * #type = char, npy_ucs4# + * #suff = npy_string, npy_unicode# + * #type = npy_char, npy_ucs4# */ static void -@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) +mergesort0_@suff@(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) { @type@ *pi, *pj, *pk, *pm; if ((size_t)(pr - pl) > SMALL_MERGESORT*len) { /* merge sort */ pm = pl + (((pr - pl)/len) >> 1)*len; - @TYPE@_mergesort0(pl, pm, pw, vp, len); - @TYPE@_mergesort0(pm, pr, pw, vp, len); + mergesort0_@suff@(pl, pm, pw, vp, len); + mergesort0_@suff@(pm, pr, pw, vp, len); @TYPE@_COPY(pw, pl, pm - pl); pi = pw + (pm - pl); pj = pw; @@ -462,7 +466,7 @@ static void } int -@TYPE@_mergesort(@type@ *start, npy_intp num, PyArrayObject *arr) +mergesort_@suff@(@type@ *start, npy_intp num, PyArrayObject *arr) { const size_t elsize = arr->descr->elsize; const size_t len = elsize / sizeof(@type@); @@ -483,7 +487,7 @@ int err = -1; goto fail_1; } - @TYPE@_mergesort0(pl, pr, pw, vp, len); + mergesort0_@suff@(pl, pr, pw, vp, len); PyDataMem_FREE(vp); fail_1: @@ -493,7 +497,7 @@ fail_0: } int -@TYPE@_quicksort(@type@ *start, npy_intp num, PyArrayObject *arr) +quicksort_@suff@(@type@ *start, npy_intp num, PyArrayObject *arr) { const size_t len = arr->descr->elsize/sizeof(@type@); @type@ *vp = malloc(arr->descr->elsize); @@ -560,7 +564,7 @@ int int -@TYPE@_heapsort(@type@ *start, npy_intp n, PyArrayObject *arr) +heapsort_@suff@(@type@ *start, npy_intp n, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); @type@ *tmp = malloc(arr->descr->elsize); @@ -609,7 +613,7 @@ int int -@TYPE@_aheapsort(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) +aheapsort_@suff@(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); npy_intp *a, i,j,l, tmp; @@ -658,7 +662,7 @@ int int -@TYPE@_aquicksort(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) +aquicksort_@suff@(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) { size_t len = arr->descr->elsize/sizeof(@type@); @type@ *vp; @@ -725,7 +729,7 @@ int static void -@TYPE@_amergesort0(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) +amergesort0_@suff@(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) { @type@ *vp; npy_intp vi, *pi, *pj, *pk, *pm; @@ -733,8 +737,8 @@ static void if (pr - pl > SMALL_MERGESORT) { /* merge sort */ pm = pl + ((pr - pl) >> 1); - @TYPE@_amergesort0(pl,pm,v,pw,len); - @TYPE@_amergesort0(pm,pr,v,pw,len); + amergesort0_@suff@(pl, pm, v, pw, len); + amergesort0_@suff@(pm, pr, v, pw, len); for (pi = pw, pj = pl; pj < pm;) { *pi++ = *pj++; } @@ -768,7 +772,7 @@ static void int -@TYPE@_amergesort(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) +amergesort_@suff@(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) { const size_t elsize = arr->descr->elsize; const size_t len = elsize / sizeof(@type@); @@ -781,7 +785,7 @@ int PyErr_NoMemory(); return -1; } - @TYPE@_amergesort0(pl, pr, v, pw, len); + amergesort0_@suff@(pl, pr, v, pw, len); PyDimMem_FREE(pw); return 0; diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src index 9aabc1100..6f59b0cf9 100644 --- a/numpy/core/src/npysort/sortmodule.c.src +++ b/numpy/core/src/npysort/sortmodule.c.src @@ -40,20 +40,24 @@ add_sortfuncs(void) * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# + * #suff = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, + * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, + * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, + * npy_cdouble, npy_clongdouble, npy_string, npy_unicode# */ descr = PyArray_DescrFromType(PyArray_@TYPE@); descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)@TYPE@_quicksort; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aquicksort; + (PyArray_SortFunc *)quicksort_@suff@; descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)@TYPE@_heapsort; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)@TYPE@_aheapsort; + (PyArray_SortFunc *)heapsort_@suff@; descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)@TYPE@_mergesort; + (PyArray_SortFunc *)mergesort_@suff@; + descr->f->argsort[PyArray_QUICKSORT] = + (PyArray_ArgSortFunc *)aquicksort_@suff@; + descr->f->argsort[PyArray_HEAPSORT] = + (PyArray_ArgSortFunc *)aheapsort_@suff@; descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)@TYPE@_amergesort; + (PyArray_ArgSortFunc *)amergesort_@suff@; /**end repeat**/ } -- cgit v1.2.1 From 1c3671c0890d8ec4edbe0314ce1ccb1474c7c9b8 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 16 Jun 2011 18:59:40 -0600 Subject: STY: Rename sort functions again, omit the npy_ bit from the suffix. --- numpy/core/include/numpy/npy_sort.h | 288 ++++++++++++++++---------------- numpy/core/src/npysort/sort.c.src | 9 +- numpy/core/src/npysort/sortmodule.c.src | 7 +- 3 files changed, 151 insertions(+), 153 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h index 0c0b77bcf..94d831f86 100644 --- a/numpy/core/include/numpy/npy_sort.h +++ b/numpy/core/include/numpy/npy_sort.h @@ -7,163 +7,163 @@ #include -int quicksort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); -int heapsort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); -int mergesort_npy_bool(npy_bool *vec, npy_intp cnt, void *null); -int aquicksort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_bool(npy_bool *vec, npy_intp cnt, void *null); +int heapsort_bool(npy_bool *vec, npy_intp cnt, void *null); +int mergesort_bool(npy_bool *vec, npy_intp cnt, void *null); +int aquicksort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); -int heapsort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); -int mergesort_npy_byte(npy_byte *vec, npy_intp cnt, void *null); -int aquicksort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int heapsort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int mergesort_npy_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int aquicksort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_byte(npy_byte *vec, npy_intp cnt, void *null); +int heapsort_byte(npy_byte *vec, npy_intp cnt, void *null); +int mergesort_byte(npy_byte *vec, npy_intp cnt, void *null); +int aquicksort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int heapsort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int mergesort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int aquicksort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_short(npy_short *vec, npy_intp cnt, void *null); -int heapsort_npy_short(npy_short *vec, npy_intp cnt, void *null); -int mergesort_npy_short(npy_short *vec, npy_intp cnt, void *null); -int aquicksort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int heapsort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int mergesort_npy_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int aquicksort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_short(npy_short *vec, npy_intp cnt, void *null); +int heapsort_short(npy_short *vec, npy_intp cnt, void *null); +int mergesort_short(npy_short *vec, npy_intp cnt, void *null); +int aquicksort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_int(npy_int *vec, npy_intp cnt, void *null); -int heapsort_npy_int(npy_int *vec, npy_intp cnt, void *null); -int mergesort_npy_int(npy_int *vec, npy_intp cnt, void *null); -int aquicksort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); -int heapsort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); -int mergesort_npy_uint(npy_uint *vec, npy_intp cnt, void *null); -int aquicksort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_long(npy_long *vec, npy_intp cnt, void *null); -int heapsort_npy_long(npy_long *vec, npy_intp cnt, void *null); -int mergesort_npy_long(npy_long *vec, npy_intp cnt, void *null); -int aquicksort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int heapsort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int mergesort_npy_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int aquicksort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int heapsort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int mergesort_npy_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int aquicksort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int heapsort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int mergesort_npy_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int aquicksort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); -int heapsort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); -int mergesort_npy_half(npy_ushort *vec, npy_intp cnt, void *null); -int aquicksort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_float(npy_float *vec, npy_intp cnt, void *null); -int heapsort_npy_float(npy_float *vec, npy_intp cnt, void *null); -int mergesort_npy_float(npy_float *vec, npy_intp cnt, void *null); -int aquicksort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_int(npy_int *vec, npy_intp cnt, void *null); +int heapsort_int(npy_int *vec, npy_intp cnt, void *null); +int mergesort_int(npy_int *vec, npy_intp cnt, void *null); +int aquicksort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_uint(npy_uint *vec, npy_intp cnt, void *null); +int heapsort_uint(npy_uint *vec, npy_intp cnt, void *null); +int mergesort_uint(npy_uint *vec, npy_intp cnt, void *null); +int aquicksort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_long(npy_long *vec, npy_intp cnt, void *null); +int heapsort_long(npy_long *vec, npy_intp cnt, void *null); +int mergesort_long(npy_long *vec, npy_intp cnt, void *null); +int aquicksort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int heapsort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int mergesort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int aquicksort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int heapsort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int mergesort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int aquicksort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int heapsort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int mergesort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int aquicksort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_half(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_half(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_half(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_float(npy_float *vec, npy_intp cnt, void *null); +int heapsort_float(npy_float *vec, npy_intp cnt, void *null); +int mergesort_float(npy_float *vec, npy_intp cnt, void *null); +int aquicksort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_double(npy_double *vec, npy_intp cnt, void *null); -int heapsort_npy_double(npy_double *vec, npy_intp cnt, void *null); -int mergesort_npy_double(npy_double *vec, npy_intp cnt, void *null); -int aquicksort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int heapsort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int mergesort_npy_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int aquicksort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_double(npy_double *vec, npy_intp cnt, void *null); +int heapsort_double(npy_double *vec, npy_intp cnt, void *null); +int mergesort_double(npy_double *vec, npy_intp cnt, void *null); +int aquicksort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int heapsort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int mergesort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int aquicksort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int heapsort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int mergesort_npy_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int aquicksort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int heapsort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int mergesort_npy_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int aquicksort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int quicksort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int heapsort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int mergesort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int aquicksort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int heapsort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int mergesort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int aquicksort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int quicksort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int heapsort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int mergesort_npy_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int aquicksort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_npy_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int heapsort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int mergesort_npy_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int aquicksort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int aheapsort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int amergesort_npy_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int quicksort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int heapsort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int mergesort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int aquicksort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int quicksort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int heapsort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int mergesort_npy_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int aquicksort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int aheapsort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int amergesort_npy_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int quicksort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); #endif diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index 3e2f320d4..bb276b826 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -49,10 +49,9 @@ * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE# - * #suff = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble# + * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, + * longlong, ulonglong, half, float, double, longdouble, + * cfloat, cdouble, clongdouble# * #type = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, * npy_ushort, npy_float, npy_double, npy_longdouble, npy_cfloat, @@ -418,7 +417,7 @@ amergesort_@suff@(@type@ *v, npy_intp *tosort, npy_intp num, void *NOT_USED) /**begin repeat * * #TYPE = STRING, UNICODE# - * #suff = npy_string, npy_unicode# + * #suff = string, unicode# * #type = npy_char, npy_ucs4# */ diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src index 6f59b0cf9..def51fe10 100644 --- a/numpy/core/src/npysort/sortmodule.c.src +++ b/numpy/core/src/npysort/sortmodule.c.src @@ -40,10 +40,9 @@ add_sortfuncs(void) * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - * #suff = npy_bool, npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, - * npy_uint, npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_half, npy_float, npy_double, npy_longdouble, npy_cfloat, - * npy_cdouble, npy_clongdouble, npy_string, npy_unicode# + * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, + * longlong, ulonglong, half, float, double, longdouble, + * cfloat, cdouble, clongdouble, string, unicode# */ descr = PyArray_DescrFromType(PyArray_@TYPE@); descr->f->sort[PyArray_QUICKSORT] = -- cgit v1.2.1 From 165c087f21ada3377353014ada41227da1e34d4f Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 16 Jun 2011 19:39:34 -0600 Subject: ENH: Directly initialize arraytypes with the sorting functions in the sort library. --- numpy/core/__init__.py | 2 +- numpy/core/setup.py | 2 +- numpy/core/src/multiarray/arraytypes.c.src | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index 4a9f3ac75..dc6f86c20 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -7,7 +7,7 @@ import umath import _internal # for freeze programs import numerictypes as nt multiarray.set_typeDict(nt.sctypeDict) -import _sort +#import _sort from numeric import * from fromnumeric import * import defchararray as char diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 0cc04b4a8..c5667bea9 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -817,7 +817,7 @@ def configuration(parent_package='',top_path=None): join(codegen_dir,'generate_numpy_api.py'), join('*.py')], depends = deps + multiarray_deps, - libraries = ['npymath']) + libraries = ['npymath', 'npysort']) ####################################################################### # umath module # diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index e4dab1f7e..dc8d493f3 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -12,6 +12,7 @@ #include "numpy/npy_math.h" #include "numpy/halffloat.h" +#include "numpy/npy_sort.h" #include "common.h" #include "ctors.h" @@ -3259,6 +3260,8 @@ static int /**begin repeat * * #from = VOID, STRING, UNICODE# + * #suff = void, string, unicode# + * #sort = 0, 1, 1# * #align = char, char, PyArray_UCS4# * #NAME = Void, String, Unicode# * #endian = |, |, =# @@ -3299,12 +3302,21 @@ static PyArray_ArrFuncs _Py@NAME@_ArrFuncs = { (PyArray_NonzeroFunc*)@from@_nonzero, (PyArray_FillFunc*)NULL, (PyArray_FillWithScalarFunc*)NULL, +#if @sort@ + { + quicksort_@suff@, heapsort_@suff@, mergesort_@suff@ + }, + { + aquicksort_@suff@, aheapsort_@suff@, amergesort_@suff@ + }, +#else { NULL, NULL, NULL }, { NULL, NULL, NULL }, +#endif NULL, (PyArray_ScalarKindFunc*)NULL, NULL, @@ -3341,6 +3353,10 @@ static PyArray_Descr @from@_Descr = { * #from = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE, OBJECT, DATETIME, TIMEDELTA# + * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, + * longlong, ulonglong, half, float, double, longdouble, + * cfloat, cdouble, clongdouble, object, datetime, timedelta# + * #sort = 1*18, 0*3# * #num = 1*15, 2*3, 1*3# * #fromtyp = Bool, byte, ubyte, short, ushort, int, uint, long, ulong, * longlong, ulonglong, npy_half, float, double, longdouble, @@ -3390,12 +3406,21 @@ static PyArray_ArrFuncs _Py@NAME@_ArrFuncs = { (PyArray_NonzeroFunc*)@from@_nonzero, (PyArray_FillFunc*)@from@_fill, (PyArray_FillWithScalarFunc*)@from@_fillwithscalar, +#if @sort@ + { + quicksort_@suff@, heapsort_@suff@, mergesort_@suff@ + }, + { + aquicksort_@suff@, aheapsort_@suff@, amergesort_@suff@ + }, +#else { NULL, NULL, NULL }, { NULL, NULL, NULL }, +#endif NULL, (PyArray_ScalarKindFunc*)NULL, NULL, -- cgit v1.2.1 From 4c7f8dfe94c0599514970d474b77e9bd99c36e97 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 16 Jun 2011 19:57:29 -0600 Subject: ENH: Remove the _sort module. --- numpy/core/SConscript | 12 +- numpy/core/__init__.py | 1 - numpy/core/setup.py | 38 +--- numpy/core/src/npysort/npy_sort_common.h | 334 ------------------------------- numpy/core/src/npysort/npysort_common.h | 334 +++++++++++++++++++++++++++++++ numpy/core/src/npysort/sort.c.src | 2 +- numpy/core/src/npysort/sortmodule.c.src | 103 ---------- 7 files changed, 337 insertions(+), 487 deletions(-) delete mode 100644 numpy/core/src/npysort/npy_sort_common.h create mode 100644 numpy/core/src/npysort/npysort_common.h delete mode 100644 numpy/core/src/npysort/sortmodule.c.src (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index 5532ced7e..e059a1353 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,7 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) +#sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', @@ -441,11 +441,6 @@ subst_dict = {'@prefix@': '$distutils_install_prefix', '@pkgname@': 'numpy.core', '@sep@': os.path.sep} npysort_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'npysort.ini'), 'npysort.ini.in', SUBST_DICT=subst_dict) - -#subst_dict = {'@posix_mathlib@': " ".join(['-l%s' % l for l in mlib]), -# '@msvc_mathlib@': " ".join(['%s.mlib' % l for l in mlib])} -#mlib_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'mlib.ini'), -# 'mlib.ini.in', SUBST_DICT=subst_dict) env.Install('$distutils_installdir/lib/npy-pkg-config', npysort_ini) #----------------- @@ -494,11 +489,6 @@ else: multiarray = env.DistutilsPythonExtension('multiarray', source = multiarray_src) env.DistutilsPythonExtension('multiarray_tests', source=multiarray_tests_src) -#------------------ -# Build sort module -#------------------ -sort = env.DistutilsPythonExtension('_sort', source = sortmodule_src) - #------------------- # Build umath module #------------------- diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index dc6f86c20..fa7df13d8 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -7,7 +7,6 @@ import umath import _internal # for freeze programs import numerictypes as nt multiarray.set_typeDict(nt.sctypeDict) -#import _sort from numeric import * from fromnumeric import * import defchararray as char diff --git a/numpy/core/setup.py b/numpy/core/setup.py index c5667bea9..1a596b5e9 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -666,7 +666,7 @@ def configuration(parent_package='',top_path=None): subst_dict) ####################################################################### - # sort library # + # npysort library # ####################################################################### #subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) @@ -676,42 +676,6 @@ def configuration(parent_package='',top_path=None): config.add_npy_pkg_config("npysort.ini.in", "lib/npy-pkg-config", subst_dict) - ####################################################################### - # sort module # - ####################################################################### - - # _sort version: this function is needed to build foo.c from foo.c.src - # when foo.c is included in another file and as such not in the src - # argument of build_ext command - def generate_sort_templated_sources(ext, build_dir): - from numpy.distutils.misc_util import get_cmd - - subpath = join('src', 'npysort') - sources = [join(local_dir, subpath, 'sortmodule.c.src')] - - # numpy.distutils generate .c from .c.src in weird directories, we have - # to add them there as they depend on the build_dir - config.add_include_dirs(join(build_dir, subpath)) - cmd = get_cmd('build_src') - cmd.ensure_finalized() - cmd.template_sources(sources, ext) - - sort_deps = [ - join('src', 'npysort', 'npy_sort_common.h'), - join('src', 'npysort', 'sort.c.src')] - - sort_src = [ - join('src', 'npysort', 'sortmodule.c.src'), - generate_sort_templated_sources, - generate_config_h, - generate_numpyconfig_h, - generate_numpy_api] - - config.add_extension('_sort', - sources = sort_src, - depends = deps + sort_deps, - libraries = ['npysort']) - ####################################################################### # multiarray module # ####################################################################### diff --git a/numpy/core/src/npysort/npy_sort_common.h b/numpy/core/src/npysort/npy_sort_common.h deleted file mode 100644 index c785b5e71..000000000 --- a/numpy/core/src/npysort/npy_sort_common.h +++ /dev/null @@ -1,334 +0,0 @@ -#ifndef __NPY_SORT_COMMON_H__ -#define __NPY_SORT_COMMON_H__ - -/* -#include "Python.h" -#include "numpy/npy_common.h" -#include "numpy/npy_math.h" -#include "npy_config.h" -*/ -#include - - -/* - ***************************************************************************** - ** SWAP MACROS ** - ***************************************************************************** - */ - -#define BOOL_SWAP(a,b) {npy_bool tmp = (b); (b)=(a); (a) = tmp;} -#define BYTE_SWAP(a,b) {npy_byte tmp = (b); (b)=(a); (a) = tmp;} -#define UBYTE_SWAP(a,b) {npy_ubyte tmp = (b); (b)=(a); (a) = tmp;} -#define SHORT_SWAP(a,b) {npy_short tmp = (b); (b)=(a); (a) = tmp;} -#define USHORT_SWAP(a,b) {npy_ushort tmp = (b); (b)=(a); (a) = tmp;} -#define INT_SWAP(a,b) {npy_int tmp = (b); (b)=(a); (a) = tmp;} -#define UINT_SWAP(a,b) {npy_uint tmp = (b); (b)=(a); (a) = tmp;} -#define LONG_SWAP(a,b) {npy_long tmp = (b); (b)=(a); (a) = tmp;} -#define ULONG_SWAP(a,b) {npy_ulong tmp = (b); (b)=(a); (a) = tmp;} -#define LONGLONG_SWAP(a,b) {npy_longlong tmp = (b); (b)=(a); (a) = tmp;} -#define ULONGLONG_SWAP(a,b) {npy_ulonglong tmp = (b); (b)=(a); (a) = tmp;} -#define HALF_SWAP(a,b) {npy_half tmp = (b); (b)=(a); (a) = tmp;} -#define FLOAT_SWAP(a,b) {npy_float tmp = (b); (b)=(a); (a) = tmp;} -#define DOUBLE_SWAP(a,b) {npy_double tmp = (b); (b)=(a); (a) = tmp;} -#define LONGDOUBLE_SWAP(a,b) {npy_longdouble tmp = (b); (b)=(a); (a) = tmp;} -#define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} -#define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} -#define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} -#define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} - -/* - ***************************************************************************** - ** COMPARISON FUNCTIONS ** - ***************************************************************************** - */ - -NPY_INLINE static int -BOOL_LT(npy_bool a, npy_bool b) -{ - return a < b; -} - - -NPY_INLINE static int -BYTE_LT(npy_byte a, npy_byte b) -{ - return a < b; -} - - -NPY_INLINE static int -UBYTE_LT(npy_ubyte a, npy_ubyte b) -{ - return a < b; -} - - -NPY_INLINE static int -SHORT_LT(npy_short a, npy_short b) -{ - return a < b; -} - - -NPY_INLINE static int -USHORT_LT(npy_ushort a, npy_ushort b) -{ - return a < b; -} - - -NPY_INLINE static int -INT_LT(npy_int a, npy_int b) -{ - return a < b; -} - - -NPY_INLINE static int -UINT_LT(npy_uint a, npy_uint b) -{ - return a < b; -} - - -NPY_INLINE static int -LONG_LT(npy_long a, npy_long b) -{ - return a < b; -} - - -NPY_INLINE static int -ULONG_LT(npy_ulong a, npy_ulong b) -{ - return a < b; -} - - -NPY_INLINE static int -LONGLONG_LT(npy_longlong a, npy_longlong b) -{ - return a < b; -} - - -NPY_INLINE static int -ULONGLONG_LT(npy_ulonglong a, npy_ulonglong b) -{ - return a < b; -} - - -NPY_INLINE static int -FLOAT_LT(npy_float a, npy_float b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -DOUBLE_LT(npy_double a, npy_double b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) -{ - return a < b || (b != b && a == a); -} - - -NPY_INLINE static int -npy_half_isnan(npy_half h) -{ - return ((h&0x7c00u) == 0x7c00u) && ((h&0x03ffu) != 0x0000u); -} - - -NPY_INLINE static int -npy_half_lt_nonan(npy_half h1, npy_half h2) -{ - if (h1&0x8000u) { - if (h2&0x8000u) { - return (h1&0x7fffu) > (h2&0x7fffu); - } else { - /* Signed zeros are equal, have to check for it */ - return (h1 != 0x8000u) || (h2 != 0x0000u); - } - } else { - if (h2&0x8000u) { - return 0; - } else { - return (h1&0x7fffu) < (h2&0x7fffu); - } - } -} - - -NPY_INLINE static int -HALF_LT(npy_half a, npy_half b) -{ - int ret; - - if (npy_half_isnan(b)) { - ret = !npy_half_isnan(a); - } else { - ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); - } - - return ret; -} - -/* - * For inline functions SUN recommends not using a return in the then part - * of an if statement. It's a SUN compiler thing, so assign the return value - * to a variable instead. - */ -NPY_INLINE static int -CFLOAT_LT(npy_cfloat a, npy_cfloat b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -NPY_INLINE static int -CDOUBLE_LT(npy_cdouble a, npy_cdouble b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -NPY_INLINE static int -CLONGDOUBLE_LT(npy_clongdouble a, npy_clongdouble b) -{ - int ret; - - if (a.real < b.real) { - ret = a.imag == a.imag || b.imag != b.imag; - } - else if (a.real > b.real) { - ret = b.imag != b.imag && a.imag == a.imag; - } - else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { - ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); - } - else { - ret = b.real != b.real; - } - - return ret; -} - - -/* The PyObject functions are stubs for later use */ -NPY_INLINE static int -PyObject_LT(PyObject *pa, PyObject *pb) -{ - return 0; -} - - -NPY_INLINE static void -STRING_COPY(char *s1, char *s2, size_t len) -{ - memcpy(s1, s2, len); -} - - -NPY_INLINE static void -STRING_SWAP(char *s1, char *s2, size_t len) -{ - while(len--) { - const char t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -STRING_LT(char *s1, char *s2, size_t len) -{ - const unsigned char *c1 = (unsigned char *)s1; - const unsigned char *c2 = (unsigned char *)s2; - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (c1[i] != c2[i]) { - ret = c1[i] < c2[i]; - break; - } - } - return ret; -} - - -NPY_INLINE static void -UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - *s1++ = *s2++; - } -} - - -NPY_INLINE static void -UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - while(len--) { - const npy_ucs4 t = *s1; - *s1++ = *s2; - *s2++ = t; - } -} - - -NPY_INLINE static int -UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) -{ - size_t i; - int ret = 0; - - for (i = 0; i < len; ++i) { - if (s1[i] != s2[i]) { - ret = s1[i] < s2[i]; - break; - } - } - return ret; -} - -#endif diff --git a/numpy/core/src/npysort/npysort_common.h b/numpy/core/src/npysort/npysort_common.h new file mode 100644 index 000000000..c785b5e71 --- /dev/null +++ b/numpy/core/src/npysort/npysort_common.h @@ -0,0 +1,334 @@ +#ifndef __NPY_SORT_COMMON_H__ +#define __NPY_SORT_COMMON_H__ + +/* +#include "Python.h" +#include "numpy/npy_common.h" +#include "numpy/npy_math.h" +#include "npy_config.h" +*/ +#include + + +/* + ***************************************************************************** + ** SWAP MACROS ** + ***************************************************************************** + */ + +#define BOOL_SWAP(a,b) {npy_bool tmp = (b); (b)=(a); (a) = tmp;} +#define BYTE_SWAP(a,b) {npy_byte tmp = (b); (b)=(a); (a) = tmp;} +#define UBYTE_SWAP(a,b) {npy_ubyte tmp = (b); (b)=(a); (a) = tmp;} +#define SHORT_SWAP(a,b) {npy_short tmp = (b); (b)=(a); (a) = tmp;} +#define USHORT_SWAP(a,b) {npy_ushort tmp = (b); (b)=(a); (a) = tmp;} +#define INT_SWAP(a,b) {npy_int tmp = (b); (b)=(a); (a) = tmp;} +#define UINT_SWAP(a,b) {npy_uint tmp = (b); (b)=(a); (a) = tmp;} +#define LONG_SWAP(a,b) {npy_long tmp = (b); (b)=(a); (a) = tmp;} +#define ULONG_SWAP(a,b) {npy_ulong tmp = (b); (b)=(a); (a) = tmp;} +#define LONGLONG_SWAP(a,b) {npy_longlong tmp = (b); (b)=(a); (a) = tmp;} +#define ULONGLONG_SWAP(a,b) {npy_ulonglong tmp = (b); (b)=(a); (a) = tmp;} +#define HALF_SWAP(a,b) {npy_half tmp = (b); (b)=(a); (a) = tmp;} +#define FLOAT_SWAP(a,b) {npy_float tmp = (b); (b)=(a); (a) = tmp;} +#define DOUBLE_SWAP(a,b) {npy_double tmp = (b); (b)=(a); (a) = tmp;} +#define LONGDOUBLE_SWAP(a,b) {npy_longdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} +#define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} +#define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} +#define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} + +/* + ***************************************************************************** + ** COMPARISON FUNCTIONS ** + ***************************************************************************** + */ + +NPY_INLINE static int +BOOL_LT(npy_bool a, npy_bool b) +{ + return a < b; +} + + +NPY_INLINE static int +BYTE_LT(npy_byte a, npy_byte b) +{ + return a < b; +} + + +NPY_INLINE static int +UBYTE_LT(npy_ubyte a, npy_ubyte b) +{ + return a < b; +} + + +NPY_INLINE static int +SHORT_LT(npy_short a, npy_short b) +{ + return a < b; +} + + +NPY_INLINE static int +USHORT_LT(npy_ushort a, npy_ushort b) +{ + return a < b; +} + + +NPY_INLINE static int +INT_LT(npy_int a, npy_int b) +{ + return a < b; +} + + +NPY_INLINE static int +UINT_LT(npy_uint a, npy_uint b) +{ + return a < b; +} + + +NPY_INLINE static int +LONG_LT(npy_long a, npy_long b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONG_LT(npy_ulong a, npy_ulong b) +{ + return a < b; +} + + +NPY_INLINE static int +LONGLONG_LT(npy_longlong a, npy_longlong b) +{ + return a < b; +} + + +NPY_INLINE static int +ULONGLONG_LT(npy_ulonglong a, npy_ulonglong b) +{ + return a < b; +} + + +NPY_INLINE static int +FLOAT_LT(npy_float a, npy_float b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +DOUBLE_LT(npy_double a, npy_double b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +LONGDOUBLE_LT(npy_longdouble a, npy_longdouble b) +{ + return a < b || (b != b && a == a); +} + + +NPY_INLINE static int +npy_half_isnan(npy_half h) +{ + return ((h&0x7c00u) == 0x7c00u) && ((h&0x03ffu) != 0x0000u); +} + + +NPY_INLINE static int +npy_half_lt_nonan(npy_half h1, npy_half h2) +{ + if (h1&0x8000u) { + if (h2&0x8000u) { + return (h1&0x7fffu) > (h2&0x7fffu); + } else { + /* Signed zeros are equal, have to check for it */ + return (h1 != 0x8000u) || (h2 != 0x0000u); + } + } else { + if (h2&0x8000u) { + return 0; + } else { + return (h1&0x7fffu) < (h2&0x7fffu); + } + } +} + + +NPY_INLINE static int +HALF_LT(npy_half a, npy_half b) +{ + int ret; + + if (npy_half_isnan(b)) { + ret = !npy_half_isnan(a); + } else { + ret = !npy_half_isnan(a) && npy_half_lt_nonan(a, b); + } + + return ret; +} + +/* + * For inline functions SUN recommends not using a return in the then part + * of an if statement. It's a SUN compiler thing, so assign the return value + * to a variable instead. + */ +NPY_INLINE static int +CFLOAT_LT(npy_cfloat a, npy_cfloat b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CDOUBLE_LT(npy_cdouble a, npy_cdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +NPY_INLINE static int +CLONGDOUBLE_LT(npy_clongdouble a, npy_clongdouble b) +{ + int ret; + + if (a.real < b.real) { + ret = a.imag == a.imag || b.imag != b.imag; + } + else if (a.real > b.real) { + ret = b.imag != b.imag && a.imag == a.imag; + } + else if (a.real == b.real || (a.real != a.real && b.real != b.real)) { + ret = a.imag < b.imag || (b.imag != b.imag && a.imag == a.imag); + } + else { + ret = b.real != b.real; + } + + return ret; +} + + +/* The PyObject functions are stubs for later use */ +NPY_INLINE static int +PyObject_LT(PyObject *pa, PyObject *pb) +{ + return 0; +} + + +NPY_INLINE static void +STRING_COPY(char *s1, char *s2, size_t len) +{ + memcpy(s1, s2, len); +} + + +NPY_INLINE static void +STRING_SWAP(char *s1, char *s2, size_t len) +{ + while(len--) { + const char t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +STRING_LT(char *s1, char *s2, size_t len) +{ + const unsigned char *c1 = (unsigned char *)s1; + const unsigned char *c2 = (unsigned char *)s2; + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (c1[i] != c2[i]) { + ret = c1[i] < c2[i]; + break; + } + } + return ret; +} + + +NPY_INLINE static void +UNICODE_COPY(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + *s1++ = *s2++; + } +} + + +NPY_INLINE static void +UNICODE_SWAP(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + while(len--) { + const npy_ucs4 t = *s1; + *s1++ = *s2; + *s2++ = t; + } +} + + +NPY_INLINE static int +UNICODE_LT(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +{ + size_t i; + int ret = 0; + + for (i = 0; i < len; ++i) { + if (s1[i] != s2[i]) { + ret = s1[i] < s2[i]; + break; + } + } + return ret; +} + +#endif diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index bb276b826..212002c77 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -28,7 +28,7 @@ #include #include -#include "npy_sort_common.h" +#include "npysort_common.h" #define NOT_USED NPY_UNUSED(unused) #define PYA_QS_STACK 100 diff --git a/numpy/core/src/npysort/sortmodule.c.src b/numpy/core/src/npysort/sortmodule.c.src deleted file mode 100644 index def51fe10..000000000 --- a/numpy/core/src/npysort/sortmodule.c.src +++ /dev/null @@ -1,103 +0,0 @@ -/* -*- c -*- */ - -/* - * The purpose of this module is to add faster sort functions - * that are type-specific. This is done by altering the - * function table for the builtin descriptors. - * - * These sorting functions are copied almost directly from numarray - * with a few modifications (complex comparisons compare the imaginary - * part if the real parts are equal, for example), and the names - * are changed. - * - * The original sorting code is due to Charles R. Harris who wrote - * it for numarray. - */ - -/* - * Quick sort is usually the fastest, but the worst case scenario can - * be slower than the merge and heap sorts. The merge sort requires - * extra memory and so for large arrays may not be useful. - * - * The merge sort is *stable*, meaning that equal components - * are unmoved from their entry versions, so it can be used to - * implement lexigraphic sorting on multiple keys. - * - * The heap sort is included for completeness. - */ - -#include -#include -#include - -static void -add_sortfuncs(void) -{ - PyArray_Descr *descr; - - /**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE# - * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong, half, float, double, longdouble, - * cfloat, cdouble, clongdouble, string, unicode# - */ - descr = PyArray_DescrFromType(PyArray_@TYPE@); - descr->f->sort[PyArray_QUICKSORT] = - (PyArray_SortFunc *)quicksort_@suff@; - descr->f->sort[PyArray_HEAPSORT] = - (PyArray_SortFunc *)heapsort_@suff@; - descr->f->sort[PyArray_MERGESORT] = - (PyArray_SortFunc *)mergesort_@suff@; - descr->f->argsort[PyArray_QUICKSORT] = - (PyArray_ArgSortFunc *)aquicksort_@suff@; - descr->f->argsort[PyArray_HEAPSORT] = - (PyArray_ArgSortFunc *)aheapsort_@suff@; - descr->f->argsort[PyArray_MERGESORT] = - (PyArray_ArgSortFunc *)amergesort_@suff@; - /**end repeat**/ - -} - -static struct PyMethodDef methods[] = { - {NULL, NULL, 0, NULL} -}; - - -#if defined(NPY_PY3K) -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_sort", - NULL, - -1, - methods, - NULL, - NULL, - NULL, - NULL -}; -#endif - -/* Initialization function for the module */ -#if defined(NPY_PY3K) -PyObject *PyInit__sort(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - if (!m) { - return NULL; - } - import_array(); - add_sortfuncs(); - return m; -} -#else -PyMODINIT_FUNC -init_sort(void) { - Py_InitModule("_sort", methods); - - import_array(); - add_sortfuncs(); -} -#endif -- cgit v1.2.1 From bdb5ed82298f170abaf14393843538a8f3af9d36 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Fri, 17 Jun 2011 19:30:09 -0600 Subject: ENH: Make npy_sort.h private until the proper interface gets sorted out. --- numpy/core/include/numpy/npy_sort.h | 169 ----------------------------- numpy/core/src/multiarray/arraytypes.c.src | 6 +- numpy/core/src/npysort/sort.c.src | 2 +- numpy/core/src/private/npy_sort.h | 169 +++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 174 deletions(-) delete mode 100644 numpy/core/include/numpy/npy_sort.h create mode 100644 numpy/core/src/private/npy_sort.h (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_sort.h b/numpy/core/include/numpy/npy_sort.h deleted file mode 100644 index 94d831f86..000000000 --- a/numpy/core/include/numpy/npy_sort.h +++ /dev/null @@ -1,169 +0,0 @@ -#ifndef __NPY_SORT_H__ -#define __NPY_SORT_H__ - -/* Python include is for future object sorts */ -#include -#include -#include - - -int quicksort_bool(npy_bool *vec, npy_intp cnt, void *null); -int heapsort_bool(npy_bool *vec, npy_intp cnt, void *null); -int mergesort_bool(npy_bool *vec, npy_intp cnt, void *null); -int aquicksort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_byte(npy_byte *vec, npy_intp cnt, void *null); -int heapsort_byte(npy_byte *vec, npy_intp cnt, void *null); -int mergesort_byte(npy_byte *vec, npy_intp cnt, void *null); -int aquicksort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int heapsort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int mergesort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); -int aquicksort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_short(npy_short *vec, npy_intp cnt, void *null); -int heapsort_short(npy_short *vec, npy_intp cnt, void *null); -int mergesort_short(npy_short *vec, npy_intp cnt, void *null); -int aquicksort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int heapsort_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int mergesort_ushort(npy_ushort *vec, npy_intp cnt, void *null); -int aquicksort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_int(npy_int *vec, npy_intp cnt, void *null); -int heapsort_int(npy_int *vec, npy_intp cnt, void *null); -int mergesort_int(npy_int *vec, npy_intp cnt, void *null); -int aquicksort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_uint(npy_uint *vec, npy_intp cnt, void *null); -int heapsort_uint(npy_uint *vec, npy_intp cnt, void *null); -int mergesort_uint(npy_uint *vec, npy_intp cnt, void *null); -int aquicksort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_long(npy_long *vec, npy_intp cnt, void *null); -int heapsort_long(npy_long *vec, npy_intp cnt, void *null); -int mergesort_long(npy_long *vec, npy_intp cnt, void *null); -int aquicksort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int heapsort_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int mergesort_ulong(npy_ulong *vec, npy_intp cnt, void *null); -int aquicksort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int heapsort_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int mergesort_longlong(npy_longlong *vec, npy_intp cnt, void *null); -int aquicksort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int heapsort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int mergesort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); -int aquicksort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_half(npy_ushort *vec, npy_intp cnt, void *null); -int heapsort_half(npy_ushort *vec, npy_intp cnt, void *null); -int mergesort_half(npy_ushort *vec, npy_intp cnt, void *null); -int aquicksort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_float(npy_float *vec, npy_intp cnt, void *null); -int heapsort_float(npy_float *vec, npy_intp cnt, void *null); -int mergesort_float(npy_float *vec, npy_intp cnt, void *null); -int aquicksort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_double(npy_double *vec, npy_intp cnt, void *null); -int heapsort_double(npy_double *vec, npy_intp cnt, void *null); -int mergesort_double(npy_double *vec, npy_intp cnt, void *null); -int aquicksort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int heapsort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int mergesort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); -int aquicksort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int heapsort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int mergesort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); -int aquicksort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int heapsort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int mergesort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); -int aquicksort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int heapsort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int mergesort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); -int aquicksort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int aheapsort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); -int amergesort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); - - -int quicksort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int heapsort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int mergesort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); -int aquicksort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int aheapsort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int amergesort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); - - -int quicksort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int heapsort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int mergesort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); -int aquicksort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int aheapsort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); -int amergesort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); - -#endif diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index dc8d493f3..28ce33605 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -7,17 +7,15 @@ #define NPY_NO_PREFIX #include "numpy/arrayobject.h" #include "numpy/arrayscalars.h" - #include "numpy/npy_3kcompat.h" - #include "numpy/npy_math.h" #include "numpy/halffloat.h" -#include "numpy/npy_sort.h" +#include "npy_config.h" +#include "npy_sort.h" #include "common.h" #include "ctors.h" #include "usertypes.h" -#include "npy_config.h" #include "_datetime.h" #include "numpyos.h" diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index 212002c77..24f25db8a 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -27,7 +27,7 @@ */ #include -#include +#include "npy_sort.h" #include "npysort_common.h" #define NOT_USED NPY_UNUSED(unused) diff --git a/numpy/core/src/private/npy_sort.h b/numpy/core/src/private/npy_sort.h new file mode 100644 index 000000000..94d831f86 --- /dev/null +++ b/numpy/core/src/private/npy_sort.h @@ -0,0 +1,169 @@ +#ifndef __NPY_SORT_H__ +#define __NPY_SORT_H__ + +/* Python include is for future object sorts */ +#include +#include +#include + + +int quicksort_bool(npy_bool *vec, npy_intp cnt, void *null); +int heapsort_bool(npy_bool *vec, npy_intp cnt, void *null); +int mergesort_bool(npy_bool *vec, npy_intp cnt, void *null); +int aquicksort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_bool(npy_bool *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_byte(npy_byte *vec, npy_intp cnt, void *null); +int heapsort_byte(npy_byte *vec, npy_intp cnt, void *null); +int mergesort_byte(npy_byte *vec, npy_intp cnt, void *null); +int aquicksort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_byte(npy_byte *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int heapsort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int mergesort_ubyte(npy_ubyte *vec, npy_intp cnt, void *null); +int aquicksort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ubyte(npy_ubyte *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_short(npy_short *vec, npy_intp cnt, void *null); +int heapsort_short(npy_short *vec, npy_intp cnt, void *null); +int mergesort_short(npy_short *vec, npy_intp cnt, void *null); +int aquicksort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_short(npy_short *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_ushort(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ushort(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_int(npy_int *vec, npy_intp cnt, void *null); +int heapsort_int(npy_int *vec, npy_intp cnt, void *null); +int mergesort_int(npy_int *vec, npy_intp cnt, void *null); +int aquicksort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_int(npy_int *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_uint(npy_uint *vec, npy_intp cnt, void *null); +int heapsort_uint(npy_uint *vec, npy_intp cnt, void *null); +int mergesort_uint(npy_uint *vec, npy_intp cnt, void *null); +int aquicksort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_uint(npy_uint *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_long(npy_long *vec, npy_intp cnt, void *null); +int heapsort_long(npy_long *vec, npy_intp cnt, void *null); +int mergesort_long(npy_long *vec, npy_intp cnt, void *null); +int aquicksort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_long(npy_long *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int heapsort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int mergesort_ulong(npy_ulong *vec, npy_intp cnt, void *null); +int aquicksort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ulong(npy_ulong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int heapsort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int mergesort_longlong(npy_longlong *vec, npy_intp cnt, void *null); +int aquicksort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_longlong(npy_longlong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int heapsort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int mergesort_ulonglong(npy_ulonglong *vec, npy_intp cnt, void *null); +int aquicksort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_ulonglong(npy_ulonglong *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_half(npy_ushort *vec, npy_intp cnt, void *null); +int heapsort_half(npy_ushort *vec, npy_intp cnt, void *null); +int mergesort_half(npy_ushort *vec, npy_intp cnt, void *null); +int aquicksort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_half(npy_ushort *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_float(npy_float *vec, npy_intp cnt, void *null); +int heapsort_float(npy_float *vec, npy_intp cnt, void *null); +int mergesort_float(npy_float *vec, npy_intp cnt, void *null); +int aquicksort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_float(npy_float *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_double(npy_double *vec, npy_intp cnt, void *null); +int heapsort_double(npy_double *vec, npy_intp cnt, void *null); +int mergesort_double(npy_double *vec, npy_intp cnt, void *null); +int aquicksort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_double(npy_double *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int heapsort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int mergesort_longdouble(npy_longdouble *vec, npy_intp cnt, void *null); +int aquicksort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_longdouble(npy_longdouble *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int heapsort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int mergesort_cfloat(npy_cfloat *vec, npy_intp cnt, void *null); +int aquicksort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_cfloat(npy_cfloat *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int heapsort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int mergesort_cdouble(npy_cdouble *vec, npy_intp cnt, void *null); +int aquicksort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_cdouble(npy_cdouble *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int heapsort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int mergesort_clongdouble(npy_clongdouble *vec, npy_intp cnt, void *null); +int aquicksort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int aheapsort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); +int amergesort_clongdouble(npy_clongdouble *vec, npy_intp *ind, npy_intp cnt, void *null); + + +int quicksort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_string(npy_char *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_string(npy_char *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); + + +int quicksort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int heapsort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int mergesort_unicode(npy_ucs4 *vec, npy_intp cnt, PyArrayObject *arr); +int aquicksort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int aheapsort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); +int amergesort_unicode(npy_ucs4 *vec, npy_intp *ind, npy_intp cnt, PyArrayObject *arr); + +#endif -- cgit v1.2.1 From e4d790f005331e5ff59d54b9954ab56ec847bafc Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 19 Jun 2011 19:26:32 -0600 Subject: ENH: Generate the npysort library for linking during build, but do not install it. --- numpy/core/SConscript | 9 +-------- numpy/core/npysort.ini.in | 18 ------------------ numpy/core/setup.py | 9 ++++----- 3 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 numpy/core/npysort.ini.in (limited to 'numpy') diff --git a/numpy/core/SConscript b/numpy/core/SConscript index e059a1353..c57549c20 100644 --- a/numpy/core/SConscript +++ b/numpy/core/SConscript @@ -384,7 +384,6 @@ nditer_src = env.GenerateFromTemplate( lowlevel_strided_loops_src = env.GenerateFromTemplate( pjoin('src', 'multiarray', 'lowlevel_strided_loops.c.src')) einsum_src = env.GenerateFromTemplate(pjoin('src', 'multiarray', 'einsum.c.src')) -#sortmodule_src = env.GenerateFromTemplate(pjoin('src', 'npysort','sortmodule.c.src')) umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umath', 'umathmodule.c.src')) umath_tests_src = env.GenerateFromTemplate(pjoin('src', 'umath', @@ -433,16 +432,10 @@ env.Install('$distutils_installdir/lib/npy-pkg-config', npymath_ini) # npysort core lib npysort_src = [env.GenerateFromTemplate(pjoin('src', 'npysort', 'sort.c.src'))] -env.DistutilsInstalledStaticExtLibrary("npysort", npysort_src, install_dir='lib') +env.StaticExtLibrary("npysort", npysort_src) env.Prepend(LIBS=["npysort"]) env.Prepend(LIBPATH=["."]) -subst_dict = {'@prefix@': '$distutils_install_prefix', - '@pkgname@': 'numpy.core', '@sep@': os.path.sep} -npysort_ini = env.SubstInFile(pjoin('lib', 'npy-pkg-config', 'npysort.ini'), - 'npysort.ini.in', SUBST_DICT=subst_dict) -env.Install('$distutils_installdir/lib/npy-pkg-config', npysort_ini) - #----------------- # Build multiarray #----------------- diff --git a/numpy/core/npysort.ini.in b/numpy/core/npysort.ini.in deleted file mode 100644 index 66e08ccfa..000000000 --- a/numpy/core/npysort.ini.in +++ /dev/null @@ -1,18 +0,0 @@ -[meta] -Name = npysort -Description = Core sort library for Numpy -Version = 0.1 - -[variables] -pkgname = @pkgname@ -prefix = ${pkgdir} -libdir = ${prefix}@sep@lib -includedir = ${prefix}@sep@include - -[default] -Libs = -L${libdir} -lnpysort -Cflags = -I${includedir} - -[msvc] -Libs = /LIBPATH:${libdir} npysort.lib -Cflags = /INCLUDE:${includedir} diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 1a596b5e9..92f96c233 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -670,11 +670,10 @@ def configuration(parent_package='',top_path=None): ####################################################################### #subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) - config.add_installed_library('npysort', - sources = [join('src', 'npysort', 'sort.c.src')], - install_dir = 'lib') - config.add_npy_pkg_config("npysort.ini.in", "lib/npy-pkg-config", - subst_dict) + config.add_library('npysort', + sources = [join('src', 'npysort', 'sort.c.src')]) +# config.add_npy_pkg_config("npysort.ini.in", "lib/npy-pkg-config", +# subst_dict) ####################################################################### # multiarray module # -- cgit v1.2.1 From c60425a59a845d998a98d2f17a728db4563e5824 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Mon, 20 Jun 2011 19:32:08 -0600 Subject: STY: Delete commented out lines and add some comments. --- numpy/core/setup.py | 6 ++---- numpy/core/src/npysort/npysort_common.h | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 92f96c233..f95107ae8 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -613,7 +613,7 @@ def configuration(parent_package='',top_path=None): config.add_data_dir('include/numpy/fenv') ####################################################################### - # dummy module # + # dummy module # ####################################################################### # npymath needs the config.h and numpyconfig.h files to be generated, but @@ -669,11 +669,9 @@ def configuration(parent_package='',top_path=None): # npysort library # ####################################################################### - #subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) + # This library is created for the build but it is not installed config.add_library('npysort', sources = [join('src', 'npysort', 'sort.c.src')]) -# config.add_npy_pkg_config("npysort.ini.in", "lib/npy-pkg-config", -# subst_dict) ####################################################################### # multiarray module # diff --git a/numpy/core/src/npysort/npysort_common.h b/numpy/core/src/npysort/npysort_common.h index c785b5e71..bc1d278b0 100644 --- a/numpy/core/src/npysort/npysort_common.h +++ b/numpy/core/src/npysort/npysort_common.h @@ -34,6 +34,8 @@ #define CFLOAT_SWAP(a,b) {npy_cfloat tmp = (b); (b)=(a); (a) = tmp;} #define CDOUBLE_SWAP(a,b) {npy_cdouble tmp = (b); (b)=(a); (a) = tmp;} #define CLONGDOUBLE_SWAP(a,b) {npy_clongdouble tmp = (b); (b)=(a); (a) = tmp;} + +/* Need this for the argsort functions */ #define INTP_SWAP(a,b) {npy_intp tmp = (b); (b)=(a); (a) = tmp;} /* -- cgit v1.2.1