redid the type hashing

account for the class the type is for
by adding it to the hash code, so even the ivars are identical the
types are not
This commit is contained in:
Torsten Ruger
2016-12-31 14:51:06 +02:00
parent 0fa7f54bcc
commit b9073d0c88
5 changed files with 86 additions and 51 deletions

View File

@ -25,7 +25,7 @@ module Parfait
def self.type_for( arguments )
my_class = Parfait.object_space.classes[:NamedList]
Parfait::Type.for_hash( my_class , arguments.merge(type: my_class.instance_type))
Type.for_hash( my_class , {type: my_class.instance_type}.merge(arguments))
end
end
end

View File

@ -42,9 +42,7 @@ module Parfait
message = @first_message
end
@classes.each do |name , cl|
raise "upps #{cl.instance_type.hash}" unless cl.instance_type.hash.is_a?(Fixnum)
@types[cl.instance_type.hash] = cl.instance_type
add_type(cl.instance_type)
end
end

View File

@ -45,30 +45,6 @@ module Parfait
Parfait.object_space.add_type(new_type)
end
def self.hash_code_for_hash( dict )
index = 1
hash_code = str_hash(dict[:type])
dict.each do |name , type|
next if name == :type
item_hash = str_hash(name) + str_hash(type)
hash_code += item_hash + (item_hash / 256 ) * index
index += 1
end
hash_code
end
def self.str_hash(str)
if RUBY_ENGINE == 'opal'
hash = 5381
str.to_s.each_char do |c|
hash = ((hash << 5) + hash) + c.to_i; # hash * 33 + c without getting bignums
end
hash % (2 ** 51)
else
str.hash
end
end
def initialize( object_class , hash )
super()
set_object_class( object_class)
@ -192,16 +168,43 @@ module Parfait
end
alias :name :sof_reference_name
def each
index = 1
while( index <= get_length() )
yield( name_at(index) , type_at(index) )
index += 1
end
end
def to_hash
hash = Dictionary.new
@names.each_with_index do |name, index |
hash[name] = @types[index]
hash = {}
each do |name , type|
hash[name] = type
end
hash
end
def hash
Type.hash_code_for_hash( to_hash )
index = 1
hash_code = Type.str_hash( @object_class.name )
each do |name , type|
item_hash = Type.str_hash(name) + Type.str_hash(type)
hash_code += item_hash + (item_hash / 256 ) * index
index += 1
end
hash_code
end
def self.str_hash(str)
if RUBY_ENGINE == 'opal'
hash = 5381
str.to_s.each_char do |c|
hash = ((hash << 5) + hash) + c.to_i; # hash * 33 + c without getting bignums
end
hash % (2 ** 51)
else
str.hash
end
end
private