Using absl::flat_hash_set with folly::small_vector

795 views Asked by At

I'm trying to create a absl::flat_hash_set of folly::small_vector. The documentation for adding support for a custom type for absl::Hash says:

An AbslHashValue overload for a type should only be declared in the same file and namespace as said type. The proper AbslHashValue implementation for a given type will be discovered via ADL.

Unfortunately I can't do this for folly, since I don't control the source or installation of the library.

So I've tried this:

// 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;
};

But I get a ton of template errors that seem to indicate that my types can't be hashed:

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));

Is there anything I can do to make folly::small_vector hashable by Abseil?

0

There are 0 answers