> -----Original Message----- > From: ts [mailto:decoux / moulon.inra.fr] > Sent: Wednesday, February 28, 2001 02:44 AM > To: ruby-talk ML > Cc: ruby-talk / ruby-lang.org > Subject: [ruby-talk:11760] Re: Array bugs? > > > >>>>> "C" == Christoph Rippel <crippel / primenet.com> writes: > > C> for hashes (you should use a recursion limited hash-value - this effects > C> the hash value of non-recursive Arrays too (and hash-value of Hashes > C> them-selves) but its simpler than an ``untangled hash value'' and more > C> efficient (even for non-recursive Arrays). > > I've not understood, how do you define the hash value of > > a = [] > a << a > > actually it give an error. Not tested ... class Fixnum # unsigned long int " self << 1 " def wrap_by_1; self[31] + (self << 1) - (self[31]<< 32) end end class Object def protected_hash(level); hash end end class Array def protected_hash (level) return length if level < 1 hsh = length each do |i| # in array.c this is h = (h<<1) | (h<0 ? 1 : 0); hsh = (hsh.wrap_by_1) | (hsh<0 ? 1 : 0); hsh ^= i.protected_hash (level- 1) end return hsh end def hash untangled_hash 5 end end ## Nicer variation IMO class Object def is_recursive?; false end end class Array def is_recursive?; true end def protected_hash (level) return length if level < 1 hsh = length each do |i| hsh = (hsh.wrap_by_1) | (hsh<0 ? 1 : 0); if i.is_recursive? hsh ^= i.protected_hash (level- 1) else hsh ^= i.hash end end return hsh end def hash untangled_hash 5 end end Christoph