// -*- coding: utf-8 -*- // Time-stamp: // Hashcodes #ifndef HASHCODE_H #define HASHCODE_H #include using namespace std; // This class provides hashcoding for the type T. // The defaut is to call a hashcode method of the class T template class hashcode { public: static unsigned int hash(const T& value) { return value.hashcode(); } }; // For integers, the hashcode is the value cast to a unsigned integer template<> class hashcode { public: static unsigned int hash(int value) { return value; } }; // Floats // The hashcode is coding of the float interpreted as an integer template<> class hashcode { public: static unsigned int hash(float value) { return *((unsigned int *) &value); } }; // Double // The hashcode is computed as // - i[0] if sizeof(double) == 4 // where i[0] is the integer value obtained by interpreting the 4 bytes // of the coding of the double as an integer // - i[0] ^ i[1] if sizeof(double) == 8 // where i[0] and i[1] are the integer values obtained by interpreting // the first 4 bytes and the last 4 bytes of the coding as integers template<> class hashcode { public: static unsigned int hash(double value) { unsigned int h = 0; unsigned int *p = (unsigned int*) &value; for (unsigned int i = 0; i < sizeof(double)/sizeof(int); ++i) h ^= p[i]; return h; } }; // Pointers // template class hashcode { // public: // static unsigned int hash(T* value) { return (unsigned int) value; } // }; // String // The hashcode of a string s is s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] // computed with unsigned int arithmetic template<> class hashcode { public: static unsigned int hash(string s) { unsigned int h = 0; for (string::iterator it = s.begin(); it != s.end(); ++it) h = h * 31 + ((unsigned int) *it); return h; } }; #endif