Last active
February 24, 2025 10:25
-
-
Save jweinst1/7ccfafc14a80951074d9e98dd78954a5 to your computer and use it in GitHub Desktop.
playing with unordered buckets in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char const *argv[]) | |
{ | |
std::unordered_map<std::string, std::string> fooMap; | |
const std::string s1 = "foobar"; | |
const std::string s2 = "doobar"; | |
const std::string s3 = "loobar"; | |
const std::string s4 = "4oobar"; | |
const std::string s5 = "77oobar"; | |
std::printf("Bucket count is %zu\n", fooMap.bucket_count()); | |
fooMap.insert(std::make_pair(s1, s2)); | |
fooMap.insert(std::make_pair(s3, s2)); | |
fooMap.insert(std::make_pair(s4, s2)); | |
fooMap.insert(std::make_pair(s5, s2)); | |
std::printf("Bucket count is %zu\n", fooMap.bucket_count()); | |
std::printf("%s is in bucket %zu\n", s1.c_str(), fooMap.bucket(s1)); | |
std::printf("%s is in bucket %zu\n", s2.c_str(), fooMap.bucket(s2)); | |
std::printf("%s is in bucket %zu\n", s3.c_str(), fooMap.bucket(s3)); | |
std::printf("%s is in bucket %zu\n", s4.c_str(), fooMap.bucket(s4)); | |
std::printf("%s is in bucket %zu\n", s5.c_str(), fooMap.bucket(s5)); | |
fooMap.rehash(30); | |
std::printf("Bucket count is %zu\n", fooMap.bucket_count()); | |
std::printf("%s is in bucket %zu\n", s1.c_str(), fooMap.bucket(s1)); | |
std::printf("%s is in bucket %zu\n", s2.c_str(), fooMap.bucket(s2)); | |
std::printf("%s is in bucket %zu\n", s3.c_str(), fooMap.bucket(s3)); | |
std::printf("%s is in bucket %zu\n", s4.c_str(), fooMap.bucket(s4)); | |
std::printf("%s is in bucket %zu\n", s5.c_str(), fooMap.bucket(s5)); | |
std::printf("hash of %s in bucket is %zu\n", s1.c_str(), fooMap.hash_function()(s1)% fooMap.bucket_count()); | |
std::printf("hash of %s in bucket is %zu\n", s2.c_str(), fooMap.hash_function()(s2) % fooMap.bucket_count()); | |
std::printf("hash of %s in bucket is %zu\n", s3.c_str(), fooMap.hash_function()(s3)% fooMap.bucket_count()); | |
std::printf("hash of %s in bucket is %zu\n", s4.c_str(), fooMap.hash_function()(s4)% fooMap.bucket_count()); | |
std::printf("hash of %s in bucket is %zu\n", s5.c_str(), fooMap.hash_function()(s5)% fooMap.bucket_count()); | |
// iteration through hash map in order of buckets | |
for (size_t i = 0; i < fooMap.bucket_count(); ++i) | |
{ | |
std::printf("bucket %zu -----\n", i); | |
for (auto elem = fooMap.begin(i); elem != fooMap.end(i); ++elem) | |
{ | |
std::printf("Key : %s Val : %s\n", elem->first.c_str(), elem->second.c_str()); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment