Permalink
Newer
Older
100644 471 lines (297 sloc) 9.04 KB
2
Copyright (C) 2001-2009, Parrot Foundation.
6
src/pmc/fixedfloatarray.pmc - fixed size array for floating point numbers only
10
This class, FixedFloatArray, implements an array of fixed size which
11
stored FLOATVALs. It uses Float PMCs to do all necessary conversions.
12
13
=head2 Functions
14
15
=over 4
16
17
=cut
18
19
*/
20
21
/* HEADERIZER HFILE: none */
22
/* HEADERIZER BEGIN: static */
23
/* HEADERIZER END: static */
24
25
pmclass FixedFloatArray auto_attrs provides array {
26
ATTR INTVAL size;
27
ATTR FLOATVAL *float_array;
29
/*
30
31
=back
32
33
=head2 Methods
34
35
=over 4
36
37
=item C<void destroy()>
38
39
Destroys the array.
40
41
=cut
42
43
*/
44
46
FLOATVAL *float_array;
47
GET_ATTR_float_array(INTERP, SELF, float_array);
48
if (float_array)
49
mem_gc_free(INTERP, float_array);
54
=item C<void init_int(INTVAL size)>
55
56
Initializes the array.
57
58
=cut
59
60
*/
61
62
VTABLE void init_int(INTVAL size) {
63
if (size < 0)
64
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
65
_("FixedFloatArray: Cannot set array size to a negative number (%d)"), size);
66
SET_ATTR_size(INTERP, SELF, size);
67
SET_ATTR_float_array(INTERP, SELF, mem_gc_allocate_n_typed(INTERP, size, FLOATVAL));
68
PObj_custom_destroy_SET(SELF);
69
}
70
71
/*
72
73
=item C<PMC *clone()>
74
75
Creates and returns a copy of the array.
76
77
=cut
78
79
*/
80
Apr 24, 2010
82
FLOATVAL *self_float_array;
83
PMC * const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
84
GET_ATTR_float_array(INTERP, SELF, self_float_array);
Apr 24, 2010
85
if (self_float_array) {
86
INTVAL size;
87
GET_ATTR_size(INTERP, SELF, size);
88
{
89
FLOATVAL * const dest_float_array = mem_gc_allocate_n_typed(INTERP,
90
size, FLOATVAL);
91
mem_copy_n_typed(dest_float_array, self_float_array,
92
size, FLOATVAL);
93
SET_ATTR_float_array(INTERP, dest, dest_float_array);
94
}
95
SET_ATTR_size(INTERP, dest, size);
96
PObj_custom_destroy_SET(dest);
97
}
98
return dest;
99
}
100
101
/*
102
103
=item C<PMC *get_iter()>
104
105
Return an Iterator for this PMC.
106
107
=cut
108
109
*/
110
111
VTABLE PMC *get_iter() {
112
return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
113
}
114
115
/*
116
117
=item C<INTVAL get_bool()>
118
119
Returns whether the array has any elements (meaning been initialized, for a
120
fixed sized array).
121
122
=cut
123
124
*/
126
const INTVAL size = SELF.elements();
127
return (INTVAL)(size != 0);
128
}
129
130
/*
131
132
=item C<INTVAL elements()>
133
134
=cut
135
136
*/
137
139
INTVAL size;
140
GET_ATTR_size(INTERP, SELF, size);
141
return size;
142
}
143
144
/*
145
146
=item C<INTVAL get_integer()>
147
148
Returns the number of elements in the array.
149
150
=cut
151
152
*/
153
156
}
157
158
159
/*
160
161
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
162
163
Returns the integer value of the element at index C<key>.
164
165
=cut
166
167
*/
168
169
VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
170
const FLOATVAL f = SELF.get_number_keyed_int(key);
172
}
173
174
/*
175
176
=item C<INTVAL get_integer_keyed(PMC *key)>
177
178
Returns the integer value of the element at index C<*key>.
179
180
=cut
181
182
*/
183
184
VTABLE INTVAL get_integer_keyed(PMC *key) {
186
const INTVAL k = VTABLE_get_integer(INTERP, key);
187
return SELF.get_integer_keyed_int(k);
188
}
189
190
191
/*
192
193
=item C<FLOATVAL get_number_keyed_int(INTVAL key)>
194
195
Returns the floating-point value of the element at index C<key>.
196
197
=cut
198
199
*/
200
201
VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
202
FLOATVAL *float_array;
203
INTVAL size;
205
GET_ATTR_size(INTERP, SELF, size);
206
if (key < 0 || key >= size)
207
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
208
"FixedFloatArray: index out of bounds!");
210
GET_ATTR_float_array(INTERP, SELF, float_array);
211
return float_array[key];
216
=item C<FLOATVAL get_number_keyed(PMC *key)>
217
218
Returns the floating-point value of the element at index C<*key>.
219
220
=cut
221
222
*/
223
224
VTABLE FLOATVAL get_number_keyed(PMC *key) {
225
const INTVAL k = VTABLE_get_integer(INTERP, key);
226
return SELF.get_number_keyed_int(k);
227
}
228
229
/*
230
231
=item C<STRING *get_string_keyed_int(INTVAL key)>
232
233
Returns the Parrot string value of the element at index C<key>.
234
235
=cut
236
237
*/
238
239
VTABLE STRING *get_string_keyed_int(INTVAL key) {
240
PMC * const e = SELF.get_pmc_keyed_int(key);
241
return VTABLE_get_string(INTERP, e);
242
}
243
244
/*
245
246
=item C<STRING *get_string_keyed(PMC *key)>
247
248
Returns the Parrot string value of the element at index C<*key>.
249
250
=cut
251
252
*/
253
254
VTABLE STRING *get_string_keyed(PMC *key) {
255
const INTVAL k = VTABLE_get_integer(INTERP, key);
256
return SELF.get_string_keyed_int(k);
257
}
258
259
260
/*
261
262
=item C<PMC *get_pmc_keyed_int(INTVAL key)>
263
264
Returns the PMC value of the element at index C<key>.
265
266
=cut
267
268
*/
269
270
VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
271
PMC * const ret = Parrot_pmc_new(INTERP, enum_class_Float);
272
const FLOATVAL val = SELF.get_number_keyed_int(key);
273
274
VTABLE_set_number_native(INTERP, ret, val);
275
return ret;
276
}
277
278
/*
279
280
=item C<PMC *get_pmc_keyed(PMC *key)>
281
282
Returns the PMC value of the element at index C<*key>.
283
284
=cut
285
286
*/
287
288
VTABLE PMC *get_pmc_keyed(PMC *key) {
289
const INTVAL k = VTABLE_get_integer(INTERP, key);
290
return SELF.get_pmc_keyed_int(k);
291
}
292
293
/*
294
295
=item C<void set_integer_native(INTVAL size)>
296
297
Resizes the array to C<size> elements.
298
299
=cut
300
301
*/
302
303
VTABLE void set_integer_native(INTVAL new_size) {
304
INTVAL old_size;
305
GET_ATTR_size(INTERP, SELF, old_size);
306
if (old_size || new_size < 1)
307
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
308
"FixedFloatArray: Can't resize!");
310
SET_ATTR_size(INTERP, SELF, new_size);
311
SET_ATTR_float_array(INTERP, SELF,
312
mem_gc_allocate_n_typed(INTERP, new_size, FLOATVAL));
314
}
315
316
/*
317
318
=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
319
320
Sets the integer value of the element at index C<key> to C<value>.
321
322
=cut
323
324
*/
325
326
VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
327
SELF.set_number_keyed_int(key, (FLOATVAL)value);
328
}
329
330
/*
331
332
=item C<void set_integer_keyed(PMC *key, INTVAL value)>
333
334
Sets the integer value of the element at index C<key> to C<value>.
335
336
=cut
337
338
*/
339
340
VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
341
const INTVAL k = VTABLE_get_integer(INTERP, key);
342
SELF.set_integer_keyed_int(k, value);
343
}
344
345
/*
346
347
=item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
348
349
Sets the floating-point value of the element at index C<key> to
350
C<value>.
351
352
=cut
353
354
*/
355
356
VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
357
FLOATVAL *float_array;
358
INTVAL size;
359
360
GET_ATTR_size(INTERP, SELF, size);
361
if (key < 0 || key >= size)
362
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
363
"FixedFloatArray: index out of bounds!");
365
GET_ATTR_float_array(INTERP, SELF, float_array);
366
float_array[key] = value;
367
}
368
369
/*
370
371
=item C<void set_number_keyed(PMC *key, FLOATVAL value)>
372
373
Sets the floating-point value of the element at index C<key> to
374
C<value>.
375
376
=cut
377
378
*/
379
380
VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
381
const INTVAL k = VTABLE_get_integer(INTERP, key);
382
SELF.set_number_keyed_int(k, value);
385
/*
386
387
=item C<void set_string_keyed_int(INTVAL key, STRING *value)>
388
389
Sets the Parrot string value of the element at index C<key> to C<value>.
390
391
=cut
392
393
*/
394
395
VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
397
PMC * const tempPMC = Parrot_pmc_new(INTERP, enum_class_Float);
398
399
VTABLE_set_string_native(INTERP, tempPMC, value);
400
tempNum = VTABLE_get_number(INTERP, tempPMC);
401
SELF.set_number_keyed_int(key, tempNum);
406
=item C<void set_string_keyed(PMC *key, STRING *value)>
407
408
Sets the string value of the element at index C<key> to
409
C<value>.
410
411
=cut
412
413
*/
414
415
VTABLE void set_string_keyed(PMC *key, STRING *value) {
416
const INTVAL k = VTABLE_get_integer(INTERP, key);
417
SELF.set_string_keyed_int(k, value);
418
}
419
420
/*
421
422
=item C<void set_pmc_keyed_int(INTVAL key, PMC *src)>
423
424
Sets the PMC value of the element at index C<key> to C<*src>.
425
426
=cut
427
428
*/
429
430
VTABLE void set_pmc_keyed_int(INTVAL key, PMC *src) {
431
const FLOATVAL tempNum = VTABLE_get_number(INTERP, src);
432
SELF.set_number_keyed_int(key, tempNum);
437
=item C<void set_pmc_keyed(PMC *key, PMC *value)>
438
439
Sets the string value of the element at index C<key> to
440
C<value>.
441
442
=cut
443
444
*/
445
446
VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
447
const INTVAL k = VTABLE_get_integer(INTERP, key);
448
SELF.set_pmc_keyed_int(k, value);
449
}
450
451
}
452
453
/*
454
455
=back
456
457
=head1 SEE ALSO
458
459
F<docs/pdds/pdd17_basic_types.pod>.
460
461
=cut
462
463
*/
464
465
/*
466
* Local variables:
467
* c-file-style: "parrot"
469
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :