简体   繁体   English

是否有“congruent hash”与“identity hash”的标准名称/模板原型?

[英]Is there a standard name / templated prototype for “congruent hash” vs “identity hash”?

I have a templated class Foo that can do identity comparisons (via == ), but has a function Foo::sameStructureAs(Foo const & other) for more of a "value" vs. "pointer" notion of equality. 我有一个模板化的类Foo可以进行身份​​比较(通过== ),但有一个函数Foo::sameStructureAs(Foo const & other) ,更多的是“值”与“指针”相等的概念。

I'd like to make an unordered_map which overrides the hash function and the equality predicate. 我想创建一个unordered_map来覆盖散列函数和等式谓词。 They default to std::equal_to<Key> and std::hash<Key> ...which I provide for my type, based on identity. 它们默认为std::equal_to<Key>std::hash<Key> ...我根据身份为我的类型提供。 But I need them to be comparing on the basis of my sameStructureAs . 但我需要他们在我的sameStructureAs一个结构的基础上进行比较。

Since Foo is a template, I do something like this: 由于Foo是一个模板,我做这样的事情:

template <class> struct same_structure_as;

template <class> struct hash_structure;

template <class T>
struct hash_structure<Foo<T>>
{
    size_t operator() (Foo<T> const & value) const
    {
        // whatever...
    }
};

template <class T>
struct same_structure_as<Foo<T>>
{
    bool operator() (Foo<T> const & left, Foo<T> const & right) const
    {
        // whatever...
    }
};

Which seems like I'm following roughly the strategy of the classes in std:: for this purpose, and creating something general. 这似乎是我为了这个目的大致遵循std::中的类的策略,并创建一般的东西。 So does that look right? 那看起来是对的吗?

Secondly: Is there any precedent for the naming of this or a prototype already existing in std::? 其次:在std ::中已经存在的这个或原型的命名有先例吗? I've thought about words like isomorphic or congruent . 我想过isomorphiccongruent词。 It seems like something that would come up often in designing classes when you have more than one idea of what it means to be "equal". 当你对“平等”意味着什么有一个以上的想法时,这似乎会在设计课程中经常出现。

If you are looking at a type through this "different" notion of comparison or equality, ask if what you really need is another type . 如果你通过这种“不同”的比较或平等概念来看待一种类型,那么问一下你真正需要的是另一种类型 Perhaps there is some kind of cast or coercion you would apply to the underlying data so that its new notion of equality/assignment/comparison fits this test you are designing. 也许你会对基础数据采用某种强制转换或强制措施,这样它的新概念/赋值/比较就适合你正在设计的这个测试。

That way you can properly implement the std:: functions for that type... and use it in collections without having to pass in these extra predicates. 这样你就可以正确地实现该类型的std :: functions ......并在集合中使用它,而不必传递这些额外的谓词。 So perhaps call the type with pointer-equality semantics FooRef and the one with value semantics Foo . 因此,可能使用指针等式语义FooRef调用类型,使用值语义Foo调用类型。

If for some reason you can't do this...then looking at the names one wants to parallel: 如果由于某种原因你不能这样做...那么看一个想要并行的名字:

  • std::equal_to<Key>

  • std::hash<Key>

Keeping the equal_to and hash in there is probably the closest to "standard" one will accomplish. 保持equal_tohash在那里可能最接近“标准”的人将完成。 So rather than introducing new terms like congruence or isometric ,call out exactly what is equal or getting hashed...and use the above as suffixes: 因此,不要引入像“ congruence或“ isometric这样的新术语,而是要确切地说出相同或者经过散列的内容......并使用上面的后缀作为后缀:

  • std::content_equal_to<Key>

  • std::content_hash<Key>

If it's the "structure" of something being compared you can apply that with structure_equal_to and structure_hash . 如果它是被比较的东西的“结构”,你可以用structure_equal_tostructure_hash来应用它。 Only caveat being that "struct"/"structure" has meaning in C++, so it may lead a reader to think it's comparing type_info or something like that. 唯一需要注意的是“结构”/“结构”在C ++中具有意义,因此它可能会引导读者认为它正在比较type_info或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM