fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / pmc / fixedstringarray.pmc
blobe59b82742b20b37ca85f053743bb61e53d18c8ab
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/fixedstringarray.pmc - fixed size array for strings only
9 =head1 DESCRIPTION
11 This class, FixedStringArray, implements an array of fixed size which
12 stores Parrot strings.
14 =head2 Functions
16 =over 4
18 =cut
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 */
32 =back
34 =head2 Methods
36 =over 4
38 =item C<void init_int(INTVAL size)>
40 Initializes the array.
42 =cut
46     VTABLE void init_int(INTVAL size) {
47         if (size < 0)
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);
50         if (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);
55         }
56     }
61 =item C<void destroy()>
63 Destroys the array.
65 =cut
69     VTABLE void destroy() {
71         STRING **str_array;
73         GET_ATTR_str_array(INTERP, SELF, str_array);
75         if (str_array)
76             mem_gc_free(INTERP, str_array);
77     }
81 =item C<PMC *clone()>
83 Creates and returns a copy of the array.
85 =cut
89     VTABLE PMC *clone() {
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);
96         if (my_str_array) {
97             INTVAL size;
98             size_t mem_size;
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);
109         }
111         return dest;
112     }
116 =item C<void mark()>
118 Marks the array as live.
120 =cut
124     VTABLE void mark() {
126         STRING **str_array;
127         GET_ATTR_str_array(INTERP, SELF, str_array);
129         if (str_array) {
130             UINTVAL size, i;
131             GET_ATTR_size(INTERP, SELF, size);
133             for (i = 0; i < size; ++i) {
134                 Parrot_gc_mark_STRING_alive(INTERP, str_array[i]);
135             }
136         }
137     }
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>.
148 =cut
151     VTABLE INTVAL get_bool() {
152         const INTVAL size = SELF.elements();
153         return (INTVAL)(size != 0);
154     }
158 =item C<PMC *get_iter()>
160 Gets an iterator for the array.
162 =cut
165     VTABLE PMC *get_iter() {
166         return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
167     }
171 =item C<INTVAL elements()>
173 Returns the number of elements in the array.
175 =cut
179     VTABLE INTVAL elements() {
180         UINTVAL size;
181         GET_ATTR_size(INTERP, SELF, size);
182         return size;
183     }
187 =item C<INTVAL get_integer()>
189 Returns the number of elements in the array.
191 =cut
195     VTABLE INTVAL get_integer() {
196         return SELF.elements();
197     }
201 =item C<FLOATVAL get_number()>
203 Returns the number of elements in the array.
205 =cut
209     VTABLE FLOATVAL get_number() {
210         const INTVAL e = SELF.elements();
211         return (FLOATVAL)e;
212     }
216 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
218 Returns the integer value of the element at index C<key>.
220 =cut
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);
227     }
231 =item C<INTVAL get_integer_keyed(PMC *key)>
233 Returns the integer value of the element at index C<*key>.
235 =cut
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);
243     }
248 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
250 Returns the floating-point value of the element at index C<key>.
252 =cut
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);
259     }
263 =item C<FLOATVAL get_number_keyed(PMC *key)>
265 Returns the floating-point value of the element at index C<*key>.
267 =cut
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);
274     }
278 =item C<STRING *get_string_keyed_int(INTVAL key)>
280 Returns the Parrot string value of the element at index C<key>.
282 =cut
286     VTABLE STRING *get_string_keyed_int(INTVAL key) {
287         STRING **str_array;
288         UINTVAL  size;
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];
297     }
301 =item C<STRING *get_string_keyed(PMC *key)>
303 Returns the Parrot string value of the element at index C<*key>.
305 =cut
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);
312     }
317 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
319 Returns the PMC value of the element at index C<key>.
321 =cut
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);
330         return ret;
331     }
335 =item C<PMC *get_pmc_keyed(PMC *key)>
337 Returns the PMC value of the element at index C<*key>.
339 =cut
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);
346     }
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.
356 =cut
360     VTABLE void set_integer_native(INTVAL new_size) {
362         UINTVAL  old_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);
374     }
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>.
382 =cut
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);
389     }
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>.
397 =cut
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);
404     }
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
411 C<value>.
413 =cut
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);
420     }
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
427 C<value>.
429 =cut
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);
436     }
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>.
444 =cut
448     VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
449         STRING **str_array;
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;
458     }
462 =item C<void set_string_keyed(PMC *key, STRING *value)>
464 Sets the string value of the element at index C<key> to
465 C<value>.
467 =cut
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);
474     }
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>.
482 =cut
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);
489     }
493 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
495 Sets the string value of the element at index C<key> to
496 C<value>.
498 =cut
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);
505     }
509 =item C<STRING *get_string()>
511 =item C<STRING *get_repr()>
513 Returns the Parrot string representation C<key>.
515 =cut
518     VTABLE STRING *get_string() {
519         return STATICSELF.get_repr();
520     }
522     VTABLE STRING *get_repr() {
524         STRING       *res = CONST_STRING(INTERP, "[ ");
525         const INTVAL  n   = SELF.get_integer();
526         INTVAL        j;
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, "\""));
534             if (j < n - 1)
535                 res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, ", "));
536         }
538         res = Parrot_str_concat(INTERP, res, CONST_STRING(INTERP, " ]"));
539         return res;
540     }
545 =item C<INTVAL is_equal(PMC *value)>
547 The C<==> operation. Compares two array to hold equal elements.
549 =cut
553     VTABLE INTVAL is_equal(PMC *value) {
554         INTVAL j, n;
556         if (value->vtable->base_type != SELF->vtable->base_type)
557             return 0;
559         n = SELF.elements();
561         if (VTABLE_elements(INTERP, value) != n)
562             return 0;
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);
568             if (item1 == item2)
569                 continue;
571             if (STRING_IS_NULL(item1) ||  STRING_IS_NULL(item2))
572                 return 0;
574             if (!Parrot_str_equal(INTERP, item1, item2))
575                 return 0;
576         }
578         return 1;
579     }
583 =back
585 =head2 Freeze/thaw Interface
587 =over 4
589 =item C<void freeze(PMC *info)>
591 Used to archive the string.
593 =cut
596     VTABLE void freeze(PMC *info) {
597         STRING           **str_array;
598         UINTVAL            size, i;
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]);
606     }
610 =item C<void thaw(PMC *info)>
612 Used to unarchive the string.
614 =cut
617     VTABLE void thaw(PMC *info) {
618         UINTVAL  i, size;
619         STRING **str_array;
621         SELF.init();
622         SUPER(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);
630     }
635 =back
637 =head1 SEE ALSO
639 F<docs/pdds/pdd17_basic_types.pod>.
641 =cut
646  * Local variables:
647  *   c-file-style: "parrot"
648  * End:
649  * vim: expandtab shiftwidth=4:
650  */