-
Notifications
You must be signed in to change notification settings - Fork 138
/
resizableintegerarray.pmc
317 lines (218 loc) · 7.06 KB
/
resizableintegerarray.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (C) 2001-2011, Parrot Foundation.
=head1 NAME
src/pmc/resizableintegerarray.pmc - resizable array for integers only
=head1 DESCRIPTION
This class, ResizableIntegerArray, implements an array of resizable
size, which stores INTVALs. It uses Integer PMCs for all of the
conversions.
=head2 Functions
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass ResizableIntegerArray extends FixedIntegerArray auto_attrs provides array {
ATTR INTVAL resize_threshold; /* max size before array needs to be resized */
/*
=item C<void init_int(INTVAL size)>
Initializes the array.
=cut
*/
VTABLE void init_int(INTVAL size) {
SUPER(size);
if (size > 0)
SET_ATTR_resize_threshold(INTERP, SELF, size);
}
/*
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
Returns the integer value of the element at index C<key>.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
INTVAL *int_array;
INTVAL size;
if (key < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: index out of bounds!");
GET_ATTR_size(INTERP, SELF, size);
if (key >= size)
return 0;
GET_ATTR_int_array(INTERP, SELF, int_array);
return int_array[key];
}
/*
=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
Sets the integer value of the element at index C<key> to C<value>.
=cut
*/
VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
INTVAL *int_array;
if (key < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: index out of bounds!");
if (key >= SELF.get_integer())
SELF.set_integer_native(key + 1);
GET_ATTR_int_array(INTERP, SELF, int_array);
int_array[key] = value;
}
/*
=item C<void set_integer_native(INTVAL size)>
Resizes the array to C<size> elements.
=cut
*/
VTABLE void set_integer_native(INTVAL size) {
INTVAL *int_array;
INTVAL resize_threshold;
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: Can't resize!");
GET_ATTR_int_array(INTERP, SELF, int_array);
GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold);
if (!int_array) {
/* empty - used fixed routine */
if (size < 8) {
SUPER(8);
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, 8);
}
else {
SUPER(size);
SET_ATTR_resize_threshold(INTERP, SELF, size);
}
}
else if (size <= resize_threshold) {
/* we could shrink here if necessary */
SET_ATTR_size(INTERP, SELF, size);
return;
}
else {
INTVAL cur = resize_threshold;
INTVAL *int_array;
if (cur < 8192)
cur = size < 2 * cur ? 2 * cur : size;
else {
const INTVAL needed = size - cur;
cur += needed + 4096;
cur &= ~0xfff;
}
GET_ATTR_int_array(INTERP, SELF, int_array);
int_array = mem_gc_realloc_n_typed(INTERP, int_array, cur, INTVAL);
SET_ATTR_int_array(INTERP, SELF, int_array);
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, cur);
}
}
/*
=item C<void push_integer(INTVAL value)>
Extends the array by adding an element of value C<value> to the end
of the array.
=cut
*/
VTABLE void push_integer(INTVAL value) {
INTVAL *int_array;
INTVAL nextix;
INTVAL resize_threshold;
GET_ATTR_size(INTERP, SELF, nextix);
GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold);
/* can't always avoid the resize, but don't duplicate the code */
if ((nextix && (nextix >= resize_threshold)) || !nextix)
SELF.set_integer_native(nextix + 1);
else
SET_ATTR_size(INTERP, SELF, nextix + 1);
/* fetch the array only after resize check; realloc may move it */
GET_ATTR_int_array(INTERP, SELF, int_array);
int_array[nextix] = value;
}
/*
=item C<INTVAL pop_integer()>
Removes and returns the last element in the array.
=cut
*/
VTABLE INTVAL pop_integer() {
INTVAL value;
INTVAL size = SELF.get_integer();
if (size == 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: Can't pop from an empty array!");
value = SELF.get_integer_keyed_int(size-1);
SELF.set_integer_native(size - 1);
return value;
}
/*
=item C<INTVAL shift_integer()>
Removes and returns an item from the start of the array.
=cut
*/
VTABLE INTVAL shift_integer() {
INTVAL value, *int_array;
INTVAL size = SELF.get_integer();
if (size == 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: Can't shift from an empty array!");
GET_ATTR_int_array(INTERP, SELF, int_array);
value = int_array[0];
--size;
memmove(int_array, int_array + 1, size * sizeof (INTVAL));
SELF.set_integer_native(size);
return value;
}
/*
=item C<void unshift_integer(INTVAL value)>
Add an integer to the start of the array.
=cut
*/
VTABLE void unshift_integer(INTVAL value) {
INTVAL *int_array;
const INTVAL size = SELF.get_integer();
SELF.set_integer_native(size + 1);
GET_ATTR_int_array(INTERP, SELF, int_array);
memmove(int_array + 1, int_array, size * sizeof (INTVAL));
int_array[0] = value;
}
/*
=item C<void delete_keyed_int(INTVAL key)>
Removes the element at C<key>.
=cut
*/
VTABLE void delete_keyed_int(INTVAL key) {
INTVAL size = SELF.get_integer();
if (size < 1 || key >= size) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableIntegerArray: index out of bounds!");
}
else {
INTVAL *int_array;
GET_ATTR_int_array(INTERP, SELF, int_array);
--size;
memmove(int_array + key, int_array + key + 1, (size - key) * sizeof (INTVAL));
SELF.set_integer_native(size);
}
}
/*
=item C<PMC *clone()>
Creates and returns a copy of the array.
=cut
*/
VTABLE PMC *clone() {
INTVAL size;
PMC * const copy = SUPER();
/* copy trimmed extra space */
GET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, copy, size);
return copy;
}
}
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd17_basic_types.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/