summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-06 15:34:10 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-06 15:34:10 +0000
commitd650fb0578a11109d3c4390c5c6435c4a2239991 (patch)
treeb1819d0d7b6f69c916a88c027cb77cb2c10b561b /hash.c
parent004127aa904cec52e80bbfc756c4a18b7f4970f0 (diff)
* process.c (proc_setgroups): [ruby-dev:25081]
* re.c (rb_reg_eqq): document fix. [ruby-talk:122541] * ext/socket/socket.c (sock_connect): use rb_str_new4(). [ruby-dev:25052] * io.c (io_write): remove rb_str_locktmp(). [ruby-dev:25050] * io.c (io_fwrite): takes VALUE string as an argument. [ruby-dev:25050] * ext/socket/socket.c (sock_connect): remove rb_str_locktmp(). [ruby-dev:25050] * ext/socket/socket.c (udp_connect): [ruby-dev:25045] * ext/socket/socket.c (udp_bind): ditto. * ext/socket/socket.c (udp_send): ditto. * ext/socket/socket.c (bsock_send): ditto. * ext/socket/socket.c (s_recvfrom): ditto. * hash.c (rb_hash_hash): should provide "hash" method where "eql?" is redefined. [ruby-talk:122482] * ext/socket/socket.c (bsock_setsockopt): [ruby-dev:25039] * hash.c (rb_hash_hash): should provide "hash" method where "eql?" is redefined. [ruby-talk:122482] * ext/socket/socket.c (bsock_setsockopt): [ruby-dev:25039] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7483 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index b51cbdddce..57357269c4 100644
--- a/hash.c
+++ b/hash.c
@@ -1519,6 +1519,49 @@ rb_hash_eql(hash1, hash2)
return hash_equal(hash1, hash2, Qtrue);
}
+
+rb_hash_hash_i(key, value, hp)
+ VALUE key, value;
+ long *hp;
+{
+ long h = *hp;
+ VALUE n;
+
+ h = (h << 1) | (h<0 ? 1 : 0);
+ n = rb_hash(key);
+ h ^= NUM2LONG(n);
+ h = (h << 1) | (h<0 ? 1 : 0);
+ n = rb_hash(value);
+ h ^= NUM2LONG(n);
+
+ *hp = h;
+ return ST_CONTINUE;
+}
+
+/*
+ * call-seq:
+ * hash.hash -> fixnum
+ *
+ * Compute a hash-code for this hash. Two hashes with the same content
+ * will have the same hash code (and will compare using <code>eql?</code>).
+ */
+
+static VALUE
+rb_hash_hash(hash)
+ VALUE hash;
+{
+ long h;
+ VALUE n;
+
+ h = RHASH(hash)->tbl->num_entries;
+ rb_hash_foreach(hash, rb_hash_hash_i, (VALUE)&h);
+ h = (h << 1) | (h<0 ? 1 : 0);
+ n = rb_hash(RHASH(hash)->ifnone);
+ h ^= NUM2LONG(n);
+
+ return LONG2FIX(h);
+}
+
static int
rb_hash_invert_i(key, value, hash)
VALUE key, value;
@@ -2454,6 +2497,7 @@ Init_Hash()
rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
rb_define_method(rb_cHash,"eql?", rb_hash_eql, 1);
+ rb_define_method(rb_cHash,"hash", rb_hash_hash, 0);
rb_define_method(rb_cHash,"[]", rb_hash_aref, 1);
rb_define_method(rb_cHash,"fetch", rb_hash_fetch, -1);
rb_define_method(rb_cHash,"[]=", rb_hash_aset, 2);