|
| 1 | +/** FNVHash class representing the FNV-0, FNV-1, and FNV-1a hashes. |
| 2 | + Use one of these for setting up hashing of unsigned 32-bit and 64-bit values. |
| 3 | +
|
| 4 | + Create one of these with a block of source data or a stream, |
| 5 | + and it calculates the FNV hash of that data. |
| 6 | +
|
| 7 | + @see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function |
| 8 | + @see http://www.isthe.com/chongo/tech/comp/fnv/index.html |
| 9 | +*/ |
| 10 | +template<typename Type, Type primeValueSource, Type offsetBasisSource, bool use1AAlgorithm> |
| 11 | +class FNVHash final |
| 12 | +{ |
| 13 | +public: |
| 14 | + //============================================================================== |
| 15 | + /** Some predetermined prime value as per FNV's specs. */ |
| 16 | + static inline constexpr Type primeValue = primeValueSource; |
| 17 | + /** In FNV-0, this should be 0. Otherwise, this will be some pre-calculated value. */ |
| 18 | + static inline constexpr Type offsetBasis = offsetBasisSource; |
| 19 | + |
| 20 | + //============================================================================== |
| 21 | + /** Creates a null FNVHash object. */ |
| 22 | + FNVHash() noexcept = default; |
| 23 | + |
| 24 | + /** Creates a copy of another FNVHash. */ |
| 25 | + FNVHash (const FNVHash&) noexcept = default; |
| 26 | + |
| 27 | + /** */ |
| 28 | + explicit FNVHash (Type initialState) noexcept : |
| 29 | + state (initialState) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + /** Creates a hash for the input from a stream. |
| 34 | +
|
| 35 | + This will read up to the given number of bytes |
| 36 | + from the stream and produce the hash of that. |
| 37 | +
|
| 38 | + If the number of bytes to read is negative, |
| 39 | + it'll read until the stream is exhausted. |
| 40 | + */ |
| 41 | + FNVHash (InputStream& input, int64 numBytesToRead = -1) |
| 42 | + { |
| 43 | + if (numBytesToRead < 0) |
| 44 | + numBytesToRead = std::numeric_limits<int64>::max(); |
| 45 | + |
| 46 | + std::array<uint8, 512> tempBuffer; |
| 47 | + |
| 48 | + while (numBytesToRead > 0 && ! input.isExhausted()) |
| 49 | + { |
| 50 | + tempBuffer.fill (0); |
| 51 | + const auto bytesRead = input.read (tempBuffer.data(), (int) jmin (numBytesToRead, (int64) tempBuffer.size())); |
| 52 | + |
| 53 | + if (bytesRead <= 0) |
| 54 | + break; |
| 55 | + |
| 56 | + numBytesToRead -= bytesRead; |
| 57 | + |
| 58 | + for (auto v : tempBuffer) |
| 59 | + process (static_cast<Type> (v)); // Deliberately widened to follow suit with the source C examples. |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** Creates a hash for a block of binary data. */ |
| 64 | + explicit FNVHash (const MemoryBlock& data) noexcept : |
| 65 | + FNVHash (MemoryInputStream (data, false)) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + /** Creates a hash for a block of binary data. */ |
| 70 | + FNVHash (const void* data, size_t numBytes) noexcept : |
| 71 | + FNVHash (MemoryBlock (data, numBytes)) |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + /** Creates a hash for the contents of a file. */ |
| 76 | + explicit FNVHash (const File& file) |
| 77 | + { |
| 78 | + if (FileInputStream fin (file); fin.openedOk()) |
| 79 | + state = FNVHash (fin).get(); |
| 80 | + } |
| 81 | + |
| 82 | + /** Creates a hash of the characters in a UTF-8 buffer. |
| 83 | +
|
| 84 | + Example: |
| 85 | + @code |
| 86 | + FNVHash myResults (myString.toUTF8()); |
| 87 | + @endcode |
| 88 | + */ |
| 89 | + explicit FNVHash (CharPointer_UTF8 utf8) noexcept : |
| 90 | + FNVHash (utf8.getAddress(), utf8.getAddress() != nullptr ? utf8.sizeInBytes() - 1 : 0) |
| 91 | + { |
| 92 | + } |
| 93 | + |
| 94 | + /** Destructor. */ |
| 95 | + ~FNVHash() noexcept = default; |
| 96 | + |
| 97 | + //============================================================================== |
| 98 | + /** Copies another FNVHash. */ |
| 99 | + FNVHash& operator= (const FNVHash&) noexcept = default; |
| 100 | + |
| 101 | + //============================================================================== |
| 102 | + /** */ |
| 103 | + Type process (Type value) noexcept |
| 104 | + { |
| 105 | + if constexpr (use1AAlgorithm) |
| 106 | + { |
| 107 | + // FNV-1a: |
| 108 | + state ^= value; |
| 109 | + state *= primeValue; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // FNV-0 and FNV-1: |
| 114 | + state *= primeValue; |
| 115 | + state ^= value; |
| 116 | + } |
| 117 | + |
| 118 | + return state; |
| 119 | + } |
| 120 | + |
| 121 | + /** @returns */ |
| 122 | + constexpr Type get() const noexcept { return state; } |
| 123 | + |
| 124 | + /** @returns */ |
| 125 | + String toHexString() const { return String::toHexString (state); } |
| 126 | + |
| 127 | + //============================================================================== |
| 128 | + /** @returns */ |
| 129 | + bool operator== (const FNVHash& other) const noexcept { return state == other.state; } |
| 130 | + /** @returns */ |
| 131 | + bool operator!= (const FNVHash& other) const noexcept { return ! operator== (other); } |
| 132 | + |
| 133 | +private: |
| 134 | + //============================================================================== |
| 135 | + Type state = offsetBasis; |
| 136 | + |
| 137 | + //============================================================================== |
| 138 | + JUCE_LEAK_DETECTOR (FNVHash) |
| 139 | +}; |
| 140 | + |
| 141 | +//============================================================================== |
| 142 | +/** An FNV0 implementation for 32-bit values. */ |
| 143 | +using FNV0Hash32 = FNVHash<uint32, 0x811c9dc5, 0, false>; |
| 144 | +/** An FNV1 implementation for 32-bit values. */ |
| 145 | +using FNV1Hash32 = FNVHash<uint32, 0x811c9dc5, 0x811c9dc5, false>; |
| 146 | +/** An FNV1a implementation for 32-bit values. */ |
| 147 | +using FNV1aHash32 = FNVHash<uint32, 0x811c9dc5, 0x811c9dc5, true>; |
| 148 | + |
| 149 | +/** An FNV0 implementation for 64-bit values. */ |
| 150 | +using FNV0Hash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0ULL, false>; |
| 151 | +/** An FNV1 implementation for 64-bit values. */ |
| 152 | +using FNV1Hash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0xcbf29ce484222325ULL, false>; |
| 153 | +/** An FNV1a implementation for 64-bit values. */ |
| 154 | +using FNV1aHash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0xcbf29ce484222325ULL, true>; |
0 commit comments