Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

272 lines (188 sloc) 6.196 kb
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/resizablefloatarray.pmc - ResizableFloatArray PMC
=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
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass ResizableFloatArray extends FixedFloatArray auto_attrs provides array {
ATTR INTVAL resize_threshold; /* max size before array needs resizing */
/*
=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) :no_wb {
FLOATVAL *float_array;
INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
if (key < 0)
key += size;
if (key < 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");
if (key >= size)
return 0.0;
GET_ATTR_float_array(INTERP, SELF, float_array);
return float_array[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 *float_array;
INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
if (key < 0)
key += size;
if (key < 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");
if (key >= size)
SELF.set_integer_native(key + 1);
GET_ATTR_float_array(INTERP, SELF, float_array);
float_array[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) {
FLOATVAL *float_array;
INTVAL resize_threshold;
if (size < 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"illegal argument");
GET_ATTR_float_array(INTERP, SELF, float_array);
GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold);
if (!float_array) {
/* empty - used fixed routine */
if (size < 8) {
SUPER(8);
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, 8);
}
else {
SUPER(size);
SET_ATTR_resize_threshold(INTERP, SELF, size);
}
}
else if (size <= resize_threshold){
/* we could shrink here if necessary */
SET_ATTR_size(INTERP, SELF, size);
return;
}
else {
INTVAL cur = resize_threshold;
if (cur < 8192)
cur = size < 2 * cur ? 2 * cur : size;
else {
const INTVAL needed = size - cur;
cur += needed + 4096;
cur &= ~0xfff;
}
SET_ATTR_float_array(INTERP, SELF,
mem_gc_realloc_n_typed(INTERP, float_array, cur, FLOATVAL));
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, cur);
}
}
/*
=item C<PMC *clone()>
Creates and returns a copy of the array.
=cut
*/
VTABLE PMC *clone() :no_wb {
INTVAL size;
PMC * const copy = SUPER();
/* copy trimmed extra space */
GET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, copy, size);
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) :manual_wb {
const 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() :manual_wb {
FLOATVAL value;
INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
if (size == 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"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, *float_array;
INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
if (size == 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"Can't shift from an empty array");
GET_ATTR_float_array(INTERP, SELF, float_array);
value = float_array[0];
SET_ATTR_size(INTERP, SELF, --size);
memmove(float_array, float_array + 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;
FLOATVAL *float_array;
GET_ATTR_size(INTERP, SELF, size);
SELF.set_integer_native(size + 1);
GET_ATTR_float_array(INTERP, SELF, float_array);
memmove(float_array + 1, float_array, size * sizeof (FLOATVAL));
float_array[0] = value;
}
}
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd17_basic_types.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Jump to Line
Something went wrong with that request. Please try again.