danielperezunlpam

Open hashing

Jun 10th, 2021
845
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <unordered_map>
  5. #include <vector>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <iterator>
  9. #include <sstream>
  10.  
  11. std::size_t hash32shiftmult(std::size_t key) {
  12.     std::size_t c2=0x27d4eb2d;
  13.     key = (key ^ 61) ^ (key >> 16);
  14.     key = key + (key << 3);
  15.     key = key ^ (key >> 4);
  16.     key = key * c2;
  17.     key = key ^ (key >> 15);
  18.     return key;
  19. }
  20.  
  21.  
  22. class KeyHasher {
  23. public:
  24.     std::size_t operator()(const int& k) const {
  25. //        return k%5; // Función Hash 1
  26. //        return k%15; // Función Hash 2
  27.         return hash32shiftmult(k)%15; // Función Hash 3
  28.     }
  29. };
  30.  
  31. template <class T>
  32. std::string listToString(std::list<T> l){
  33.     std::stringstream ss;
  34.     for(std::list<int>::iterator it = l.begin(); it!=l.end(); ++it){
  35.         ss << *it;
  36.         if(std::distance(it, l.end())>1)
  37.             ss << ", ";
  38.     }
  39.     return "[" + ss.str() + "]";
  40. }
  41.  
  42.  
  43. int main () {
  44.     std::list<int> lints = {66, 81, 133, 121, 99, 96, 60, 117, 128, 122};
  45.     std::unordered_map<int,int, KeyHasher> mymap;
  46.     mymap.reserve(15);
  47.  
  48.     for(auto v : lints){
  49.         mymap.insert({v,v});
  50.     }
  51.     std::cout << std::endl << listToString(lints);
  52.  
  53.     unsigned n = mymap.bucket_count();
  54.  
  55.     std::cout << "mymap has " << n << " buckets.\n";
  56.  
  57.     for (unsigned i=0; i<n; ++i) {
  58.         std::cout << "bucket #" << i << " contains: ";
  59.         for (auto it = mymap.begin(i); it!=mymap.end(i); ++it)
  60.             std::cout << "[" << it->first << ":" << it->second << "] ";
  61.         std::cout << "\n";
  62.     }
  63.     return 0;
  64. }
  65.  
RAW Paste Data