/*
Copyright (C) 2001-2008, The Perl Foundation.
$Id: /mirror/trunk/src/pmc/resizablefloatarray.pmc 31999 2008-10-15T04:41:32.358777Z cotto  $

=head1 NAME

src/pmc/resizablefloatarray.pmc - resizable array for floating point
numbers only

=head1 DESCRIPTION

This class, C<ResizableFloatArray>, implements an array of resizable size,
which stores FLOATVALs. It uses Float PMCs to do all necessary conversions.

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"

pmclass ResizableFloatArray extends FixedFloatArray need_ext provides array {


/*

=item C<FLOATVAL get_number_keyed_int(INTVAL key)>

Returns the floating-point value of the element at index C<key>.

=cut

*/

    VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
        FLOATVAL *data;

        if (key < 0)
            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                    "ResizableFloatArray: index out of bounds!");

        if (key >= PMC_int_val(SELF))
            return 0.0;

        data = (FLOATVAL *)PMC_data(SELF);
        return data[key];
    }

/*

=item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>

Sets the floating-point value of the element at index C<key> to
C<value>.

=cut

*/

    VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
        FLOATVAL *data;

        if (key < 0)
            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                    "ResizableFloatArray: index out of bounds!");

        if (key >= PMC_int_val(SELF))
            SELF.set_integer_native(key+1);

        data      = (FLOATVAL *)PMC_data(SELF);
        data[key] = value;
    }

/*

=item C<void set_integer_native(INTVAL size)>

Resizes the array to C<size> elements.

When growing, if the new size stays smaller than twice the old size,
grow to twice the old size; otherwise, grow to the new size.

When shrinking, if the new size is smaller than half the old size,
shrink to one and half times the new size (which is less than or
equal to three quarters of the old size).

=cut

*/

    VTABLE void set_integer_native(INTVAL size) {
        if (size < 0)
            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                    "ResizableFloatArray: Can't resize to negative value!");

        if (!PMC_data(SELF)) {
            /* empty - used fixed routine */
            if (size < 8) {
                SUPER(8);
                PMC_int_val(SELF)  = size;
                PMC_int_val2(SELF) = 8;
            }
            else {
                SUPER(size);
                PMC_int_val2(SELF) = size;
            }
        }
        else if (size <= PMC_int_val2(SELF)) {
            /* we could shrink here if necessary */
            PMC_int_val(SELF) = size;
            return;
        }
        else {
            INTVAL cur = PMC_int_val2(SELF);
            if (cur < 8192)
                cur = size < 2 * cur ? 2 * cur : size;
            else {
                INTVAL needed = size - cur;
                cur          += needed + 4096;
                cur          &= ~0xfff;
            }
            PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
                    cur * sizeof (FLOATVAL));
            PMC_int_val2(SELF) = cur;
            PMC_int_val(SELF)  = size;
        }
    }

/*

=item C<PMC *clone()>

Creates and returns a copy of the array.

=cut

*/

    VTABLE PMC *clone() {
        PMC *copy          = SUPER();

        /* copy trimmed extra space */
        PMC_int_val2(copy) = PMC_int_val(SELF);

        return copy;
    }

/*

=item C<void push_float(FLOATVAL value)>

Adds C<value> to the end of the array.

=cut

*/

    VTABLE void push_float(FLOATVAL value) {
        INTVAL nextix = SELF.elements();
        SELF.set_number_keyed_int(nextix, value);
    }

/*

=item C<FLOATVAL pop_float()>

Removes and returns the last element in the array.

=cut

*/

    VTABLE FLOATVAL pop_float() {
        FLOATVAL value;
        INTVAL size = PMC_int_val(SELF);

        if (size == 0)
            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                    "ResizableFloatArray: Can't pop from an empty array!");

        value = SELF.get_number_keyed_int(size-1);
        SELF.set_integer_native(size - 1);
        return value;
    }
/*

=item C<INTVAL shift_float()>

Removes and returns an item from the start of the array.

=cut

*/

    VTABLE FLOATVAL shift_float() {
        FLOATVAL value, *data;
        INTVAL size = PMC_int_val(SELF);

        if (size == 0)
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                    "ResizableFloatArray: Can't shift from an empty array!");

        data              = (FLOATVAL *)PMC_data(SELF);
        value             = data[0];
        PMC_int_val(SELF) = --size;

        mem_sys_memmove(data, data + 1, size * sizeof (FLOATVAL));
        return value;
    }

/*

=item C<void unshift_float(FLOATVAL value)>

Add and integer to the start of the array.

=cut

*/

    VTABLE void unshift_float(FLOATVAL value) {
        INTVAL    size = PMC_int_val(SELF);
        FLOATVAL *data;

        SELF.set_integer_native(size + 1);
        data    = (FLOATVAL *)PMC_data(SELF);
        mem_sys_memmove(data + 1, data, size * sizeof (FLOATVAL));
        data[0] = value;
    }

}

/*

=back

=head1 SEE ALSO

F<docs/pdds/pdd17_basic_types.pod>.

=head1 HISTORY

Initial version                  - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
moved available size to int_val2 - Matt Fowles 2005-07-22

=cut

*/

/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
