Share
Embed
rarray_value.pdf
morihirok
February 23, 2020
Transcript
18 slides
-
ഁյతϝιουͱඇഁյతϝ ιου • ඇഁյతϝιουϨγʔόͷঢ়ଶʹӨڹΛٴ΅ ͢ • Array#+ ඇഁյతϝιου • ഁյతϝιουϨγʔόͷঢ়ଶʹӨڹΛٴ΅͢
• Array#concatഁյతϝιου -
#+ ͱ #concat ͷҧ͍ ary1 = ['hoge', 'fuga'] ary2 =
['foo', ‘bar'] ary3 = ary1 + ary2 p ary3 => ["hoge", "fuga", "foo", "bar"] p ary1 => ["hoge", “fuga"] ary1.concat(ary2) p ary1 => ["hoge", "fuga", "foo", "bar"] -
ඇഁյతϝιουϝϞϦޮ ͕ѱ͍ͷͰʁ ary1 = ['hoge', 'fuga'] ary2 = ['foo', ‘bar']
ary3 = ary1 + ary2 p ary3 => ["hoge", "fuga", "foo", "bar"] p ary1 => ["hoge", “fuga"] ary1.concat(ary2) p ary1 => ["hoge", "fuga", "foo", "bar"] ৽ͨͳྻΛੜ͍ͯ͠Δ -
ͱ͍͏Θ͚Ͱௐࠪ ary1 = ['hoge', 'fuga'] ary2 = ['foo', 'bar'] ary1.object_id
=> 180 ary1.map(&:object_id) => [200, 220] ary2.object_id => 240 ary2.map(&:object_id) => [260, 280] -
Array#concat ͷ߹ p ary1.object_id => 180 p ary1.map(&:object_id) => [200,
220] p ary2.map(&:object_id) => [260, 280] ary1.concat(ary2) p ary1.object_id => 180 p ary1.map(&:object_id) => [200, 220, 260, 280] ಉҰͷΦϒδΣΫτΛอ࣋͢Δ ྻ͕ -
Array#+ ͷ߹ p ary1.map(&:object_id) => [200, 220] p ary2.map(&:object_id) =>
[260, 280] ary3 = ary1 + ary2 ary3.object_id => 300 ary3.map(&:object_id) => [200, 220, 260, 280] ಉҰͷΦϒδΣΫτΛอ࣋͢Δ ྻ͕ ৽͍͠ΦϒδΣΫτ͕ੜ͞Ε ͦΕΛอ࣋͢Δྻ͕Ͱ͖Δͱ ࢥ͍ͬͯͨʜ -
Arrayͷߏମʹ͍ͭͯௐΔ struct RArray { struct RBasic basic; union { struct
{ long len; union { long capa; #if defined(__clang__) /* <- clang++ is sane */ || \ !defined(__cplusplus) /* <- C99 is sane */ || \ (__cplusplus > 199711L) /* <- C++11 is sane */ const #endif VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; -
Arrayͷߏମʹ͍ͭͯௐΔ struct RArray { struct RBasic basic; union { struct
{ long len; union { long capa; #if defined(__clang__) /* <- clang++ is sane */ || \ !defined(__cplusplus) /* <- C99 is sane */ || \ (__cplusplus > 199711L) /* <- C++11 is sane */ const #endif VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ϫʔΫΞϥϯυͷΑ͏ͳͷͰ ಡΈਐΊ͍ͯ͘ʹ͋ͨͬͯ Ұ୴ແࢹͯ͠ΈΔ -
Arrayͷߏମʹ͍ͭͯௐΔ struct RArray { struct RBasic basic; union { struct
{ long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; -
Arrayͷߏମʹ͍ͭͯௐΔ struct RArray { struct RBasic basic; union { struct
{ long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ࣗͷܕॴଐΫϥεϑϥά શͯͷΦϒδΣΫτߏମ͕࣋ͭ ࣗͷྻͷ ྻͷશମαΠζ ڞ༗ϑϥάͬΆ͍ -
Arrayͷߏମʹ͍ͭͯௐΔ struct RArray { struct RBasic basic; union { struct
{ long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ࣮ମϝϞϦͷϙΠϯλ ཁૉ͕3"33":@&.#&%@-&.@."9ҎԼͩͱ 3"SSBZߏମͷதʹྻΛ࣋ͭΒ͍͠ -
VALUEͱ • ߏମͷϙΠϯλΛද͢ • ͭ·ΓRubyͷArrayͷ࣮ମϝϞϦ unsigned longͷྻ ruby.h :102 typedef
unsigned long VALUE; -
Array#+ͷ࣮ VALUE rb_ary_plus(VALUE x, VALUE y) { VALUE z; long
len, xlen, ylen; y = to_ary(y); xlen = RARRAY_LEN(x); ylen = RARRAY_LEN(y); len = xlen + ylen; z = rb_ary_new2(len); ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x)); ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y)); ARY_SET_LEN(z, len); return z; } -
Array#+ͷ࣮ VALUE rb_ary_plus(VALUE x, VALUE y) { VALUE z; long
len, xlen, ylen; y = to_ary(y); xlen = RARRAY_LEN(x); ylen = RARRAY_LEN(y); len = xlen + ylen; z = rb_ary_new2(len); ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x)); ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y)); ARY_SET_LEN(z, len); return z; } ৽͍͠3"SSBZߏମΛ࡞Δ 7"-6&ΛNFNDQZ͢Δ ֤ΦϒδΣΫτߏମΛίϐʔ͢ΔΘ͚Ͱͳ͍