text
stringlengths
0
2.2M
result_type operator()(const char *data, size_t length);
// Return a hash of the specified 'length' bytes of 'data'.
};
//..
// Then, we declare our functor, 'SeededHash', which will take a seed
// generator, and be able to run any of our hashing algorithms by generating
// the correct size seed with the seed generator.
//..
template <class HASH_ALGORITHM>
class SeededHash {
// This class template implements an interface similar to 'std::hash',
// which will used the (template parameter) type 'SEED_GENERATOR' and
// 'HASH_ALGORITHM' to compute hashes.
public:
typedef typename HASH_ALGORITHM::result_type result_type;
// Type of the hash that will be returned.
private:
char seed[HASH_ALGORITHM::k_SEED_LENGTH];
// Stores the seed that will be used to run the (template
// parameter) type 'HASH_ALGORITHM'
public:
template<class SEED_GENERATOR>
SeededHash(SEED_GENERATOR seedGenerator);
//Create a 'SeededHash' and generate a seed using the specified
//'seedGenerator'.
result_type operator()(const char *data, size_t length) const;
// Returns a hash generated by the (template parameter) type
// 'HASH_ALGORITHM' for the specified 'length' bytes of 'data'.
};
//..
// Next, we define our constructor where we actually use 'bslh::SeedGenerator'.
// 'bslh::SeedGenerator' allows us to create arbitrary length seeds to match
// the requirements of the above declared algorithms.
//..
template <class HASH_ALGORITHM>
template<class SEED_GENERATOR>
SeededHash<HASH_ALGORITHM>::SeededHash(SEED_GENERATOR seedGenerator) {
seedGenerator.generateSeed(seed, HASH_ALGORITHM::k_SEED_LENGTH);
}
template <class HASH_ALGORITHM>
typename SeededHash<HASH_ALGORITHM>::result_type
SeededHash<HASH_ALGORITHM>::operator()(const char *data,
size_t length) const {
HASH_ALGORITHM hashAlg(seed);
return hashAlg(data, length);
}
//=============================================================================
// ELIDED USAGE EXAMPLE IMPLEMENTATIONS
//-----------------------------------------------------------------------------
unsigned int someSeededHash(const char *seed,
size_t seedLength,
const char *data,
size_t length)
// Hash the specified 'length' bytes of 'data' using the specified
// 'seedLength' bytes of 'seed' to seed the hash. This is not a real hash
// function. DO NOT USE FOR ACTUAL HASHING
{
const unsigned int *castedSeed = reinterpret_cast<const unsigned int *>(
seed);
unsigned int hash = 0;
for (size_t i = 0; i < seedLength/4; ++i) {
hash ^= castedSeed[i];
}
for (size_t i = 0; i < length; ++i) {
hash *= data[i];
}
return hash;
}
Seeded32BitHashingAlgorithm::Seeded32BitHashingAlgorithm(const char *seed) :
d_seed(seed) { }
Seeded32BitHashingAlgorithm::result_type
Seeded32BitHashingAlgorithm::operator()(const char *data, size_t length) {
return someSeededHash(d_seed, 4, data, length);
}
Seeded64BitHashingAlgorithm::Seeded64BitHashingAlgorithm(const char *seed) :
d_seed(seed) { }
Seeded64BitHashingAlgorithm::result_type
Seeded64BitHashingAlgorithm::operator()(const char *data, size_t length) {
return someSeededHash(d_seed, 8, data, length);
}
Seeded1024BitHashingAlgorithm::Seeded1024BitHashingAlgorithm(const char *seed)
: d_seed(seed) { }