Search for std :: unordered_set by hash value and predicate

How can I search for the hash value of std :: unordered_set and have some predicate? (A predicate defining equivalence on pred(x) && pred(y), meaning x == y.)

+3
source share
1 answer

Well, you can ignore the hash value and iterate over all unsorted_setpredicate testing. Not ideal efficiency, since you would prefer to sort out only one bucket, but it does what you ask.

The standard unordered_sethas an interface begin(size_t)to get an iterator for a specific bucket (by number) and an interface bucket_count()to get the number of buckets.

Objects with a given hash are guaranteed to display in the same bucket, so repeating that bucket checking the predicate is enough for what you want to do.

I can't see anything in the standard to guarantee the right bucket for iteration hash_value % bucket_count(). There is a function to get a bucket for a given object, but not to get a bucket for a given hash value. Try this on your implementation though: I think this is a reasonable guess, and I may just not find the key limitation in the standard.

In general, I think you want something like:

size_t bucket = hash_value % myset.bucket_count();
find_if(myset.begin(bucket), myset.end(bucket), pred);

but I'm not sure.

+3

Source: https://habr.com/ru/post/1769333/


All Articles