mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
mrbgems/mruby-array-ext/array.c@1:8ccd1d494a4b, 2015-04-13 (annotated)
- Committer:
- mzta
- Date:
- Mon Apr 13 05:20:15 2015 +0000
- Revision:
- 1:8ccd1d494a4b
- Parent:
- 0:158c61bb030f
- code refactoring.; - add SPI, SPISlave, I2C class to mruby-mbed (Incomplete).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | #include "mruby.h" |
mzta | 0:158c61bb030f | 2 | #include "mruby/value.h" |
mzta | 0:158c61bb030f | 3 | #include "mruby/array.h" |
mzta | 0:158c61bb030f | 4 | #include "mruby/range.h" |
mzta | 0:158c61bb030f | 5 | #include "mruby/hash.h" |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | /* |
mzta | 0:158c61bb030f | 8 | * call-seq: |
mzta | 0:158c61bb030f | 9 | * ary.assoc(obj) -> new_ary or nil |
mzta | 0:158c61bb030f | 10 | * |
mzta | 0:158c61bb030f | 11 | * Searches through an array whose elements are also arrays |
mzta | 0:158c61bb030f | 12 | * comparing _obj_ with the first element of each contained array |
mzta | 0:158c61bb030f | 13 | * using obj.==. |
mzta | 0:158c61bb030f | 14 | * Returns the first contained array that matches (that |
mzta | 0:158c61bb030f | 15 | * is, the first associated array), |
mzta | 0:158c61bb030f | 16 | * or +nil+ if no match is found. |
mzta | 0:158c61bb030f | 17 | * See also <code>Array#rassoc</code>. |
mzta | 0:158c61bb030f | 18 | * |
mzta | 0:158c61bb030f | 19 | * s1 = [ "colors", "red", "blue", "green" ] |
mzta | 0:158c61bb030f | 20 | * s2 = [ "letters", "a", "b", "c" ] |
mzta | 0:158c61bb030f | 21 | * s3 = "foo" |
mzta | 0:158c61bb030f | 22 | * a = [ s1, s2, s3 ] |
mzta | 0:158c61bb030f | 23 | * a.assoc("letters") #=> [ "letters", "a", "b", "c" ] |
mzta | 0:158c61bb030f | 24 | * a.assoc("foo") #=> nil |
mzta | 0:158c61bb030f | 25 | */ |
mzta | 0:158c61bb030f | 26 | |
mzta | 0:158c61bb030f | 27 | static mrb_value |
mzta | 0:158c61bb030f | 28 | mrb_ary_assoc(mrb_state *mrb, mrb_value ary) |
mzta | 0:158c61bb030f | 29 | { |
mzta | 0:158c61bb030f | 30 | mrb_int i; |
mzta | 0:158c61bb030f | 31 | mrb_value v, k; |
mzta | 0:158c61bb030f | 32 | |
mzta | 0:158c61bb030f | 33 | mrb_get_args(mrb, "o", &k); |
mzta | 0:158c61bb030f | 34 | |
mzta | 0:158c61bb030f | 35 | for (i = 0; i < RARRAY_LEN(ary); ++i) { |
mzta | 0:158c61bb030f | 36 | v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); |
mzta | 0:158c61bb030f | 37 | if (!mrb_nil_p(v) && RARRAY_LEN(v) > 0 && |
mzta | 0:158c61bb030f | 38 | mrb_equal(mrb, RARRAY_PTR(v)[0], k)) |
mzta | 0:158c61bb030f | 39 | return v; |
mzta | 0:158c61bb030f | 40 | } |
mzta | 0:158c61bb030f | 41 | return mrb_nil_value(); |
mzta | 0:158c61bb030f | 42 | } |
mzta | 0:158c61bb030f | 43 | |
mzta | 0:158c61bb030f | 44 | /* |
mzta | 0:158c61bb030f | 45 | * call-seq: |
mzta | 0:158c61bb030f | 46 | * ary.rassoc(obj) -> new_ary or nil |
mzta | 0:158c61bb030f | 47 | * |
mzta | 0:158c61bb030f | 48 | * Searches through the array whose elements are also arrays. Compares |
mzta | 0:158c61bb030f | 49 | * _obj_ with the second element of each contained array using |
mzta | 0:158c61bb030f | 50 | * <code>==</code>. Returns the first contained array that matches. See |
mzta | 0:158c61bb030f | 51 | * also <code>Array#assoc</code>. |
mzta | 0:158c61bb030f | 52 | * |
mzta | 0:158c61bb030f | 53 | * a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ] |
mzta | 0:158c61bb030f | 54 | * a.rassoc("two") #=> [2, "two"] |
mzta | 0:158c61bb030f | 55 | * a.rassoc("four") #=> nil |
mzta | 0:158c61bb030f | 56 | */ |
mzta | 0:158c61bb030f | 57 | |
mzta | 0:158c61bb030f | 58 | static mrb_value |
mzta | 0:158c61bb030f | 59 | mrb_ary_rassoc(mrb_state *mrb, mrb_value ary) |
mzta | 0:158c61bb030f | 60 | { |
mzta | 0:158c61bb030f | 61 | mrb_int i; |
mzta | 0:158c61bb030f | 62 | mrb_value v, value; |
mzta | 0:158c61bb030f | 63 | |
mzta | 0:158c61bb030f | 64 | mrb_get_args(mrb, "o", &value); |
mzta | 0:158c61bb030f | 65 | |
mzta | 0:158c61bb030f | 66 | for (i = 0; i < RARRAY_LEN(ary); ++i) { |
mzta | 0:158c61bb030f | 67 | v = RARRAY_PTR(ary)[i]; |
mzta | 0:158c61bb030f | 68 | if (mrb_type(v) == MRB_TT_ARRAY && |
mzta | 0:158c61bb030f | 69 | RARRAY_LEN(v) > 1 && |
mzta | 0:158c61bb030f | 70 | mrb_equal(mrb, RARRAY_PTR(v)[1], value)) |
mzta | 0:158c61bb030f | 71 | return v; |
mzta | 0:158c61bb030f | 72 | } |
mzta | 0:158c61bb030f | 73 | return mrb_nil_value(); |
mzta | 0:158c61bb030f | 74 | } |
mzta | 0:158c61bb030f | 75 | |
mzta | 0:158c61bb030f | 76 | /* |
mzta | 0:158c61bb030f | 77 | * call-seq: |
mzta | 0:158c61bb030f | 78 | * ary.at(index) -> obj or nil |
mzta | 0:158c61bb030f | 79 | * |
mzta | 0:158c61bb030f | 80 | * Returns the element at _index_. A |
mzta | 0:158c61bb030f | 81 | * negative index counts from the end of +self+. Returns +nil+ |
mzta | 0:158c61bb030f | 82 | * if the index is out of range. See also <code>Array#[]</code>. |
mzta | 0:158c61bb030f | 83 | * |
mzta | 0:158c61bb030f | 84 | * a = [ "a", "b", "c", "d", "e" ] |
mzta | 0:158c61bb030f | 85 | * a.at(0) #=> "a" |
mzta | 0:158c61bb030f | 86 | * a.at(-1) #=> "e" |
mzta | 0:158c61bb030f | 87 | */ |
mzta | 0:158c61bb030f | 88 | |
mzta | 0:158c61bb030f | 89 | static mrb_value |
mzta | 0:158c61bb030f | 90 | mrb_ary_at(mrb_state *mrb, mrb_value ary) |
mzta | 0:158c61bb030f | 91 | { |
mzta | 0:158c61bb030f | 92 | mrb_int pos; |
mzta | 0:158c61bb030f | 93 | mrb_get_args(mrb, "i", &pos); |
mzta | 0:158c61bb030f | 94 | |
mzta | 0:158c61bb030f | 95 | return mrb_ary_entry(ary, pos); |
mzta | 0:158c61bb030f | 96 | } |
mzta | 0:158c61bb030f | 97 | |
mzta | 0:158c61bb030f | 98 | static mrb_value |
mzta | 0:158c61bb030f | 99 | mrb_ary_values_at(mrb_state *mrb, mrb_value self) |
mzta | 0:158c61bb030f | 100 | { |
mzta | 0:158c61bb030f | 101 | mrb_int argc; |
mzta | 0:158c61bb030f | 102 | mrb_value *argv; |
mzta | 0:158c61bb030f | 103 | |
mzta | 0:158c61bb030f | 104 | mrb_get_args(mrb, "*", &argv, &argc); |
mzta | 0:158c61bb030f | 105 | |
mzta | 0:158c61bb030f | 106 | return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref); |
mzta | 0:158c61bb030f | 107 | } |
mzta | 0:158c61bb030f | 108 | |
mzta | 0:158c61bb030f | 109 | /* |
mzta | 0:158c61bb030f | 110 | * call-seq: |
mzta | 0:158c61bb030f | 111 | * ary.to_h -> Hash |
mzta | 0:158c61bb030f | 112 | * |
mzta | 0:158c61bb030f | 113 | * Returns the result of interpreting <i>aray</i> as an array of |
mzta | 0:158c61bb030f | 114 | * <tt>[key, value]</tt> paris. |
mzta | 0:158c61bb030f | 115 | * |
mzta | 0:158c61bb030f | 116 | * [[:foo, :bar], [1, 2]].to_h |
mzta | 0:158c61bb030f | 117 | * # => {:foo => :bar, 1 => 2} |
mzta | 0:158c61bb030f | 118 | */ |
mzta | 0:158c61bb030f | 119 | |
mzta | 0:158c61bb030f | 120 | static mrb_value |
mzta | 0:158c61bb030f | 121 | mrb_ary_to_h(mrb_state *mrb, mrb_value ary) |
mzta | 0:158c61bb030f | 122 | { |
mzta | 0:158c61bb030f | 123 | mrb_int i; |
mzta | 0:158c61bb030f | 124 | mrb_value v, hash; |
mzta | 0:158c61bb030f | 125 | |
mzta | 0:158c61bb030f | 126 | hash = mrb_hash_new_capa(mrb, 0); |
mzta | 0:158c61bb030f | 127 | |
mzta | 0:158c61bb030f | 128 | for (i = 0; i < RARRAY_LEN(ary); ++i) { |
mzta | 0:158c61bb030f | 129 | v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); |
mzta | 0:158c61bb030f | 130 | |
mzta | 0:158c61bb030f | 131 | if (mrb_nil_p(v)) { |
mzta | 0:158c61bb030f | 132 | mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)", |
mzta | 0:158c61bb030f | 133 | mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, RARRAY_PTR(ary)[i])), |
mzta | 0:158c61bb030f | 134 | mrb_fixnum_value(i) |
mzta | 0:158c61bb030f | 135 | ); |
mzta | 0:158c61bb030f | 136 | } |
mzta | 0:158c61bb030f | 137 | |
mzta | 0:158c61bb030f | 138 | if (RARRAY_LEN(v) != 2) { |
mzta | 0:158c61bb030f | 139 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)", |
mzta | 0:158c61bb030f | 140 | mrb_fixnum_value(i), |
mzta | 0:158c61bb030f | 141 | mrb_fixnum_value(RARRAY_LEN(v)) |
mzta | 0:158c61bb030f | 142 | ); |
mzta | 0:158c61bb030f | 143 | } |
mzta | 0:158c61bb030f | 144 | |
mzta | 0:158c61bb030f | 145 | mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]); |
mzta | 0:158c61bb030f | 146 | } |
mzta | 0:158c61bb030f | 147 | |
mzta | 0:158c61bb030f | 148 | return hash; |
mzta | 0:158c61bb030f | 149 | } |
mzta | 0:158c61bb030f | 150 | |
mzta | 0:158c61bb030f | 151 | void |
mzta | 0:158c61bb030f | 152 | mrb_mruby_array_ext_gem_init(mrb_state* mrb) |
mzta | 0:158c61bb030f | 153 | { |
mzta | 0:158c61bb030f | 154 | struct RClass * a = mrb->array_class; |
mzta | 0:158c61bb030f | 155 | |
mzta | 0:158c61bb030f | 156 | mrb_define_method(mrb, a, "assoc", mrb_ary_assoc, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 157 | mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 158 | mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 159 | mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); |
mzta | 0:158c61bb030f | 160 | mrb_define_method(mrb, a, "to_h", mrb_ary_to_h, MRB_ARGS_REQ(0)); |
mzta | 0:158c61bb030f | 161 | } |
mzta | 0:158c61bb030f | 162 | |
mzta | 0:158c61bb030f | 163 | void |
mzta | 0:158c61bb030f | 164 | mrb_mruby_array_ext_gem_final(mrb_state* mrb) |
mzta | 0:158c61bb030f | 165 | { |
mzta | 0:158c61bb030f | 166 | } |
mzta | 0:158c61bb030f | 167 |