2 Copyright (C) 2001-2010, Parrot Foundation.
7 src/pmc/fixedstringarray.pmc - fixed size array for strings only
11 This class, FixedStringArray, implements an array of fixed size which
12 stores Parrot strings.
22 /* HEADERIZER HFILE: none */
23 /* HEADERIZER BEGIN: static */
24 /* HEADERIZER END: static */
26 pmclass FixedStringArray auto_attrs provides array {
27 ATTR STRING **str_array; /* where the STRINGs are stored */
28 ATTR UINTVAL size; /* element count */
38 =item C<void init_int(INTVAL size)>
40 Initializes the array.
46 VTABLE void init_int(INTVAL size) {
48 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
49 _("FixedStringArray: Cannot set array size to a negative number (%d)"), size);
51 SET_ATTR_size(INTERP, SELF, size);
52 SET_ATTR_str_array(INTERP,
53 SELF, mem_gc_allocate_n_zeroed_typed(INTERP, size, STRING *));
54 PObj_custom_mark_destroy_SETALL(SELF);
61 =item C<void destroy()>
69 VTABLE void destroy() {
73 GET_ATTR_str_array(INTERP, SELF, str_array);
76 mem_gc_free(INTERP, str_array);
83 Creates and returns a copy of the array.
91 STRING **my_str_array, **dest_str_array;
92 PMC *const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
94 GET_ATTR_str_array(INTERP, SELF, my_str_array);
100 GET_ATTR_size(INTERP, SELF, size);
101 mem_size = size * sizeof (STRING *);
103 dest_str_array = mem_gc_allocate_n_zeroed_typed(INTERP, size, STRING *);
104 mem_sys_memcopy(dest_str_array, my_str_array, mem_size);
105 SET_ATTR_str_array(INTERP, dest, dest_str_array);
106 SET_ATTR_size(INTERP, dest, size);
108 PObj_custom_mark_destroy_SETALL(dest);
118 Marks the array as live.
127 GET_ATTR_str_array(INTERP, SELF, str_array);
131 GET_ATTR_size(INTERP, SELF, size);
133 for (i = 0; i < size; ++i) {
134 Parrot_gc_mark_STRING_alive(INTERP, str_array[i]);
141 =item C<INTVAL get_bool()>
143 Returns 1 if the array has any elements; otherwise, returns 0.
144 Since this is a fixed size array, C<get_bool> will always
145 return true once the array has been initialized and had its
146 size set by C<set_integer_native>.
151 VTABLE INTVAL get_bool() {
152 const INTVAL size = SELF.elements();
153 return (INTVAL)(size != 0);
158 =item C<PMC *get_iter()>
160 Gets an iterator for the array.
165 VTABLE PMC *get_iter() {
166 return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
171 =item C<INTVAL elements()>
173 Returns the number of elements in the array.
179 VTABLE INTVAL elements() {
181 GET_ATTR_size(INTERP, SELF, size);
187 =item C<INTVAL get_integer()>
189 Returns the number of elements in the array.
195 VTABLE INTVAL get_integer() {
196 return SELF.elements();
201 =item C<FLOATVAL get_number()>
203 Returns the number of elements in the array.
209 VTABLE FLOATVAL get_number() {
210 const INTVAL e = SELF.elements();
216 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
218 Returns the integer value of the element at index C<key>.
224 VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
225 STRING * const element = SELF.get_string_keyed_int(key);
226 return Parrot_str_to_int(INTERP, element);
231 =item C<INTVAL get_integer_keyed(PMC *key)>
233 Returns the integer value of the element at index C<*key>.
239 VTABLE INTVAL get_integer_keyed(PMC *key) {
240 /* simple int keys only */
241 const INTVAL k = VTABLE_get_integer(INTERP, key);
242 return SELF.get_integer_keyed_int(k);
248 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
250 Returns the floating-point value of the element at index C<key>.
256 VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
257 STRING * const element = SELF.get_string_keyed_int(key);
258 return Parrot_str_to_num(INTERP, element);
263 =item C<FLOATVAL get_number_keyed(PMC *key)>
265 Returns the floating-point value of the element at index C<*key>.
271 VTABLE FLOATVAL get_number_keyed(PMC *key) {
272 const INTVAL k = VTABLE_get_integer(INTERP, key);
273 return SELF.get_number_keyed_int(k);
278 =item C<STRING *get_string_keyed_int(INTVAL key)>
280 Returns the Parrot string value of the element at index C<key>.
286 VTABLE STRING *get_string_keyed_int(INTVAL key) {
290 GET_ATTR_size(INTERP, SELF, size);
291 if (key < 0 || (UINTVAL)key >= size)
292 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
293 "FixedStringArray: index out of bounds!");
295 GET_ATTR_str_array(INTERP, SELF, str_array);
296 return str_array[key];
301 =item C<STRING *get_string_keyed(PMC *key)>
303 Returns the Parrot string value of the element at index C<*key>.
309 VTABLE STRING *get_string_keyed(PMC *key) {
310 const INTVAL k = VTABLE_get_integer(INTERP, key);
311 return SELF.get_string_keyed_int(k);
317 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
319 Returns the PMC value of the element at index C<key>.
325 VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
326 PMC * const ret = Parrot_pmc_new(INTERP, enum_class_String);
327 STRING * const val = SELF.get_string_keyed_int(key);
329 VTABLE_set_string_native(INTERP, ret, val);
335 =item C<PMC *get_pmc_keyed(PMC *key)>
337 Returns the PMC value of the element at index C<*key>.
343 VTABLE PMC *get_pmc_keyed(PMC *key) {
344 const INTVAL k = VTABLE_get_integer(INTERP, key);
345 return SELF.get_pmc_keyed_int(k);
350 =item C<void set_integer_native(INTVAL size)>
352 Sets the size of the array to C<size> elements. Once the array
353 has been given an initial size, attempts to resize it will
354 cause an exception to be thrown.
360 VTABLE void set_integer_native(INTVAL new_size) {
363 GET_ATTR_size(INTERP, SELF, old_size);
365 if (old_size || new_size < 1)
366 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
367 "FixedStringArray: Can't resize!");
369 SET_ATTR_size(INTERP, SELF, new_size);
370 SET_ATTR_str_array(INTERP, SELF,
371 mem_gc_allocate_n_zeroed_typed(INTERP, new_size, STRING*));
373 PObj_custom_mark_destroy_SETALL(SELF);
378 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
380 Sets the integer value of the element at index C<key> to C<value>.
386 VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
387 STRING *val = Parrot_str_from_int(INTERP, value);
388 SELF.set_string_keyed_int(key, val);
393 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
395 Sets the integer value of the element at index C<key> to C<value>.
401 VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
402 const INTVAL k = VTABLE_get_integer(INTERP, key);
403 SELF.set_integer_keyed_int(k, value);
408 =item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
410 Sets the floating-point value of the element at index C<key> to
417 VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
418 STRING *val = Parrot_str_from_num(INTERP, value);
419 SELF.set_string_keyed_int(key, val);
424 =item C<void set_number_keyed(PMC *key, FLOATVAL value)>
426 Sets the floating-point value of the element at index C<key> to
433 VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
434 const INTVAL k = VTABLE_get_integer(INTERP, key);
435 SELF.set_number_keyed_int(k, value);
440 =item C<void set_string_keyed_int(INTVAL key, STRING *value)>
442 Sets the Parrot string value of the element at index C<key> to C<value>.
448 VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
451 if (key < 0 || key >= SELF.elements())
452 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
453 "FixedStringArray: index out of bounds!");
455 GET_ATTR_str_array(INTERP, SELF, str_array);
457 str_array[key] = value;
462 =item C<void set_string_keyed(PMC *key, STRING *value)>
464 Sets the string value of the element at index C<key> to
471 VTABLE void set_string_keyed(PMC *key, STRING *value) {
472 const INTVAL k = VTABLE_get_integer(INTERP, key);
473 SELF.set_string_keyed_int(k, value);
478 =item C<void set_pmc_keyed_int(INTVAL key, PMC *src)>
480 Sets the PMC value of the element at index C<key> to C<*src>.
486 VTABLE void set_pmc_keyed_int(INTVAL key, PMC *src) {
487 STRING * const temp = VTABLE_get_string(INTERP, src);
488 SELF.set_string_keyed_int(key, temp);
493 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
495 Sets the string value of the element at index C<key> to
502 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
503 const INTVAL k = VTABLE_get_integer(INTERP, key);
504 SELF.set_pmc_keyed_int(k, value);
509 =item C<STRING *get_string()>
511 =item C<STRING *get_repr()>
513 Returns the Parrot string representation C<key>.
518 VTABLE STRING *get_string() {
519 return STATICSELF.get_repr();
522 VTABLE STRING *get_repr() {
524 STRING *res = CONST_STRING(INTERP, "[ ");
525 const INTVAL n = SELF.get_integer();
528 for (j = 0; j < n; ++j) {
529 STRING * const val = SELF.get_string_keyed_int(j);
530 res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, "\""));
531 res = Parrot_str_concat(INTERP, res, val);
532 res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, "\""));
535 res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, ", "));
538 res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, " ]"));
545 =item C<INTVAL is_equal(PMC *value)>
547 The C<==> operation. Compares two array to hold equal elements.
553 VTABLE INTVAL is_equal(PMC *value) {
556 if (value->vtable->base_type != SELF->vtable->base_type)
561 if (VTABLE_elements(INTERP, value) != n)
564 for (j = 0; j < n; ++j) {
565 STRING * const item1 = SELF.get_string_keyed_int(j);
566 STRING * const item2 = VTABLE_get_string_keyed_int(INTERP, value, j);
571 if (STRING_IS_NULL(item1) || STRING_IS_NULL(item2))
574 if (!Parrot_str_equal(INTERP, item1, item2))
585 =head2 Freeze/thaw Interface
589 =item C<void freeze(PMC *info)>
591 Used to archive the string.
596 VTABLE void freeze(PMC *info) {
600 GET_ATTR_size(INTERP, SELF, size);
601 GET_ATTR_str_array(INTERP, SELF, str_array);
602 VTABLE_push_integer(INTERP, info, size);
604 for (i = 0; i < size; ++i)
605 VTABLE_push_string(INTERP, info, str_array[i]);
610 =item C<void thaw(PMC *info)>
612 Used to unarchive the string.
617 VTABLE void thaw(PMC *info) {
624 size = VTABLE_shift_integer(INTERP, info);
625 SELF.set_integer_native((INTVAL)size);
626 GET_ATTR_str_array(INTERP, SELF, str_array);
628 for (i = 0; i < size; ++i)
629 str_array[i] = VTABLE_shift_string(INTERP, info);
639 F<docs/pdds/pdd17_basic_types.pod>.
647 * c-file-style: "parrot"
649 * vim: expandtab shiftwidth=4: