Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-array-ext/src/array.c

raw
1 #include <mruby.h>
2 #include <mruby/value.h>
3 #include <mruby/array.h>
4 #include <mruby/range.h>
5 #include <mruby/hash.h>
6 #include <mruby/internal.h>
7 #include <mruby/presym.h>
8
9 /*
10 * call-seq:
11 * ary.assoc(obj) -> new_ary or nil
12 *
13 * Searches through an array whose elements are also arrays
14 * comparing _obj_ with the first element of each contained array
15 * using obj.==.
16 * Returns the first contained array that matches (that
17 * is, the first associated array),
18 * or +nil+ if no match is found.
19 * See also <code>Array#rassoc</code>.
20 *
21 * s1 = [ "colors", "red", "blue", "green" ]
22 * s2 = [ "letters", "a", "b", "c" ]
23 * s3 = "foo"
24 * a = [ s1, s2, s3 ]
25 * a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
26 * a.assoc("foo") #=> nil
27 */
28
29 static mrb_value
30 ary_assoc(mrb_state *mrb, mrb_value ary)
31 {
32 mrb_int i;
33 mrb_value v;
34 mrb_value k = mrb_get_arg1(mrb);
35
36 for (i = 0; i < RARRAY_LEN(ary); i++) {
37 v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]);
38 if (!mrb_nil_p(v) && RARRAY_LEN(v) > 0 &&
39 mrb_equal(mrb, RARRAY_PTR(v)[0], k))
40 return v;
41 }
42 return mrb_nil_value();
43 }
44
45 /*
46 * call-seq:
47 * ary.rassoc(obj) -> new_ary or nil
48 *
49 * Searches through the array whose elements are also arrays. Compares
50 * _obj_ with the second element of each contained array using
51 * <code>==</code>. Returns the first contained array that matches. See
52 * also <code>Array#assoc</code>.
53 *
54 * a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
55 * a.rassoc("two") #=> [2, "two"]
56 * a.rassoc("four") #=> nil
57 */
58
59 static mrb_value
60 ary_rassoc(mrb_state *mrb, mrb_value ary)
61 {
62 mrb_int i;
63 mrb_value v;
64 mrb_value value = mrb_get_arg1(mrb);
65
66 for (i = 0; i < RARRAY_LEN(ary); i++) {
67 v = RARRAY_PTR(ary)[i];
68 if (mrb_array_p(v) &&
69 RARRAY_LEN(v) > 1 &&
70 mrb_equal(mrb, RARRAY_PTR(v)[1], value))
71 return v;
72 }
73 return mrb_nil_value();
74 }
75
76 /*
77 * call-seq:
78 * ary.at(index) -> obj or nil
79 *
80 * Returns the element at _index_. A
81 * negative index counts from the end of +self+. Returns +nil+
82 * if the index is out of range. See also <code>Array#[]</code>.
83 *
84 * a = [ "a", "b", "c", "d", "e" ]
85 * a.at(0) #=> "a"
86 * a.at(-1) #=> "e"
87 */
88
89 static mrb_value
90 ary_at(mrb_state *mrb, mrb_value ary)
91 {
92 mrb_int pos = mrb_as_int(mrb, mrb_get_arg1(mrb));
93
94 return mrb_ary_entry(ary, pos);
95 }
96
97 static mrb_value
98 ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n)
99 {
100 return mrb_ary_entry(ary, n);
101 }
102
103 static mrb_value
104 ary_values_at(mrb_state *mrb, mrb_value self)
105 {
106 mrb_int argc = mrb_get_argc(mrb);
107 const mrb_value *argv = mrb_get_argv(mrb);
108
109 return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, ary_ref);
110 }
111
112 mrb_value mrb_ary_delete_at(mrb_state *mrb, mrb_value self);
113
114 /*
115 * call-seq:
116 * ary.slice!(index) -> obj or nil
117 * ary.slice!(start, length) -> new_ary or nil
118 * ary.slice!(range) -> new_ary or nil
119 *
120 * Deletes the element(s) given by an +index+ (optionally up to +length+
121 * elements) or by a +range+.
122 *
123 * Returns the deleted object (or objects), or +nil+ if the +index+ is out of
124 * range.
125 *
126 * a = [ "a", "b", "c" ]
127 * a.slice!(1) #=> "b"
128 * a #=> ["a", "c"]
129 * a.slice!(-1) #=> "c"
130 * a #=> ["a"]
131 * a.slice!(100) #=> nil
132 * a #=> ["a"]
133 */
134
135 static mrb_value
136 ary_slice_bang(mrb_state *mrb, mrb_value self)
137 {
138 struct RArray *a = mrb_ary_ptr(self);
139 mrb_int i, j, len, alen;
140 mrb_value *ptr;
141 mrb_value ary;
142
143 mrb_ary_modify(mrb, a);
144
145 if (mrb_get_argc(mrb) == 1) {
146 mrb_value index = mrb_get_arg1(mrb);
147
148 if (mrb_type(index) == MRB_TT_RANGE) {
149 if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) {
150 goto delete_pos_len;
151 }
152 return mrb_nil_value();
153 }
154 return mrb_ary_delete_at(mrb, self);
155 }
156
157 mrb_get_args(mrb, "ii", &i, &len);
158 delete_pos_len:
159 alen = ARY_LEN(a);
160 if (i < 0) i += alen;
161 if (i < 0 || alen < i) return mrb_nil_value();
162 if (len < 0) return mrb_nil_value();
163 if (alen == i) return mrb_ary_new(mrb);
164 if (len > alen - i) len = alen - i;
165
166 ptr = ARY_PTR(a) + i;
167 ary = mrb_ary_new_from_values(mrb, len, ptr);
168
169 for (j = i; j < alen - len; j++) {
170 *ptr = *(ptr+len);
171 ptr++;
172 }
173
174 mrb_ary_resize(mrb, self, alen - len);
175 return ary;
176 }
177
178 /*
179 * call-seq:
180 * ary.compact -> new_ary
181 *
182 * Returns a copy of +self+ with all +nil+ elements removed.
183 *
184 * [ "a", nil, "b", nil, "c", nil ].compact
185 * #=> [ "a", "b", "c" ]
186 */
187
188 static mrb_value
189 ary_compact(mrb_state *mrb, mrb_value self)
190 {
191 mrb_value ary = mrb_ary_new(mrb);
192 mrb_int len = RARRAY_LEN(self);
193 mrb_value *p = RARRAY_PTR(self);
194
195 for (mrb_int i = 0; i < len; i++) {
196 if (!mrb_nil_p(p[i])) {
197 mrb_ary_push(mrb, ary, p[i]);
198 }
199 }
200 return ary;
201 }
202
203 /*
204 * call-seq:
205 * ary.compact! -> ary or nil
206 *
207 * Removes +nil+ elements from the array.
208 * Returns +nil+ if no changes were made, otherwise returns
209 * <i>ary</i>.
210 *
211 * [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
212 * [ "a", "b", "c" ].compact! #=> nil
213 */
214 static mrb_value
215 ary_compact_bang(mrb_state *mrb, mrb_value self)
216 {
217 struct RArray *a = mrb_ary_ptr(self);
218 mrb_int i, j = 0;
219 mrb_int len = ARY_LEN(a);
220
221 mrb_ary_modify(mrb, a);
222 mrb_value *p = ARY_PTR(a);
223 for (i = 0; i < len; i++) {
224 if (!mrb_nil_p(p[i])) {
225 if (i != j) p[j] = p[i];
226 j++;
227 }
228 }
229 if (i == j) return mrb_nil_value();
230 ARY_SET_LEN(RARRAY(self), j);
231 return self;
232 }
233
234
235 /*
236 * call-seq:
237 * ary.rotate(count=1) -> new_ary
238 *
239 * Returns a new array by rotating +self+ so that the element at +count+ is
240 * the first element of the new array.
241 *
242 * If +count+ is negative then it rotates in the opposite direction, starting
243 * from the end of +self+ where +-1+ is the last element.
244 *
245 * a = [ "a", "b", "c", "d" ]
246 * a.rotate #=> ["b", "c", "d", "a"]
247 * a #=> ["a", "b", "c", "d"]
248 * a.rotate(2) #=> ["c", "d", "a", "b"]
249 * a.rotate(-3) #=> ["b", "c", "d", "a"]
250 */
251 static mrb_value
252 ary_rotate(mrb_state *mrb, mrb_value self)
253 {
254 mrb_int count=1;
255 mrb_get_args(mrb, "|i", &count);
256
257 mrb_value ary = mrb_ary_new(mrb);
258 mrb_int len = RARRAY_LEN(self);
259 mrb_value *p = RARRAY_PTR(self);
260 mrb_int idx;
261
262 if (len <= 0) return ary;
263 if (count < 0) {
264 idx = len - (~count % len) - 1;
265 }
266 else {
267 idx = count % len;
268 }
269 for (mrb_int i = 0; i<len; i++) {
270 mrb_ary_push(mrb, ary, p[idx++]);
271 if (idx == len) idx = 0;
272 }
273 return ary;
274 }
275
276 static void
277 rev(mrb_value *p, mrb_int beg, mrb_int end)
278 {
279 for (mrb_int i=beg,j=end-1; i<j; i++,j--) {
280 mrb_value v = p[i];
281 p[i] = p[j];
282 p[j] = v;
283 }
284 }
285
286 /*
287 * call-seq:
288 * ary.rotate!(count=1) -> ary
289 *
290 * Rotates +self+ in place so that the element at +count+ comes first, and
291 * returns +self+.
292 *
293 * If +count+ is negative then it rotates in the opposite direction, starting
294 * from the end of the array where +-1+ is the last element.
295 *
296 * a = [ "a", "b", "c", "d" ]
297 * a.rotate! #=> ["b", "c", "d", "a"]
298 * a #=> ["b", "c", "d", "a"]
299 * a.rotate!(2) #=> ["d", "a", "b", "c"]
300 * a.rotate!(-3) #=> ["a", "b", "c", "d"]
301 */
302 static mrb_value
303 ary_rotate_bang(mrb_state *mrb, mrb_value self)
304 {
305 mrb_int count=1;
306 mrb_get_args(mrb, "|i", &count);
307
308 struct RArray *a = mrb_ary_ptr(self);
309 mrb_int len = ARY_LEN(a);
310 mrb_int idx;
311
312 mrb_ary_modify(mrb, a);
313 mrb_value *p = ARY_PTR(a);
314 if (len == 0 || count == 0) return self;
315 if (count == 1) {
316 mrb_value v = p[0];
317 for (mrb_int i=1; i<len; i++) {
318 p[i-1] = p[i];
319 }
320 p[len-1] = v;
321 return self;
322 }
323 if (count < 0) {
324 idx = len - (~count % len) - 1;
325 }
326 else {
327 idx = count % len;
328 }
329 /* e.g. [1,2,3,4,5].rotate!(2) -> [3,4,5,1,2] */
330 /* first, reverse the whole array */
331 /* [1,2,3,4,5] -> [5,4,3,2,1] */
332 rev(p, 0, len);
333 /* then, re-reverse part before idx */
334 /* [5,4,3,2,1] -> [3,4,5,2,1] */
335 /* ^idx ~~~~~ */
336 rev(p, 0, len-idx);
337 /* finally, re-reverse part after idx */
338 /* [3,4,5,2,1] -> [3,4,5,1,2] */
339 /* ^idx ~~~ */
340 rev(p, len-idx, len);
341 return self;
342 }
343
344 void
345 mrb_mruby_array_ext_gem_init(mrb_state* mrb)
346 {
347 struct RClass * a = mrb->array_class;
348
349 mrb_define_method(mrb, a, "assoc", ary_assoc, MRB_ARGS_REQ(1));
350 mrb_define_method(mrb, a, "at", ary_at, MRB_ARGS_REQ(1));
351 mrb_define_method(mrb, a, "rassoc", ary_rassoc, MRB_ARGS_REQ(1));
352 mrb_define_method(mrb, a, "values_at", ary_values_at, MRB_ARGS_ANY());
353 mrb_define_method(mrb, a, "slice!", ary_slice_bang, MRB_ARGS_ARG(1,1));
354 mrb_define_method(mrb, a, "compact", ary_compact, MRB_ARGS_NONE());
355 mrb_define_method(mrb, a, "compact!", ary_compact_bang, MRB_ARGS_NONE());
356 mrb_define_method(mrb, a, "rotate", ary_rotate, MRB_ARGS_OPT(1));
357 mrb_define_method(mrb, a, "rotate!", ary_rotate_bang, MRB_ARGS_OPT(1));
358 }
359
360 void
361 mrb_mruby_array_ext_gem_final(mrb_state* mrb)
362 {
363 }