Skip to content

Commit

Permalink
move the implementation about #roatation from Array to NSArray, in or…
Browse files Browse the repository at this point in the history
…der to use its methods with Array/NSArray
  • Loading branch information
Watson1978 committed Jul 4, 2012
1 parent 30d0432 commit 281300f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
13 changes: 13 additions & 0 deletions NSArray.m
Expand Up @@ -1155,6 +1155,8 @@
rb_objc_define_method(rb_cArray, "permutation", rary_permutation, -1);
rb_objc_define_method(rb_cArray, "cycle", rary_cycle, -1);
rb_objc_define_method(rb_cArray, "sample", rary_sample, -1);
rb_objc_define_method(rb_cArray, "rotate", rary_rotate, -1);
rb_objc_define_method(rb_cArray, "rotate!", rary_rotate_bang, -1);
}

// MRI compatibility API.
Expand Down Expand Up @@ -1200,6 +1202,17 @@
return Qnil;
}

void
rb_ary_elt_set(VALUE ary, long offset, VALUE item)
{
if (IS_RARY(ary)) {
rary_elt_set(ary, offset, item);
}
else {
[(id)ary replaceObjectAtIndex:(NSUInteger)offset withObject:RB2OC(item)];
}
}

VALUE
rb_ary_replace(VALUE rcv, VALUE other)
{
Expand Down
18 changes: 8 additions & 10 deletions array.c
Expand Up @@ -1347,9 +1347,9 @@ static void
ary_reverse(VALUE ary, long pos1, long pos2)
{
while (pos1 < pos2) {
VALUE elem = rary_elt(ary, pos1);
rary_elt_set(ary, pos1, rary_elt(ary, pos2));
rary_elt_set(ary, pos2, elem);
VALUE elem = rb_ary_elt(ary, pos1);
rb_ary_elt_set(ary, pos1, rb_ary_elt(ary, pos2));
rb_ary_elt_set(ary, pos2, elem);
pos1++;
pos2--;
}
Expand All @@ -1359,7 +1359,7 @@ VALUE
ary_rotate(VALUE ary, long cnt)
{
if (cnt != 0) {
long len = RARY(ary)->len;
long len = rb_ary_len(ary);
if (len > 0 && (cnt = rotate_count(cnt, len)) > 0) {
--len;
if (cnt < len) {
Expand Down Expand Up @@ -1393,7 +1393,7 @@ ary_rotate(VALUE ary, long cnt)
* a.rotate!(-3) #=> ["a", "b", "c", "d"]
*/

static VALUE
VALUE
rary_rotate_bang(VALUE ary, SEL sel, int argc, VALUE *argv)
{
VALUE n;
Expand Down Expand Up @@ -1423,7 +1423,7 @@ rary_rotate_bang(VALUE ary, SEL sel, int argc, VALUE *argv)
* a.rotate(-3) #=> ["b", "c", "d", "a"]
*/

static VALUE
VALUE
rary_rotate(VALUE ary, SEL sel, int argc, VALUE *argv)
{
VALUE rotated;
Expand All @@ -1435,8 +1435,8 @@ rary_rotate(VALUE ary, SEL sel, int argc, VALUE *argv)
cnt = NUM2LONG(n);
}

rotated = rary_dup(ary, 0);
if (RARY(ary)->len > 0) {
rotated = rb_ary_dup(ary);
if (rb_ary_len(ary) > 0) {
ary_rotate(rotated, cnt);
}
return rotated;
Expand Down Expand Up @@ -3829,8 +3829,6 @@ Init_Array(void)
rb_objc_define_method(rb_cRubyArray, "rindex", rary_rindex, -1);
rb_objc_define_method(rb_cRubyArray, "reverse", rary_reverse, 0);
rb_objc_define_method(rb_cRubyArray, "reverse!", rary_reverse_bang, 0);
rb_objc_define_method(rb_cRubyArray, "rotate", rary_rotate, -1);
rb_objc_define_method(rb_cRubyArray, "rotate!", rary_rotate_bang, -1);
rb_objc_define_method(rb_cRubyArray, "sort", rary_sort, 0);
rb_objc_define_method(rb_cRubyArray, "sort!", rary_sort_bang, 0);
rb_objc_define_method(rb_cRubyArray, "sort_by!", rary_sort_by_bang, 0);
Expand Down
2 changes: 2 additions & 0 deletions array.h
Expand Up @@ -184,6 +184,8 @@ VALUE rary_sample(VALUE ary, SEL sel, int argc, VALUE *argv);
VALUE rary_diff(VALUE ary1, SEL sel, VALUE ary2);
VALUE rary_and(VALUE ary1, SEL sel, VALUE ary2);
VALUE rary_or(VALUE ary1, SEL sel, VALUE ary2);
VALUE rary_rotate(VALUE ary, SEL sel, int argc, VALUE *argv);
VALUE rary_rotate_bang(VALUE ary, SEL sel, int argc, VALUE *argv);

VALUE rb_ary_make_hash(VALUE ary1, VALUE ary2);
VALUE rb_ary_make_hash_by(VALUE ary);
Expand Down
2 changes: 2 additions & 0 deletions include/ruby/ruby.h
Expand Up @@ -475,6 +475,8 @@ long rb_str_clen(VALUE);

long rb_ary_len(VALUE);
VALUE rb_ary_elt(VALUE, long);
void rb_ary_elt_set(VALUE, long, VALUE);

#define RARRAY_LEN(a) (rb_ary_len((VALUE)a))
#define RARRAY_AT(a,i) (rb_ary_elt((VALUE)a, (long)i))
/* IMPORTANT: try to avoid using RARRAY_PTR if necessary, because it's
Expand Down

0 comments on commit 281300f

Please sign in to comment.