我正在为字符串实现CaseAccentInsensitiveEqualityComparer.我不确定如何实现GetHashCode [英] I'm implementing a CaseAccentInsensitiveEqualityComparer for Strings. I'm not sure how to implement the GetHashCode

查看:39
本文介绍了我正在为字符串实现CaseAccentInsensitiveEqualityComparer.我不确定如何实现GetHashCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的:

public class CaseAccentInsensitiveEqualityComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0;
        }

        public int GetHashCode(string obj)
        {
             // not sure what to put here
        }
    }

我知道在这种情况下 GetHashCode 的作用,我所缺少的是如何生成 InvariantCulture IgnoreNonSpace obj 的> IgnoreCase 版本,以便我可以返回它的 HashCode .

I know the role of GetHashCode in this context, what I'm missing is how to produce the InvariantCulture, IgnoreNonSpace and IgnoreCase version of obj so that I can return it's HashCode.

我本人可以从 obj 中删除变音符号和案件,然后将其返回为 hashcode ,但我想知道是否还有更好的选择.

I could remove diacritics and the case from obj myself and then return it's hashcode, but I wonder if there's a better alternative.

推荐答案

由于 Dictionaries HashMaps 调用 Equals(),即使两个对象的 GetHashCode()返回相同的值.
规则是,如果对象相等,则GetHashCode()必须返回相同的值.
缺点是 HashSet (或 Dictionary )性能下降到与使用List相同的程度.要查找项目,必须为每个比较调用 Equals().
一种更快的方法是将其转换为Accent Insensitive字符串并获取其哈希码.

Returning 0 inside GetHashCode() works (as pointed out by @Michael Perrenoud) because Dictionaries and HashMaps call Equals() just if GetHashCode() for two objects return the same values.
The rule is, GetHashCode() must return the same value if objects are equal.
The drawback is that the HashSet (or Dictionary) performance decreases to the point it becomes the same as using a List. To find an item it has to call Equals() for each comparison.
A faster approach would be converting to Accent Insensitive string and getting its hashcode.

从此帖子中删除音调(变音符号)的代码

Code to remove accent (diacritics) from this post

static string RemoveDiacritics(string text)
{
    return string.Concat(
        text.Normalize(NormalizationForm.FormD)
        .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
                                        UnicodeCategory.NonSpacingMark)
    ).Normalize(NormalizationForm.FormC);
}

比较器代码:

public class CaseAccentInsensitiveEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0;
    }

    public int GetHashCode(string obj)
    {
        return obj != null ? RemoveDiacritics(obj).ToUpperInvariant().GetHashCode() : 0;
    }

    private string RemoveDiacritics(string text)
    {
        return string.Concat(
            text.Normalize(NormalizationForm.FormD)
            .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
                                          UnicodeCategory.NonSpacingMark)
          ).Normalize(NormalizationForm.FormC);
    }
}

这篇关于我正在为字符串实现CaseAccentInsensitiveEqualityComparer.我不确定如何实现GetHashCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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