00001 #include "Checksum.hpp"
00002 #include <iostream>
00003 #include <cmath>
00004
00005 namespace aitools{
00006 namespace invertedindex {
00007
00008 bool
00009 Checksum::is_prime(uint32_t number)
00010 {
00011 if (number == 2) return true;
00012 if (number < 2 || number % 2 == 0) return false;
00013 for (unsigned i(3); i <= std::sqrt(number); ++++i)
00014 {
00015 if (number % i == 0) return false;
00016 }
00017 return true;
00018 }
00019
00020 uint32_t
00021 Checksum::next_prime(uint32_t number)
00022 {
00023 while (!is_prime(number)) ++number;
00024 return number;
00025 }
00026
00027 uint16_t
00028 Checksum::hash16(const std::string& key)
00029 {
00030 return hash32(key) % uint16_max;
00031 }
00032
00033 uint32_t
00034 Checksum::hash32(const std::string& key)
00035 {
00036 return hash32(key.data(), key.length());
00037 }
00038
00039 uint32_t
00040 Checksum::hash32(const char* key, size_t length)
00041 {
00042
00043 uint32_t result(static_cast<uint32_t>(2166136261UL));
00044 for (; length > 0; --length)
00045 {
00046 result ^= static_cast<uint32_t>(*key++);
00047 result *= static_cast<uint32_t>(16777619UL);
00048 }
00049 return result;
00050 }
00051
00052 }
00053 }