From bfabf8325f96fcf9a2b43170b0472fc58db8f04d Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 13 Dec 2022 05:33:55 +0800 Subject: [PATCH] change hash function to fnv1a (#48) change mbtiles hash function from hashCode to fnv1a. --- mbtiles.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mbtiles.cpp b/mbtiles.cpp index 01df016f..d1df7290 100644 --- a/mbtiles.cpp +++ b/mbtiles.cpp @@ -100,13 +100,14 @@ sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable) { } void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size) { - // Store tiles by a hash of their contents. node-mbtiles uses MD5, - // but I am resisting adding the dependency, so instead here is - // everybody's first hash function. It is the same as Java's String.hashCode(), - // https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#hashCode() - unsigned long long h = 0; + // Store tiles by a hash of their contents (fnv1a 64-bit) + // http://www.isthe.com/chongo/tech/comp/fnv/ + const unsigned long long fnv_offset_basis = 14695981039346656037u; + const unsigned long long fnv_prime = 1099511628211u; + unsigned long long h = fnv_offset_basis; for (int i = 0; i < size; i++) { - h = h * 31 + data[i]; + h ^= (unsigned char) data[i]; + h *= fnv_prime; } std::string hash = std::to_string(h);