Add methods for converting between Collections (asBag, asSet, etc)
[panda.git] / src / st-float-array.c
blob2d3cf8b9ecaf1ed513267c297079d82d225948ca
1 /*
2 * st-float-array.c
4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "st-float-array.h"
26 #include "st-object.h"
28 static st_oop
29 allocate_arrayed (st_space *space, st_oop class, st_smi size)
31 st_oop object;
32 double *elements;
33 st_smi size_oops;
35 st_assert (size >= 0);
37 /* get actual size in oops (dependent on whether system is 64bit or 32bit) */
38 size_oops = size * (sizeof (double) / sizeof (st_oop));
40 object = st_space_allocate_object (space, class, ST_SIZE_OOPS (struct st_float_array) + size_oops);
41 ST_ARRAYED_OBJECT (object)->size = st_smi_new (size);
43 elements = ST_FLOAT_ARRAY (object)->elements;
44 for (st_smi i = 0; i < size; i++)
45 elements[i] = (double) 0;
47 return object;
50 static st_oop
51 float_array_copy (st_oop object)
53 st_oop copy;
54 st_smi size;
56 size = st_smi_value (st_arrayed_object_size (object));
58 copy = allocate_arrayed (memory->moving_space, ST_HEADER (object)->class, size);
60 memcpy (st_float_array_elements (copy),
61 st_float_array_elements (object),
62 sizeof (double) * size);
64 return copy;
67 static st_uint
68 float_array_size (st_oop object)
70 return ST_SIZE_OOPS (struct st_arrayed_object) + (st_smi_value (st_arrayed_object_size (object)) * ST_SIZE_OOPS (double));
73 static void
74 float_array_contents (st_oop object, struct contents *contents)
76 contents->oops = NULL;
77 contents->size = 0;
80 st_descriptor *
81 st_float_array_descriptor (void)
83 static st_descriptor __descriptor =
84 { .allocate = NULL,
85 .allocate_arrayed = allocate_arrayed,
86 .copy = float_array_copy,
87 .size = float_array_size,
88 .contents = float_array_contents,
91 return & __descriptor;