如何在HashSet或HashMap中使用自定义哈希函数

如何在HashSet或HashMap中使用自定义哈希函数

本文介绍了如何在HashSet或HashMap中使用自定义哈希函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于SipHasher对于我的用例而言太慢,因此我正在尝试实现自定义哈希函数.我找到了一个例子作为进行所有编译的基础.

Since SipHasher is too slow for my use case, I'm trying to implement a custom hash function. I found an example which I used as base to get everything compiling.

我当前的代码如下:

use std::collections::hash_state::{DefaultState};
use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::hash::{Hash, Hasher, SipHasher};
use std::marker;

pub struct FnvHasher(u64);

impl Default for FnvHasher {
    fn default() -> FnvHasher { FnvHasher(0xcbf29ce484222325) }
}

impl Hasher for FnvHasher {
    fn write(&mut self, bytes: &[u8]) {
        let FnvHasher(mut hash) = *self;
        for byte in bytes {
            hash = hash ^ (*byte as u64);
            hash = hash * 0x100000001b3;
        }
        *self = FnvHasher(hash);
    }
    fn finish(&self) -> u64 { self.0 }
}

fn main() {
    let mut set:HashSet<i64, DefaultState<FnvHasher>> = HashSet::with_hash_state(DefaultState::<FnvHasher>);
}

我编译时收到以下错误消息:

When I compile I get the following error message:

$ rustc -V
rustc 1.0.0-nightly (522d09dfe 2015-02-19) (built 2015-02-19)

$ rustc hash.rs
hash.rs:26:86: 26:111 error: mismatched types:
 expected `std::collections::hash::state::DefaultState<FnvHasher>`,
    found `fn(core::marker::PhantomData<FnvHasher>) -> std::collections::hash::state::DefaultState<FnvHasher> {std::collections::hash::state::DefaultState}`
(expected struct `std::collections::hash::state::DefaultState`,
    found fn item) [E0308]
hash.rs:26         let mut set:HashSet<i64, DefaultState<FnvHasher>> = HashSet::with_hash_state(DefaultState::<FnvHasher>);
                                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

如何获取上面的示例以在HashSet/HashMap中编译或使用自定义哈希函数?

How can I get the above example to compile or use a custom hash function in a HashSet/HashMap?

推荐答案

看看 DefaultState 的定义:

pub struct DefaultState<H>(marker::PhantomData<H>);

您正在像对待它一样对待它:

You are treating it as though it were as it used to be:

pub struct DefaultState<H>;

此更改是最近的;现在必须明确使用通用参数.

This change is recent; generic parameters must now be expressly used.

为此,您现在必须使用 Default :: default(),因为该字段是私有的,因此无法编写文字表达式.因此, DefaultState ::< FnvHasher> 变为 Default :: default().

For this you must now use Default::default(), for that field is private, preventing literal expressions from being written. Thus, DefaultState::<FnvHasher> becomes Default::default().

一旦使用了 Default :: default(),您就不妨将整个猪圈替换成 HashSet :: with_hash_state(Default :: default()) Default :: default():

And once you’re using Default::default() for that, you might as well go the whole hog and replace HashSet::with_hash_state(Default::default()) with Default::default():

let mut set: HashSet<i64, DefaultState<FnvHasher>> = Default::default();

这篇关于如何在HashSet或HashMap中使用自定义哈希函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:05