[#26488] Add Standard Deviation Function to Math Module — Daniel Cohen <danielc2017@...>

This patch adds a Standard Deviation function to the Math Module. It takes

25 messages 2009/11/02
[#26489] Re: Add Standard Deviation Function to Math Module — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26490] Re: Add Standard Deviation Function to Math Module — Daniel Cohen <danielc2017@...> 2009/11/03

OK,

[#26493] Re: Add Standard Deviation Function to Math Module — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26511] Re: Add Standard Deviation Function to Math Module — Yusuke ENDOH <mame@...> 2009/11/03

Hi,

[#26492] HashWithIndifferentAccess to core — Urabe Shyouhei <shyouhei@...>

Hello,

35 messages 2009/11/03
[#26496] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26507] Re: HashWithIndifferentAccess to core — Jeremy Kemper <jeremy@...> 2009/11/03

On Tue, Nov 3, 2009 at 6:48 AM, Yukihiro Matsumoto <matz@ruby-lang.org> wro=

[#26514] Re: HashWithIndifferentAccess to core — "Martin J. Dst" <duerst@...> 2009/11/04

Just a thought: What about implementing this with an option on Hash:new,

[#26522] Re: HashWithIndifferentAccess to core — Yusuke ENDOH <mame@...> 2009/11/04

Hi,

[#26555] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/05

Hi,

[#26584] Re: HashWithIndifferentAccess to core — Yugui <yugui@...> 2009/11/07

2009/11/6 Yukihiro Matsumoto <matz@ruby-lang.org>:

[#26589] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/07

Hi,

[#26593] Re: HashWithIndifferentAccess to core — Lourens Naud<lourens@...> 2009/11/07

Hi,

[#26523] [Bug #2330] Non systematic segmentation fault with autoload rubyspec — Marc-Andre Lafortune <redmine@...>

Bug #2330: Non systematic segmentation fault with autoload rubyspec

12 messages 2009/11/04

[#26560] [Feature #2340] Removing YAML/Syck — Yui NARUSE <redmine@...>

Feature #2340: Removing YAML/Syck

38 messages 2009/11/06
[#26562] [Feature #2340] Removing YAML/Syck — Yui NARUSE <redmine@...> 2009/11/06

Issue #2340 has been updated by Yui NARUSE.

[#26567] Re: [Feature #2340] Removing YAML/Syck — James Edward Gray II <james@...> 2009/11/06

On Nov 6, 2009, at 4:02 AM, Yui NARUSE wrote:

[#26568] Re: [Feature #2340] Removing YAML/Syck — Jon <jon.forums@...> 2009/11/06

> > Issue #2340 has been updated by Yui NARUSE.

[#26571] Re: [Feature #2340] Removing YAML/Syck — "NARUSE, Yui" <naruse@...> 2009/11/06

Jon wrote:

[#26574] Re: [Feature #2340] Removing YAML/Syck — Aaron Patterson <aaron@...> 2009/11/06

On Sat, Nov 07, 2009 at 12:59:25AM +0900, NARUSE, Yui wrote:

[#26635] [Feature #2348] RBTree Should be Added to the Standard Library — James Gray <redmine@...>

Feature #2348: RBTree Should be Added to the Standard Library

20 messages 2009/11/08
[#28842] [Feature #2348] RBTree Should be Added to the Standard Library — James Gray <redmine@...> 2010/03/21

Issue #2348 has been updated by James Gray.

[#26650] [Feature #2350] Unicode specific functionality on String in 1.9 — Manfred Stienstra <redmine@...>

Feature #2350: Unicode specific functionality on String in 1.9

12 messages 2009/11/09
[#28985] [Feature #2350](Rejected) Unicode specific functionality on String in 1.9 — Yusuke Endoh <redmine@...> 2010/03/25

Issue #2350 has been updated by Yusuke Endoh.

[#28993] Re: [Feature #2350](Rejected) Unicode specific functionality on String in 1.9 — Nikolai Weibull <now@...> 2010/03/25

On Thu, Mar 25, 2010 at 14:45, Yusuke Endoh <redmine@ruby-lang.org> wrote:

[#26704] Maintainer confirmation process done. — "Yugui (Yuki Sonoda)" <yugui@...>

I'm sorry for my closing the maintainer confirmation process so late.

13 messages 2009/11/12

[#26736] [Bug #2365] Matrix: poor handling of coercion errors [patch] — Marc-Andre Lafortune <redmine@...>

Bug #2365: Matrix: poor handling of coercion errors [patch]

12 messages 2009/11/14

[#26772] [Bug #2378] Regression in ParseDate.parsedate('nn-nn') — Vladimir Sizikov <redmine@...>

Bug #2378: Regression in ParseDate.parsedate('nn-nn')

10 messages 2009/11/16

[#26774] Ruby constant lookup — Yehuda Katz <wycats@...>

Over the past six months or so, I have been working with the new Ruby 1.9

22 messages 2009/11/16
[#26775] Re: Ruby constant lookup — Shugo Maeda <shugo@...> 2009/11/17

Hi,

[#26777] Re: Ruby constant lookup — Yehuda Katz <wycats@...> 2009/11/17

Shugo,

[#26778] Re: Ruby constant lookup — Shugo Maeda <shugo@...> 2009/11/17

Hi,

[#26869] Caching #to_s for immutables (and a possible future for constant-folding) — Kurt Stephens <ks@...>

I have a proof-of-concept patch to MRI that caches #to_s values for

16 messages 2009/11/23
[#26936] Re: Caching #to_s for immutables (and a possible future for constant-folding) — Roger Pack <rogerdpack@...> 2009/11/29

> =A0It reduces the number of #to_s Strings created during the MRI test sui=

[#26958] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Kurt Stephens <ks@...> 2009/11/30

The attached patch add caching of #to_s results to the main immutable

[#26960] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Roger Pack <rogerdpack@...> 2009/11/30

> Yes. =A0The MRI test suite runs at 45 sec with these changes and at 53 se=

[#26963] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Kurt Stephens <ks@...> 2009/11/30

I just ran rubyspec against it; ~ 5% time improvement.

[ruby-core:26593] Re: HashWithIndifferentAccess to core

From: Lourens Naud<lourens@...>
Date: 2009-11-07 16:40:52 UTC
List: ruby-core #26593
Hi,

I've been toying with allowing custom hashing from the Ruby side for  
the past 2 days and have a semi-functional version (patch attached  
against subversion trunk 25671 - http://gist.github.com/228773),  
however my lack of VM control frame knowledge pretty much blocks  
progress atm.

Experimental API :

Hash#index_with(Object)

The general expected contract being the object implements 2 methods,  
hash(a) and compare(a,b)

Here's a rough HashWithIndifferentAccess ( referenced as "hwia" moving  
forward) implementation :

   module Hwia
     def hash(obj)
       case obj
         when Symbol, String
           obj.to_s.hash
         else
           obj.hash
       end
     end

     def compare(a,b)
       if String === a && Symbol === b || String === b && Symbol === a
         a.to_s <=> b.to_s
       else
         a <=> b
       end
     end
     extend self
   end

h = { "foo" => "bar" }
h.index_with(Hwia)
h[:foo] # "bar"

Hash#custom_index? #=> true | false

Hash#indexed_with #=> Object

Given the arguments accepted by st_hash_type members being restricted  
to 1 and 2 arguments respectively, I opted for falling back to the  
current
ruby control frame after consulting with Aman Gupta, who also uses  
this in perftools.rb to get to self :

     rb_thread_t *th = GET_THREAD();
     VALUE hash = th->cfp->self;

Which works OK initially, but results in

   2) Error:
test_index_with(TestHash):
TypeError: wrong argument type TestHash (expected Hash)
     test/ruby/test_hash.rb:906:in `test_index_with'

after passing through rb_hash_aref in :

     h.index_with(Hwia)
     assert_equal Hwia, h.indexed_with
     assert h.custom_index?
     h["foo"] # fails

as th->cfp->self becomes TestHash in that scope.

Any thoughts / feedback much appreciated.Apologies for the naming  
confusion in the original mailing list post as well.

The current source tree is available at : http://github.com/methodmissing/ruby/tree/custom_hashing

- Lourens

methodmissing:ruby lourens$ git diff --patch-with-stat github/ 
trunk..HEAD
  gc.c                   |    1 +
  hash.c                 |   70 +++++++++++++++++++++++++++++++++++++++ 
+++++++-
  include/ruby/ruby.h    |    2 +
  test/ruby/test_hash.rb |   36 ++++++++++++++++++++++++
  4 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/gc.c b/gc.c
index 7711101..55cdb85 100644
--- a/gc.c
+++ b/gc.c
@@ -1697,6 +1697,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE  
ptr, int lev)

        case T_HASH:
         mark_hash(objspace, obj->as.hash.ntbl, lev);
+       gc_mark(objspace, obj->as.hash.index_with, lev);
         ptr = obj->as.hash.ifnone;
         goto again;

diff --git a/hash.c b/hash.c
index ef8b9a8..66280b6 100644
--- a/hash.c
+++ b/hash.c
@@ -14,6 +14,7 @@
  #include "ruby/ruby.h"
  #include "ruby/st.h"
  #include "ruby/util.h"
+#include "vm_core.h"

  #ifdef __APPLE__
  #include <crt_externs.h>
@@ -33,7 +34,7 @@ rb_hash_freeze(VALUE hash)
  VALUE rb_cHash;

  static VALUE envtbl;
-static ID id_hash, id_yield, id_default;
+static ID id_hash, id_yield, id_default, id_compare;

  static int
  rb_any_cmp(VALUE a, VALUE b)
@@ -99,11 +100,37 @@ rb_any_hash(VALUE a)
      return (st_index_t)RSHIFT(hnum, 1);
  }

+static int
+rb_custom_cmp(VALUE a, VALUE b)
+{
+    rb_thread_t *th = GET_THREAD();
+    VALUE hash = th->cfp->self;
+    Check_Type(hash, T_HASH);
+    return FIX2INT(rb_funcall(RHASH(hash)->index_with, id_compare, 2,  
a, b));
+}
+
+static st_index_t
+rb_custom_hash(VALUE a)
+{
+    st_index_t hnum;
+    rb_thread_t *th = GET_THREAD();
+    VALUE hash = th->cfp->self;
+    Check_Type(hash, T_HASH);
+    hnum = FIX2LONG(rb_funcall(RHASH(hash)->index_with, id_hash, 1,  
a));
+    hnum <<= 1;
+    return (st_index_t)RSHIFT(hnum, 1);
+}
+
  static const struct st_hash_type objhash = {
      rb_any_cmp,
      rb_any_hash,
  };

+static const struct st_hash_type customhash = {
+    rb_custom_cmp,
+    rb_custom_hash,
+};
+
  static const struct st_hash_type identhash = {
      st_numcmp,
      st_numhash,
@@ -219,6 +246,7 @@ hash_alloc(VALUE klass)
      OBJSETUP(hash, klass, T_HASH);

      hash->ifnone = Qnil;
+    hash->index_with = Qnil;

      return (VALUE)hash;
  }
@@ -241,6 +269,7 @@ rb_hash_dup(VALUE hash)
          FL_SET(ret, HASH_PROC_DEFAULT);
      }
      ret->ifnone = RHASH(hash)->ifnone;
+    ret->index_with = RHASH(hash)->index_with;
      return (VALUE)ret;
  }

@@ -492,7 +521,6 @@ VALUE
  rb_hash_aref(VALUE hash, VALUE key)
  {
      VALUE val;
-
      if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key,  
&val)) {
         return rb_funcall(hash, id_default, 1, key);
      }
@@ -1080,6 +1108,7 @@ rb_hash_replace(VALUE hash, VALUE hash2)
      }
      rb_hash_foreach(hash2, replace_i, hash);
      RHASH(hash)->ifnone = RHASH(hash2)->ifnone;
+    RHASH(hash)->index_with = RHASH(hash2)->index_with;
      if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
         FL_SET(hash, HASH_PROC_DEFAULT);
      }
@@ -1852,6 +1881,37 @@ rb_hash_compare_by_id_p(VALUE hash)
      return Qfalse;
  }

+static VALUE
+rb_hash_index_with(VALUE hash, VALUE idx)
+{
+    if (rb_respond_to(idx, id_hash) && rb_respond_to(idx, id_compare)){
+      rb_hash_modify(hash);
+      RHASH(hash)->index_with = idx;
+      RHASH(hash)->ntbl->type = &customhash;
+      rb_hash_rehash(hash);
+      return hash;
+    }else{
+      rb_raise(rb_eRuntimeError, "a custom hashing scheme should  
implement #hash(a) and #compare(a,b)");
+    }
+}
+
+static VALUE
+rb_hash_custom_index_p(VALUE hash)
+{
+    if (!RHASH(hash)->ntbl)
+        return Qfalse;
+    if (RHASH(hash)->ntbl->type == &customhash) {
+       return Qtrue;
+    }
+    return Qfalse;
+}
+
+static VALUE
+rb_hash_indexed_with(VALUE hash)
+{
+       return RHASH(hash)->index_with;
+}
+
  static int path_tainted = -1;

  static char **origenviron;
@@ -2647,6 +2707,7 @@ Init_Hash(void)
      id_hash = rb_intern("hash");
      id_yield = rb_intern("yield");
      id_default = rb_intern("default");
+    id_compare = rb_intern("compare");

      rb_cHash = rb_define_class("Hash", rb_cObject);

@@ -2716,6 +2777,11 @@ Init_Hash(void)
      rb_define_method(rb_cHash,"compare_by_identity",  
rb_hash_compare_by_id, 0);
      rb_define_method(rb_cHash,"compare_by_identity?",  
rb_hash_compare_by_id_p, 0);

+    rb_define_method(rb_cHash,"index_with", rb_hash_index_with, 1);
+    rb_define_method(rb_cHash,"indexed_with", rb_hash_indexed_with, 0);
+
+    rb_define_method(rb_cHash,"custom_index?",  
rb_hash_custom_index_p, 0);
+#define RHASH_IDX_WITH(h) (RHASH(h)->index_with)
  #define RHASH_SIZE(h) (RHASH(h)->ntbl ? RHASH(h)->ntbl- 
 >num_entries : 0)
  #define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0)

diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index c860b25..4e3f7f9 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -871,4 +871,40 @@ class TestHash < Test::Unit::TestCase
      def o.hash; 2<<100; end
      assert_equal({x=>1}.hash, {x=>1}.hash)
    end
+
+  module Hwia
+    def hash(obj)
+      case obj
+        when Symbol, String
+          obj.to_s.hash
+        else
+          obj.hash
+      end
+    end
+
+    def compare(a,b)
+      if String === a && Symbol === b || String === b && Symbol === a
+        a.to_s <=> b.to_s
+      else
+        a <=> b
+      end
+    end
+    extend self
+  end
+
+  def test_index_with
+    a = "foo"
+    assert(!{}.custom_index?)
+    h = { a => "bar" }
+    assert_equal nil, h.indexed_with
+    assert_raises(RuntimeError) do
+      h.index_with(Object.new)
+    end
+    h.index_with(Hwia)
+    assert_equal Hwia, h.indexed_with
+    assert h.custom_index?
+    h["foo"]
+    #assert_equal "bar", h["foo"]
+    #assert_equal "bar", h[:foo]
+  end
  end


On 2009/11/07, at 13:10, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: [ruby-core:26584] Re: HashWithIndifferentAccess to  
> core"
>    on Sat, 7 Nov 2009 20:06:07 +0900, Yugui <yugui@yugui.jp> writes:
>
> |How about Hash::Type class as an API for rb_hash_type?
>
> Depends on API, and performance.
>
> 							matz.
>


In This Thread