Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
xxhash.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/ArrayView.h"
6#include "Containers/StringFwd.h"
7#include "CoreTypes.h"
8#include "HAL/UnrealMemory.h"
9#include "Memory/MemoryFwd.h"
10#include "Misc/ByteSwap.h"
11#include "Serialization/Archive.h"
12#include "String/BytesToHex.h"
13
14class FCompositeBuffer;
15template <typename CharType> class TStringBuilderBase;
16
17/** A 64-bit hash from XXH3. */
19{
20 [[nodiscard]] static FXxHash64 HashBuffer(FMemoryView View);
21 [[nodiscard]] static FXxHash64 HashBuffer(const void* Data, uint64 Size);
22 [[nodiscard]] static FXxHash64 HashBuffer(const FCompositeBuffer& Buffer);
23
24 /** Load the hash from its canonical (big-endian) representation. */
25 static inline FXxHash64 FromByteArray(uint8 (&Bytes)[sizeof(uint64)])
26 {
27 uint64 HashBigEndian;
28 FMemory::Memcpy(&HashBigEndian, Bytes, sizeof(uint64));
29 return {NETWORK_ORDER64(HashBigEndian)};
30 }
31
32 /** Store the hash to its canonical (big-endian) representation. */
33 inline void ToByteArray(uint8 (&Bytes)[sizeof(uint64)]) const
34 {
35 const uint64 HashBigEndian = NETWORK_ORDER64(Hash);
36 FMemory::Memcpy(Bytes, &HashBigEndian, sizeof(uint64));
37 }
38
39 inline bool operator==(const FXxHash64& B) const
40 {
41 return Hash == B.Hash;
42 }
43
44 inline bool operator!=(const FXxHash64& B) const
45 {
46 return Hash != B.Hash;
47 }
48
49 inline bool operator<(const FXxHash64& B) const
50 {
51 return Hash < B.Hash;
52 }
53
54 friend inline uint32 GetTypeHash(const FXxHash64& InHash)
55 {
56 return uint32(InHash.Hash);
57 }
58
59 friend inline FArchive& operator<<(FArchive& Ar, FXxHash64& InHash)
60 {
61 return Ar << InHash.Hash;
62 }
63
64 template <typename CharType>
65 friend inline TStringBuilderBase<CharType>& operator<<(TStringBuilderBase<CharType>& Builder, const FXxHash64& InHash)
66 {
67 uint8 Bytes[8];
70 return Builder;
71 }
72
73
74 /**
75 * The hash in its native representation.
76 *
77 * Use the canonical representation from ToByteArray to serialize or display the hash.
78 */
79 uint64 Hash{};
80};
81
82/** A 128-bit hash from XXH3. */
84{
85public:
86 [[nodiscard]] static FXxHash128 HashBuffer(FMemoryView View);
87 [[nodiscard]] static FXxHash128 HashBuffer(const void* Data, uint64 Size);
88 [[nodiscard]] static FXxHash128 HashBuffer(const FCompositeBuffer& Buffer);
89
90 /** Load the hash from its canonical (big-endian) representation. */
91 static inline FXxHash128 FromByteArray(uint8 (&Bytes)[sizeof(uint64[2])])
92 {
93 uint64 HashBigEndian[2];
94 FMemory::Memcpy(&HashBigEndian, Bytes, sizeof(uint64[2]));
95 return {NETWORK_ORDER64(HashBigEndian[1]), NETWORK_ORDER64(HashBigEndian[0])};
96 }
97
98 /** Store the hash to its canonical (big-endian) representation. */
99 inline void ToByteArray(uint8 (&Bytes)[sizeof(uint64[2])]) const
100 {
101 const uint64 HashBigEndian[2]{NETWORK_ORDER64(HashHigh), NETWORK_ORDER64(HashLow)};
102 FMemory::Memcpy(Bytes, &HashBigEndian, sizeof(uint64[2]));
103 }
104
105 /**
106 * The low 64 bits of the hash in its native representation.
107 *
108 * Use the canonical representation from ToByteArray to serialize or display the hash.
109 */
110 uint64 HashLow{};
111 /**
112 * The high 64 bits of the hash in its native representation.
113 *
114 * Use the canonical representation from ToByteArray to serialize or display the hash.
115 */
116 uint64 HashHigh{};
117
118 inline bool operator==(const FXxHash128& B) const
119 {
120 return HashLow == B.HashLow && HashHigh == B.HashHigh;
121 }
122
123 inline bool operator!=(const FXxHash128& B) const
124 {
125 return HashLow != B.HashLow || HashHigh != B.HashHigh;
126 }
127
128 inline bool operator<(const FXxHash128& B) const
129 {
131 }
132
133 friend inline uint32 GetTypeHash(const FXxHash128& Hash)
134 {
135 return uint32(Hash.HashLow);
136 }
137
138 friend inline FArchive& operator<<(FArchive& Ar, FXxHash128& Hash)
139 {
140 return Ar << Hash.HashLow << Hash.HashHigh;
141 }
142
143 template <typename CharType>
144 friend inline TStringBuilderBase<CharType>& operator<<(TStringBuilderBase<CharType>& Builder, const FXxHash128& Hash)
145 {
146 uint8 Bytes[16];
149 return Builder;
150 }
151};
152
153/** Calculates a 64-bit hash with XXH3. */
155{
156public:
158
161
162 void Reset();
163
164 void Update(FMemoryView View);
165 void Update(const void* Data, uint64 Size);
166 void Update(const FCompositeBuffer& Buffer);
167
169
170private:
171 alignas(64) char StateBytes[576];
172};
173
174/** Calculates a 128-bit hash with XXH3. */
176{
177public:
179
182
183 void Reset();
184
185 void Update(FMemoryView View);
186 void Update(const void* Data, uint64 Size);
187 void Update(const FCompositeBuffer& Buffer);
188
190
191private:
192 alignas(64) char StateBytes[576];
193};
#define NETWORK_ORDER64(x)
Definition ByteSwap.h:147
FXxHash128Builder(const FXxHash128Builder &)=delete
void Update(FMemoryView View)
char StateBytes[576]
Definition xxhash.h:192
FXxHash128 Finalize() const
void Update(const void *Data, uint64 Size)
FXxHash128Builder & operator=(const FXxHash128Builder &)=delete
void Update(const FCompositeBuffer &Buffer)
void Update(FMemoryView View)
void Update(const void *Data, uint64 Size)
FXxHash64Builder(const FXxHash64Builder &)=delete
void Update(const FCompositeBuffer &Buffer)
char StateBytes[576]
Definition xxhash.h:171
FXxHash64 Finalize() const
FXxHash64Builder & operator=(const FXxHash64Builder &)=delete
static FORCEINLINE void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
void ToByteArray(uint8(&Bytes)[sizeof(uint64[2])]) const
Definition xxhash.h:99
bool operator!=(const FXxHash128 &B) const
Definition xxhash.h:123
friend uint32 GetTypeHash(const FXxHash128 &Hash)
Definition xxhash.h:133
uint64 HashLow
Definition xxhash.h:110
bool operator==(const FXxHash128 &B) const
Definition xxhash.h:118
static FXxHash128 HashBuffer(FMemoryView View)
static FXxHash128 FromByteArray(uint8(&Bytes)[sizeof(uint64[2])])
Definition xxhash.h:91
static FXxHash128 HashBuffer(const FCompositeBuffer &Buffer)
static FXxHash128 HashBuffer(const void *Data, uint64 Size)
friend TStringBuilderBase< CharType > & operator<<(TStringBuilderBase< CharType > &Builder, const FXxHash128 &Hash)
Definition xxhash.h:144
uint64 HashHigh
Definition xxhash.h:116
bool operator<(const FXxHash128 &B) const
Definition xxhash.h:128
static FXxHash64 FromByteArray(uint8(&Bytes)[sizeof(uint64)])
Definition xxhash.h:25
friend TStringBuilderBase< CharType > & operator<<(TStringBuilderBase< CharType > &Builder, const FXxHash64 &InHash)
Definition xxhash.h:65
bool operator==(const FXxHash64 &B) const
Definition xxhash.h:39
static FXxHash64 HashBuffer(FMemoryView View)
bool operator<(const FXxHash64 &B) const
Definition xxhash.h:49
void ToByteArray(uint8(&Bytes)[sizeof(uint64)]) const
Definition xxhash.h:33
bool operator!=(const FXxHash64 &B) const
Definition xxhash.h:44
static FXxHash64 HashBuffer(const void *Data, uint64 Size)
uint64 Hash
Definition xxhash.h:79
static FXxHash64 HashBuffer(const FCompositeBuffer &Buffer)
friend uint32 GetTypeHash(const FXxHash64 &InHash)
Definition xxhash.h:54