dart,是两个哈希码总是要返回一个新的唯一哈希码? [英] dart, is xoring two hashcodes always going to return a new unique hashcode?

查看:404
本文介绍了dart,是两个哈希码总是要返回一个新的唯一哈希码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个类,需要有一个唯一的哈希码基于它的两个字段,我想知道是否异或2字段哈希码足以生成一个唯一和一致的哈希码我的对象?

I am writing a class that needs to have a unique hashcode based on two of its fields and I wanted to know whether XORing the 2 fields hashcodes is enough to generate a unique and consistent hashcode for my object?

class _TypeMatch{
  final Type _potentialSubtype;
  final Type _supertype;

  int _cachedHashCode;  

  _TypeMatch(this._potentialSubtype, this._supertype){
    _cachedHashCode = _potentialSubtype.hashCode ^ _supertype.hashCode;
  }

  int get hashCode => _cachedHashCode;

  bool operator ==(other){
    return other is _TypeMatch && _cachedHashCode == other._cachedHashCode;
  }
}

我看到这个问题这里似乎暗示XORing两个其他哈希码是正确的事情,但他们增加每个hashcode由2个大素数,我不知道为什么或如果这是必要的。我主要关心两种类型A和B:

I have seen this question here which seems to suggest that XORing two other hashcodes is the correct thing to do but they multiiply each hashcode by 2 large primes first, and I wasnt sure why or if this was necessary. I'm primarily concerned that for two Types A and B:

new _TypeMatch(A, B) == new _TypeMatch(A, B) // is true for all A and B

组合散列的计算是尽可能高效的,因为创建新的_TypeMatch将是系统的核心部分,因此任何性能低效将在整个系统中被彻底感觉到。

and that the calculation of the combined hash is as efficient as possible as creating a new _TypeMatch is going to be a core part of the system and so any performance inefficiencies will be felt drastically throughout the system.

散列码用于例如哈希表或散列表中,以将存储的键/值平均分配到槽中。一个插槽可以包含许多键/值,但通过使用散列码,可以方便快捷地在地图中找到一个插槽,并从中找到一个更小的值集合中的具体键/值。这提高了在地图中搜索时的速度非常快,不管使用什么类型的数据类型的密钥。当哈希码对于存储的键/值而改变时,不能再通过键检索该值。
你可以使用 1 作为每个对象的哈希码,但这会破坏性能。
你会得到相反的效果(最佳性能)与良好的分布(不同的对象的不同的哈希码),但有一个限制。当您使用例如32位整数类型作为哈希码时,可能的哈希码的数量是有限的。
有关详情,请参见 http://en.wikipedia.org/wiki/Hash_table
尽管

Hashcodes are used for example in a hashmap or hashtable to distribute the stored key/values equally into 'slots'. One slot can contain many key/values but by using the hashcode it is easy and quick to find a slot in the map and from there to look for a concrete key/value in a much smaller set of values. This improves the speed when searching in a map very fast no matter what kind of data type is used for the key. When the hashcode would change for a stored key/value the value can't be retrieved anymore by key. You could just use 1 as hashcode for every object but this would ruin the performance. You get the opposite effect (optimal performance) with a good distribution (different hashcodes for different objects) but there is a limit. When you use for example a 32bit integer type for the hashcode the number of possible hashcodes is limited. See http://en.wikipedia.org/wiki/Hash_table for more details. There are many more use cases for hashes though.

推荐答案

我建议使用 hash2 来自quiver包的方法

I suggest using the hash2 method from quiver package

https://github.com/google/quiver-dart/blob/master/lib/src/core/hash.dart#L26

您可以使用

import 'package:quiver/core.dart' show hash2;

@override
int get hashCode => hash2(val1, val2);

他们使用此代码组合散列代码

They use this code to combine hash codes

int _combine(int hash, int value) {
  hash = 0x1fffffff & (hash + value);
  hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
  return hash ^ (hash >> 6);
}

这篇关于dart,是两个哈希码总是要返回一个新的唯一哈希码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆