diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/setup.py | 2 | ||||
-rw-r--r-- | numpy/core/src/common/npy_partition.h | 27 | ||||
-rw-r--r-- | numpy/core/src/common/npy_partition.h.src | 134 | ||||
-rw-r--r-- | numpy/core/src/npysort/selection.cpp | 335 |
4 files changed, 114 insertions, 384 deletions
diff --git a/numpy/core/setup.py b/numpy/core/setup.py index c5465196f..f406c006f 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -954,7 +954,7 @@ def configuration(parent_package='',top_path=None): join('src', 'npysort', 'timsort.cpp'), join('src', 'npysort', 'heapsort.cpp'), join('src', 'npysort', 'radixsort.cpp'), - join('src', 'common', 'npy_partition.h.src'), + join('src', 'common', 'npy_partition.h'), join('src', 'npysort', 'selection.cpp'), join('src', 'common', 'npy_binsearch.h'), join('src', 'npysort', 'binsearch.cpp'), diff --git a/numpy/core/src/common/npy_partition.h b/numpy/core/src/common/npy_partition.h new file mode 100644 index 000000000..85a0727c5 --- /dev/null +++ b/numpy/core/src/common/npy_partition.h @@ -0,0 +1,27 @@ +#ifndef NUMPY_CORE_SRC_COMMON_PARTITION_H_ +#define NUMPY_CORE_SRC_COMMON_PARTITION_H_ + +#include "npy_sort.h" + +/* Python include is for future object sorts */ +#include <Python.h> + +#include <numpy/ndarraytypes.h> +#include <numpy/npy_common.h> + +#define NPY_MAX_PIVOT_STACK 50 + +#ifdef __cplusplus +extern "C" { +#endif + +NPY_NO_EXPORT PyArray_PartitionFunc * +get_partition_func(int type, NPY_SELECTKIND which); +NPY_NO_EXPORT PyArray_ArgPartitionFunc * +get_argpartition_func(int type, NPY_SELECTKIND which); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/numpy/core/src/common/npy_partition.h.src b/numpy/core/src/common/npy_partition.h.src deleted file mode 100644 index 5ba652b41..000000000 --- a/numpy/core/src/common/npy_partition.h.src +++ /dev/null @@ -1,134 +0,0 @@ -/* - ***************************************************************************** - ** IMPORTANT NOTE for npy_partition.h.src -> npy_partition.h ** - ***************************************************************************** - * The template file loops.h.src is not automatically converted into - * loops.h by the build system. If you edit this file, you must manually - * do the conversion using numpy/distutils/conv_template.py from the - * command line as follows: - * - * $ cd <NumPy source root directory> - * $ python numpy/distutils/conv_template.py numpy/core/src/private/npy_partition.h.src - * $ - */ - - -#ifndef __NPY_PARTITION_H__ -#define __NPY_PARTITION_H__ - - -#include "npy_sort.h" - -/* Python include is for future object sorts */ -#include <Python.h> -#include <numpy/npy_common.h> -#include <numpy/ndarraytypes.h> - -#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) - -#define NPY_MAX_PIVOT_STACK 50 - -#ifdef __cplusplus -extern "C" { -#endif - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, 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, - * npy_cdouble, npy_clongdouble# - */ - -NPY_NO_EXPORT int introselect_@suff@(@type@ *v, npy_intp num, - npy_intp kth, - npy_intp * pivots, - npy_intp * npiv, - void *NOT_USED); -NPY_NO_EXPORT int aintroselect_@suff@(@type@ *v, npy_intp* tosort, npy_intp num, - npy_intp kth, - npy_intp * pivots, - npy_intp * npiv, - void *NOT_USED); - - -/**end repeat**/ - -#ifdef __cplusplus -} -#endif - -typedef struct { - int typenum; - PyArray_PartitionFunc * part[NPY_NSELECTS]; - PyArray_ArgPartitionFunc * argpart[NPY_NSELECTS]; -} part_map; - -static part_map _part_map[] = { -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, 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, - * npy_cdouble, npy_clongdouble# - */ - { - NPY_@TYPE@, - { - (PyArray_PartitionFunc *)&introselect_@suff@, - }, - { - (PyArray_ArgPartitionFunc *)&aintroselect_@suff@, - } - }, -/**end repeat**/ -}; - - -static NPY_INLINE PyArray_PartitionFunc * -get_partition_func(int type, NPY_SELECTKIND which) -{ - npy_intp i; - npy_intp ntypes = ARRAY_SIZE(_part_map); - - if (which >= NPY_NSELECTS) { - return NULL; - } - for (i = 0; i < ntypes; i++) { - if (type == _part_map[i].typenum) { - return _part_map[i].part[which]; - } - } - return NULL; -} - - -static NPY_INLINE PyArray_ArgPartitionFunc * -get_argpartition_func(int type, NPY_SELECTKIND which) -{ - npy_intp i; - npy_intp ntypes = ARRAY_SIZE(_part_map); - - for (i = 0; i < ntypes; i++) { - if (type == _part_map[i].typenum) { - return _part_map[i].argpart[which]; - } - } - return NULL; -} - -#undef ARRAY_SIZE - -#endif diff --git a/numpy/core/src/npysort/selection.cpp b/numpy/core/src/npysort/selection.cpp index 8eb4a57aa..ea73bf5bc 100644 --- a/numpy/core/src/npysort/selection.cpp +++ b/numpy/core/src/npysort/selection.cpp @@ -22,6 +22,7 @@ #include "npysort_common.h" #include "numpy_tag.h" +#include <array> #include <cstdlib> #include <utility> @@ -377,269 +378,105 @@ introselect_(type *v, npy_intp *tosort, npy_intp num, npy_intp kth, return 0; } -/*************************************** - * C > C++ dispatch - ***************************************/ +/* + ***************************************************************************** + ** GENERATOR ** + ***************************************************************************** + */ -extern "C" { -NPY_NO_EXPORT int -introselect_bool(npy_bool *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::bool_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_byte(npy_byte *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::byte_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_ubyte(npy_ubyte *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ubyte_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_short(npy_short *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::short_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_ushort(npy_ushort *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ushort_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_int(npy_int *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::int_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_uint(npy_uint *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::uint_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_long(npy_long *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::long_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_ulong(npy_ulong *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ulong_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_longlong(npy_longlong *v, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::longlong_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_ulonglong(npy_ulonglong *v, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ulonglong_tag, false>(v, nullptr, num, kth, - pivots, npiv); -} -NPY_NO_EXPORT int -introselect_half(npy_half *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::half_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_float(npy_float *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::float_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_double(npy_double *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::double_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_longdouble(npy_longdouble *v, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::longdouble_tag, false>(v, nullptr, num, kth, - pivots, npiv); -} -NPY_NO_EXPORT int -introselect_cfloat(npy_cfloat *v, npy_intp num, npy_intp kth, npy_intp *pivots, - npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::cfloat_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_cdouble(npy_cdouble *v, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::cdouble_tag, false>(v, nullptr, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -introselect_clongdouble(npy_clongdouble *v, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) +template <typename Tag> +static int +introselect_noarg(void *v, npy_intp num, npy_intp kth, npy_intp *pivots, + npy_intp *npiv, void *) { - return introselect_<npy::clongdouble_tag, false>(v, nullptr, num, kth, - pivots, npiv); + return introselect_<Tag, false>((typename Tag::type *)v, nullptr, num, kth, + pivots, npiv); } -NPY_NO_EXPORT int -aintroselect_bool(npy_bool *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::bool_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_byte(npy_byte *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::byte_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_ubyte(npy_ubyte *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ubyte_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_short(npy_short *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::short_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_ushort(npy_ushort *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) -{ - return introselect_<npy::ushort_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_int(npy_int *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::int_tag, true>(v, tosort, num, kth, pivots, npiv); -} -NPY_NO_EXPORT int -aintroselect_uint(npy_uint *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::uint_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_long(npy_long *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::long_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_ulong(npy_ulong *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::ulong_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_longlong(npy_longlong *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) -{ - return introselect_<npy::longlong_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_ulonglong(npy_ulonglong *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) -{ - return introselect_<npy::ulonglong_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_half(npy_half *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) -{ - return introselect_<npy::half_tag, true>(v, tosort, num, kth, pivots, - npiv); -} -NPY_NO_EXPORT int -aintroselect_float(npy_float *v, npy_intp *tosort, npy_intp num, npy_intp kth, - npy_intp *pivots, npy_intp *npiv, void *NOT_USED) +template <typename Tag> +static int +introselect_arg(void *v, npy_intp *tosort, npy_intp num, npy_intp kth, + npy_intp *pivots, npy_intp *npiv, void *) { - return introselect_<npy::float_tag, true>(v, tosort, num, kth, pivots, - npiv); + return introselect_<Tag, true>((typename Tag::type *)v, tosort, num, kth, + pivots, npiv); } -NPY_NO_EXPORT int -aintroselect_double(npy_double *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) + +struct arg_map { + int typenum; + PyArray_PartitionFunc *part[NPY_NSELECTS]; + PyArray_ArgPartitionFunc *argpart[NPY_NSELECTS]; +}; + +template <class... Tags> +static constexpr std::array<arg_map, sizeof...(Tags)> +make_partition_map(npy::taglist<Tags...>) { - return introselect_<npy::double_tag, true>(v, tosort, num, kth, pivots, - npiv); + return std::array<arg_map, sizeof...(Tags)>{ + arg_map{Tags::type_value, &introselect_noarg<Tags>, + &introselect_arg<Tags>}...}; } -NPY_NO_EXPORT int -aintroselect_longdouble(npy_longdouble *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) + +struct partition_t { + using taglist = + npy::taglist<npy::bool_tag, npy::byte_tag, npy::ubyte_tag, + npy::short_tag, npy::ushort_tag, npy::int_tag, + npy::uint_tag, npy::long_tag, npy::ulong_tag, + npy::longlong_tag, npy::ulonglong_tag, npy::half_tag, + npy::float_tag, npy::double_tag, npy::longdouble_tag, + npy::cfloat_tag, npy::cdouble_tag, + npy::clongdouble_tag>; + + static constexpr std::array<arg_map, taglist::size> map = + make_partition_map(taglist()); +}; +constexpr std::array<arg_map, partition_t::taglist::size> partition_t::map; + +static NPY_INLINE PyArray_PartitionFunc * +_get_partition_func(int type, NPY_SELECTKIND which) { - return introselect_<npy::longdouble_tag, true>(v, tosort, num, kth, pivots, - npiv); + npy_intp i; + npy_intp ntypes = partition_t::map.size(); + + if (which >= NPY_NSELECTS) { + return NULL; + } + for (i = 0; i < ntypes; i++) { + if (type == partition_t::map[i].typenum) { + return partition_t::map[i].part[which]; + } + } + return NULL; } -NPY_NO_EXPORT int -aintroselect_cfloat(npy_cfloat *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) + +static NPY_INLINE PyArray_ArgPartitionFunc * +_get_argpartition_func(int type, NPY_SELECTKIND which) { - return introselect_<npy::cfloat_tag, true>(v, tosort, num, kth, pivots, - npiv); + npy_intp i; + npy_intp ntypes = partition_t::map.size(); + + for (i = 0; i < ntypes; i++) { + if (type == partition_t::map[i].typenum) { + return partition_t::map[i].argpart[which]; + } + } + return NULL; } -NPY_NO_EXPORT int -aintroselect_cdouble(npy_cdouble *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) + +/* + ***************************************************************************** + ** C INTERFACE ** + ***************************************************************************** + */ +extern "C" { +NPY_NO_EXPORT PyArray_PartitionFunc * +get_partition_func(int type, NPY_SELECTKIND which) { - return introselect_<npy::cdouble_tag, true>(v, tosort, num, kth, pivots, - npiv); + return _get_partition_func(type, which); } -NPY_NO_EXPORT int -aintroselect_clongdouble(npy_clongdouble *v, npy_intp *tosort, npy_intp num, - npy_intp kth, npy_intp *pivots, npy_intp *npiv, - void *NOT_USED) +NPY_NO_EXPORT PyArray_ArgPartitionFunc * +get_argpartition_func(int type, NPY_SELECTKIND which) { - return introselect_<npy::clongdouble_tag, true>(v, tosort, num, kth, - pivots, npiv); + return _get_argpartition_func(type, which); } } |