From 5896d973ae725adc859b0cb6cca624a9656d6f5f Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Fri, 25 Jan 2013 16:37:20 -0800 Subject: [PATCH] Add a sort method to FixedFloatArray --- examples/benchmarks/sort_ffa.pir | 59 +++++++++++++++++++++++++++ src/pmc/fixedfloatarray.pmc | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 examples/benchmarks/sort_ffa.pir diff --git a/examples/benchmarks/sort_ffa.pir b/examples/benchmarks/sort_ffa.pir new file mode 100644 index 0000000000..19e89ebe25 --- /dev/null +++ b/examples/benchmarks/sort_ffa.pir @@ -0,0 +1,59 @@ +# Copyright (C) 2013, Parrot Foundation. + +=head1 NAME + +examples/benchmarks/sort_ffa.pir - Sort an FixedFloatArray of N floats + +=head1 SYNOPSIS + + % time ./parrot examples/benchmarks/sort_ffa.pir 100000 + +Or use the default number of iterations: + + % time ./parrot examples/benchmarks/sort.pir + +=head1 DESCRIPTION + +Sorts an FixedFloatArray of N random integers using builtin sort +function for FixedFloatArray. The argument N is specified from the +command line. + +=cut + +.loadlib 'math_ops' + +.sub main :main + .param pmc argv + .local int N, i, j + + N = argv[1] + if N < 1 goto USE_DEFAULT_SIZE + goto USE_DEFINED_SIZE + +USE_DEFAULT_SIZE: + N = 1000000 + +USE_DEFINED_SIZE: + $P0 = new ['FixedFloatArray'], N + i = 0 + j = 0 + +LOOP: + j = rand 0, N + $P0[i] = j + inc i + if i < N goto LOOP + + $P0.'sort'() + +.end + + +=cut + +# Local Variables: +# mode: pir +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4 ft=pir: + diff --git a/src/pmc/fixedfloatarray.pmc b/src/pmc/fixedfloatarray.pmc index c23a475035..a26cdee201 100644 --- a/src/pmc/fixedfloatarray.pmc +++ b/src/pmc/fixedfloatarray.pmc @@ -12,12 +12,25 @@ stored FLOATVALs. It uses Float PMCs to do all necessary conversions. =head2 Functions +=over 4 + =cut */ /* HEADERIZER HFILE: none */ /* HEADERIZER BEGIN: static */ +/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */ + +PARROT_PURE_FUNCTION +static int auxcmpfunc(ARGIN(const FLOATVAL *i), ARGIN(const FLOATVAL *j)) + __attribute__nonnull__(1) + __attribute__nonnull__(2); + +#define ASSERT_ARGS_auxcmpfunc __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ + PARROT_ASSERT_ARG(i) \ + , PARROT_ASSERT_ARG(j)) +/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */ /* HEADERIZER END: static */ pmclass FixedFloatArray auto_attrs provides array { @@ -26,10 +39,43 @@ pmclass FixedFloatArray auto_attrs provides array { /* +=back + =head2 Methods =over 4 +=item C + +Sort the array and return self. + +=cut + +*/ + + METHOD sort(PMC *cmp_func :optional) { + UINTVAL n; + INTVAL size; + + GET_ATTR_size(INTERP, SELF, size); + n = (UINTVAL)size; + + if (n > 1) { + FLOATVAL *float_array; + GET_ATTR_float_array(INTERP, SELF, float_array); + if (PMC_IS_NULL(cmp_func)) + qsort(float_array, n, sizeof (INTVAL), + (int (*)(const void *, const void*))auxcmpfunc); + else + Parrot_util_quicksort(INTERP, (void**)float_array, n, cmp_func, "NN->N"); + } + RETURN(PMC *SELF); + } + +/* + +=over 4 + =item C Destroys the array. @@ -531,6 +577,30 @@ Reverse the contents of the array. =back +=head2 Auxiliary functions + +=over 4 + +=item C + +INTVAL compare function for qsort usage. + +=cut + +*/ + +PARROT_PURE_FUNCTION +static int +auxcmpfunc(ARGIN(const FLOATVAL *i), ARGIN(const FLOATVAL *j)) +{ + ASSERT_ARGS(auxcmpfunc) + return (int) (*i - *j); +} + +/* + +=back + =head1 SEE ALSO F.