哈希多态类型的正确方式

Hashing polymorphic type the proper way

本文关键字:方式 多态 类型 哈希      更新时间:2023-10-16

我有一个使用Howard Hinnant方法实现的哈希过程(基于hash_append重载的通用哈希(。

该方法的目的是创建类的哈希以"记住"计算结果(请参阅本答案的末尾(,因此我面临一些问题。具体而言,请考虑以下可能需要哈希处理的可能Input类:

struct A {
virtual int do_stuff() const = 0;
virtual ~A(); 
};
struct B: A {
int do_stuff() const override { return 0; }
};
struct C: A {
const int u;
int do_stuff() const override { return u; }
};
struct Input {
A const& a; // store a reference to an instance of B or C
};

现在,如果我想散列Input,我将得到类似的东西:

template <class HashAlgorithm>
void hash_append(HashAlgorithm& h, Input const& input) {
hash_append(h, typeid(input));
hash_append(h, typeid(input.a));
}

所以我需要超载hash_appendA

template <class HashAlgorithm>
void hash_append(HashAlgorithm& h, A const& a) {
hash_append(h, typeid(a)); 
}

这里的问题是,根据a的运行时类型,我需要向哈希添加额外的信息,例如,对于C我需要添加u

我想到了以下解决方案(和缺点(:

  1. A添加一个虚拟方法,该方法返回可添加到typeid()哈希的特定值,但是:

    • 这意味着在A中添加一个与A目的无关的方法,因此我不太喜欢这个想法(特别是因为我有多个类似A类的类(;
    • 这打破了hash_append的概念,因为该方法将具有所有继承类的唯一返回类型。
  2. hash_append里面做一堆dynamic_cast

    • 我觉得这很丑...特别是如果我有多个类似于A的类;
    • 这很容易出错:如果有人添加了A的新子项并且没有在hash_append中添加dynamic_cast。

有没有办法散列多态类型,而不必修改类型本身或依赖一堆dynamic_cast


这样做的最终目标是能够记住一些重函数的结果。让我们勾勒出我的应用程序的基本结构:

struct Input { };
struct Result { };
Result solve(Input const&);

solve函数的计算量很大,所以我想使用Inputs 的哈希将先前计算的结果保存在文件中,例如:

// depends on hash_append
std::string hash(Input const&);
Result load_or_solve(Input const& input) {
auto h = hash(input);
Result result;
if (exists(h)) { // if result exists, load it
result = load(h);
}
else { // otherwize, solve + store
result = solve(input);
store(h, result);
}
return result;
}

loadstore方法将从文件中加载和存储结果,目标是记住不同运行之间的解决方案。

如果您对如何在不必处理上述问题的情况下记住这些结果有建议,我将很高兴阅读它们。

您可以在Ahash_append版本中使用双重调度,并将请求转发到正确的版本(即用于BC的版本(。缺点是您必须向这些类添加样板才能接受访问者,我不能说您是否可以接受。
这里有一堆代码应该说明这个想法:

struct B;
struct C;
struct Visitor {
virtual void visit(const B &) = 0;
virtual void visit(const C &) = 0;
};
template<typename T, typename... O>
struct HashVisitor: T, HashVisitor<O...> {
template<typename U>
std::enable_if_t<std::is_same<T, U>::value> tryVisit(const U &u) {
T::operator()(u);
}
template<typename U>
std::enable_if_t<not std::is_same<T, U>::value> tryVisit(const U &u) {
HashVisitor<O...>::visit(u);
}
void visit(const B &b) override { tryVisit<B>(b); }
void visit(const C &c) override { tryVisit<C>(c); }
};
template<>
struct HashVisitor<>: Visitor {};
template<typename... F
auto factory(F&&... f) {
return HashVisitor<std::decay_t<F>>{std::forward<F>(f)...};
}
struct A {
virtual void accept(Visitor &) = 0;
virtual int do_stuff() const = 0;
virtual ~A();
};
struct B: A {
void accept(Visitor &v) override { v.visit(*this); }
int do_stuff() const override { return 0; }
};
struct C: A {
const int u;
void accept(Visitor &v) override { v.visit(*this); }
int do_stuff() const override { return u; }
};
template <class HashAlgorithm>
void hash_append(HashAlgorithm &, const B &) {
// do something
}
template <class HashAlgorithm>
void hash_append(HashAlgorithm &, const C &) {
// do something
}
template <class HashAlgorithm>
void hash_append(HashAlgorithm &h, const A &a) {
auto vis = factory(
[&h](const B &b){ hash_append(h, b); },
[&h](const C &c){ hash_append(h, c); }
);
a.accept(vis);
}