mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 02:54:54 +03:00
368e74fdf8
For some algorithms, such as bloom filters, it's possible to reuse a hash function (rather than having different hashing functions) if the seed is different each time the hash function is used. Modify AK::string_hash() to take a seed parameter, which defaults to 0 (the value the hash value was originally initialized to).
30 lines
522 B
C++
30 lines
522 B
C++
/*
|
|
* Copyright (c) 2018-2021, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Types.h>
|
|
|
|
namespace AK {
|
|
|
|
constexpr u32 string_hash(char const* characters, size_t length, u32 seed = 0)
|
|
{
|
|
u32 hash = seed;
|
|
for (size_t i = 0; i < length; ++i) {
|
|
hash += (u32)characters[i];
|
|
hash += (hash << 10);
|
|
hash ^= (hash >> 6);
|
|
}
|
|
hash += hash << 3;
|
|
hash ^= hash >> 11;
|
|
hash += hash << 15;
|
|
return hash;
|
|
}
|
|
|
|
}
|
|
|
|
using AK::string_hash;
|