1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "antlr4-common.h"
13 class ANTLR4CPP_PUBLIC MurmurHash {
16 #if __cplusplus >= 201703L
17 static constexpr size_t DEFAULT_SEED = 0;
24 /// Initialize the hash using the default seed value.
25 /// Returns the intermediate hash value.
27 static size_t initialize();
29 /// Initialize the hash using the specified seed.
30 static size_t initialize(size_t seed);
32 /// Update the intermediate hash value for the next input {@code value}.
33 /// <param name="hash"> the intermediate hash value </param>
34 /// <param name="value"> the value to add to the current hash </param>
35 /// Returns the updated intermediate hash value.
36 static size_t update(size_t hash, size_t value);
39 * Update the intermediate hash value for the next input {@code value}.
41 * @param hash the intermediate hash value
42 * @param value the value to add to the current hash
43 * @return the updated intermediate hash value
46 static size_t update(size_t hash, Ref<T> const& value) {
47 return update(hash, value != nullptr ? value->hashCode() : 0);
51 static size_t update(size_t hash, T *value) {
52 return update(hash, value != nullptr ? value->hashCode() : 0);
56 /// Apply the final computation steps to the intermediate value {@code hash}
57 /// to form the final result of the MurmurHash 3 hash function.
59 /// <param name="hash"> the intermediate hash value </param>
60 /// <param name="entryCount"> the number of calls to update() before calling finish() </param>
61 /// <returns> the final hash result </returns>
62 static size_t finish(size_t hash, size_t entryCount);
64 /// Utility function to compute the hash code of an array using the MurmurHash3 algorithm.
66 /// @param <T> the array element type </param>
67 /// <param name="data"> the array data </param>
68 /// <param name="seed"> the seed for the MurmurHash algorithm </param>
69 /// <returns> the hash code of the data </returns>
70 template<typename T> // where T is C array type
71 static size_t hashCode(const std::vector<Ref<T>> &data, size_t seed) {
72 size_t hash = initialize(seed);
73 for (auto entry : data) {
74 hash = update(hash, entry->hashCode());
77 return finish(hash, data.size());