I would start working with ranges, like so:
template<typename BoostUnorderedMap, typename Key>
boost::iterator_range< typename BoostUnorderedMap::local_iterator > get_bucket_range( BoostUnorderedMap& myMap, Key const& k ) {
int bucketIndex = myMap.bucket( k );
return boost::iterator_range< typename BoostUnorderedMap::local_iterator >(
myMap.begin(bucketIndex),
myMap.end(bucketIndex)
}
}
template<typename BoostUnorderedMap, typename Key>
boost::iterator_range< typename BoostUnorderedMap::local_const_iterator > get_bucket_range( BoostUnorderedMap const& myMap, Key const& k ) {
int bucketIndex = myMap.bucket( k );
return boost::iterator_range< typename BoostUnorderedMap::local_const_iterator >(
myMap.begin(bucketIndex),
myMap.end(bucketIndex)
}
}
then, at least in C++11, you can do the following:
for (auto && entry : get_bucket_range( some_map, "bob" ) )
and it iterates over everything in the "bob"
bucket.
While this does use bucketIndex
, it hides these details from the end consumer, and simply gives you a boost::range
instead.