首页 > 解决方案 > 将 absl::flat_hash_set 与 folly::small_vector 一起使用

问题描述

我正在尝试创建一个absl::flat_hash_setof folly::small_vector。添加对 absl::Hash 的自定义类型的支持的文档说:

类型的AbslHashValue重载只能在与所述类型相同的文件和命名空间中声明。给定类型的正确AbslHashValue实现将通过 ADL 发现。

不幸的是,我不能这样做,因为我不控制库的源代码或安装。

所以我试过这个:

// Typedefs.h

constexpr size_t MAX_SEQUENCE_LENGTH = 128; 

using tSequence = folly::small_vector<
    int8_t, 
    MAX_BET_SEQUENCE_LENGTH, 
    folly::small_vector_policy::NoHeap>;
using tSequences = folly::small_vector<
    tSequence, 
    4, 
    folly::small_vector_policy::NoHeap>;

template<typename H>
H AbslHashValue(H h, const tSequence& sequence) {
  return H::combine_contiguous(std::move(h), sequence.data(), sequence.size());
}

template<typename H>
H AbslHashValue(H h, const tSequences& sequences) {
  for(const auto& sequence : sequences) {
    h = H::combine_contiguous(std::move(h), sequence.data(), sequence.size());
  }
  return h;
}

// MyClass.h

class MyClass {
  //...
  private:
    absl::flat_hash_set<tSequences> _sequences;
};

但是我得到了大量的模板错误,这些错误似乎表明我的类型不能被散列:

error: no match for call to ‘(const absl::hash_internal::Hash<folly::small_vector<folly::small_vector<signed char, 128, folly::small_vector_policy::NoHeap>, 4, folly::small_vector_policy::NoHeap> >) (const key_type&)’
  551 |   auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));

我能做些什么来让folly::small_vectorAbseil 可以散列吗?

标签: c++hashfollyabseil

解决方案


推荐阅读