]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/MurmurHash.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / misc / MurmurHash.h
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.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlr4 {
11 namespace misc {
12
13   class ANTLR4CPP_PUBLIC MurmurHash {
14
15   private:
16 #if __cplusplus >= 201703L
17     static constexpr size_t DEFAULT_SEED = 0;
18 #else
19     enum : size_t {
20       DEFAULT_SEED = 0,
21     };
22 #endif
23
24     /// Initialize the hash using the default seed value.
25     /// Returns the intermediate hash value.
26   public:
27     static size_t initialize();
28
29     /// Initialize the hash using the specified seed.
30     static size_t initialize(size_t seed);
31
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);
37
38     /**
39      * Update the intermediate hash value for the next input {@code value}.
40      *
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
44      */
45     template <class T>
46     static size_t update(size_t hash, Ref<T> const& value) {
47       return update(hash, value != nullptr ? value->hashCode() : 0);
48     }
49
50     template <class T>
51     static size_t update(size_t hash, T *value) {
52       return update(hash, value != nullptr ? value->hashCode() : 0);
53     }
54
55     /// <summary>
56     /// Apply the final computation steps to the intermediate value {@code hash}
57     /// to form the final result of the MurmurHash 3 hash function.
58     /// </summary>
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);
63
64     /// Utility function to compute the hash code of an array using the MurmurHash3 algorithm.
65     ///
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());
75       }
76
77       return finish(hash, data.size());
78     }
79   };
80
81 } // namespace atn
82 } // namespace antlr4