Created
October 11, 2012 12:39
-
-
Save zaki/3872038 to your computer and use it in GitHub Desktop.
TokyoRails contribute to ruby project for fun: Array#transpose with uneven arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static VALUE | |
rb_ary_transpose_test(VALUE ary) | |
{ | |
long elen = -1, alen, i, j, k; | |
VALUE tmp, result = 0; | |
// get the longest sub-array so we know how much to extend to | |
long elen_max = -1; | |
for (i=0; i<alen; i++) { | |
tmp = to_ary(rb_ary_elt(ary, i)); | |
long length = RARRAY_LEN(tmp); | |
if (elen_max < length) { | |
elen_max = length; | |
} | |
} | |
alen = RARRAY_LEN(ary); | |
if (alen == 0) return rb_ary_dup(ary); | |
for (i=0; i<alen; i++) { | |
tmp = to_ary(rb_ary_elt(ary, i)); | |
if (elen < 0) { /* first element */ | |
elen = RARRAY_LEN(tmp); | |
result = rb_ary_new2(elen); | |
for (j=0; j<elen; j++) { | |
rb_ary_store(result, j, rb_ary_new2(alen)); | |
} | |
} | |
for (j=0; j<elen; j++) { | |
long len = RARRAY_LEN(tmp); | |
if (len < elen_max) { | |
if (!rb_block_given_p()) { | |
rb_raise(rb_eIndexError, "element size differs (%ld should be %ld) and no block was given", | |
RARRAY_LEN(tmp), elen); | |
} | |
else { | |
for(k = len; k < elen_max; k++) { | |
// fill in missing elements | |
VALUE filler = rb_yield(rb_ary_new3(2L, LONG2FIX(i), LONG2FIX(k))); | |
rb_ary_push_1(tmp, filler); | |
} | |
} | |
} | |
rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j)); | |
} | |
} | |
return result; | |
} | |
rb_define_method(rb_cArray, "transpose_test", rb_ary_transpose_test, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
class TestArray < Test::Unit::TestCase | |
def setup | |
@verbose = $VERBOSE | |
$VERBOSE = nil | |
@cls = Array | |
end | |
def teardown | |
$VERBOSE = @verbose | |
end | |
# LOTS OF OTHER TESTS | |
def test_transpose | |
assert_equal([[1, :a], [2, :b], [3, :c]], | |
[[1, 2, 3], [:a, :b, :c]].transpose_test) | |
assert_raise(IndexError) { [[1, 2, 3], [:a, :b]].transpose_test } | |
end | |
def test_transpose_with_block | |
assert_equal([[1, :a], [2, :b], [3, :hoge]], | |
[[1,2,3], [:a, :b]].transpose_test { |x, y| :hoge }) | |
end | |
def test_transpose_block_parameters | |
[[1, :a], [2, :b], [3]].transpose_test do |x, y| | |
assert_equal(2, x) | |
assert_equal(1, y) | |
:foo | |
end | |
end | |
def test_transpose_block_parameters | |
arr = [[1, nil, :a], [nil, :b], [3]].transpose_test do |x, y| | |
:foo | |
end | |
assert_equal([ | |
[1, nil, 3], | |
[nil, :b ,:foo], | |
[:a, :foo, :foo] | |
], arr) | |
end | |
#LOTS OF OTHER TESTS | |
end |
randym
commented
Oct 11, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment