[#13161] hacking on the "heap" implementation in gc.c — Lloyd Hilaiel <lloyd@...>

Hi all,

16 messages 2007/11/01

[#13182] Thinking of dropping YAML from 1.8 — Urabe Shyouhei <shyouhei@...>

Hello all.

14 messages 2007/11/03

[#13315] primary encoding and source encoding — David Flanagan <david@...>

I've got a couple of questions about the handling of primary encoding.

29 messages 2007/11/08
[#13331] Re: primary encoding and source encoding — Yukihiro Matsumoto <matz@...> 2007/11/09

Hi,

[#13368] method names in 1.9 — "David A. Black" <dblack@...>

Hi --

61 messages 2007/11/10
[#13369] Re: method names in 1.9 — Yukihiro Matsumoto <matz@...> 2007/11/10

Hi,

[#13388] Re: method names in 1.9 — Charles Oliver Nutter <charles.nutter@...> 2007/11/11

Yukihiro Matsumoto wrote:

[#13403] Re: method names in 1.9 — "Austin Ziegler" <halostatue@...> 2007/11/11

On 11/11/07, Charles Oliver Nutter <charles.nutter@sun.com> wrote:

[#13410] Re: method names in 1.9 — David Flanagan <david@...> 2007/11/11

Austin Ziegler wrote:

[#13413] Re: method names in 1.9 — Charles Oliver Nutter <charles.nutter@...> 2007/11/11

David Flanagan wrote:

[#13423] Re: method names in 1.9 — Jordi <mumismo@...> 2007/11/12

Summing it up:

[#13386] Re: method names in 1.9 — Trans <transfire@...> 2007/11/11

[#13391] Re: method names in 1.9 — Matthew Boeh <mboeh@...> 2007/11/11

On Sun, Nov 11, 2007 at 05:50:18PM +0900, Trans wrote:

[#13457] mingw rename — "Roger Pack" <rogerpack2005@...>

Currently for different windows' builds, the names for RUBY_PLATFORM

13 messages 2007/11/13

[#13485] Proposal: Array#walker — Wolfgang Nádasi-Donner <ed.odanow@...>

Good morning all together!

23 messages 2007/11/14
[#13486] Re: Proposal: Array#walker — Wolfgang Nádasi-Donner <ed.odanow@...> 2007/11/14

A nicer version may be...

[#13488] Re: Proposal: Array#walker — Trans <transfire@...> 2007/11/14

[#13495] Re: Proposal: Array#walker — Trans <transfire@...> 2007/11/14

[#13498] state of threads in 1.9 — Jordi <mumismo@...>

Are Threads mapped to threads on the underlying operating system in

30 messages 2007/11/14
[#13519] Re: state of threads in 1.9 — "Bill Kelly" <billk@...> 2007/11/14

[#13526] Re: state of threads in 1.9 — Eric Hodel <drbrain@...7.net> 2007/11/14

On Nov 14, 2007, at 11:18 , Bill Kelly wrote:

[#13528] test/unit and miniunit — Ryan Davis <ryand-ruby@...>

When is the 1.9 freeze?

17 messages 2007/11/14

[#13564] Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc. — Wolfgang Nádasi-Donner <ed.odanow@...>

Good evening all together!

53 messages 2007/11/15
[#13575] Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc. — "Nikolai Weibull" <now@...> 2007/11/15

On Nov 15, 2007 8:14 PM, Wolfgang N=E1dasi-Donner <ed.odanow@wonado.de> wro=

[#13578] Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc. — Michael Neumann <mneumann@...> 2007/11/16

Nikolai Weibull schrieb:

[#13598] wondering about #tap (was: Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc.) — "David A. Black" <dblack@...> 2007/11/16

Hi --

[#13605] Re: wondering about #tap (was: Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc.) — Trans <transfire@...> 2007/11/16

[#13612] Re: wondering about #tap (was: Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc.) — "David A. Black" <dblack@...> 2007/11/16

Hi --

[#13624] Re: wondering about #tap (was: Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc.) — "Nikolai Weibull" <now@...> 2007/11/16

On Nov 16, 2007 12:40 PM, David A. Black <dblack@rubypal.com> wrote:

[#13632] Re: wondering about #tap — David Flanagan <david@...> 2007/11/16

David A. Black wrote:

[#13634] Re: wondering about #tap — "David A. Black" <dblack@...> 2007/11/16

Hi --

[#13636] Re: wondering about #tap — "Rick DeNatale" <rick.denatale@...> 2007/11/16

On Nov 16, 2007 12:40 PM, David A. Black <dblack@rubypal.com> wrote:

[#13637] Re: wondering about #tap — murphy <murphy@...> 2007/11/16

Rick DeNatale wrote:

[#13640] Re: wondering about #tap — Wolfgang Nádasi-Donner <ed.odanow@...> 2007/11/16

murphy schrieb:

[#13614] Suggestion for native thread tests — "Eust痃uio Rangel" <eustaquiorangel@...>

Hi!

12 messages 2007/11/16

[#13685] Problems with \M-x in utf-8 encoded strings — Wolfgang Nádasi-Donner <ed.odanow@...>

Hi!

11 messages 2007/11/18

[#13741] retry semantics changed — Dave Thomas <dave@...>

In 1.8, I could write:

46 messages 2007/11/23
[#13742] Re: retry semantics changed — "Brian Mitchell" <binary42@...> 2007/11/23

On Nov 23, 2007 12:06 PM, Dave Thomas <dave@pragprog.com> wrote:

[#13743] Re: retry semantics changed — Dave Thomas <dave@...> 2007/11/23

[#13746] Re: retry semantics changed — Yukihiro Matsumoto <matz@...> 2007/11/23

Hi,

[#13747] Re: retry semantics changed — Dave Thomas <dave@...> 2007/11/23

[#13748] Re: retry semantics changed — Yukihiro Matsumoto <matz@...> 2007/11/23

Hi,

[#13749] Re: retry semantics changed — Dave Thomas <dave@...> 2007/11/23

Array#split_at

From: murphy <murphy@...>
Date: 2007-11-16 09:47:50 UTC
List: ruby-core #13597
Trans wrote:
> On Nov 15, 4:49 am, murphy <mur...@rubychan.de> wrote:
>> VALUE
>> rb_ary_split_at(VALUE ary, VALUE pos)
> This is nice. Add in an extra length option, and IMO it's a worthy of
> consideration for core.
I tried, and it's working fine with tests, but I have performance issues:

      short.each_with_index do |el, i|
        pre, post = short.split_at(i)
      end

is always slower (!) than

      last = long.size - 1
      short.each_with_index do |el, i|
        pre = short.first(i)
        post = short.last(last - i)
      end

and I assume it's because of the parallel assignment. I'm using
ary_shared_array just like #first and #last, rb_assoc_new for the result
and avoid rb_scan_args. can anything more be done to make it faster?

I'm still not sure about the name...
[murphy]

Attachments (2)

array_split_at_draft_1.diff (2.71 KB, text/x-diff)
Index: array.c
===================================================================
--- array.c	(revision 13948)
+++ array.c	(working copy)
@@ -781,6 +781,76 @@
     return rb_ary_entry(ary, NUM2LONG(pos));
 }
 
+/* 
+ *  call-seq:
+ *     array.split_at(index)          -> [left_array, right_array]
+ *     array.split_at(start, length)  -> [left_array, right_array]
+ *
+ *  Splits an array into two parts, with the first one containing
+ *  all elements before the given range, and the second one containing
+ *  all elements after it. See Array#slice for information about the 
+ *  range expressions.
+ * 
+ *     a = [ "a", "b", "c", "d", "e" ]
+ *     a.split_at(0)          #=> [[], ["b", "c", "d", "e"]]
+ *     a.split_at(2)          #=> [["a", "b"], ["d", "e"]]
+ *     a.split_at(-1)         #=> [["a", "b", "c", "d"], []]
+ *     a.split_at(1, 2)       #=> [["a"], ["d", "e"]]
+ *     a.split_at(1, 0)       #=> [["a"], ["b", "c", "d", "e"]]
+ *  
+ */
+
+VALUE
+rb_ary_split_at(int argc, VALUE *argv, VALUE ary)
+{
+    return Qnil;
+    
+    long pos, len;
+    len = 1;  /* default */
+    
+    switch (argc) {
+      case 2:
+	len = NUM2LONG(argv[1]);
+	/* fall through */
+      case 1:
+	pos = NUM2LONG(argv[0]);
+	break;
+      default:
+	rb_scan_args(argc, argv, "11", 0, 0);
+    }
+    
+    if (RARRAY_LEN(ary) == 0) {
+	return rb_assoc_new(rb_ary_new2(0), rb_ary_new2(0));
+    }
+    
+    if (pos < 0) {
+	pos += RARRAY_LEN(ary);
+    }
+    
+    /* pos out of bounds? */
+    if (pos <= 0) {
+      return rb_assoc_new(rb_ary_new2(0), ary_shared_array(rb_cArray, ary));
+    }
+    
+    /* pos+len out of bounds? */
+    if (len < 0) {
+	/* Array#slice doesn't fail on negative lengths, so we copy */
+	len = 0;
+    }
+    if (pos + len > RARRAY_LEN(ary)) {
+      return rb_assoc_new(ary_shared_array(rb_cArray, ary), rb_ary_new2(0));
+    }
+    
+    VALUE left  = ary_shared_array(rb_cArray, ary);
+    VALUE right = ary_shared_array(rb_cArray, ary);
+    
+    RARRAY(left)->len = pos;
+    RARRAY(right)->ptr += pos + len;
+    RARRAY(right)->len += RARRAY_LEN(ary) - (pos + len);
+    
+    return rb_assoc_new(left, right);
+}
+
 /*
  *  call-seq:
  *     array.first     ->   obj or nil
@@ -2557,7 +2627,7 @@
     hash = ary_make_hash(ary2, 0);
 
     if (RHASH_EMPTY_P(hash))
-        return ary3;
+	return ary3;
 
     for (i=0; i<RARRAY_LEN(ary1); i++) {
 	v = vv = rb_ary_elt(ary1, i);
@@ -3288,6 +3358,7 @@
 
     rb_define_method(rb_cArray, "slice", rb_ary_aref, -1);
     rb_define_method(rb_cArray, "slice!", rb_ary_slice_bang, -1);
+    rb_define_method(rb_cArray, "split_at", rb_ary_split_at, -1);
 
     rb_define_method(rb_cArray, "assoc", rb_ary_assoc, 1);
     rb_define_method(rb_cArray, "rassoc", rb_ary_rassoc, 1);
array_split_at_tests_draft_1.diff (4.74 KB, text/x-diff)
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 13948)
+++ test/ruby/test_array.rb	(working copy)
@@ -1103,6 +1103,80 @@
     assert_equal(@cls[], @cls[].sort!)
   end
 
+  def test_split_at
+    a = @cls[*(1..100).to_a]
+
+    assert_equal [ @cls[],                      a ],  a.split_at(0,0)
+    assert_equal [ @cls[1],  @cls[*(2..100).to_a] ],  a.split_at(1,0)
+    assert_equal [ @cls[*(1..99).to_a], @cls[100] ],  a.split_at(99,0)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100,0)
+    assert_equal [ @cls[*(1..99).to_a], @cls[100] ],  a.split_at(-1,0)
+    assert_equal [ @cls[*(1..98).to_a], @cls[99,100] ],  a.split_at(-2,0)
+    assert_equal [ @cls[],                      a ],  a.split_at(-100,0)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101,0)
+
+    assert_equal [ @cls[],   @cls[*(2..100).to_a] ],  a.split_at(0)
+    assert_equal [ @cls[1],  @cls[*(3..100).to_a] ],  a.split_at(1)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(99)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(-1)
+    assert_equal [ @cls[*(1..98).to_a], @cls[100] ],  a.split_at(-2)
+    assert_equal [ @cls[],   @cls[*(2..100).to_a] ],  a.split_at(-100)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101)
+
+    assert_equal [ @cls[],   @cls[*(2..100).to_a] ],  a.split_at(0,1)
+    assert_equal [ @cls[1],  @cls[*(3..100).to_a] ],  a.split_at(1,1)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(99,1)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100,1)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(-1,1)
+    assert_equal [ @cls[*(1..98).to_a], @cls[100] ],  a.split_at(-2,1)
+    assert_equal [ @cls[],   @cls[*(2..100).to_a] ],  a.split_at(-100,1)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101,1)
+
+    assert_equal [ @cls[],   @cls[*(3..100).to_a] ],  a.split_at(0,2)
+    assert_equal [ @cls[1],  @cls[*(4..100).to_a] ],  a.split_at(1,2)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(99,2)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100,2)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(-1,2)
+    assert_equal [ @cls[*(1..98).to_a],    @cls[] ],  a.split_at(-2,2)
+    assert_equal [ @cls[],   @cls[*(3..100).to_a] ],  a.split_at(-100,2)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101,2)
+
+    assert_equal [ @cls[],              @cls[100] ],  a.split_at(0,99)
+    assert_equal [ @cls[1],                @cls[] ],  a.split_at(1,99)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(99,99)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100,99)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(-1,99)
+    assert_equal [ @cls[*(1..98).to_a],    @cls[] ],  a.split_at(-2,99)
+    assert_equal [ @cls[],              @cls[100] ],  a.split_at(-100,99)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101,99)
+
+    assert_equal [ @cls[],                 @cls[] ],  a.split_at(0,100)
+    assert_equal [ @cls[1],                @cls[] ],  a.split_at(1,100)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(99,100)
+    assert_equal [ a,                      @cls[] ],  a.split_at(100,100)
+    assert_equal [ @cls[*(1..99).to_a],    @cls[] ],  a.split_at(-1,100)
+    assert_equal [ @cls[*(1..98).to_a],    @cls[] ],  a.split_at(-2,100)
+    assert_equal [ @cls[],                 @cls[] ],  a.split_at(-100,100)
+    assert_equal [ @cls[],                      a ],  a.split_at(-101,100)
+
+    assert_equal [ @cls[*(1..9).to_a], @cls[*(13..100).to_a] ], a.split_at(9, 3)
+    assert_equal [ @cls[*(1..9).to_a], @cls[*(13..100).to_a] ], a.split_at(-91, 3)
+
+    # assert_equal [ @cls[],   @cls[*(1..100).to_a] ], a.split_at(0..0)
+    # assert_equal [ @cls[*(1..99).to_a],    @cls[] ], a.split_at(99..99)
+    # assert_equal [ @cls[*(1..100).to_a],   @cls[] ], a.split_at(100..100)
+    # assert_equal [ @cls[*(1..99).to_a],    @cls[] ], a.split_at(99..200)
+    # assert_equal [ @cls[*(1..99).to_a],    @cls[] ], a.split_at(-1..-1)
+    # assert_equal [ @cls[*(1..98).to_a], @cls[100] ], a.split_at(-2..-2)
+    # 
+    # assert_equal [ @cls[*(1..9).to_a], @cls[*(13..100).to_a] ], a.split_at(9..11)
+    # assert_equal [ @cls[*(1..9).to_a], @cls[*(13..100).to_a] ], a.split_at(-91..-89)
+    
+    assert_equal [ @cls[*(1..10).to_a], @cls[*(11..100).to_a] ], a.split_at(10, -3)
+    # assert_equal [ @cls[*(1..10).to_a], @cls[*(11..100).to_a] ], a.split_at(10..7)
+  end
+
   def test_to_a
     a = @cls[ 1, 2, 3 ]
     a_id = a.__id__

In This Thread