tagged release 0.6.4
[parrot.git] / src / pmc / intlist.pmc
blob8c922de24622951db532f89dc0d33e299ca00c39
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/intlist.pmc - Array of integers
9 =head1 DESCRIPTION
11 C<IntList> provides an integer-only array.
13 =head2 Methods
15 =over 4
17 =cut
21 #include "parrot/parrot.h"
23 pmclass IntList provides array {
27 =item C<PMC *clone()>
29 Creates and returns a clone of the list.
31 =cut
35     VTABLE PMC *clone() {
36         PMC * const dest     = pmc_new_noinit(INTERP, SELF->vtable->base_type);
37         PObj_custom_mark_SET(dest);
38         PMC_struct_val(dest) = intlist_clone(INTERP,
39                                              (IntList *)PMC_struct_val(SELF));
40         return dest;
41     }
45 =item C<void init()>
47 Initializes the list.
49 =cut
53     VTABLE void init() {
54         PMC_struct_val(SELF) = intlist_new(INTERP);
55         PObj_custom_mark_SET(SELF);
56     }
60 =item C<void mark()>
62 Marks the list as live.
64 =cut
68     VTABLE void mark() {
69         intlist_mark(INTERP, (IntList *)PMC_struct_val(SELF));
70     }
74 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
76 Sets C<value> at index C<key>.
78 =cut
82     VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
83         intlist_assign(INTERP, (IntList *)PMC_struct_val(SELF), key, value);
84     }
86     VTABLE void set_integer_native(INTVAL len) {
87         list_set_length(INTERP, (List *)PMC_struct_val(SELF), len);
88     }
91 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
93 Sets C<value> at index C<*key>.
95 =cut
99     VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
100         INTVAL ix;
102         if (!key)
103             return;
105         ix = key_integer(INTERP, key);
106         intlist_assign(INTERP, (IntList *)PMC_struct_val(SELF), ix, value);
107     }
111 =item C<INTVAL get_integer()>
113 Returns the number of elements in the list.
115 =cut
119     VTABLE INTVAL get_integer() {
120         return intlist_length(INTERP, (IntList *)PMC_struct_val(SELF));
121     }
123     VTABLE INTVAL elements() {
124         return intlist_length(INTERP, (IntList *)PMC_struct_val(SELF));
125     }
129 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
131 Returns the value of the element at index C<key>.
133 =cut
137     VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
138         return intlist_get(INTERP, (IntList *)PMC_struct_val(SELF), key);
139     }
143 =item C<INTVAL get_integer_keyed(PMC *key)>
145 Returns the value of the element at index C<*key>.
147 =cut
151     VTABLE INTVAL get_integer_keyed(PMC *key) {
152         INTVAL ix;
153         if (!key) return 0;
154         ix = key_integer(INTERP, key);
155         return intlist_get(INTERP, (IntList*) PMC_struct_val(SELF), ix);
156     }
160 =item C<void push_integer(INTVAL value)>
162 Adds C<value> to the end of the list.
164 =cut
168     VTABLE void push_integer(INTVAL value) {
169         intlist_push(INTERP, (IntList *)PMC_struct_val(SELF), value);
170     }
174 =item C<INTVAL pop_integer()>
176 Removes and returns the last element in the list.
178 =cut
182     VTABLE INTVAL pop_integer() {
183         return intlist_pop(INTERP, (IntList *)PMC_struct_val(SELF));
184     }
188 =item C<void unshift_integer(INTVAL value)>
190 Adds C<value> to the start of the list.
192 =cut
196     VTABLE void unshift_integer(INTVAL value) {
197         IntList **list = (IntList **)PMC_struct_val(SELF);
198         intlist_unshift(INTERP, list, value);
199     }
203 =item C<INTVAL shift_integer()>
205 Removes and returns the first element in the list.
207 =cut
211     VTABLE INTVAL shift_integer() {
212         IntList **list = (IntList **)PMC_struct_val(SELF);
213         return intlist_shift(INTERP, list);
214     }
216     void splice(PMC *value, INTVAL offset, INTVAL count) {
217         if (SELF->vtable->base_type != value->vtable->base_type)
218             real_exception(INTERP, NULL, E_TypeError,
219                     "Type mismatch in splice");
221         list_splice(INTERP, (List *)PMC_struct_val(SELF),
222             (List *)PMC_struct_val(value), offset, count);
223     }
225     VTABLE PMC *slice(PMC *key, INTVAL f) {
226         if (f) {
227             real_exception(INTERP, NULL, E_IndexError,
228                 "IntList: Unknown slice type");
229         }
230         else {
231             PMC * const iter = pmc_new_init(INTERP, enum_class_Iterator, SELF);
232             PMC_struct_val(iter) = key;
233             return iter;
234         }
235     }
237     VTABLE PMC *get_iter() {
238         PMC * const iter     = pmc_new_init(INTERP, enum_class_Iterator, SELF);
239         PMC * const key      = pmc_new(INTERP, enum_class_Key);
241         PMC_struct_val(iter) = key;
242         PObj_get_FLAGS(key) |= KEY_integer_FLAG;
243         PMC_int_val(key)     = 0;
245         if (!((List *)PMC_struct_val(SELF))->length)
246             PMC_int_val(key) = -1;
248         return iter;
249     }
254 =back
256 =cut
261  * Local variables:
262  *   c-file-style: "parrot"
263  * End:
264  * vim: expandtab shiftwidth=4:
265  */