2 Copyright (C) 2001-2008, The Perl Foundation.
7 src/pmc/fixedfloatarray.pmc - fixed size array for floating point numbers only
11 This class, FixedFloatArray, implements an array of fixed size which
12 stored FLOATVALs. It uses Float PMCs to do all necessary conversions.
22 #include "parrot/parrot.h"
24 pmclass FixedFloatArray need_ext provides array {
36 Initializes the array.
43 PMC_int_val(SELF) = 0;
44 PMC_data(SELF) = NULL;
49 =item C<void destroy()>
57 VTABLE void destroy() {
59 mem_sys_free(PMC_data(SELF));
61 PMC_data(SELF) = NULL;
62 PMC_int_val(SELF) = 0;
69 Creates and returns a copy of the array.
78 PMC * const dest = pmc_new(INTERP, SELF->vtable->base_type);
83 size = PMC_int_val(SELF);
84 PMC_int_val(dest) = size;
85 mem_size = size * sizeof (FLOATVAL);
87 PMC_data(dest) = mem_sys_allocate(mem_size);
88 mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), mem_size);
89 PObj_active_destroy_SET(dest);
96 =item C<INTVAL get_bool()>
98 Returns whether the array has any elements (meaning been initialized, for a
104 VTABLE INTVAL get_bool() {
105 const INTVAL size = SELF.elements();
106 return (INTVAL)(size != 0);
111 =item C<INTVAL elements()>
117 VTABLE INTVAL elements() {
118 return PMC_int_val(SELF);
123 =item C<INTVAL get_integer()>
125 Returns the number of elements in the array.
131 VTABLE INTVAL get_integer() {
132 return SELF.elements();
138 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
140 Returns the integer value of the element at index C<key>.
146 VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
147 const FLOATVAL f = SELF.get_number_keyed_int(key);
153 =item C<INTVAL get_integer_keyed(PMC *key)>
155 Returns the integer value of the element at index C<*key>.
161 VTABLE INTVAL get_integer_keyed(PMC *key) {
162 /* simple int keys only */
163 const INTVAL k = key_integer(INTERP, key);
164 return SELF.get_integer_keyed_int(k);
170 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
172 Returns the floating-point value of the element at index C<key>.
178 VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
181 if (key < 0 || key >= PMC_int_val(SELF))
182 real_exception(INTERP, NULL, E_IndexError,
183 "FixedFloatArray: index out of bounds!");
185 data = (FLOATVAL *)PMC_data(SELF);
191 =item C<FLOATVAL get_number_keyed(PMC *key)>
193 Returns the floating-point value of the element at index C<*key>.
199 VTABLE FLOATVAL get_number_keyed(PMC *key) {
200 const INTVAL k = key_integer(INTERP, key);
201 return SELF.get_number_keyed_int(k);
206 =item C<STRING *get_string_keyed_int(INTVAL key)>
208 Returns the Parrot string value of the element at index C<key>.
214 VTABLE STRING *get_string_keyed_int(INTVAL key) {
215 PMC * const e = SELF.get_pmc_keyed_int(key);
216 return VTABLE_get_string(INTERP, e);
221 =item C<STRING *get_string_keyed(PMC *key)>
223 Returns the Parrot string value of the element at index C<*key>.
229 VTABLE STRING *get_string_keyed(PMC *key) {
230 const INTVAL k = key_integer(INTERP, key);
231 return SELF.get_string_keyed_int(k);
237 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
239 Returns the PMC value of the element at index C<key>.
245 VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
246 PMC * const ret = pmc_new(INTERP, enum_class_Float);
247 const FLOATVAL val = SELF.get_number_keyed_int(key);
249 VTABLE_set_number_native(INTERP, ret, val);
255 =item C<PMC *get_pmc_keyed(PMC *key)>
257 Returns the PMC value of the element at index C<*key>.
263 VTABLE PMC *get_pmc_keyed(PMC *key) {
264 const INTVAL k = key_integer(INTERP, key);
265 return SELF.get_pmc_keyed_int(k);
270 =item C<void set_integer_native(INTVAL size)>
272 Resizes the array to C<size> elements.
278 VTABLE void set_integer_native(INTVAL size) {
279 if (PMC_int_val(SELF) || size < 1)
280 real_exception(INTERP, NULL, E_IndexError,
281 "FixedFloatArray: Can't resize!");
283 PMC_int_val(SELF) = size;
284 PMC_data(SELF) = mem_sys_allocate(size * sizeof (FLOATVAL));
285 PObj_active_destroy_SET(SELF);
290 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
292 Sets the integer value of the element at index C<key> to C<value>.
298 VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
299 SELF.set_number_keyed_int(key, (FLOATVAL)value);
304 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
306 Sets the integer value of the element at index C<key> to C<value>.
312 VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
313 const INTVAL k = key_integer(INTERP, key);
314 SELF.set_integer_keyed_int(k, value);
319 =item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
321 Sets the floating-point value of the element at index C<key> to
328 VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
330 if (key < 0 || key >= PMC_int_val(SELF))
331 real_exception(INTERP, NULL, E_IndexError,
332 "FixedFloatArray: index out of bounds!");
334 data = (FLOATVAL *)PMC_data(SELF);
340 =item C<void set_number_keyed(PMC *key, FLOATVAL value)>
342 Sets the floating-point value of the element at index C<key> to
349 VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
350 const INTVAL k = key_integer(INTERP, key);
351 SELF.set_number_keyed_int(k, value);
356 =item C<void set_string_keyed_int(INTVAL key, STRING *value)>
358 Sets the Parrot string value of the element at index C<key> to C<value>.
364 VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
366 PMC * const tempPMC = pmc_new(INTERP, enum_class_Float);
368 VTABLE_set_string_native(INTERP, tempPMC, value);
369 tempNum = VTABLE_get_number(INTERP, tempPMC);
370 SELF.set_number_keyed_int(key, tempNum);
375 =item C<void set_string_keyed(PMC *key, STRING *value)>
377 Sets the string value of the element at index C<key> to
384 VTABLE void set_string_keyed(PMC *key, STRING *value) {
385 const INTVAL k = key_integer(INTERP, key);
386 SELF.set_string_keyed_int(k, value);
391 =item C<void set_pmc_keyed_int(INTVAL key, PMC *src)>
393 Sets the PMC value of the element at index C<key> to C<*src>.
399 VTABLE void set_pmc_keyed_int(INTVAL key, PMC *src) {
400 const FLOATVAL tempNum = VTABLE_get_number(INTERP, src);
401 SELF.set_number_keyed_int(key, tempNum);
406 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
408 Sets the string value of the element at index C<key> to
415 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
416 const INTVAL k = key_integer(INTERP, key);
417 SELF.set_pmc_keyed_int(k, value);
428 F<docs/pdds/pdd17_basic_types.pod>.
436 * c-file-style: "parrot"
438 * vim: expandtab shiftwidth=4: