Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- #include <unordered_map>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <iterator>
- #include <sstream>
- std::size_t hash32shiftmult(std::size_t key) {
- std::size_t c2=0x27d4eb2d;
- key = (key ^ 61) ^ (key >> 16);
- key = key + (key << 3);
- key = key ^ (key >> 4);
- key = key * c2;
- key = key ^ (key >> 15);
- return key;
- }
- class KeyHasher {
- public:
- std::size_t operator()(const int& k) const {
- // return k%5; // Función Hash 1
- // return k%15; // Función Hash 2
- return hash32shiftmult(k)%15; // Función Hash 3
- }
- };
- template <class T>
- std::string listToString(std::list<T> l){
- std::stringstream ss;
- for(std::list<int>::iterator it = l.begin(); it!=l.end(); ++it){
- ss << *it;
- if(std::distance(it, l.end())>1)
- ss << ", ";
- }
- return "[" + ss.str() + "]";
- }
- int main () {
- std::list<int> lints = {66, 81, 133, 121, 99, 96, 60, 117, 128, 122};
- std::unordered_map<int,int, KeyHasher> mymap;
- mymap.reserve(15);
- for(auto v : lints){
- mymap.insert({v,v});
- }
- std::cout << std::endl << listToString(lints);
- unsigned n = mymap.bucket_count();
- std::cout << "mymap has " << n << " buckets.\n";
- for (unsigned i=0; i<n; ++i) {
- std::cout << "bucket #" << i << " contains: ";
- for (auto it = mymap.begin(i); it!=mymap.end(i); ++it)
- std::cout << "[" << it->first << ":" << it->second << "] ";
- std::cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement