Hash function result and bucket number in unordered_set

261 Views Asked by At

Suppose I have an std::unordered_set<T> mySet initialized with my own hash function hashFunc(T item). What I want to do is first insert some items into mySet, and then have a function search(T item) that takes an item, finds which bucket b it would go if it were to be inserted (but does not insert it) and finally returns all other items on bucket b. Can I calculate b like this?

b = hashFunc(item)

Is it guaranteed that b is gonna be the bucket I'm looking for? If not, what alternatives do I have?

1

There are 1 best solutions below

2
On BEST ANSWER

The bucket method on unordered_set takes a key and returns the bucket index that an element with that key would go into. For example:

#include <iostream>
#include <string>
#include <unordered_set>

int main() {
  std::unordered_set<std::string> x = {"foo", "bar", "baz"};

  std::cout << "foo: " << x.bucket("foo") << "\n";
  std::cout << "fox: " << x.bucket("fox") << "\n";
  std::cout << "bat: " << x.bucket("bat") << "\n";
}

on my implementation, prints

foo: 2
fox: 0
bat: 1

Once you have the bucket index, you would then iterate over the items in that bucket by calling the begin, end overloads that take a bucket index.

You shouldn't need to directly call your hash function.