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 pmclass FixedStringArray auto_attrs provides array {
23 ATTR STRING **str_array; /* where the STRINGs are stored */
24 ATTR UINTVAL size; /* element count */
36 Initializes the array.
43 PObj_custom_mark_destroy_SETALL(SELF);
48 =item C<void init_int(INTVAL size)>
50 Initializes the array.
56 VTABLE void init_int(INTVAL size) {
58 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
59 _("FixedStringArray: Cannot set array size to a negative number (%d)"), size);
60 SET_ATTR_size(INTERP, SELF, size);
61 SET_ATTR_str_array(INTERP, SELF, mem_gc_allocate_n_zeroed_typed(INTERP, size, STRING *));
62 PObj_custom_mark_destroy_SETALL(SELF);
68 =item C<void destroy()>
76 VTABLE void destroy() {
80 GET_ATTR_str_array(INTERP, SELF, str_array);
83 mem_gc_free(INTERP, str_array);
90 Creates and returns a copy of the array.
98 STRING **my_str_array, **dest_str_array;
99 PMC *const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
101 GET_ATTR_str_array(INTERP, SELF, my_str_array);
107 GET_ATTR_size(INTERP, SELF, size);
108 mem_size = size * sizeof (STRING *);
110 dest_str_array = mem_gc_allocate_n_zeroed_typed(INTERP, size, STRING *);
111 mem_sys_memcopy(dest_str_array, my_str_array, mem_size);
112 SET_ATTR_str_array(INTERP, dest, dest_str_array);
113 SET_ATTR_size(INTERP, dest, size);
115 PObj_custom_mark_destroy_SETALL(dest);
125 Marks the array as live.
134 GET_ATTR_str_array(INTERP, SELF, str_array);
138 GET_ATTR_size(INTERP, SELF, size);
140 for (i = 0; i < size; i++) {
141 Parrot_gc_mark_STRING_alive(INTERP, str_array[i]);
148 =item C<INTVAL get_bool()>
150 Returns 1 if the array has any elements; otherwise, returns 0.
151 Since this is a fixed size array, C<get_bool> will always
152 return true once the array has been initialized and had its
153 size set by C<set_integer_native>.
158 VTABLE INTVAL get_bool() {
159 const INTVAL size = SELF.elements();
160 return (INTVAL)(size != 0);
165 =item C<PMC *get_iter()>
167 Gets an iterator for the array.
172 VTABLE PMC *get_iter() {
173 return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
178 =item C<INTVAL elements()>
180 Returns the number of elements in the array.
186 VTABLE INTVAL elements() {
188 GET_ATTR_size(INTERP, SELF, size);
194 =item C<INTVAL get_integer()>
196 Returns the number of elements in the array.
202 VTABLE INTVAL get_integer() {
203 return SELF.elements();
208 =item C<FLOATVAL get_number()>
210 Returns the number of elements in the array.
216 VTABLE FLOATVAL get_number() {
217 const INTVAL e = SELF.elements();
223 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
225 Returns the integer value of the element at index C<key>.
231 VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
232 STRING * const element = SELF.get_string_keyed_int(key);
233 return Parrot_str_to_int(interp, element);
238 =item C<INTVAL get_integer_keyed(PMC *key)>
240 Returns the integer value of the element at index C<*key>.
246 VTABLE INTVAL get_integer_keyed(PMC *key) {
247 /* simple int keys only */
248 const INTVAL k = VTABLE_get_integer(INTERP, key);
249 return SELF.get_integer_keyed_int(k);
255 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
257 Returns the floating-point value of the element at index C<key>.
263 VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
264 STRING * const element = SELF.get_string_keyed_int(key);
265 return Parrot_str_to_num(interp, element);
270 =item C<FLOATVAL get_number_keyed(PMC *key)>
272 Returns the floating-point value of the element at index C<*key>.
278 VTABLE FLOATVAL get_number_keyed(PMC *key) {
279 const INTVAL k = VTABLE_get_integer(INTERP, key);
280 return SELF.get_number_keyed_int(k);
285 =item C<STRING *get_string_keyed_int(INTVAL key)>
287 Returns the Parrot string value of the element at index C<key>.
293 VTABLE STRING *get_string_keyed_int(INTVAL key) {
297 GET_ATTR_size(INTERP, SELF, size);
298 if (key < 0 || (UINTVAL)key >= size)
299 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
300 "FixedStringArray: index out of bounds!");
302 GET_ATTR_str_array(INTERP, SELF, str_array);
303 return str_array[key];
308 =item C<STRING *get_string_keyed(PMC *key)>
310 Returns the Parrot string value of the element at index C<*key>.
316 VTABLE STRING *get_string_keyed(PMC *key) {
317 const INTVAL k = VTABLE_get_integer(INTERP, key);
318 return SELF.get_string_keyed_int(k);
324 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
326 Returns the PMC value of the element at index C<key>.
332 VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
333 PMC * const ret = Parrot_pmc_new(INTERP, enum_class_String);
334 STRING * const val = SELF.get_string_keyed_int(key);
336 VTABLE_set_string_native(INTERP, ret, val);
342 =item C<PMC *get_pmc_keyed(PMC *key)>
344 Returns the PMC value of the element at index C<*key>.
350 VTABLE PMC *get_pmc_keyed(PMC *key) {
351 const INTVAL k = VTABLE_get_integer(INTERP, key);
352 return SELF.get_pmc_keyed_int(k);
357 =item C<void set_integer_native(INTVAL size)>
359 Sets the size of the array to C<size> elements. Once the array
360 has been given an initial size, attempts to resize it will
361 cause an exception to be thrown.
367 VTABLE void set_integer_native(INTVAL new_size) {
370 GET_ATTR_size(INTERP, SELF, old_size);
372 if (old_size || new_size < 1)
373 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
374 "FixedStringArray: Can't resize!");
376 SET_ATTR_size(INTERP, SELF, new_size);
377 SET_ATTR_str_array(INTERP, SELF,
378 mem_gc_allocate_n_zeroed_typed(INTERP, new_size, STRING*));
380 PObj_custom_mark_destroy_SETALL(SELF);
385 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
387 Sets the integer value of the element at index C<key> to C<value>.
393 VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
394 STRING *val = Parrot_str_from_int(interp, value);
395 SELF.set_string_keyed_int(key, val);
400 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
402 Sets the integer value of the element at index C<key> to C<value>.
408 VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
409 const INTVAL k = VTABLE_get_integer(INTERP, key);
410 SELF.set_integer_keyed_int(k, value);
415 =item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
417 Sets the floating-point value of the element at index C<key> to
424 VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
425 STRING *val = Parrot_str_from_num(interp, value);
426 SELF.set_string_keyed_int(key, val);
431 =item C<void set_number_keyed(PMC *key, FLOATVAL value)>
433 Sets the floating-point value of the element at index C<key> to
440 VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
441 const INTVAL k = VTABLE_get_integer(INTERP, key);
442 SELF.set_number_keyed_int(k, value);
447 =item C<void set_string_keyed_int(INTVAL key, STRING *value)>
449 Sets the Parrot string value of the element at index C<key> to C<value>.
455 VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
458 if (key < 0 || key >= SELF.elements())
459 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
460 "FixedStringArray: index out of bounds!");
462 GET_ATTR_str_array(INTERP, SELF, str_array);
464 str_array[key] = value;
469 =item C<void set_string_keyed(PMC *key, STRING *value)>
471 Sets the string value of the element at index C<key> to
478 VTABLE void set_string_keyed(PMC *key, STRING *value) {
479 const INTVAL k = VTABLE_get_integer(INTERP, key);
480 SELF.set_string_keyed_int(k, value);
485 =item C<void set_pmc_keyed_int(INTVAL key, PMC *src)>
487 Sets the PMC value of the element at index C<key> to C<*src>.
493 VTABLE void set_pmc_keyed_int(INTVAL key, PMC *src) {
494 STRING * const temp = VTABLE_get_string(INTERP, src);
495 SELF.set_string_keyed_int(key, temp);
500 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
502 Sets the string value of the element at index C<key> to
509 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
510 const INTVAL k = VTABLE_get_integer(INTERP, key);
511 SELF.set_pmc_keyed_int(k, value);
516 =item C<STRING *get_string()>
518 =item C<STRING *get_repr()>
520 Returns the Parrot string representation C<key>.
525 VTABLE STRING *get_string() {
526 return STATICSELF.get_repr();
529 VTABLE STRING *get_repr() {
531 STRING *res = CONST_STRING(INTERP, "[ ");
532 const INTVAL n = SELF.get_integer();
535 for (j = 0; j < n; ++j) {
536 STRING * const val = SELF.get_string_keyed_int(j);
537 res = Parrot_str_append(INTERP, res, CONST_STRING(INTERP, "\""));
538 res = Parrot_str_append(INTERP, res, val);
539 res = Parrot_str_append(INTERP, res, CONST_STRING(INTERP, "\""));
542 res = Parrot_str_append(INTERP, res, CONST_STRING(INTERP, ", "));
545 res = Parrot_str_append(INTERP, res, CONST_STRING(INTERP, " ]"));
552 =item C<INTVAL is_equal(PMC *value)>
554 The C<==> operation. Compares two array to hold equal elements.
560 VTABLE INTVAL is_equal(PMC *value) {
563 if (value->vtable->base_type != SELF->vtable->base_type)
568 if (VTABLE_elements(INTERP, value) != n)
571 for (j = 0; j < n; ++j) {
572 STRING * const item1 = SELF.get_string_keyed_int(j);
573 STRING * const item2 = VTABLE_get_string_keyed_int(INTERP, value, j);
578 if (item1 == NULL || item2 == NULL)
581 if (!Parrot_str_equal(interp, item1, item2))
592 =head2 Freeze/thaw Interface
596 =item C<void freeze(PMC *info)>
598 Used to archive the string.
603 VTABLE void freeze(PMC *info) {
607 GET_ATTR_size(INTERP, SELF, size);
608 GET_ATTR_str_array(INTERP, SELF, str_array);
609 VTABLE_push_integer(INTERP, info, size);
611 for (i = 0; i < size; ++i)
612 VTABLE_push_string(INTERP, info, str_array[i]);
617 =item C<void thaw(PMC *info)>
619 Used to unarchive the string.
624 VTABLE void thaw(PMC *info) {
631 size = VTABLE_shift_integer(INTERP, info);
632 SELF.set_integer_native((INTVAL)size);
633 GET_ATTR_str_array(INTERP, SELF, str_array);
635 for (i = 0; i < size; ++i)
636 str_array[i] = VTABLE_shift_string(INTERP, info);
646 F<docs/pdds/pdd17_basic_types.pod>.
654 * c-file-style: "parrot"
656 * vim: expandtab shiftwidth=4: