Skip to content

Commit

Permalink
Add a sort method to FixedFloatArray
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Jan 26, 2013
1 parent 8de89f8 commit 5896d97
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 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:

70 changes: 70 additions & 0 deletions src/pmc/fixedfloatarray.pmc
Expand Up @@ -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 {
Expand All @@ -26,10 +39,43 @@ pmclass FixedFloatArray auto_attrs provides array {

/*

=back

=head2 Methods

=over 4

=item C<PMC *sort()>

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<void destroy()>

Destroys the array.
Expand Down Expand Up @@ -531,6 +577,30 @@ Reverse the contents of the array.

=back

=head2 Auxiliary functions

=over 4

=item C<static int auxcmpfunc(const FLOATVAL *i, const FLOATVAL *j)>

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<docs/pdds/pdd17_basic_types.pod>.
Expand Down

0 comments on commit 5896d97

Please sign in to comment.